没事可以看看,有一些思路还是很好的
链表 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 #include <iostream> using namespace std ;typedef struct link { int data; struct link *next ; }Link; int main () { Link *addNode (Link *head) ; Link *deleteNode (Link *head, int nodeData) ; Link *insertNode (Link *head, int nodeData) ; void displayNode (Link *head) ; void freeNode (Link *head) ; int menu () ; Link *head = NULL ; int i = 0 , j; char c; int nodeData; while (true ) { j = menu(); switch (j) { case 1 : head = addNode(head); i++; break ; case 2 : cout <<"Please input the node you want to delete: " ; cin >>nodeData; head = deleteNode(head, nodeData); if (i > 0 ) i--; break ; case 3 : cout <<"Please input the node you want to insert: " ; cin >>nodeData; head = insertNode(head, nodeData); i++; break ; case 4 : displayNode(head); cout <<i<<" new nodes have been founded !" <<endl ; break ; case 5 : freeNode(head); cout <<"Quited ......" <<endl <<endl ; system("pause" ); exit (0 ); break ; default : cout <<"输入无效!" ; break ; } } return 0 ; } int menu () { int i; cout <<endl <<"What do you want to do ?" <<endl <<endl ; cout <<"****************************" <<endl ; cout <<" * 1. Add node" <<endl <<" * 2. Delete node" <<endl <<" * 3. Insert node" <<endl << " * 4. Display node data" <<endl <<" * 5. Free node" <<endl <<"****************************" <<endl <<endl ; cout <<"Input your choose: " ; cin >>i; return i; } Link *addNode (Link *head) { Link *p = NULL , *pr = head; int data; p = new Link; if (p == NULL ) { cout <<"No enough memory to allocate !" <<endl ; exit (0 ); } cout <<"Input node data: " ; cin >>data; if (head == NULL ) { head = p; } else { while (pr -> next != NULL ) { pr = pr -> next; } pr -> next = p; } p -> data = data; p -> next = NULL ; return head; } Link *deleteNode (Link *head, int nodeData) { Link *p = head, *pr = head; if (head == NULL ) { cout <<"Linked table is empty !" <<endl ; return head; } while (nodeData != p -> data && p -> next != NULL ) { pr = p; p = p -> next; } if (nodeData == p -> data) { if (p == head) { head = p -> next; } else { pr -> next = p -> next; } delete p; } else { cout <<"This node has not been found !" <<endl ; } return head; } Link *insertNode (Link *head, int nodeData) { Link *p = head, *pr = head, *temp = NULL ; p = new Link; if (p == NULL ) { cout <<"No enough memory to allocate !" <<endl ; exit (0 ); } p -> data = nodeData; p -> next = NULL ; if (head == NULL ) { head = p; } else { while (pr -> data < nodeData && pr -> next != NULL ) { temp = pr; pr = pr -> next; } if (pr -> data >= nodeData) { if (pr == head) { p -> next = head; head = p; } else { pr = temp; p -> next = pr -> next; pr -> next = p; } } else { pr -> next = p; } } return head; } void displayNode (Link *head) { Link *p = head; int j = 1 ; while (p != NULL ) { cout <<" The " <<j<<" node is" <<" " <<p -> data<<endl ; p = p -> next; j++; } } void freeNode (Link *head) { Link *p = head, *pr = NULL ; while (p != NULL ) { pr = p; p = p -> next; delete p; } }
实验报告一 1-1a 编写金字塔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream> using namespace std ;int main () { int i, j, n, k; cout <<"Please input the row number: " ; cin >>n; for (i = 1 ; i <= n; i++) { for (j = 20 ; j - i > 0 ; j--) { cout <<" " ; } for (k = 0 ; k < 2 * i - 1 ; k++) { cout <<"*" ; } cout <<endl ; } return 0 ; }
1-1b 编写杨辉三角
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> using namespace std ;int main () { const int N = 100 ; int n, a[N][N], i, j; cout <<"Input the row: " ; cin >>n; for (i = 1 ; i <= n; i++) a[i][1 ] = a[i][i] = 1 ; for (i = 3 ; i <= n; i++) { for (j = 2 ; j <= i - 1 ; j++) { a[i][j] = a[i-1 ][j-1 ] + a[i-1 ][j]; } } for (i = 1 ; i <= n; i++) { int k; for (k = 30 ; k - i * 2 > 0 ; k--) { cout <<" " ; } for (j = 1 ; j <= i; j++) { cout <<a[i][j]<<" " ; } cout <<endl ; } return 0 ; }
2. 改错 对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。 请计算需要经过几步才能将n变到1,具体可见样例 输入: 测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000) 输出: 对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行
样例输入: 3 1 0 样例输出: 5 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 #include <iostream> using namespace std ;int main () { int i, n, k = 0 ; int a[100 ], t[100 ]; cout <<"please input numbers:" ; for (i = 0 ; i < 100 ; i++) { cin >>a[i]; k++; if (a[i] == 0 ) break ; } for (i = 0 , t[i] = 0 ; i < k; i++) { while (a[i] != 1 ) { if (a[i] % 2 == 0 ) { a[i] = a[i] / 2 ; } else { a[i] = (a[i] * 3 + 1 ) / 2 ; } t[i]++; } cout <<t[i]<<endl ; } return 0 ;
改错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 for (i = 0 ; i < k; i++) t[i] = 0 ; if (a[i] == 0 ) break ; while (a[i] != 1 ) { if (a[i] % 2 == 0 ) { a[i] = a[i] / 2 ; } else { a[i] = (a[i] * 3 + 1 ) / 2 ; } t[i]++; } cout <<t[i]<<endl ; }
3. 编写顺序查找算法 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 #include <iostream> using namespace std ;int main () { int arr[] = {2 , 12 , 4 , 7 , 13 , 6 , 11 , 78 , 65 , 66 }; int i; for (i = 0 ; i < 10 ; i++) { cout <<" " <<arr[i]; } int n; cout <<endl <<endl <<"Input the number you want to find: " ; cin >>n; int m; for (i = 0 ; i < 10 ; i++) { if (arr[i] == n) { m = i; cout <<"the position of the key " <<n; cout <<" in the array is: " <<m<<endl ; return 0 ; } } m = -1 ; cout <<"the position of the key " <<n; cout <<" in the array is: " <<m<<endl ; return 0 ; }
4. 编写折半查找算法 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 #include <iostream> using namespace std ;int main () { int arr[] = {2 , 4 , 6 , 7 , 11 , 12 , 13 , 65 , 66 , 78 }; int low = 0 , high = 9 , mid; int i; for (i = 0 ; i < 10 ; i++) { cout <<" " <<arr[i]; } int n; cout <<endl <<endl <<"Input the number you want to find: " ; cin >>n; while (low <= high) { mid = (low + high) / 2 ; if (arr[mid] > n) { high = mid - 1 ; } else if (arr[mid] < n) { low = mid + 1 ; } else break ; } if (low <= high) { cout <<"the position of the key " <<n<<" in the array is: " <<mid<<endl ; } else { mid = -1 ; cout <<"the position of the key " <<n<<" in the array is: " <<mid<<endl ; } return 0 ; }
5. 编写代码 输入10个整数,将这10个整数按升序排列输出,并且奇数在前,偶数在后
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 #include <iostream> using namespace std ;int main () { int n = 10 ; int a[n], b[n], c[n]; cout <<"Please input ten numbers: " ; int i, j = 0 , k = 0 ; for (i = 0 ; i < 10 ; i++) { cin >>a[i]; } for (i = 0 ; i < 10 ; i++) { cout <<" " <<a[i]; } cout <<endl <<"n=" <<n<<endl ; for (i = 0 ; i < 10 ; i++) { if (a[i] % 2 == 0 ) { b[j] = a[i]; j++; } else { c[k] = a[i]; k++; } } int m, temp; for (i = 0 ; i < j - 1 ; i++) { for (m = 0 ; m < j - i - 1 ; m++) { if (b[m] > b[m+1 ]) { temp = b[m]; b[m] = b[m+1 ]; b[m+1 ] = temp; } } } for (i = 0 ; i < k - 1 ; i++) { for (m = 0 ; m < k - i - 1 ; m++) { if (c[m] > c[m+1 ]) { temp = c[m]; c[m] = c[m+1 ]; c[m+1 ] = temp; } } } for (i = 0 ; i < k; i++) { cout <<" " <<c[i]; } for (i = 0 ; i < j; i++) { cout <<" " <<b[i]; } cout <<endl ; return 0 ; }
6-1a 从键盘输入一个整数,判断该数是几位数,逆向输出该数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> using namespace std ;int main () { long n; cin >>n; int i, m = 0 , count = 0 ; for (i = 0 ; i < 100 ; i++) { if (n > 0 ) { count++; m = m * 10 + n % 10 ; n = n / 10 ; } } cout <<m<<endl ; cout <<"该数是" <<count<<"位数" <<endl ; return 0 ; }
6-1b 将用户输入的一个字符串反向形式输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> #include <string> using namespace std ;int main () { int i; string str1,str2; cin >>str1; for (i = 0 ; str1[i] != 0 ; i++); for (--i; i >= 0 ; i--){ str2 = str2 + str1[i]; } cout <<str2<<endl ; return 0 ; }
实验报告二 1. 子字符串出现次数 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 #include <iostream> #include <string.h> using namespace std ;int main () { int findCount (string &s1, string &s2) ; string s1, s2; cout <<"输入源字符串:" <<endl ; cin >>s1; cout <<"输入子字符串:" <<endl ; cin >>s2; int count; count = findCount(s1, s2); cout <<s2<<" 出现的次数为:" <<count<<endl ; system("pause" ); return 0 ; } int findCount (string &s1, string &s2) { int n = 0 ; int k; for (k = 0 ; s2[k] != 0 ; k++); for (int i = 0 ; s1[i] != 0 ;) { bool find = false ; for (int j = 0 ; s2[j] != 0 ;) { if (s1[i] == s2[j]) { i++; j++; if (j == k) find = true ; } else { i++; j = 0 ; } if (s1[i] == 0 ) { break ; } } if (find) n++; } return n; }
2. 电话号码 现有一电话号码簿,其中有姓名、电话号码,当输入电话号码时,查找出姓名和电话号码;输入姓名时,同样查找出姓名与电话号码;还允许不完全输入查找,如输入010时,查找出所有以010开头的号码,输入“李”时,列出所有姓名以“李”开头的号码
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 #include <iostream> #include <string> using namespace std ;class Contact { public : Contact (string s, string n):name(s), num(n) {} string name; string num; }; int main () { bool findName (Contact &c, string f) ; bool findNum (Contact &c, string f) ; Contact cs[10 ] = { Contact("郑大" , "123456" ), Contact("郑中" , "123567" ), Contact("郑小" , "123678" ), Contact("李大" , "234567" ), Contact("李中" , "234678" ), Contact("李小" , "234789" ), Contact("张大" , "345678" ), Contact("张中" , "345789" ), Contact("张小" , "345890" ), Contact("小双" , "987654" ) }; int c; cout <<"1. 姓名" <<" " <<"2. 电话号码" <<endl ; cout <<"你想输入的是:" ; cin >>c; int i; string name, num; bool isFind = false ; switch (c) { case 1 : cout <<"请输入姓名:" ; cin >>name; for (i = 0 ; i < 10 ; i++) { if (findName(cs[i], name)) { isFind = true ; cout <<cs[i].name<<" " <<cs[i].num<<endl ; } } if (!isFind) cout <<"无法找到 !" ; break ; case 2 : cout <<"请输入号码:" ; cin >>num; for (i = 0 ; i < 10 ; i++) { if (findNum(cs[i], num)) { isFind = true ; cout <<cs[i].num<<" " <<cs[i].name<<endl ; } } if (!isFind) cout <<"无法找到 !" ; break ; default : cout <<"无效输入 !" <<endl ; break ; } system("pause" ); return 0 ; } bool findName (Contact &c, string f) { int k, j; for (k = 0 ; f[k] != 0 ; k++); bool find = false ; for (j = 0 ; j < k;) { if (c.name[j] == f[j]) { j++; if (j == k) find = true ; } else break ; } return find; } bool findNum (Contact &c, string f) { int k, j; for (k = 0 ; f[k] != 0 ; k++); bool find = false ; for (j = 0 ; j < k;) { if (c.num[j] == f[j]) { j++; if (j == k) find = true ; } else break ; } return find; }
3. 课本习题 这个太琐碎了,就放在 gitee 上了,点此处跳转
4. 学生链表 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 #include <iostream> #include <string> using namespace std ;typedef struct student { string name; int grade; struct student *next ; }Student; int main () { Student *sort (Student *head) ; Student *del (Student *head) ; void display (Student *head) ; void free (Student *head) ; cout <<"Input name and score(-1 to exit): " <<endl ; Student *head = new Student; cin >>head -> name>>head -> grade; head -> next = NULL ; if (head -> grade >= 0 ) { Student *p = new Student; cin >>p -> name>>p -> grade; p -> next = NULL ; head -> next = p; Student *pr = p; while (p -> grade >= 0 ) { p = new Student; if (p == NULL ) { cout <<"No enough memory to allocate !" <<endl ; exit (0 ); } cin >>p -> name>>p -> grade; p -> next = NULL ; pr -> next = p; pr = pr -> next; } } if (head -> grade < 0 ) { cout <<"Linked table is empty !" <<endl ; exit (0 ); } else { cout <<endl <<"Before delete:" <<endl ; head = sort(head); display(head); cout <<endl <<"After delete:" <<endl ; head = del(head); if (head == NULL ) { cout <<"Both fail !" <<endl ; } else display(head); } free (head); system("pause" ); return 0 ; } Student *sort (Student *head) { Student *p = head, *pr = NULL , temp; while (p -> next != NULL ) { pr = p->next; while (pr != NULL ) { if (pr -> grade > p -> grade) { temp.name = pr -> name; pr -> name = p -> name; p -> name = temp.name; temp.grade = pr -> grade; pr -> grade = p -> grade; p -> grade = temp.grade; } pr = pr -> next; } p = p -> next; } return head; } Student *del (Student *head) { Student *p = head, *pr = NULL ; while (p != NULL ) { if (head -> grade < 60 ) { while (p != NULL ) { pr = p; p = p -> next; delete p; } head = p; } else { if (p -> grade >= 60 ) { pr = p; p = p -> next; } else { pr -> next = p -> next; delete p; p = pr -> next; } } } return head; } void display (Student *head) { Student *p = head; while (p != NULL ) { if (p -> grade >= 0 ) { cout <<p -> name<<" " <<p -> grade<<endl ; p = p -> next; } else p = p -> next; } } void free (Student *head) { Student *p = head, *pr = NULL ; while (p != NULL ) { pr = p; p = p -> next; delete p; } }
实验报告三 1. 课本习题 这个太琐碎了,就放在 gitee 上了点此处跳转
2. 求两点的距离 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 #include <iostream> #include <math.h> using namespace std ;class Point { public : Point (double a, double b):x(a), y(b) {} double distance (Point &p) ; double x; double y; }; double Point::distance (Point &p) { double dst; dst = pow ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y), 1.0 /2 ); return dst; } int main () { Point p1(1, 1), p2(2, 2); cout <<p1.distance(p2)<<endl ; system("pause" ); return 0 ; }
3. 公路收费程序 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 #include <iostream> using namespace std ;class Free { public : Free () { car = 0 ; truck = 0 ; bus = 0 ; carfee = 0 ; truckfee = 0 ; busfee = 0 ; } void cars () ; void trucks () ; void buses () ; void process () ; void dispaly () ; int car; int truck; int bus; int carfee; int truckfee; int busfee; int sum; }; void Free::cars () { car++; carfee += 10 ; } void Free::trucks () { truck++; truckfee += 25 ; }void Free::buses () { bus++; busfee += 15 ; } void Free::process () { sum = carfee + truckfee + busfee; } void Free::dispaly () { cout <<"收费统计如下:" <<endl ; cout <<"小汽车:" <<car<<"辆" <<" " <<"收费小计:" <<carfee<<endl ; cout <<"卡车:" <<truck<<"辆" <<" " <<"收费小计:" <<truckfee<<endl ; cout <<"公汽:" <<bus<<"辆" <<" " <<"收费小计:" <<busfee<<endl ; cout <<" " <<"合计收费:" <<sum<<endl ; } int main () { Free obj; cout <<"1. 小汽车 (10元/辆)" <<endl <<"2. 卡车 (25元/辆)" <<endl <<"3. 公汽 (15元/辆)" <<"0 : 退出" <<endl <<endl ; int n, i = 1 ; bool active = true ; while (active) { cout <<" " <<i<<"-车型:" ; cin >>n; switch (n) { case 0 : active = false ; break ; case 1 : obj.cars(); i++; break ; case 2 : obj.trucks(); i++; break ; case 3 : obj.buses(); i++; break ; default : cout <<"无效输入!" <<endl ; break ; } } obj.process(); obj.dispaly(); system("pause" ); return 0 ; }
实验报告四 1. 课本习题 这个太琐碎了,就放在 gitee 上了点此处跳转
计算工资 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 #include <iostream> #include <string> using namespace std ;class Salary { public : void setdata () ; void computed () ; void sort () ; void display () ; private : long num; string name; int base_salary; int post_salary; int fund; int tax; int sum; int i, n; Salary *salary1; }; void Salary::setdata () { cout <<"请输入职工的人数:" ; cin >>n; salary1 = new Salary[n]; for (i = 0 ; i < n; i++) { cin >>salary1[i].num>>salary1[i].name>>salary1[i].base_salary>>salary1[i].post_salary>>salary1[i].fund>>salary1[i].tax; } } void Salary::computed () { for (int i = 0 ; i < n; i++) { salary1[i].sum = salary1[i].base_salary + salary1[i].post_salary - salary1[i].fund - salary1[i].tax; } } void Salary::sort () { int j; for (i = 0 ; i < n - 1 ; i++) { for (j = i + 1 ; j< n; j++) { Salary temp; if (salary1[i].sum < salary1[j].sum) { temp = salary1[i]; salary1[i] = salary1[j]; salary1[j] = temp; } } } } void Salary::display () { cout <<"职工实际发放的工资是:(降序排列)" <<endl ; for (int i = 0 ; i < n; i++) { cout <<salary1[i].num<<" " <<salary1[i].name<<" " <<salary1[i].base_salary<<" " <<salary1[i].post_salary<<" " <<salary1[i].fund<<" " <<salary1[i].tax<<" " <<salary1[i].sum<<endl ; } delete [] salary1; } int main () { Salary employees; employees.setdata(); employees.computed(); employees.sort(); employees.display(); system("pause" ); 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 #include <iostream> #include <string> using namespace std ;class Students { public : void setdata () ; void search (long n) ; void display () ; void allDisplay () ; private : string name; long num; int mathGrade; int cplusGrade; int enGrade; int i, n; Students *student; }; void Students::setdata () { cout <<"请输入学生总数: " ; cin >>n; student = new Students[n]; for (i = 0 ; i < n; i++) { cout <<"请输入学生姓名:" ; cin >>student[i].name; cout <<"请输入学生学号:" ; cin >>student[i].num; cout <<"请输入学生高等数学成绩:" ; cin >>student[i].mathGrade; cout <<"请输入学生C++成绩:" ; cin >>student[i].cplusGrade; cout <<"请输入学生大学英语成绩:" ; cin >>student[i].enGrade; cout <<endl ; } } void Students::search (long n) { for (i = 0 ; i < n; i++) { if (student[i].num == n) { student[i].display(); return ; } } cout <<"无效学号输入!" <<endl ; } void Students::display () { cout <<"姓名:" <<name<<endl ; cout <<"学号:" <<num<<endl ; cout <<"高等数学成绩:" <<mathGrade<<endl ; cout <<"C++成绩:" <<cplusGrade<<endl ; cout <<"大学英语成绩:" <<enGrade<<endl ; } void Students::allDisplay () { cout <<"------------------------------" <<endl ; cout <<"现有所有学生数据:" <<endl ; for (i = 0 ; i < n; i++) { cout <<"姓名:" <<student[i].name<<" " <<"学号:" <<student[i].num<<" " ; cout <<"高等数学成绩:" <<student[i].mathGrade<<endl ; cout <<"C++成绩:" <<student[i].cplusGrade<<" " <<"大学英语成绩:" <<student[i].enGrade<<endl ; } } int main () { Students students; students.setdata(); char search; int num; do { cout <<"请输入要查询的学生的学号: " ; cin >>num; students.search(num); cout <<"是否继续查询?(y/n): " ; cin >>search; } while (search == 'y' ); students.allDisplay(); system("pause" ); return 0 ; }
实验报告五 1. 三角形 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 #include <iostream> #include <math.h> using namespace std ;class Ctriangle { public : Ctriangle(int x, int y, int z):a(x), b(y), c(z) {} void display () {cout <<"Ctriangle: a=" <<a<<" b=" <<b<<" c=" <<c<<endl ;} void length () ; void are () ; private : int a; int b; int c; int perimeter; float area; }; void Ctriangle::length () { perimeter = a + b + c; cout <<"Perimeter: " <<perimeter<<endl ; } void Ctriangle::are () { float p; p = (a + b + c) / 2.0 ; area = pow (p * (p - a) * (p - b) * (p - c), 1.0 / 2 ); cout <<"Area: " <<area<<endl ; } int main () { int a, b, c; cin >>a>>b>>c; Ctriangle triangle1 (a, b, c) ; triangle1.display(); triangle1.length(); triangle1.are(); system("pause" ); return 0 ; }
2. 时钟 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 #include <iostream> using namespace std ;class Clock { public : Clock(int h, int m, int s):_hour(h), _minute(m), _second(s) {} void setAlarm (int hour, int minute, int second) { h = hour; m = minute; s = second; } void run () ; void bell () ; void display () ; private : int _hour; int _minute; int _second; int h; int m; int s; }; void Clock::run () { while (true ) { if (_hour < h) { _second++; if (_second >= 60 ) { _second -= 60 ; _minute++; if (_minute >= 60 ) { _minute -= 60 ; _hour++; } } } else if (_hour == h) { if (_minute < m) { _second++; if (_second >= 60 ) { _second -= 60 ; _minute++; } } else if (_minute == m) { if (_second < s) { _second++; } else { bell(); break ; } } } } } void Clock::bell () { cout <<"Plink!Plink!Plink!..." <<endl ; } void Clock::display () { cout <<"Now: " <<_hour<<":" <<_minute<<":" <<_second<<endl ; } int main () { Clock clock (7 , 59 , 57 ) ; clock.display(); clock.setAlarm(8 , 59 , 57 ); clock.run(); clock.display(); system("pause" ); return 0 ; }
3. 最长公共子串 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 149 150 151 152 153 154 155 156 157 158 159 160 161 #include <iostream> #include <string.h> using namespace std ;class String { public : String() { len = 0 ; s = NULL ; } String(const String &s1) { len = s1.len; s = new char [s1.len + 1 ]; strcpy (s, s1.s); } ~String() { delete [] s; } void getstring () ; void display () ; friend String maxsubstring (String &, String &) ; private : int len; char *s; }; void String::getstring () { s = new char [200 ]; cout <<"输入字符串:" ; gets(s); len = strlen (s); } void String::display () { static int n = 0 ; n++; if (n < 3 ) { cout <<" s" <<n<<"字符串=" <<s<<" (长度为" <<len<<")" <<endl ; } else { int i, j; for (i = 0 , j = len - 1 ; i <= j;) { if (s[i] == 32 ) i++; if (s[j] == 32 ) j--; if (s[i] != 32 && s[j] != 32 ) break ; } if (i > j) cout <<" s1和s2最长公共字符串= (长度为0)" <<endl ; else { cout <<" s1和s2最长公共字符串=" <<s<<" (长度为" <<j - i + 1 <<")" <<endl ; } } } String maxsubstring (String & str1, String & str2) { int x; int **record = new int * [str1.len]; for (x = 0 ; x < str1.len; x++) record[x] = new int [str2.len]; int i, j; int max = 0 ; int last; for (i = 0 ; i < str2.len; i++) { for (j = 0 ; j < str1.len; j++) { if (str2.s[i] == str1.s[j]) { if (i == 0 || j == 0 ) { record[j][i] = 1 ; } else { record[j][i] = record[j - 1 ][i - 1 ] + 1 ; } } else record[j][i] = 0 ; if (max < record[j][i]) { max = record[j][i]; last = j; } } } for (x = 0 ; x < str1.len; x++) delete [] record[x]; delete [] record; if (max == 0 ) { cout <<"没有相同字符串!" <<endl ; exit (0 ); } char *s1 = new char [max + 1 ]; j = 0 ; for (i = last - max + 1 ; i <= last; i++) { s1[j] = str1.s[i]; j++; } s1[j] = '\0' ; String str; str.len = max; str.s = new char [max + 1 ]; strcpy (str.s, s1); delete [] s1; return str; } int main () { String str1, str2; str1.getstring(); str2.getstring(); str1.display(); str2.display(); String str3 (maxsubstring(str1, str2)) ; str3.display(); system("pause" ); return 0 ; }
4. 信息管理 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 #include <iostream> #include <string.h> using namespace std ;int main () { string user[] = {"xiao" , "shuang" , "Wangming" , "Xiaoming" }; long pass[] = {20170000 , 20170001 , 20170002 , 20170003 }; string name; cout <<"Input username: " ; cin >>name; int i, n; for (i = 0 ; user[i] != "" ; i++); for (int j = 0 ; j < i; j++) { if (user[j] == name) { n = j; break ; } } long password; cout <<"Input password: " ; cin >>password; if (password == pass[n]) { cout <<"Success login!" <<endl ; } else { cout <<"Default password!" <<endl ; } system("pause" ); return 0 ; }
5. 课后习题 这个太琐碎了,就放在 gitee 上了点此处跳转
实验报告六 1. 课后习题 这个太琐碎了,就放在 gitee 上了点此处跳转
2. 建筑物 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 #include <iostream> #include <string> using namespace std ;class Building { public : Building(string n, int f, int r, float a): name(n), floor (f), room(r), area(a) {} void display () { cout <<"建筑物名称:" <<name<<endl ; cout <<"层数:" <<floor <<endl ; cout <<"房间数:" <<room<<endl ; cout <<"面积:" <<area<<endl ; } private : string name; int floor ; int room; float area; }; class Teach_Building : public Building{ public : Teach_Building(string n, int f, int r, float a, string fun): Building(n, f, r, a), function(fun) {} void display () { Building::display(); cout <<"建筑物功能:" <<function<<endl <<endl ; } private : string function; }; class Dorm_Building : Building{ public : Dorm_Building(string n, int f, int r, float a, int p): Building(n, f, r, a), peoples(p) {} void display () { Building::display(); cout <<"容纳人数:" <<peoples<<endl ; } private : int peoples; }; int main () { Teach_Building t ("4号楼" , 5 , 40 , 4000 , "计信教学楼" ) ; Dorm_Building d ("宿舍1栋" , 7 , 140 , 1200 , 500 ) ; t.display(); d.display(); return 0 ; }
实验报告七 课后习题 难得全是课后习题点此处跳转
实验报告八 1. 课后习题 这个太琐碎了,就放在 gitee 上了点此处跳转
2. 工资管理系统 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 #include <fstream> #include <iostream> #include <string> using namespace std ;class Person { public : virtual void input () {} friend void display (Person *p) ; protected : long number; string name; char sex; int salary; }; class Teacher :virtual public Person{ public : virtual void input () ; void pay () ; protected : int teaching_hours; string title; }; void Teacher::input () { cout <<"职工号:" ; cin >>number; cout <<"姓名:" ; cin >>name; cout <<"性别:" ; cin >>sex; cout <<"授课时长:" ; cin >>teaching_hours; int n; cout <<"1-----------教授" <<endl ; cout <<"2-----------副教授" <<endl ; cout <<"3-----------讲师" <<endl ; cout <<"4-----------助教" <<endl ; cout <<"请选择职称:" ; cin >>n; if (n == 1 ) title = "教授" ; if (n == 2 ) title = "副教授" ; if (n == 3 ) title = "讲师" ; if (n == 4 ) title = "助教" ; } void Teacher::pay () { if (title == "教授" ) salary = teaching_hours * 80 + 6000 ; else if (title == "副教授" ) salary = teaching_hours * 70 + 5000 ; else if (title == "讲师" ) salary = teaching_hours * 60 + 4000 ; else salary = teaching_hours * 50 + 3000 ; } class Staff :virtual public Person{ public : void input () ; void pay () ; protected : string position; }; void Staff::input () { cout <<"职工号:" ; cin >>number; cout <<"姓名:" ; cin >>name; cout <<"性别:" ; cin >>sex; int n; cout <<"1-----------院级员工" <<endl ; cout <<"2-----------处级员工" <<endl ; cout <<"3-----------科级员工" <<endl ; cout <<"4-----------一般工作人员" <<endl ; cout <<"请选择职称:" ; cin >>n; if (n == 1 ) position = "院级员工" ; if (n == 2 ) position = "处级员工" ; if (n == 3 ) position = "科级员工" ; if (n == 4 ) position = "一般工作人员" ; } void Staff::pay () { if (position == "院级员工" ) salary = 6000 ; else if (position == "处级员工" ) salary = 5000 ; else if (position == "科级员工" ) salary = 4000 ; else if (position == "一般工作人员" ) salary = 3000 ; } class Temporary_workers :public Person{ public : void input () ; void pay () ; protected : int hourlyPay; int workingHours; }; void Temporary_workers::input () { cout <<"职工号:" ; cin >>number; cout <<"姓名:" ; cin >>name; cout <<"性别:" ; cin >>sex; cout <<"时薪:" ; cin >>hourlyPay; cout <<"工作时长:" ; cin >>workingHours; } void Temporary_workers::pay () { salary = workingHours * hourlyPay; } class Staff_Teacher :public Teacher, public Staff{ public : void input () ; void pay () ; }; void Staff_Teacher::input () { cout <<"职工号:" ; cin >>number; cout <<"姓名:" ; cin >>name; cout <<"性别:" ; cin >>sex; cout <<"授课时长:" ; cin >>teaching_hours; int n; cout <<"1-----------教授" <<endl ; cout <<"2-----------副教授" <<endl ; cout <<"3-----------讲师" <<endl ; cout <<"4-----------助教" <<endl ; cout <<"请选择教师职称:" ; cin >>n; if (n == 1 ) title = "教授" ; if (n == 2 ) title = "副教授" ; if (n == 3 ) title = "讲师" ; if (n == 4 ) title = "助教" ; cout <<"1-----------院级员工" <<endl ; cout <<"2-----------处级员工" <<endl ; cout <<"3-----------科级员工" <<endl ; cout <<"4-----------一般工作人员" <<endl ; cout <<"请选择行政员工职称:" ; cin >>n; if (n == 1 ) position = "院级员工" ; if (n == 2 ) position = "处级员工" ; if (n == 3 ) position = "科级员工" ; if (n == 4 ) position = "一般工作人员" ; } void Staff_Teacher::pay () { Staff::pay( ); if (title == "教授" ) salary = teaching_hours * 80 + 6000 + salary * 0.5 ; else if (title == "副教授" ) salary = teaching_hours * 70 + 5000 + salary * 0.5 ; else if (title == "讲师" ) salary = teaching_hours * 60 + 4000 + salary * 0.5 ; else salary = teaching_hours *50 +3000 + salary * 0.5 ; } int menu () { int n; cout <<"***************欢迎使用工资管理系统********************" <<endl ; cout <<"*** 1、教师 ***" <<endl ; cout <<"*** 2、一般职工 *** " <<endl ; cout <<"*** 3、临时工 *** " <<endl ; cout <<"*** 4、双肩挑教师 *** " <<endl ; cout <<"*** 0、退出 *** " <<endl ; cout <<"请选择:" ; cin >>n; return n; } void display (Person *p) { ofstream outfile ("demo23.dat" , ios::out |ios::app) ; outfile<<" 工号 姓名 性别 收入\n" ; outfile<<" " <<p -> number<<" " <<p -> name<<" " <<p -> sex<<" " <<p -> salary<<"\n" ; outfile<<" " <<"-------------------------------------------------------------\n" ; cout <<" 工号 姓名 性别 收入" <<endl ; cout <<" " <<p -> number<<" " <<p -> name<<" " <<p -> sex<<" " <<p -> salary<<endl ; cout <<" " <<"-------------------------------------------------------------" <<endl ; outfile.close(); } int main () { int n; Teacher t; Staff s; Temporary_workers t_w; Staff_Teacher s_t ; do { n = menu(); switch (n) { case 1 : t.input(); t.pay(); display(&t); break ; case 2 : s.input(); s.pay(); display(&s); break ; case 3 : t_w.input(); t_w.pay(); display(&t_w); break ; case 4 : s_t .input(); s_t .pay(); display(&s_t );; break ; case 0 : cout <<"感谢使用!程序退出中~~~" <<endl ; break ; } } while (n != 0 ); system("pause" ); return 0 ; }
完结结课! 撒花撒花