1、C+程序设计YDaniel Liang 第十八十九二十章课后习题答案Exercise18_2#include using namespace std;/ Function prototypetemplatevoid arraycopy(T source, int sourceStartIndex, T target, int targetStartIndex, int length);templatevoid merge(T list1, int list1Size, T list2, int list2Size, T temp);/* The function for sorting the
2、 numbers */templatevoid mergeSort(T list, int arraySize) if (arraySize 1) / Merge sort the first half T *firstHalf = new TarraySize / 2; arraycopy(list, 0, firstHalf, 0, arraySize / 2); mergeSort(firstHalf, arraySize / 2); / Merge sort the second half int secondHalfLength = arraySize - arraySize / 2
3、; T *secondHalf = new TsecondHalfLength; arraycopy(list, arraySize / 2, secondHalf, 0, secondHalfLength); mergeSort(secondHalf, secondHalfLength); / Merge firstHalf with secondHalf T *temp = new TarraySize; merge(firstHalf, arraySize / 2, secondHalf, secondHalfLength, temp); arraycopy(temp, 0, list,
4、 0, arraySize); delete temp; delete firstHalf; delete secondHalf; templatevoid merge(T list1, int list1Size, T list2, int list2Size, T temp) int current1 = 0; / Index in list1 int current2 = 0; / Index in list2 int current3 = 0; / Index in temp while (current1 list1Size & current2 list2Size) if (lis
5、t1current1 list2current2) tempcurrent3+ = list1current1+; else tempcurrent3+ = list2current2+; while (current1 list1Size) tempcurrent3+ = list1current1+; while (current2 list2Size) tempcurrent3+ = list2current2+;templatevoid arraycopy(T source, int sourceStartIndex, T target, int targetStartIndex, i
6、nt length) for (int i = 0; i length; i+) targeti + targetStartIndex = sourcei + sourceStartIndex; templatevoid printArray(T list, int arraySize) for (int i = 0; i arraySize; i+) cout listi ; cout endl;int main() int list1 = 3, 5, 1, 0, 2, 8, 7; mergeSort(list1, 7); printArray(list1, 7); double list2
7、 = 3.5, 0.5, 1.4, 0.4, 2.5, 1.8, 4.7; mergeSort(list2, 7); printArray(list2, 7); string list3 = Atlanta, Denver, Chicago, Dallas; mergeSort(list3, 4); printArray(list3, 4); return 0;Exercise18_6#include using namespace std;/ T is a generic typetemplatebool ordered(T list, int size);/ T is a generic
8、typetemplatebool ordered(T list, int size, bool ascending);templatebool ordered(T list, int size) bool ordered = true; for (int i = 0; i listi + 1) ordered = false; break; return ordered;/ T is a generic typetemplatebool ordered(T list, int size, bool ascending) if (ascending) return ordered(list, s
9、ize); bool ordered = true; for (int i = 0; i size - 1; i+) if (listi listi + 1) ordered = false; break; return ordered;templatevoid printArray(T list, int arraySize) for (int i = 0; i arraySize; i+) cout listi ; cout endl;int main() int list1 = 3, 5, 1, 0, 2, 8, 7; cout ordered(list1, 7) endl; cout
10、ordered(list1, 7, true) endl; int list2 = 1, 2, 3, 5, 7, 8; cout ordered(list2, 6) endl; cout ordered(list2, 6, true) endl; cout ordered(list2, 6, false) endl; return 0;Exercise19_2#include #include #include using namespace std;templateElementType maxElement(ContainerType &container, const ElementTy
11、pe &value) for (int i = 0; i container.size(); i+) if (containeri = value) return i; return -1;int main() int list1 = 3, 5, 1, 0, 2, 8, 7, 6; vector intVector(list1, list1 + 8); cout The maximum value in list1: (maxElementint, vector (intVector, 8); return 0;Exercise19_4#include #include #include us
12、ing namespace std;templatevoid reverse(ContainerType &container) int count = 0; / return count;int main() int list1 = 3, 5, 1, 8, 2, 8, 7, 6; vector intVector(list1, list1 + 8); reversevector (intVector); return 0;Exercise19_6#include using namespace std;int v1 = 10;int main() unsigned short x = 0XAEAE 4; cout x endl; return 0;Exercise20_2#include #include #include #include using namespace std;int nextNum() srand(time(0); return rand();int main() double values = 1.3, 2.4, 4.5, 6.7, 9.0; deque doubleDeque(values, values + 5); deque doubleDeque1(values, values + 5); ostream_iterator
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1