您好,欢迎来到九壹网。
搜索
您的当前位置:首页MOOC西安交通大学C++编程作业

MOOC西安交通大学C++编程作业

来源:九壹网
MOOC西交大C++编程作业

一周目

1、1-1我爱C++(20分) 题目内容:

在屏幕上显示下列两句话 Hello C++.

I like programming.

提示:本题与helloworld类似,只是显示多行信息。 #include int main() { using namespace std; cout << \"Hello C++.\\nI like programming.\" << endl; return 0; }

2、1-2来自系统的问候(20分) 题目内容:

编写程序,输入一个人的名字,系统显示Hello ***.。 人名中间可能会有空格。 程序运行结果如下: Zhang Wei

Hello Zhang Wei.

提示:输入带空格的字符串,用cin.getline()。 #include int main() { using namespace std; char name[50]; cin.getline(name, 49); cout << \"Hello \" << name << '.' << endl; return 0; }

3、1-3乘法计算器(20分) 题目内容:

编写一个乘法计算器程序。用户输入两个数,计算它们的乘积并显示。 程序运行结果如下: 3.4 72 244.8

提示:声明三个double类型的变量,乘法用*号。 #include int main() {

using namespace std; double a, b, c; cin >> a >> b; c = a*b; cout << c << endl; return 0; }

4、1-4单位换算(20分) 题目内容:

编写一个程序,将英寸换算为厘米。输入英寸,输出厘米。 换算关系:1inch=2.54cm 程序运行结果如下: 14

14inch=35.56cm

提示:显示:输入的数、\"inch=\"、换算结果和\"cm\"。 #include const double Inch = 2.54; int main() { using namespace std; double inch; cin >> inch; cout << inch << \"inch=\" << inch*Inch << \"cm\" << endl; return 0; }

5、1-5平方根计算器(20分) 题目内容:

编写程序,计算一个正数的平方根。用户输入一个正数(可能为实数),输出它的平方根。 程序运行结果如下: 2

1.41421

提示:开平方使用函数sqrt(x),x是双精度型,需要包含头文件cmath。 #include #include int main() { using namespace std; double a; cin >> a; cout << sqrt(a) << endl; return 0; }

二周目

1、温度转换(20分) 题目内容:

输入华氏温度,用下列公式将其转换为摄氏温度并输出。 C=5/9*(F-32)

#include using namespace std; int main() { double C, F; cin >> F; C = (5.0 / 9.0 * (F - 32)); cout << C << endl; return 0; }

2、计算数学函数式的值(20分) 题目内容: 编程求函数

y=sin(x*x)/(1-cos(x)) 的值。

#include #include using namespace std; int main() { double x, y; cin >> x; y = sin(x*x) / (1 - cos(x)); cout << y << endl; return 0; }

3、数据的简单统计(20分) 题目内容:

编程实现,用户从键盘输入3个整数,计算并打印这三个数的和、平均值及平均值的四舍五入整数值。 注意:输入的三个整数、它们的和、平均值的四舍五入值用整型变量表示,平均值用双精度变量表示。 #include using namespace std; int main() { int x, y, z, sum = 0; double average; cin >> x >> y >> z; sum = x + y + z; cout << sum << endl; average = sum / 3.0; cout << average << endl; average = int(average + 0.5); cout << average << endl; return 0; }

4、找零钱(20分) 题目内容:

为顾客找零钱时,希望选用的纸币张数最少。例如73元,希望零钱的面值为五十元1张,二十元1张,一元3张。设零钱面值有五十元、二十元、十元、五元和一元,请编写程序,用户输入100以下的数,计算找给顾客的各面值的纸币张数,数据间以空格隔开。 #include using namespace std; int main() { unsigned int cash, m1, m5, m10, m20, m50; cin >> cash; m50 = cash / 50; cash = cash - 50 * m50; m20 = cash / 20; cash = cash - 20 * m20; m10 = cash / 10; cash = cash - 10 * m10; m5 = cash / 5; cash = cash - 5 * m5; m1 = cash; cout << m50 << ' ' << m20 << ' ' << m10 << ' ' << m5 << ' ' << m1 << endl; return 0; }

5、小写转大写(20分) 题目内容:

用户输入一个字符,如果是小写字母输出对应的大写字母,其他字符不转换。 提示:使用三目条件运算符 ...?... :... #include using namespace std;

int main() { char c; c = cin.get();

(c<'A' || c>'z') ? c : (c >= 'A'&&c <= 'Z') ? c : c -= 32; cout << c << endl; return 0; }

三周目

1、3-1打印3个相邻字母(20分) 题目内容:

当用户输入一个英文字母后,程序能够按照字母表的顺序打印出3个相邻的字母,其中用户输入的字母在中间。

#include using namespace std;

int main() { char x, a, b; cin >> x; if (x >= 65 && x <= 90)

{ a = (x - 'A' - 1 + 26) % 26 + 'A'; b = (x - 'A' + 1) % 26 + 'A'; } else { a = (x - 'a' - 1 + 26) % 26 + 'a'; b = (x - 'a' + 1) % 26 + 'a'; } cout << a << x << b << endl; return 0; }

2、3-2歌唱大赛选手成绩计算(20分) 题目内容:

歌唱大赛选手成绩计算方法如下:去掉一个最高分,去掉一个最低分,将剩下分数的平均值作为选手的最后成绩。这里假设共有10位评委,都是按照百分制打分。 #include using namespace std; int main() { int arr[10]; int max, min, sum; max = sum = 0; min = 101; double average; for (int i = 0; i < 10; i++) { cin >> arr[i]; if (arr[i] > 100 || arr[i] < 0) { cout << \"the score is invalid.\" << endl; return 0; } if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; sum = sum + arr[i]; } average = double(sum - max - min) / 8; cout << average << endl; return 0;

}

3、3-3猴子吃桃(20分) 题目内容:

有一天,某只猴子摘了一些桃子,当时吃了一半,又不过瘾,于是就多吃了一个。以后每天如此,到第n天想吃时,发现就只剩下一个桃子。输入n,表示到第n天剩下1个桃子,请计算第一天猴子摘的桃子数。 #include using namespace std; int main() { int peach = 1; int day; cin >> day; for (int i = 0; i < day - 1; ++i) { if (day > 1) peach = 2 * (peach + 1); else if (day== 1) peach = 1; else cerr << \"input error.\" << endl; } cout << peach << endl; return 0; }

4、3-4搬砖问题(20分) 题目内容:

现有n块砖,要由n人一次搬完,假定男人一次可以搬4块,女人一次可以搬3块,两个小孩搬1块,计算这n人中男人、女人和小孩的人数。输入人数和砖数n,输出可能的解决方案。 #include using namespace std; int main() { int men, women, children; int n; int x, y, z; cin >> n; for (men = 0; men <= n / 4; men++) { for (women = 0; women <= n / 3; women++) { children = 2 * (n - 4 * men - 3 * women); if (children > 0 && children == n - men - women) { x = men; y = women; z = children; cout << \"men\" << men << endl; cout << \"women\" << women << endl; cout << \"children\" << children << endl;

} } } if (x == 0 || y == 0 || z == 0) cout << \"no result!\" << endl; return 0; }

5、3-5美分找钱(20分) 题目内容:

将n美分转换成25、10、5和1美分的硬币总共有多少种转换方法? #include using namespace std; int main() { int n, count = 0; cin >> n; if (n < 0 || n > 99) { cout << \"the money is invalid!\" << endl; } else { for (int i = 0; i <= n; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= n; ++k) for (int l = 0; l <= n; ++l) if (25 * i + 10 * j + 5 * k + l == n) count = count + 1; cout << count << endl; } return 0; }

四周目

1、恺撒加密(20分) 题目内容:

恺撒加密法加密规则是:将原来的小写字母用字母表中其后面的第3个字母的大写形式来替换,大写字母按同样规则用小写字母替换,对于字母表中最后的三个字母,可将字母表看成是首未衔接的。如字母c就用F来替换,字母y用B来替换,而字母Z用c代替。编程实现以下功能:输入一个字符串,将其加密后输出。

程序运行结果如下: AMDxyzXYZ dpgABCabc

#include using namespace std; int main()

{ char str[100] = { 0 }; char sub[100] = { 0 }; cin >> str; for (int i = 0; str[i] != '\\0'; ++i) { if (str[i] >= 'A'&&str[i] <= 'Z') sub[i] = 'a' + (str[i] - 'A' + 3) % 26; else if (str[i] >= 'a'&&str[i] <= 'z') sub[i] = 'A' + (str[i] - 'a' + 3) % 26; } cout << sub << endl; return 0; }

2、矩阵转置(20分) 题目内容:

用户输入矩阵阶数,然后按行输入所有矩阵元素(整数),将该矩阵转置输出。阶数应是[1,5]之间的整数,不在该区间时,显示“matrix order error”。 #include using namespace std; int main() { int n; int arr1[5][5], arr2[5][5]; cin >> n; if (n < 1 || n>5) cout << \"matrix order error\" << endl; else { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr1[i][j]; arr2[j][i] = arr1[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == n - 1) cout << arr2[i][j]; else cout << arr2[i][j] << \" \"; } cout << endl;

} } return 0; }

3、按点击率显示歌曲(20分) 题目内容:

连续录入5首歌的歌名、歌手和点击率清单并按照点击率由高到低的顺序显示歌曲清单的信息。如果点击率相同,则按照录入的顺序显示。 歌曲清单格式如下: 曲名 演唱者 点击率 #include using namespace std; struct music { char musicName[50]; char Artists[20]; int clicked; };

int main() { music m[5]; for (int i = 0; i < 5; ++i) cin >> m[i].musicName >> m[i].Artists >> m[i].clicked; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5 - i - 1; ++j) { if (m[j].clicked < m[j + 1].clicked) swap(m[j], m[j + 1]); } } for (int i = 0; i < 5; ++i) cout << m[i].musicName << \" \" << m[i].Artists << \" \" << m[i].clicked << endl; return 0; }

4、星期转换(20分) 题目内容:

将用户输入的阿拉伯数字转换成对应星期几的英文单词(monday,tuesday,wednesday,thursday,friday,saturday,sunday)。输入1显示\"monday\输入7显示“sunday”,如果用户输入的数字不在1~7之间,显示信息:invalid #include using namespace std; int main() { unsigned n; cin >> n; if (n < 1 || n>7)

cout << \"invalid\" << endl; else { switch (n) { case 1: cout << \"monday\" << endl; break; case 2: cout << \"tuesday\" << endl; break; case 3: cout << \"wednesday\" << endl; break; case 4: cout << \"thursday\" << endl; break; case 5: cout << \"friday\" << endl; break; case 6: cout << \"saturday\" << endl; break; case 7: cout << \"sunday\" << endl; break; } } return 0; }

5、插入加密(20分) 题目内容:

插入式加密是在明文字母中按照指定间隔插入另一些字母以形成密文。例如对明文china,在间隔为1的位置插入其它字母序列中的字母a,b,c,d,e,就变成密文cahbicndae;间隔为2时的密文为chainbac,要求输入明文和间隔,从存放其它字母的序列(仅包含a,b,c,d,e)中依次取出对应字母插入到明文中,如果其它字母序列的字母取完,则从头再取,要求密文中最后一个字母一定是其它字母序列中的字母。 #include #include using namespace std; int main() { string s[5] = { \"a\ string text1, text2; int k; cin >> text1; cin >> k; int len = text1.length();

}

int p;

if (len % k == 0) p = len / k; else p = len / k + 1; for (int i = 0; i < p; i++) text2 += (text1.substr(i*k, k) + s[i % 5]); cout << text2 << endl; return 0;

五周目

1、编写字符串反转函数mystrrev(20分) 题目内容:

编写字符串反转函数mystrrev,该函数的功能是将指定字符串中的字符顺序颠倒(前变后,后变前)。然后再编写主函数验证之。注意,输入输出应在主函数中进行。 函数原型为 void mystrrev(char str[]) #include using namespace std; void mystrrev(char str[]) { int count = 0; char *head = str, *tail = nullptr, temp; while (*head++) //一定要用复制计算长度,如果源计算长度会改变源的起始位置 count++; head = str; tail = str + (count - 1); while (head < tail) { temp = *head; *head = *tail; *tail = temp; head++; tail--; } cout << str << endl; }

int main() { char str[100] = { 0 }; cin.getline(str, 99); mystrrev(str); return 0; }

2、编写一组求数组中最大最小元素的函数(20分) 题目内容:

编写一组求数组中最大最小元素的函数。该组函数的原型为 int imax(int array[], int count); // 求整型数组的最大元素 int imin(int array[], int count); // 求整型数组的最小元素

其中参数count为数组中的元素个数,函数的返回值即为求得的最大或最小元素之值。要求同时编写出主函数进行验证。 #include using namespace std;

int imax(int array[], int count); int imin(int array[], int count); int main() { int n, Max, Min, array[100] = { 0 }; cin >> n; for (int i = 0; i < n; ++i) cin >> array[i]; Max = imax(array, n); Min = imin(array, n); cout << Max << '\\n' << Min << endl; return 0; }

int imax(int array[], int count) { int max = -999; for (int i = 0; i < count; ++i) { if (array[i] > max) max = array[i]; } return max; }

int imin(int array[], int count) { int min = 999; for (int i = 0; i < count; ++i) { if (array[i] < min) min = array[i]; } return min; }

3、编写函数判断一个整数是否为素数(20分) 题目内容:

编写函数int isprime(int a);用来判断整数a是否为素数,若是素数,函数返回1,否则返回0。调用该函数找出任意给定的n个整数中的素数。 注意,1不是素数。 #include using namespace std; int isprime(int a)

{ if (a == 1) return 1; //返回1表示不是素数 for (int i = 2; i <= a / 2; ++i) { if (a%i == 0) { return 1; break; } } return 0; //返回0表示是素数 }

int main() { int x, count = 0; int a[100], b[25] = { 0 }; int *p = b; for (int i = 0; i < 100; ++i) { cin >> a[i]; if (a[i] == 0) break; x = isprime(a[i]); if (x == 0) { *p = a[i]; *p++; count++; } } for (int i = 0; i < count; ++i) { if (i == count - 1) cout << b[i]; else cout << b[i] << ' '; } return 0; }

4、编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母(20分) 题目内容:

编写函数去除字符串中包含的非字母字符(不包括空格),并将小写字母转换成大写字母。 注意,不在函数中输出。输入输出应在主函数中进行。

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。 #include using namespace std;

char *delSymbol(char* str) { int count = 0, lenth = 0; char arr[200], *p = str; while (*p++) lenth++; p = str; for (int i = 0; i < lenth; ++i) { if (str[i] >= 'a'&&str[i] <= 'z') { arr[count] = str[i] - 32; count++; } else if (str[i] >= 'A'&&str[i] <= 'Z' || str[i] == ' ') { arr[count] = str[i]; count++; } } arr[count] = '\\0'; //一定要给结尾添加杠0不然不是完整的字符串 for (int i = 0; i < lenth; ++i) p[i] = arr[i]; return p; }

int main() { char arr[200]; cin.getline(arr, 199); cout << delSymbol(arr) << endl; return 0; }

5、编写函数计算一个英文句子中的单词个数(20分) 题目内容:

编写函数计算一个英文字符串中的单词个数。 输入格式:

一个最长500个字母的英文字符串,不包含数字和特殊字符,但可能包含一些英文标点符号(逗号、句点、问号)。标点符号出现时不视为一个单词。 单词间可能包含一个或多个空格。 输出格式:

该句子的单词个数

注意:本题应使用字符数组实现,不能使用字符串处理库函数,不能使用string类。 #include using namespace std; int main() { int lenth = 0, count = 0; char arr[500], *p = arr;

}

cin.getline(arr, 499); while (*p++) lenth++;

for (int i = 0; i < lenth; ++i) { if ((arr[i] >= 'a'&&arr[i] <= 'z') || (arr[i] >= 'A'&&arr[i] <= 'Z')) { count++; while ((arr[i] >= 'a'&&arr[i] <= 'z') || (arr[i] >= 'A'&&arr[i] <= 'Z')) i++; } else i++; }

cout << count << endl; return 0;

六周目

1、递归猴子摘桃(20分) 题目内容:

猴子摘桃:一天,一只猴子摘了若干桃子,当天吃掉一半,觉得不过瘾,又吃了一个;第二天将剩下的桃子吃掉一半又多吃了一个;…,每天将前一天剩下的桃子吃掉一半又多吃一个,直到第n天,发现只剩下一个桃子,问第一天它摘了多少桃子。

编写递归函数,计算第一天猴子摘的桃子的数量。在主函数中输入n,调用函数计算第一天摘的桃子的数量,在主函数中输出。

【提示】函数格式:int monkeyandPeak(int k,int n),其中n是1只桃子的天数,k是求哪天的桃子数,返回是第k天的桃子数。主函数的调用格式:

count= monkeyandPeak(1,n); //第n天只剩1只桃,求第1天的桃子数 【注意】使用递归实现。 #include using namespace std;

int monkeyAndPeak(int peach, int day) { if (day == 1) return peach; else

return peach = monkeyAndPeak(((1 + peach) * 2), --day);

}

int main() { int a, sum; cin >> a; sum = monkeyAndPeak(1, a); cout << sum << endl; return 0;

}

2、编写内联函数求矩形的面积和周长(20分) 题目内容:

编写函数求矩形的面积和周长,由于算式非常简单,请使用内联函数方式编写,提高程序运行效率 #include using namespace std;

inline double area(double a, double b) { return a*b; }

inline double Circumference(double a, double b) { return a * 2 + b * 2; }

int main() { double a, b; cin >> a >> b; cout << area(a, b) << ' ' << Circumference(a, b) << endl; return 0; }

3、编写重载函数显示字符串(20分) 题目内容:

编写函数 print_spaced 显示字符串,要求显示出的字符串每个字母之间都有一个空格。要求编写两个同名函数,一个支持字符数组输入,另一个支持string类型输入。然后编写main函数测试这两个函数,第一个使用字符数组输入,第二个使用string类型输入。注意字符串的最后一个字母后面没有空格。 #include #include using namespace std;

void print_spaced(char* str) { int count = 0; char str2[100] = { 0 }; for (int i = 0; str[i]!='\\0'; ++i) { str2[count] = str[i]; count++; str2[count] = ' '; count++; } str2[count - 1] = '\\0'; cout << str2 << endl; }

void print_spaced(string str) { int len = str.length(); string str2;

int i; for (i = 0; i < len - 1; ++i) { str2 += (str.substr(i * 1, 1) + ' '); } str2 = str2 + str.substr(i, 1); cout << str2 << endl; }

int main() { char arr[100]; string str; cin.getline(arr, 99); getline(cin, str); print_spaced(arr); print_spaced(str); return 0; }

4、排序函数重载(20分) 题目内容:

编写一组重载的排序函数,可以对两个整数、三个整数、四个整数、整数数组从大到小排序,函数名为sort,其中数组排序应使用递归的方法,另补充print函数,在一行显示排序后的数组元素。 主函数如下: int main() {

int a,b,c,d; int data[100]; int k,n,i; cin>>k; switch(k) {

case 1:

cin>>a>>b; sort(a,b);

cout<cin>>a>>b>>c; sort(a,b,c);

cout<cin>>a>>b>>c>>d; sort(a,b,c,d);

cout<cin>>n;

for(i=0;icin>>data[i]; }

sort(data,n); print(data,n); break; }

return 0; }

============================================================ #include using namespace std; void sort(int& a, int& b) { int tmp; if(avoid sort(int& a, int& b, int& c) { sort(a, b); sort(a, c); sort(b, c); }

void sort(int& a, int& b, int& c, int& d) { sort(a, b, c); sort(a, d); sort(b, c, d); }

void sort(int* a,int n) { if (n > 0) { for (int i = 0; i < (n - 1); ++i) sort(a[i], a[i + 1]); sort(a, n - 1); } }

void print(int* a, int n) {

for (int i = 0; i < n; ++i) { if (i == (n - 1)) cout << a[i]; else cout << a[i] << ' '; } }

int main() { int a, b, c, d; int data[100]; int k, n, i; cin >> k; switch (k) { case 1: cin >> a >> b; sort(a, b); cout << a << \" \" << b << endl; break; case 2: cin >> a >> b >> c; sort(a, b, c); cout << a << \" \" << b << \" \" << c << endl; break; case 3: cin >> a >> b >> c >> d; sort(a, b, c, d); cout << a << \" \" << b << \" \" << c << \" \" << d << endl; break; case 4: cin >> n; for (i = 0; i < n; i++) { cin >> data[i]; } sort(data, n); print(data, n); break; } return 0; }

5、编写递归函数来使字符串逆序(20分) 题目内容:

编写函数来使一个字符串逆序输出,要求必须用递归函数。 【注意】使用字符数组和递归实现。

#include #include using namespace std;

void ReversChar(int first, int end, char* str, int len) { if (len > (end / 2)) //使用字符串长度不断减少的方式交换头尾字符 { char temp = str[first]; str[first] = str[len - 1]; str[len - 1] = temp; ReversChar(first + 1, end, str, len - 1);//改变头和尾的字符位置直到字符串长度不能二分为止 } }

void Show(char* arr, int lenth) { for (int i = 0; i < lenth; ++i) cout << arr[i]; }

int main() { char arr[100] = { 0 }; cin.getline(arr, 99); int lenth = strlen(arr); int end = lenth; int first = 0; ReversChar(first, end, arr, lenth); Show(arr, lenth); return 0; }

七周目

1、编写函数重置两个变量的值(20分) 题目内容:

编写函数重置两个变量的值,该函数的原型为 void reset(int *a, int *b); 函数内部将两个值重置为两个变量原值的平均数(出现小数则四舍五入)。 #include using namespace std; void reset(int *a, int *b) { if ((*a + *b) % 2 != 0) { *a = (*a + *b) / 2 + 1; *b = *a; } else {

*a = (*a + *b) / 2; *b = *a; } }

int main() { int x, y; cin >> x >> y; reset(&x, &y); cout << x << ' ' << y << endl; return 0; }

2、编写函数对数组中的元素求和(20分) 题目内容:

编写函数 add_array 对数组中的元素求和,函数原型为: void add_array(int a, int *sum);

该函数可以重复调用多次,每次只使用参数a传入数组中的一个元素,函数内部可以累计历次传入的值进行求和,每次执行后均把当前的和通过参数sum写入主函数中的某个变量中。提示:使用静态变量。 #include using namespace std;

void add_array(int a, int *sum) { *sum += a; }

int main() { int a[100]; int sum = 0; for (int i = 0; i < 100; ++i) { cin >> a[i]; if (a[i] != -1) add_array(a[i], &sum); else break; } cout << sum; return 0; }

3、数组清零(20分) 题目内容:

编写一个函数,用于将一个int类型的数组清零(即将指定前n项元素全部置为0)数组以-1结尾,且-1不包括在此数组中。要求数组使用地址传递(传指针)。

提示:本题只要在形参中使用整型指针,对应的实参是数组名(因为数组名是数组的首地址),函数中仍使用下标访问数组元素。例如 int a[100],*p=a; //a是数组a的首地址。则p[i]相当于a[i]。 #include using namespace std;

void eraseElement(int *a, int n) { for (int i = 0; i < n; ++i) a[i] = 0; }

int main() { int a[100]; int *p = a; int n, count = 0; for (int i = 0; i < 100; ++i) { cin >> a[i]; if (a[i] != -1) count++; else break; } cin >> n; eraseElement(p, n); for (int i = 0; i < count; ++i) { if (i == count - 1) cout << a[i]; else cout << a[i] << ' '; } return 0; }

4、使用函数指针切换加密方法(20分) 题目内容:

编写两个加密函数,第一个函数使用凯撒加密法,即将将原来的小写字母用字母表中其后面的第3个字母的大写形式来替换,大写字母按同样规则用小写字母替换,可将字母表看成是首末衔接的。例如\"AMDxyzXYZ\" 加密为 \"dpgABCabc\"。第二个函数使用单双号加密法,即将字符串\"abcde\根据单双号区分为两个字符串\"ace\"和\"bd\",再连接在一起成为密文\"acebd\"。

用户输入一个字符串作为明文,再输入数字1或2,输入1使用第一个函数加密并输出密文,输入2使用第二个函数加密并输出密文。要求使用函数指针来切换加密函数。 提示:三个函数的原型可设为: void caesar(char s[]); void oddeven(char s[]);

void cipher(void (*f)(char s[]),char s[]);//形参为指向函数的指针,对应实参可为相应格式的函数名。 #include #include using namespace std; void caesar(char s[]) { for (int i = 0; s[i] != '\\0'; ++i)

{ if (s[i] >= 'a'&&s[i] <= 'z') s[i] = (s[i] - 32 - 'A' + 3) % 26 + 'A'; else if (s[i] >= 'A'&&s[i] <= 'Z') s[i] = (s[i] + 32 - 'a' + 3) % 26 + 'a'; } }

void oddeven(char s[]) { int len = 0; int count = 0; char str[100]; while (s[len] != '\\0') len++; int i = (len+1) / 2; for (int k = 0; k < len; ++k) { if (k % 2 == 0) { str[count] = s[k]; count++; } else { str[i] = s[k]; i++; } } for (int j = 0; j < len; ++j) s[j] = str[j]; }

void cipher(void(*f)(char s[]), char s[]) { (*f)(s); }

int main() { char arr[100]; int b; cin.getline(arr, 99); cin >> b; if (b == 1) cipher(caesar, arr); else if (b == 2) cipher(oddeven, arr); for (int i = 0; i < strlen(arr); ++i) cout << arr[i];

return 0; }

5、编写求函数区间平均值的通用函数(20分) 题目内容:

编写求数学函数区间平均值的通用函数,可以计算出在指定区间内函数的平均值(取整即可)。 待求区间平均值的两个函数的原型为: int func1(int x); int func2(int x)

只考虑参数为整数的情况即可。

func1的数学表达式为:y=a*x^2+b*x+c,a,b,c由用户输入; func2的数学表达式为:y=x^m,m由用户输入;

通用函数的参数为待求区间平均值函数的指针,以及给出的区间下界与上界。 比如 func1 = 3*x^2+2*x+1, 区间下界与上界分别为0和3,则 func1(0)=1 func1(1)=6 func1(2)=17 func1(3)=34 则平均值为:(1+6+17+34)/4=14 (直接取整不四舍五入) 提示:(1)由于函数原型的,a,b,c和m参数可以使用全局变量传递。 (2)通用函数原型可设为:int avg( int (*f)(int),int x1,int x2); #include #include using namespace std; int a, b, c, m; int func1(int x); int func2(int x);

int avg(int(*f)(int), int x1, int x2); int main() { int upper, lower; cin >> a >> b >> c; cin >> m; cin >> lower >> upper; int x1 = avg(func1, lower, upper); int x2 = avg(func2, lower, upper); cout << x1 << '\\n' << x2 << endl; system(\"pause\"); return 0; }

int func1(int x) { return a * pow(x, 2) + b * x + c; }

int func2(int x) { return pow(x, m); }

int avg(int(*f)(int), int x1, int x2) { int sum = 0; for (int i = x1; i <= x2; ++i) sum += (*f)(i); return sum / (x2 - x1 + 1); }

八周目

1、输出数字的英文名称(20分) 题目内容:

编写一个函数,将表示数字的数值(0-12)转换成对应的英文名称(小写)。用户输入阿拉伯数字,程序输出对应数的英文单词。要求必须使用指针数组完成。 【提示】:函数格式: char * digitName(int n); #include //这种方法有点不合题意 using namespace std; int main() { int num; cin >> num; char str[][8]{ \"zero\ \"four\ \"eight\ char(*p)[8] = str; cout << p[num]; return 0; }

#include //符合题意的方法 using namespace std; char* digitName(int n) { static char str[][8] = { \"zero\ \"four\ \"eight\ char(*p)[8] = str; return p[n]; }

int main() { unsigned num; cin >> num; cout << digitName(num) << endl; return 0; }

2、去除字符串首尾多余的空格(20分) 题目内容:

用户输入一个字符串,首尾有多余的空格,编写程序来去除这些多余的空格。要求必须使用指针来完成本题。

#include using namespace std; int main() { int length = 0, start = 0, end; char arr[100]{}; char* c = arr; cin.getline(arr, 99, '#'); while (*c++) length++; c = arr; for (int i = 0; i < length; ++i) { if (c[i] != ' ') break; start++; } end = length; for (int i = length - 1; i > 0; --i) { if (c[i] != ' ') break; end--; } for (int i = start; i < end; ++i) cout << c[i]; cout << '#'; return 0; }

3、遍历二维数组(20分) 题目内容:

用户首先输入两个整数m和n,然后输入m*n个元素,建立一个m*n的二维数组。要求使用 行指针 来遍历这个二维数组,输出该数组中所有元素的和。 #include //动态申请二维数组 using namespace std; int main() { int **a; //指向指针的指针 int n, m;//n行 m列 int i, j, sum = 0; cin >> n >> m; //输入行数和列数 //申请空间 a = new int *[n]; //n个 int 指针 数组

for (i = 0; i < n; i++) //n个大小为m的一维数组 { a[i] = new int[m]; //1个大小为m的一维数组,a[i]是int指针 } //输入数据 for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { //cin>>a[i][j];//输入 cin >> *(*(a + i) + j);//同上a相当于行指针 sum += *(*(a + i) + j); } } //输出数据,提交时要删除这段 for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { //cout<4、动态申请大数组(20分) 题目内容:

本题要求编写两个函数并测试。

第一个函数原型为 int *new_array(int n); 该函数可以根据参数n动态申请n个元素的整型数组,最后将数组指针返回。

第二个函数原型为 void init_array(int *p, int n,int c); 将指定的n个元素的数组每个元素的值均初始化为c。 用户输入数组大小n和待初始化的值c,调用new_array函数申请空间,再调用init_array初始化,最后输出这个数组的所有元素。 #include using namespace std; int *new_array(int n) { int* p; p = new int[n]; return p;

}

void init_array(int *p, int n, int c) { for (int i = 0; i < n; ++i) p[i] = c; }

int main() { int n, c; cin >> n >> c; int *arr = new_array(n); init_array(arr, n, c); for (int i = 0; i < n; ++i) { if (i == n - 1) cout << *arr; else cout << *arr << ' '; arr++; } return 0; }

5、矩阵对角线元素之和(20分) 题目内容:

编写函数,求n阶方阵的对角线元素之和。编写主程序,用户输入矩阵的阶数n,动态申请n*n的存储空间,再输入n行、n列的元素,调用函数求矩阵的对角元素之和,在主函数中输出这个和。设元素均为整数。n>=1。

函数格式:int sumDiagonal(int *a,int n);

#include //不使用题目的函数做法 using namespace std; int main() { int n; int sum = 0; int* *arr; cin >> n; arr = new int*[n]; for (int i = 0; i < n; ++i) { arr[i] = new int[n]; for (int j = 0; j < n; ++j) cin >> arr[i][j]; } for (int i = 0; i < n; ++i) sum += arr[i][i]; cout << sum;

for (int i = 0; i < n; ++i) //删除与答题无关,只为养成良好的习惯 delete[] arr[i]; delete[] arr; return 0; } 6、(本题只记3分)十进制点分IP转换为32位二进制IP(3分) 题目内容:

编写程序,将十进制点分的IP转换为32位二进制IP地址。程序要能验证输入的十进制点分IP地址的合法性。用户输入的IP不和法时,输出\"data error\"。

请使用模块化程序设计的思想,将功能模块编写成函数。通过指针传递参数,操作数据,返回结果。在主函数中输入IP地址,调用函数进行合法性验证和转换,在主函数中输出32位二进制IP。 提示:十进制转换为二进制。对整数部分,除2取余,直到商为0。例如 13/2=6.....1(低位) 6/2=3......0 3/2=1......1 1/2=0......1

转换后的二进制数位1101

#include //递归法转换IP为二进制 using namespace std; void dec2bin(int n, int k) { if (k > 1) dec2bin(n >> 1, k - 1); cout << n % 2; }

int main() { int a, b, c, d; scanf(\"%d.%d.%d.%d\ if ((a > 255 || a < 0) || (b > 255 || b < 0) || (c > 255 || c < 0) || (d > 255 || d < 0)) cout << \"data error\"; else { dec2bin(a, 8); dec2bin(b, 8); dec2bin(c, 8); dec2bin(d, 8); } return 0; }

九周目

1、设计Person类(20分) 题目内容:

设计一个Person类,包含name、age、sex属性以及对这些属性操作的方法。实现并测试这个类。 根据类的封装性要求,把name、age、sex声明为私有的数据成员,声明公有的成员函数Register()、ShowMe()

来访问这些属性,在Register()函数中对数据成员进行初始化。person1通过cin来得到信息,person2通过Register(\"Zhang3\来得到信息。 #include #include using namespace std; class Preson {

public: void Register(string m_name, int m_age, char m_sex); void ShowMe(); private: string name; int age; char sex; };

int main() { Preson preson1, preson2; string name; int age; char sex; cin >> name >> age >> sex; preson1.Register(name, age, sex); preson2.Register(\"Zhang3\ preson1.ShowMe(); preson2.ShowMe(); return 0; }

void Preson::Register(string m_name, int m_age, char m_sex) { name = m_name; age = m_age; sex = m_sex; }

void Preson::ShowMe() { cout << name << ' ' << age << ' ' << sex << endl; }

2、设计Dog类(20分) 题目内容:

设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。

根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。 #include #include using namespace std;

class Dog {

public: void setdata(string Dname, int Dage, string Dsex, double Dweigth); void GetName(); void GetAge(); void GetSex(); void GetWeight(); private: string name; int age; string sex; double weight; };

int main() { Dog dog; string name; int age; string sex; double weight; cin >> name >> age >> sex >> weight; dog.setdata(name, age, sex, weight); cout << \"It is my dog.\" << endl; dog.GetName(); dog.GetAge(); dog.GetSex(); dog.GetWeight(); return 0; }

void Dog::setdata(string Dname, int Dage, string Dsex, double Dweigth) { name = Dname; age = Dage; sex = Dsex; weight = Dweigth; }

void Dog::GetName() { cout << \"Its name is \" << name << \".\" << endl; }

void Dog::GetAge() { cout << \"It is \" << age << \" years old.\" << endl; }

void Dog::GetSex() {

if (sex == \"m\") cout << \"It is male.\" << endl; else cout << \"It is female.\" << endl; }

void Dog::GetWeight() { cout << \"It is \" << weight << \" kg.\" << endl; }

3、设计并测试Trapezium类(20分) 题目内容:

设计并测试一个名为Trapezium的梯形类,其属性为梯形的四个顶点的坐标。该梯形上边和下边均和x轴平行。

根据类的封装性要求,在类的声明中用8个私有的整型变量表示4个点的坐标值,声明成员函数initial(int,int,int,int,int,int,int,int)初始化数据成员,函数GetPosition(int&,int&,int&,int&,int&,int&,int&,int&)读取坐标值,函数Area()计算面积。梯形的面积,依次为左上(x1,y1)、右上(x2,y2)、左下(x3,y3)和右下(x4,y4)角的顶点。

#include using namespace std; class Trapezium {

public: void InitTra(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); void GetPosition(int &x1, int &y1, int &x2, int &y2, int &x3, int &y3, int &x4, int &y4); void Area(); private: int xur, yur; int xul, yul; int xdl, ydl; int xdr, ydr; };

int main() { int a1, a2, b1, b2, c1, c2, d1, d2; cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2 >> d1 >> d2; Trapezium Tt; Tt.InitTra(a1, a2, b1, b2, c1, c2, d1, d2); Tt.Area(); return 0; }

void Trapezium::InitTra(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { xur = x2; yur = y2; xul = x1; yul = y1; xdl = x3;

ydl = y3; xdr = x4; ydr = y4; }

void Trapezium::GetPosition(int & x1, int & y1, int & x2, int & y2, int & x3, int & y3, int & x4, int & y4) { cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << \" \" << x3 << \" \" << y3 << \" \" << x4 << \" \" << y4 << endl; }

void Trapezium::Area() { double s = (xur - xul + xdr - xdl)*(yur - ydr) / 2; cout << s << endl; }

4、设计MyTime类(20分) 题目内容:

设计一个MyTime类,成员函数SetTime()设置时间,print_12()以12(0-11)小时制显示时间(AM上午,PM下午),print_24()以24(0-23)小时制显示时间。

按照12小时制和24小时制依次显示时间,注意时间格式中的冒号是英文冒号,时分秒都是两位,AM,PM前有一个空格,晚上12:00是00:00:00 AM,中午十二点是00:00:00 PM。 #include using namespace std; class MyTime {

public: void SetTime(int h, int m, int s); void print12(); void print24(); private: int hh, mm, ss; };

int main() { int h, m, s; cin >> h >> m >> s; MyTime tt; tt.SetTime(h, m, s); tt.print12(); tt.print24(); return 0; }

void MyTime::SetTime(int h, int m, int s) { hh = h; mm = m; ss = s; }

void MyTime::print12() { if ((hh - 12) == 0) cout << \"00\"; else if ((hh - 12) > 0) { if ((hh - 12) > 10) cout << hh - 12; else cout << 0 << hh - 12; } else { if (hh > 9) cout << hh; else cout << 0 << hh; } cout << \":\"; if (mm < 10) cout << 0 << mm; else cout << mm; cout << \":\"; if (ss < 10) cout << 0 << ss; else cout << ss; if ((hh - 12) > 0 || (hh == 12 && (mm != 0 || ss != 0))) cout << \" PM\" << endl; else if (((hh - 12) == 0) && mm == 0 && ss == 0) cout << \" PM\" << endl; else cout << \" AM\" << endl; }

void MyTime::print24() { if (hh < 10) cout << 0 << hh; else cout << hh; cout << \":\"; if (mm < 10) cout << 0 << mm; else cout << mm; cout << \":\";

if (ss < 10) cout << 0 << ss << endl; else cout << ss << endl; }

5、设计Weekday类(20分) 题目内容:

设计一个Weekday类,成员函数SetDay()设置星期几,IncDay()前进一天,NowDay()打印当前是星期几。 #include using namespace std; class Weekday {

public: void SetDay(int day); void IncDay(); void NowDay(); private: int today; };

int main() { int n; cin >> n; Weekday wday; wday.SetDay(n); wday.NowDay(); wday.IncDay(); wday.NowDay(); wday.IncDay(); wday.NowDay(); return 0; }

void Weekday::SetDay(int day) { today = day; }

void Weekday::IncDay() { today = (today + 1) % 7; }

void Weekday::NowDay() { switch (today) {

}

case 0: cout << \"星期日\" << endl; break; case 1: cout << \"星期一\" << endl; break; case 2: cout << \"星期二\" << endl; break; case 3: cout << \"星期三\" << endl; break; case 4: cout << \"星期四\" << endl; break; case 5: cout << \"星期五\" << endl; break; case 6: cout << \"星期六\" << endl; break; default: cout << \"Try again\"; break; }

十周目

1、定义一个带重载构造函数的日期类(20分) 题目内容:

定义一个带重载构造函数的日期类Date,数据成员有年、月、日;成员函数包括:一个带参数的构造函数Date(int,int,int),一个不带参数的构造函数(设置日期为1900年1月1日),一个按“年-月-日”格式显示日期的函数,一个对数据成员赋值的函数void init(int,int,int)。 主函数中对类的测试要求:

1. 分别使用两个不同的重载构造函数创建两个日期类对象(必须为d1,d2,d2初始值为2100-12-12); 2. 按“年-月-日”格式分别显示两个对象的值; 3. 输入数据,用init函数为d1赋值; 4. 按“年-月-日”格式显示对象d1的值;。 #include using namespace std; class Date {

public: Date() :_year(1900), _month(1), _day(1) {}; Date(int, int, int) :_year(2100), _month(12), _day(12) {}; ~Date() {};

void Init(int y, int m, int d); void Show(); private: int _year; int _month; int _day; };

int main() { Date *d1 = new Date(); Date *d2 = new Date(2100, 12, 12); int y, m, d; cin >> y >> m >> d; d1->Show(); d2->Show(); d1->Init(y, m, d); d1->Show(); delete d1, d2; return 0; }

void Date::Init(int y, int m, int d) { this->_year = y; this->_month = m; this->_day = d; }

void Date::Show() { cout << _year << \"-\" << _month << \"-\" << _day << endl; }

2、动态生成Person类的对象(20分) 题目内容:

编写Person类,数据成员为姓名(20字符长度)、年龄(int)和性别(char)。 编写无参数的构造函数,其中姓名赋值为“XXX”,年龄0,性别'm';

编写析构函数,在其中输出字符串“Now destroying the instance of Person”; 编写Register成员函数,为数据成员赋值;

编写showme成员函数,显示姓名、年龄和性别。 编写主函数:

用Person类创建2个指针,p1和 p2;

用new创建两个Person对象,分别将指针赋值给p1,p2; 用showme成员函数显示p1,p2所指对象的值;

再输入一组“姓名、年龄和性别”值,用成员函数Register为p1的成员赋值; 将p1所指对象的值赋值给p2所指对象; 用showme显示p1、p2所指对象的值。 删除动态对象。 #include #include

using namespace std; class Person {

public: Person() { strcpy(name, \"XXX\"); age = 0; sex = 'm'; } ~Person() { cout << \"Now destroying the instance of Person\" << endl; } void Register(char *n,int a, char s) { strcpy(name, n); age = a; sex = s; } void showme() { cout << name << \" \" << age << \" \" << sex << endl; } private: char name[20]; int age; char sex; };

int main() { char name[20]; int age; char sex; Person *p1 = new Person(); Person *p2 = new Person(); cin >> name >> age >> sex; cout << \"person1:\"; p1->showme(); cout << \"person2:\"; p2->showme(); p1->Register(name, age, sex); cout << \"person1:\"; p1->showme(); *p2 = *p1; cout << \"person2:\"; p2->showme(); delete p1; delete p2; return 0;

}

3、设计带构造函数的Dog类(20分) 题目内容:

设计一个Dog类,包含name、age、sex和weight等属性,在有参数的构造函数中对数据成员进行初始化。

公有成员函数有:GetName()、GetAge()、GetSex()和GetWeight()可获取名字、年龄、性别和体重。编写成员函数speak()

显示狗的叫声。编写主函数,输入狗的名字、年龄、性别和体重;声明Dog对象并用输入的数据通过构造函数初始化对象,通过成员函数获取狗的属性并显示出来。 #include #include using namespace std; class Ellipse {

public: Ellipse() :x(0), y(0), a(0), b(0) {} Ellipse(int, int, double, double); inline double Area() { return 3.14 * a * b; } private: int x, y; double a, b; };

int main() { int x, y; double a, b, s; cin >> x >> y >> a >> b; Ellipse *e = new Ellipse(x,y,a,b); s = e->Area(); cout << s << endl; delete e; return 0; }

Ellipse::Ellipse(int _x, int _y, double _a, double _b) { x = _x; y = _y; a = _a; b = _b; }

4、设计并测试一个椭圆类(20分) 题目内容:

设计并测试一个名为Ellipse的椭圆类,其属性为圆心坐标及长半轴和短半轴的长度。设计一个构造函数(Ellipse(int,int,double,double))对这些属性进行初始化,并通过成员函数计算出椭圆的面积(double Area())。

S(椭圆面积)=PI(圆周率)×a(长半轴)×b(短半轴)其中PI取3.14 #include

#include using namespace std; class Ellipse {

public: Ellipse() :x(0), y(0), a(0), b(0) {} Ellipse(int, int, double, double); inline double Area() { return 3.14 * a * b; } private: int x, y; double a, b; };

int main() { int x, y; double a, b, s; cin >> x >> y >> a >> b; Ellipse *e = new Ellipse(x,y,a,b); s = e->Area(); cout << s << endl; delete e; return 0; }

Ellipse::Ellipse(int _x, int _y, double _a, double _b) { x = _x; y = _y; a = _a; b = _b; }

5、设计一个多功能的MyTime类(20分) 题目内容:

设计一个多功能的MyTime类,设计多个重载的构造函数,可以设置时间,进行时间的加减运算,按各种可能的格式(24小时制、12小时制)输出时间。 注意:

(1)请考虑设置的时间的合理性(时0-23; 分0-59;秒0-59)。 (2)12小时制中,12:00:00前为AM, 12:00:00及以后为PM

(3)加减运算的加数、减数是一个时间的长度,单位为“时、分、秒”

(4)构造函数:没参数时,设置时间为0时 0分 0秒;有参数时,设置成给定的时、分、秒。 在主函数中

(1)声明两个对象t1,t2,并通过构造函数初始化它们(t2初始化为为8:10:30) (2)显示按12、14小时制显示t1、t2的时间。 (3)再设置t1的时间,数据由用户输入。 (4)再输入待加减的时间。

(5)t1加输入的时间,并按12小时和24小时制显示。 (6)t2减输入的时间,并按12小时和24小时制显示。 #include

#include using namespace std; class MyTime {

public: MyTime() :hour(0), minute(0), second(0) {} MyTime(int, int, int); inline void SetTime(int h, int m, int s) { hour = h; minute = m; second = s; } void AddTime(int h, int m, int s); void SubTime(int h, int m, int s); void Show12(); void Show24(); private: int hour, minute, second; };

int main() { MyTime *t1 = new MyTime; MyTime *t2 = new MyTime(8, 10, 30); int h1, h2, m1, m2, s1, s2; cin >> h1 >> m1 >> s1; cin >> h2 >> m2 >> s2; t1->Show12(); t1->Show24(); t2->Show12(); t2->Show24(); t1->SetTime(h1, m1, s1); t1->AddTime(h2, m2, s2); t2->SubTime(h2, m2, s2); t1->Show12(); t1->Show24(); t2->Show12(); t2->Show24(); delete t1; delete t2; return 0; }

MyTime::MyTime(int h, int m, int s) { hour = h; minute = m; second = s; }

void MyTime::AddTime(int h, int m, int s) {

hour += h; minute += m; second += s; if (second >= 60) { second -= 60; minute += 1; } if (minute >= 60) { minute -= 60; hour += 1; } if (hour >= 24) hour -= 24; }

void MyTime::SubTime(int h, int m, int s) { if (second < s) { second += 60; minute--; } if (minute < m) { minute += 60; hour--; } if (hour < h) hour += 24; hour -= h; minute -= m; second -= s; }

void MyTime::Show12() { int tmp; hour >= 12 ? tmp = (hour - 12) : tmp = hour; if (tmp < 10) cout << \"0\"; cout << tmp << \":\"; if (minute < 10) cout << \"0\"; cout << minute << \":\"; if (second < 10)

cout << '0'; cout << second; if (hour > 12 || ((hour == 12) && ((minute > 0) || (second > 0)))) cout << \" PM\" << endl; else cout << \" AM\" << endl; }

void MyTime::Show24() { if (hour < 10) cout << '0'; cout << hour << \":\"; if (minute < 10) cout << '0'; cout << minute << \":\"; if (second < 10) cout << '0'; cout << second << endl; }

十一周目

1、公有继承中派生类Student对基类Person成员的访问(20分) 题目内容:

已知基类Person的定义如下: class Person { char Name[20]; char Sex; int Age; public:

void Register(char *name, int age, char sex) ; void ShowMe(); };

请通过继承的方法建立一个派生类Student,其中 1.新增的数据成员有: int Number;

char ClassName[10]; 2.新增的成员函数有:

void RegisterStu(char *classname, int number, char *name, int age, char sex) //对数据成员赋值,并使用基类的Register

void ShowStu() //显示数据成员信息,并使用基类的ShowMe

在主程序中建立一个派生类对象,利用已有的成员函数分别显示派生类对象和基类对象的数据成员。 #include #include using namespace std; class Person

{

public: void Register(char* name, int age, char sex); inline void ShowMe() { cout << Name << ' ' << Age << ' ' << Sex << endl; } private: char Name[20]; char Sex; int Age; };

class Student :public Person {

public: void RegisterStu(char* classname, int number, char* name, int age, char sex); inline void ShowStu() { cout << Number << ' ' << ClassName << ' '; Person::ShowMe(); } private: int Number; char ClassName[10]; };

int main() { char name[20], classname[10]; char sex; int age, number; Student* stu1 = new Student; cin >> classname >> number >> name >> age >> sex; stu1->RegisterStu(classname, number, name, age, sex); stu1->ShowStu(); stu1->ShowMe(); delete stu1; return 0; }

void Person::Register(char* name, int age, char sex) { strcpy(Name, name); Age = age; Sex = sex; }

void Student::RegisterStu(char* classname, int number, char* name, int age, char sex) { Person::Register(name, age, sex); strcpy(ClassName, classname); Number = number; }

2、一个基类Person的多个派生类(20分) 题目内容:

已知基类Person的定义如下: class Person {

protected:

char Name[10]; char Sex; int Age; public:

void Register(char *name,int age,char sex); void ShowMe(); };

请通过继承的方法建立两个派生类,其中 派生类Teacher:

1.新增的数据成员有: char Dept[20]; int Salary;

2.新增的成员函数有:

构造函数,并使用基类的Register 3.重写的成员函数有:

void ShowMe() //显示数据成员信息,并使用基类的ShowMe 派生类Student:

1.新增的数据成员有: char ID[12]; char Class[12];

2.新增的成员函数有:

Student(char *name,int age,char sex, char *id,char *classid); 3.重写的成员函数有:

void ShowMe() //显示数据成员信息,并使用基类的ShowMe

在主程序中分别建立两个派生类对象,利用已有的成员函数分别显示两个派生类对象的数据成员。 #include #include using namespace std; class Person {

public: inline void Register(char* name, int age, char sex) { strcpy(Name, name); Age = age; Sex = sex; } inline void ShowMe() { cout << \"姓名 \" << Name << endl; cout << \"性别 \";

if (Sex == 'm') cout << \"男\" << endl; else cout << \"女\" << endl; cout << \"年龄 \" << Age << endl; } private: char Name[10]; char Sex; int Age; };

class Teacher :public Person {

public: Teacher(char* name, int age, char sex, char* dept, int salary) { Person::Register(name, age, sex); strcpy(Dept, dept); Salary = salary; } void ShowMe() { Person::ShowMe(); cout << \"工作单位 \" << Dept << endl; cout << \"月薪 \" << Salary << endl; } private: char Dept[20]; int Salary; };

class Student :public Person {

public: Student(char* name, int age, char sex, char* id, char* classid) { Person::Register(name, age, sex); strcpy(ID, id); strcpy(Class, classid); } void ShowMe() { cout << \"学号 \" << ID << endl; Person::ShowMe(); cout << \"班级 \" << Class << endl; } private: char ID[12];

char Class[12]; };

int main() { char name1[10], name2[10], dept[20], id[12], classid[12]; char sex1, sex2; int age1, age2, salay; cin >> name1 >> age1 >> sex1 >> dept >> salay; cin >> name2 >> age2 >> sex2 >> id >> classid; Teacher* tea = new Teacher(name1, age1, sex1, dept, salay); tea->ShowMe(); Student* stu = new Student(name2, age2, sex2, id, classid); stu->ShowMe(); delete tea; delete stu; return 0; }

3、派生类Student的构造函数和析构函数(20分) 题目内容:

已知基类Person的定义如下: class Person

{ char Name[10]; //姓名 int Age; //年龄 public:

Person(char* name,int age) { strcpy(Name, name); Age = age;

cout<<\"constructor of person \"<{ cout<<\"deconstructor of person \"<Student(char *name, int age, char *classname, char *name1, int age1) //name1和age1是班长的信息 ~Student()

在主程序中建立一个派生类对象。 #include #include using namespace std; class Person {

public: Person(char* name, int age) { strcpy(Name, name);

Age = age; cout << \"constructor of person \" << Name << endl; } ~Person() { cout << \"deconstructor of person \" << Name << endl; } private: char Name[10]; int Age; };

class Student :public Person {

public: Student(char* name, int age, char* classname, char* name1, int age1) : Person(name, age), Monitor(name1, age1) { strcpy(ClassName, classname); cout << \"constructor of Student\" << endl; } ~Student() { cout << \"deconstructor of Student\" << endl; } private: char ClassName[10]; Person Monitor; };

int main() { char name[10], name1[10], classname[10]; int age, age1; cin >> name >> age >> classname >> name1 >> age1; Student* stu = new Student(name, age, classname, name1, age1); delete stu; return 0; }

4、从Point类继承的Circle类(20分) 题目内容:

已知基类Point的定义如下: class Point {

int x, y; //点的x和y坐标

public: Point( int = 0, int = 0 ); // 构造函数 void SetPoint( int, int ); // 设置坐标 int GetX() { return x; } // 取x坐标 int GetY() { return y; } // 取y坐标 void Print(); //输出点的坐标 };

Point( int a, int b ) { SetPoint( a, b ); }

void SetPoint( int a, int b ) { x = a; y = b; }

void Print() { cout << \"[\" << x << \ }

请通过继承的方法建立一个派生类Circle,其中

1.新增的数据成员有: double radius; 2.新增的成员函数有:

Circle(int x = 0, int y = 0 , double r = 0.0); //对数据成员赋值,并使用SetRadius和基类的Point void SetRadius( double ); //设置半径 double GetRadius(); //取半径 double Area(); //计算面积

void Print(); //输出圆心坐标和半径,并使用基类的Print

在主程序中分别建立基类对象和派生类对象,使用用户输入的初值分别对基类对象和派生类对象的数据成员赋值后,利用已有的成员函数分别显示基类对象和派生类对象的数据成员信息。 圆周率取3.14。 #include using namespace std; const double PI = 3.14; class Point {

public: Point(int a, int b) { SetPoint(a, b); } void SetPoint(int a, int b) { x = a; y = b; } int GetX() { return x; } int GetY() { return y; } void Print() { cout << \"[\" << x << \private: int x, y; };

class Circle :public Point {

public: Circle(int x = 0, int y = 0, double r = 0.0); void SetRadius(double r) { radius = r; } double GetRadius() { return radius; } double Area(); void Print(); private: double radius; };

int main() { int x1, x2, y1, y2; double r; cin >> x1 >> y1; cin >> x2 >> y2 >> r; Point myPoint(x1, y1); Circle myCircle(x2, y2, r); cout << \"Point p \"; myPoint.Print(); cout << endl; myCircle.Print();

myCircle.Area(); return 0; }

Circle::Circle(int x, int y, double r) :Point(x, y) { SetRadius(r); }

double Circle::Area() { cout << \"The center of circle c \"; //注意:输入样例的center单词是写错的 Point::Print(); cout << \"\\nThe area of circle c \"; cout << PI*radius*radius << endl; return PI*radius*radius; }

void Circle::Print() { cout << \"Circle c Center=\"; Point::Print(); cout << \"\\nRadius=\" << radius << endl; }

5、从Student类和Teacher类多重派生Graduate类(20分) 题目内容:

已知基类Person定义如下: class Person {

char Name[10]; char Sex[10]; int Age; public:

void Register(char *name,int age,char *sex); void ShowMe(); };

请通过继承的方法建立两个派生类,其中 派生类Teacher:

1.新增的数据成员有: char Dept[20]; int Salary;

2.新增的成员函数有:

Teacher(char *name,int age,char *sex,char *dept,int salary); void Show() //显示新增数据成员 派生类Student:

1.新增的数据成员有: char ID[12]; char Class[12];

2.新增的成员函数有:

Student(char *name,int age,char *sex,char *ID,char *Class);

void Show()//显示新增数据成员

请通过继承的方法从Teacher和Student中建立派生类Graduate,其中 1.新增的成员函数有:

Graduate(char *name,int age,char *sex,char *dept,int salary,char *id,char *classid); 2.重写的成员函数有:

void ShowMe()//显示数据成员,要求调用基类的Show和ShowMe

在主程序中建立一个派生类Graduate的对象,利用成员函数显示对象的数据成员。 #include #include using namespace std; class Person {

public: inline void Register(char* name, int age, char* sex) { strcpy(Name, name); Age = age; strcpy(Sex, sex); } inline void ShowMe() { cout << \"姓名 \" << Name; if (Sex[0] == 'f') cout << \"\\n性别 女\"; else cout << \"\\n性别 男\"; cout << \"\\n年龄 \" << Age << endl; } private: char Name[10]; char Sex[10]; int Age; };

class Teacher :public Person {

public: Teacher(char *name, int age, char *sex, char* dept, int salary) { Person::Register(name, age, sex); strcpy(Dept, dept); Salary = salary; } inline void Show() { cout << \"工作单位 \" << Dept << \"\\n月薪 \" << Salary << endl; } private: char Dept[20]; int Salary; };

class Student :public Person {

public: Student(char* name, int age, char* sex, char* id, char* classid) { Person::Register(name, age, sex); strcpy(ID, id); strcpy(Class, classid); } void Show() { cout << \"班级 \" << Class << \"\\n学号 \" << ID << endl; } private: char ID[12]; char Class[12]; };

class Graduate :public Teacher, public Student {

public: Graduate(char* name, int age, char* sex, char* dept, int salary, char* id, char* classid) : Student(name, age, sex, id, classid), Teacher(name, age, sex, dept, salary) {} void ShowMe() { Student::Show(); Student::ShowMe(); Teacher::Show(); } };

int main() { char name[10], sex[10], dept[20], id[12], classid[12]; int age, salary; cin >> name >> age >> sex >> dept >> salary >> id >> classid; Graduate *gra = new Graduate(name, age, sex, dept, salary, id, classid); gra->ShowMe(); delete gra; return 0; }

十二周目

1、虚函数实现多态性(20分) 题目内容:

定义宠物类Pet,包含虚函数Speak,显示如下信息“How does a pet speak?”;定义公有派生类Cat和Dog,其Speak成员函数分别显示:“miao! miao!”和“wang! wang!”。主函数中定义Pet,Cat和Dog对象,再定义Pet指针变量,分别指向Pet,Cat和Dog对象,并通过指针调用Speak函数,观察并分析输出结果。

#include using namespace std; class Pet {

public: Pet() {} ~Pet() {} virtual void Speak() { cout << \"How does a pet speak?\" << endl; } };

class Cat :public Pet {

public: Cat() {} ~Cat() {} virtual void Speak() { cout << \"miao!miao!\" << endl; } };

class Dog :public Pet {

public: Dog() {} ~Dog() {} virtual void Speak() { cout << \"wang!wang!\" << endl; } };

int main() { Pet pet, *pt; Cat cat; Dog dog; pt = &pet; pt->Speak(); pt = &cat; pt->Speak(); pt = &dog; pt->Speak(); return 0; }

2、抽象宠物类的实现(20分) 题目内容:

定义抽象宠物类Pet,其中数据成员包括:名字,年龄和颜色;成员函数包括:构造函数;获取成员数据值的函数;纯虚函数Speak和纯虚函数GetInfo;

定义Pet的派生类Cat和Dog,其中Speak函数分别显示猫和狗的叫声,而GetInfo函数分别输出Cat和Dog的属性。主函数中定义Pet指针变量,分别指向动态生成的Cat和Dog对象,并通过指针分别调用GetInfo函数和Speak函数,观察并分析输出结果。 #include #include using namespace std; class Pet

{

public: Pet(char* name, int age, char* color) { strcpy(Name, name); Age = age; strcpy(Color, color); } ~Pet() {} virtual void Speak() = 0; virtual void GetInfo() = 0; protected: char Name[10]; int Age; char Color[10]; };

class Cat :public Pet {

public: Cat(char* name, int age, char* color) :Pet(name, age, color) {} ~Cat() {} virtual void Speak() { cout << \"猫的叫声:miao!miao!\" << endl; } virtual void GetInfo() { cout << \"猫的名字:\" << Name << endl; cout << \"猫的年龄:\" << Age << endl; cout << \"猫的颜色:\" << Color << endl; } };

class Dog :public Pet {

public: Dog(char* name, int age, char* color) :Pet(name, age, color) {} ~Dog() {} virtual void Speak() { cout << \"狗的叫声:wang!wang!\" << endl; } virtual void GetInfo() { cout << \"狗的名字:\" << Name << endl; cout << \"狗的年龄:\" << Age << endl; cout << \"狗的颜色:\" << Color << endl; } };

int main() { char name[10], color[10]; int age; cin >> name >> age >> color;

Cat* cat = new Cat(name, age, color); cin >> name >> age >> color; Dog* dog = new Dog(name, age, color); cat->GetInfo(); cat->Speak(); dog->GetInfo(); dog->Speak(); return 0; }

3、重载加法运算符的复数运算(20分) 题目内容:

定义一个复数类,并重载加法运算符(+)和赋值运算符(=)以适用对复数运算的要求。 #include using namespace std; class Complex {

public: Complex(double Real = 0, double Img = 0) { this->Real = Real; this->Img = Img; } ~Complex() {} Complex operator +(Complex& x) { Complex* temp = new Complex; temp->Real = Real + x.Real; temp->Img = Img + x.Img; return *temp; } void Show() { cout << Real; if (Img >= 0) cout << \"+j\" << Img << endl; else cout << \"-j\" << -Img << endl; } private: double Real; double Img; };

int main() { double a, b, c, d; cin >> a >> b >> c >> d; Complex test1(a, b); Complex test2(c, d); test1.Show(); test2.Show(); Complex* c1 = new Complex(test1 + test2);

c1->Show(); delete c1; return 0; }

4、重载矩阵加法运算(20分) 题目内容:

编写一个矩阵类,重载矩阵加法运算。设A,B,C均为m行,n列的矩阵,要求程序能实现C=A+B的操作。

提示:由于涉及深浅拷贝的问题,不建议使用动态数组。 #include using namespace std; class Matrix {

public: Matrix() :Row(0), Column(0), Index(0) {} Matrix(int row, int column) { Row = row; Column = column; Data = new double[row*column]; } void InitData() { for (int i = 0; i < Row*Column; ++i) cin >> Data[i]; } void Show() { Index = 0; for (int i = 0; i < Row; ++i) { for (int j = 0; j < Column; ++j) { if (j != Column - 1) cout << Data[Index] << ' '; else cout << Data[Index] << endl; Index++; } } } Matrix operator +(const Matrix& mat) { static Matrix temp(Row, Column); for (int i = 0; i < Row*Column; ++i) temp.Data[i] = Data[i] + mat.Data[i]; return temp;

} ~Matrix() {} private: int Row, Column, Index; double* Data; };

int main() { int row, col; cin >> row >> col; Matrix mat1(row, col); Matrix mat2(row, col); mat1.InitData(); mat2.InitData(); Matrix* mat3 = new Matrix(mat1 + mat2); mat3->Show(); delete mat3; return 0; }

5、纯虚函数与基类指针数组的应用(20分) 题目内容:

定义抽象基类Shape,

其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea()计算几何图形的面积。并由Shape类派生出5个派生类:Circle(圆形),数据成员为半径、Square(正方形)

,数据成员为边长、Rectangle(长方形) ,数据成员为长和宽、Trapezoid(梯形) ,数据成员为上底、下底和高、Triangle(三角形)

,数据成员为底和高。测试过程,定义一个指向基类的指针数组,使其每个元素指向一个动态产生的派生类对象,分别调用相应的成员函数显示各个几何图形的属性及面积,最终输出总面积值。 #include using namespace std;

const double PI = 3.14159; class Shape {

public: Shape() {} ~Shape() {} virtual void printName() = 0; virtual void printArea() = 0; virtual double area() = 0; };

class Circle :public Shape {

public: Circle(double radius) { m_radius = radius; } virtual void printName() { cout << \"圆:半径=\" << m_radius << \ virtual void printArea() {

double area; area = m_radius*m_radius*PI; cout << \"面积:\" << area << endl; } double area() { return m_radius*m_radius*PI; } ~Circle() {} private: double m_radius; };

class Square :public Shape {

public: Square(double side) { m_side = side; } virtual void printName() { cout << \"正方形:边长=\" << m_side << \ virtual void printArea() { double area = m_side*m_side; cout << \"面积:\" << area << endl; } double area() { return m_side*m_side; } ~Square() {} private: double m_side; };

class Rectangle :public Shape {

public: Rectangle(double width, double height) { m_width = width; m_height = height; } virtual void printName() { cout << \"长方形:长=\" << m_height << \宽=\" << m_width << \ virtual void printArea() { double area = m_width*m_height; cout << \"面积:\" << area << endl; } double area() { return m_height*m_width; } ~Rectangle() {} private: double m_width, m_height; };

class Trapezoid :public Shape {

public: Trapezoid(double upside, double downside, double height)

{ m_upside = upside; m_downside = downside; m_height = height; } virtual void printName() { cout << \"梯形:上底=\" << m_upside << \下底=\" << m_downside << \高=\" << m_height << \ virtual void printArea() { double area = (m_upside + m_downside) / 2 * m_height; cout << \"面积:\" << area << endl; } double area() { return (m_upside + m_downside) / 2 * m_height; } ~Trapezoid() {} private: double m_upside, m_downside, m_height; };

class Triangle :public Shape {

public: Triangle(double downside, double height) { m_downside = downside; m_height = height; } virtual void printName() { cout << \"三角形:底边=\" << m_downside << \高=\" << m_height << \ virtual void printArea() { double area = (m_downside*m_height) / 2; cout << \"面积:\" << area << endl; } double area() { return (m_downside*m_height) / 2; } ~Triangle() {} private: double m_downside, m_height; };

int main() { double Area = 0; double cirRadius, squSide, rectHeight, rectWidth, traUpside, traDownside, traHeight, triHeight, triDownside;

cin >> cirRadius >> squSide >> rectHeight >> rectWidth >> traUpside >> traDownside >> traHeight >> triHeight >> triDownside; Shape* arr[5]; arr[0] = new Circle(cirRadius); arr[1] = new Square(squSide); arr[2] = new Rectangle(rectWidth, rectHeight);

}

arr[3] = new Trapezoid(traUpside, traDownside, traHeight); arr[4] = new Triangle(triHeight, triDownside); for (int i = 0; i < 5; ++i) { arr[i]->printName(); arr[i]->printArea(); Area += arr[i]->area(); }

cout << \"总面积:\" << Area << endl; return 0;

十三周目

1、计算某个正整数平方根,并按要求输出(20分) 题目内容:

输入一个正整数。计算其平方根(用sqrt函数),并将结果按取1~6位小数分六行显示出来。 #include #include int main() { int number; std::cin >> number; double resualt = sqrt(number); std::cout.setf(std::ios::showpoint | std::ios::fixed); for (int i = 1; i < 7; ++i) { std::cout.precision(i); std::cout << resualt << std::endl; } return 0; }

2、读取文件,添加行号显示(20分) 题目内容:

输入5行信息,生成文件A.txt。然后再次打开该文件,为每一行前面加一个行号后显示在屏幕上,行号占据4个字符宽,行号左对齐显示。 #include #include #include using namespace std; int main() { int n = 5; char fix = ' '; char str[5][80]; for (int i = 0; i < 5; ++i) cin.getline(str[i], 80);

cout.unsetf(ios::left); for (int i = 0; i < n; ++i) cout << i + 1 << setw(3) << cout.fill(fix) << str[i] << endl; //ifstream in(\"A.txt\ //if (!in) //{ // cerr << \"read failed.\" << endl; // return EXIT_FAILURE; //} //for (int i = 0; i < 5; ++i) // in.getline(str[i], 50); //in.close(); //int j = 1; //ofstream out(\"A1.txt\"); //if (!out) //{ // cerr << \"write failed.\" << endl; // return EXIT_FAILURE; //} //for (int i = 0; i < 5; ++i) //{ // out << j << \" \" << str[i] << endl; // j++; //} //out.close(); return 0; }

3、读写文件并转换字符(20分) 题目内容: 编写一个程序,从键盘输入一行字符(可包含各种字符,遇回车符结束),写入到文件a1.txt中;再从a1.txt中读出文件内容,将其中的小写字母转换成大写字母后显示在屏幕上。

提示:输入带空格的字符串,用cin.getline()。注:提交时,注释文件操作语句。 #include #include #include #include using namespace std; int main() { string str; //fstream outFile; getline(cin, str); //outFile.open(\"a1.txt\"); //fstream的对象如果找不到目标文件不会像ofstream对象那样创建一个 //if (outFile.fail()) //{ // system(\"@echo File not found.creating file.\"); // system(\"@echo > a1.txt\");

// system(\"@echo File has been created.\"); // system(\"@echo off\"); // cerr << \"File has been created, but can't write. Please run again.\"; // system(\"pause\"); // return EXIT_FAILURE; //} transform(str.begin(), str.end(), str.begin(), ::toupper); //outFile << str << endl; cout << str << endl; //outFile.close(); return 0; }

4、读文件中的数字,算平均值(20分) 题目内容:

输入n个数字(实数),将他们写入文件out1.txt,数字之间用空格分开。然后再次打开该文件,读出全部数字,计算他们平均值并输出在屏幕上。 #include #include using namespace std; int main() { int num; double sum = 0.0, sum1 = 0.0; cin >> num; double *p = new double[num]; for (int i = 0; i < num; ++i) { cin >> p[i]; sum += p[i]; } //ofstream out(\"out1.txt\"); //将输入的信息保存到out1.txt文件中 //for (int i = 0; i < num; ++i) // out << p[i] << \" \"; //out.close(); //ifstream in(\"out1.txt\//以只读的方式从out1.txt中读取数据 //for (int i = 0; i < num; ++i) //{ // in >> p[i]; // sum1 += p[i]; //} //in.close(); cout << \"Avg=\" << sum / num << endl; delete[] p; return 0; }

5、读文件中的字符并排序输出(20分) 题目内容:

输入n个字符写入文本文件A.txt,字符间用空格分开。在打开该文件,读取所有字符并排序后,按从小到大顺序写入B.txt(字符间用空格分开),同时将文件B的内容显示在屏幕上。 #include #include using namespace std; int main() { //ofstream out(\"A.txt\"); int num; cin >> num; char *p = new char[num]; for (int i = 0; i < num; ++i) { cin >> p[i]; //out << p[i]; } //out.close(); for (int i = 0; i < num - 1; ++i) { int nFlag = i; for (int j = i + 1; j < num; ++j) { if (p[j] < p[nFlag]) nFlag = j; } swap(p[i], p[nFlag]); } //ofstream out2(\"B.txt\"); for (int i = 0; i < num; ++i) { if (i == num - 1) { cout << p[i] << endl; //out2 << p[i]; } else { cout << p[i] << \" \"; //out2 << p[i] << \" \"; } } //out2.close(); return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务