题目一
【问题描述】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 #include <iostream> using namespace std;template <class ElemType > class MyLinkList ; template <class ElemType >class Node { public : Node () { next=NULL ; } Node (ElemType val) { data=val; next=NULL ; } friend class MyLinkList <ElemType>; private : ElemType data; Node<ElemType> * next; }; template <class ElemType >class MyLinkList { private : unsigned int listlength; Node<ElemType> * pTemp; Node<ElemType> * pTail; Node<ElemType> * pHead; public : MyLinkList (); void addAtTail (ElemType x) ; void traversal () ; void Delete (ElemType x) ; void insertHead (ElemType x) ; }; template <class ElemType >MyLinkList<ElemType>::MyLinkList () { pTemp=NULL ; pTail=NULL ; pHead=NULL ; listlength=0 ; } template <class ElemType >void MyLinkList<ElemType>::addAtTail (ElemType x){ pTemp=new Node <ElemType>(x); if (pTail==NULL ) { pHead=pTemp; pTail=pTemp; } else { pTail->next=pTemp; pTail=pTemp; } ++listlength; } template <class ElemType >void MyLinkList<ElemType>::traversal (){ pTemp=pHead; while (pTemp!=NULL ) { cout<<pTemp->data<<" " ; pTemp=pTemp->next; } cout<<endl; } template <class ElemType >void MyLinkList<ElemType>::insertHead (ElemType x){ pTemp=new Node <ElemType>(x); if (pTail==NULL ) pTail=pTemp; pTemp->next=pHead; pHead=pTemp; } class Student { public : Student (int id = 0 , int height = 0 ) { this ->ID = id; this ->Height = height; } bool operator ==(Student &a2); friend ostream &operator <<(ostream &out, const Student &s) { out << "ID:" << s.ID << " Height:" << s.Height; return out; } private : int ID; int Height; }; int main () { MyLinkList<int > list1; for (int i=0 ;i<10 ;i++) list1.insertHead (i); list1.insertHead (9 ); list1.insertHead (9 ); list1.insertHead (9 ); list1.traversal (); list1.Delete (9 ); list1.traversal (); MyLinkList<Student> list2; list2.insertHead (Student (1 ,174 )); list2.addAtTail (Student (2 ,176 )); list2.addAtTail (Student (3 ,173 )); list2.traversal (); list2.Delete (Student (1 ,174 )); list2.traversal (); return 0 ; }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 #include <iostream> using namespace std;template <class ElemType > class MyLinkList ; template <class ElemType >class Node { public : Node () { next=NULL ; } Node (ElemType val) { data=val; next=NULL ; } friend class MyLinkList <ElemType>; private : ElemType data; Node<ElemType> * next; }; template <class ElemType >class MyLinkList { private : unsigned int listlength; Node<ElemType> * pTemp; Node<ElemType> * pTail; Node<ElemType> * pHead; public : MyLinkList (); void addAtTail (ElemType x) ; void traversal () ; void Delete (ElemType x) ; void insertHead (ElemType x) ; }; template <class ElemType >MyLinkList<ElemType>::MyLinkList () { pTemp=NULL ; pTail=NULL ; pHead=NULL ; listlength=0 ; } template <class ElemType >void MyLinkList<ElemType>::addAtTail (ElemType x){ pTemp=new Node <ElemType>(x); if (pTail==NULL ) { pHead=pTemp; pTail=pTemp; } else { pTail->next=pTemp; pTail=pTemp; } ++listlength; } template <class ElemType >void MyLinkList<ElemType>::traversal (){ pTemp=pHead; while (pTemp!=NULL ) { cout<<pTemp->data<<" " ; pTemp=pTemp->next; } cout<<endl; } template <class ElemType >void MyLinkList<ElemType>::Delete (ElemType x){ Node<ElemType> *p = pHead; Node<ElemType> *q = NULL ; while (p != NULL && !(p->data == x)) { q = p; p = p->next; } if (p == NULL ) { return ; } if (p == pHead) { pHead = pHead->next; } else { q->next = p->next; } if (p == pTail) { pTail = q; } delete p; --listlength; } template <class ElemType >void MyLinkList<ElemType>::insertHead (ElemType x){ pTemp=new Node <ElemType>(x); if (pTail==NULL ) pTail=pTemp; pTemp->next=pHead; pHead=pTemp; } class Student { public : Student (int id = 0 , int height = 0 ) { this ->ID = id; this ->Height = height; } bool operator ==(Student &a2); friend ostream &operator <<(ostream &out, const Student &s) { out << "ID:" << s.ID << " Height:" << s.Height; return out; } private : int ID; int Height; }; bool Student::operator ==(Student &a2){ return ID == a2.ID; } int main () { MyLinkList<int > list1; for (int i=0 ;i<10 ;i++) list1.insertHead (i); list1.insertHead (9 ); list1.insertHead (9 ); list1.insertHead (9 ); list1.traversal (); list1.Delete (9 ); list1.traversal (); MyLinkList<Student> list2; list2.insertHead (Student (1 ,174 )); list2.addAtTail (Student (2 ,176 )); list2.addAtTail (Student (3 ,173 )); list2.traversal (); list2.Delete (Student (1 ,174 )); list2.traversal (); return 0 ; }
题目二
【问题描述】
完善程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 #include <iostream> #include <iomanip> using namespace std;template <typename ElemType>class myArrayList { private :int mSize;int mLen;ElemType *mpArr; public :myArrayList (int n);myArrayList (ElemType *a, int n);void show () ;ElemType getMax () ;void sort () ;}; template <typename ElemType>myArrayList<ElemType>::myArrayList (myArrayList<ElemType> &other) { this ->mLen = other.mLen;this ->mSize = other.mSize;this ->mpArr = new ElemType[this ->mLen];for (int i = 0 ; i < this ->mLen; i++)this ->mpArr[i] = other.mpArr[i];} template <typename ElemType>myArrayList<ElemType>::myArrayList (int n) { this ->mSize = n;this ->mLen = 0 ;this ->mpArr = new ElemType[mSize];} template <typename ElemType>void myArrayList<ElemType>::show (){ for (int i = 0 ; i < mLen; i++)cout << mpArr[i]<<" " ; cout << endl; } template <typename ElemType>ElemType myArrayList<ElemType>::getMax () { ElemType max; max = mpArr[0 ]; for (int i = 1 ; i < mLen; i++)if (max < mpArr[i])max = mpArr[i]; return max;} class Student { private :int mId;float height;int score;public :Student (int id = 0 , float h = 0 , int s = 0 ) :height (h), mId (id), score (s){ } }; int main () {int a[] = { 1 , 2 , 3 , 5 , 7 , 9 , 12 , 8 };double b[] = { 1 , 2.5 , 3.6 , 5 , 7 , 9 , 12.8 , 8 };myArrayList <int > list1 (a, 8 ); list1.sort (); list1.show (); cout << "max=" << list1.getMax () << endl; myArrayList <double > list2 (b, 8 ); list2.sort (); list2.show (); cout << "max=" << list2.getMax () << endl; Student s[3 ] = { Student (1 , 175 , 80 ), Student (2 , 178 , 90 ), Student (3 , 195 , 83 ) }, s1; myArrayList <Student> list3 (s, 3 ); list3.sort (); list3.show (); cout << "max=" <<list3.getMax ()<< endl; }
【代码】
1 myArrayList (myArrayList<ElemType>& other);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template <typename ElemType>myArrayList<ElemType>::myArrayList (ElemType* a, int n) { this ->mSize = n; this ->mpArr = new ElemType[n]; this ->mLen = n; for (int i = 0 ; i < n; i++) mpArr[i] = a[i]; } template <typename ElemType>void myArrayList<ElemType>::sort (){ for (int i = 0 ; i < mLen - 1 ; i++) { for (int j = i + 1 ; j < mLen; j++) if (mpArr[i] > mpArr[j]) swap (mpArr[i], mpArr[j]); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 bool operator >(const Student& s) { return this ->score > s.score; } bool operator <(const Student& s) { return this ->score < s.score; } bool operator ==(const Student& s) { return this ->score == s.score; } friend ostream& operator <<(ostream& out, const Student& s) { out << "ID:" << s.mId << " Height:" << s.height << " Score:" << s.score << endl; return out; }
题目三
【问题描述】
理解主函数代码,设计一个将两个一维数组中同下标元素求和保存到第三个数组中的函数模板。而对两个字符串求和则要求实现两个字符串的连接并保存到第三个字符串中。编程实现上述功能并使程序正确运行。
说明:若
int x[3] = { 2,4,6 };int y[3] = { 1,2,3};
x和y求和保存到z,则:
z[3] = { 3,6,9 };
char c[]=“abc”,d[]=“xy”;
c和d求和保存到str,str中就是“abcxy”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int main () { const int N = 5 ; int x[N] = { 2 ,-4 ,6 ,8 ,0 },y[N], z[N] = {0 }; char c[] = "Hello," , d[100 ] ; for (int i (0 ); i < N; i++) cin >> *(y + i); while (cin.get () != '\n' ); cin.getline (d, 100 ); Add <int , N>(x, y, z); for (int i (0 ); i < N; i++) cout << z[i]<< " " ; cout << endl; char * pc = new char [strlen (c) + strlen (d) + 1 ]; Add (c, d, pc); cout << pc << endl; delete []pc; return 0 ; }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <iostream> #include <string.h> using namespace std;template <typename T,int N>void Add (T x[], T y[], T z[]) { for (int i = 0 ; i < N; i++) z[i] = x[i] + y[i]; } void Add (char c[], char d[], char str[]) { strcpy (str, c); strcat (str, d); } int main () { const int N = 5 ; int x[N] = { 2 ,-4 ,6 ,8 ,0 },y[N], z[N] = {0 }; char c[] = "Hello," , d[100 ] ; for (int i (0 ); i < N; i++) cin >> *(y + i); while (cin.get () != '\n' ); cin.getline (d, 100 ); Add <int , N>(x, y, z); for (int i (0 ); i < N; i++) cout << z[i]<< " " ; cout << endl; char * pc = new char [strlen (c) + strlen (d) + 1 ]; Add (c, d, pc); cout << pc << endl; delete []pc; return 0 ; }
题目四
【问题描述】
根据主函数代码,编写求一维数组中最大、最小元素及所有元素平均值的函数模板,使程序正确运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { int imax, imin; double * pd = new double [5 ]; assert (pd); for (int i (0 ); i < 5 ; i++) cin >> pd[i]; int iarry[] = { -20 ,30 ,40 ,50 ,33 ,5 ,8 ,9 ,-7 ,0 }; cout << GetValue <int >(iarry, imax, imin) << endl; cout << imax << "," << imin << endl; double dmax, dmin; cout <<GetValue <double , 5 >(pd, dmax, dmin) << endl; cout << dmax << "," << dmin << endl; return 0 ; }
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <iostream> #include <cassert> using namespace std;template <typename T, int N =10 >double GetValue (T* arr, T& maxVal, T& minVal) { T sum = 0 ; maxVal = arr[0 ]; minVal = arr[0 ]; for (int i = 0 ; i < N; i++) { sum += arr[i]; if (arr[i] > maxVal) { maxVal = arr[i]; } if (arr[i] < minVal) { minVal = arr[i]; } } return sum / N; } int main () { int imax, imin; double * pd = new double [5 ]; assert (pd); for (int i (0 ); i < 5 ; i++) cin >> pd[i]; int iarry[] = { -20 ,30 ,40 ,50 ,33 ,5 ,8 ,9 ,-7 ,0 }; cout << GetValue <int >(iarry, imax, imin) << endl; cout << imax << "," << imin << endl; double dmax, dmin; cout <<GetValue <double , 5 >(pd, dmax, dmin) << endl; cout << dmax << "," << dmin << endl; return 0 ; }