我是靠谱客的博主 年轻金毛,这篇文章主要介绍大一下 c + + 上机实验总结(十四),现在分享给大家,希望可以做个参考。

大一下c + +上机实验总目录:大一下c + +上机实验总结目录


1、阅读程序,其中s::connect()函数实现字符串连接。把这个成员函数改写为重载+运算符函数,并修改main函数的对应代码,使其正确运行。

复制代码
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<cstring> using namespace std; class s { public: s() { *str = ''; len = 0; } s( char *pstr ) { strcpy( str,pstr ); len = strlen(pstr); } char *gets() { return str; } int getLen() { return len; } s connect( s obj ); //字符串连接函数声明 private: char str[100]; int len; }; s s::connect( s obj ) //字符串连接函数定义 { strcat( str,obj.str ); return str; } int main() { s obj1( "Visual" ), obj2( " C++" ), obj3(" language"); obj3 = (obj1.connect(obj2)).connect(obj3); //调用字符串连接函数 cout << "obj3.str = "<<obj3.gets() << endl; cout<<"obj3.len = "<<obj3.getLen()<<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
#include <iostream> #include<cstring> using namespace std; class s { public: s() { *str = ''; len = 0; } s( char *pstr ) { strcpy( str,pstr ); len = strlen(pstr); } char *gets() { return str; } int getLen() { return len; } s operator+( s obj ); private: char str[100]; int len; }; s s::operator+( s obj ) { strcat( str,obj.str ); return str; } int main() { s obj1( "Visual" ), obj2( " C++" ), obj3(" language"); obj3 = obj1 + obj2 + obj3; cout << "obj3.str = "<<obj3.gets() << endl; cout<<"obj3.len = "<<obj3.getLen()<<endl; return 0; }

2、改写下述程序中的student类,用重载运算符>>函数代替input函数;用重载运算符<<函数代替output函数;并修改main函数,使其得到正确的运行结果。

复制代码
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
#include <iostream> using namespace std; class student { char name[20]; unsigned id; double score; public: student(char s[20]="", unsigned k=0, double t=0) { strcpy(name,s); id=k; score=t; } void input() { cout<<"name? "; cin>>name; cout<<"id? "; cin>>id; cout<<"score? "; cin>>score; } void output() { cout<<"name: "<<name<<"tid: "<<id<<"tscore: "<<score<<endl; } }; int main() { student s; s.input(); s.output(); 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> using namespace std; class student { char name[20]; unsigned id; double score; public: student(char s[20]="", unsigned k=0, double t=0) { strcpy(name,s); id=k; score=t; } friend ostream & operator<<(ostream &, const student &); friend istream & operator>>(istream &, student &); }; ostream & operator<<(ostream &out, const student &s) { out<<"name: "<<s.name<<"tid: "<<s.id<<"tscore: "<<s.score<<endl; return out; } istream & operator>>(istream &in, student &s) { cout<<"name? "; in>>s.name; cout<<"id? "; in>>s.id; cout<<"score? "; in>>s.score; return in; } int main() { student s; cin>>s; cout<<s; return 0; }

3、建立一个矩阵类Array,存储一个4×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; class Array { int a[4][4]; public: void set() { for(int i=0;i<4;i++) for(int j=0;j<4;j++) cin>>a[i][j]; } int max(); double ave(); }; double Array::ave() { double sum=0; for(int i=0;i<4;i++) for(int j=0;j<4;j++) sum=sum+a[i][j]; return sum/16; } int Array::max() { int maxv=a[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(a[i][j]>maxv) maxv=a[i][j]; return maxv; } int main() { Array a; a.set(); cout<<a.ave()<<endl; cout<<a.max()<<endl; return 0; }

4、编写程序,将数组A中的n个数据从小到大写入数组B中,数据类型可以是整型、单精度型、双精度型。用重载函数实现。

复制代码
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
#include<iostream.h> void sort_write(int *a,int *b,int n) {int t; for(i=0;i<n;i++)//对数组a进行选择法排序 for(int j=i+1;j<n) if(a[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;} for(int i=0;i<n;i++)//将数组a的数据复制给数组b b[i]=a[i]; } void sort_write(float *a,float *b,int n) {float t; for(i=0;i<n;i++)//对数组a进行选择法排序 for(int j=i+1;j<n) if(a[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;} for(int i=0;i<n;i++)//将数组a的数据复制给数组b b[i]=a[i]; } void sort_write(double *a,double *b,int n) {double t; for(i=0;i<n;i++)//对数组b进行选择法排序 for(int j=i+1;j<n) if(a[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;} for(int i=0;i<n;i++)//将数组a的数据复制给数组b b[i]=a[i]; } int main() {int n=10,a[]={12,5,7,9,1,3,23,6,10,2},a1[10]; double b[]={12.34,4.56,56.78,8.89,9.98,1.2,45.4,23.1,34.12,11.2},b1[10]; sort_write(a,a1,10); sort_write(b,b1,10); for(int i=0;i<10;i++) cout<<a1[i]<<" "; cout<<endl; for(i=0;i<10;i++) cout<<b1[i]<<" "; cout<<endl; return 0; } }

5、输入n个字符串,将其中以字母A开头的字符串输出。提示:因为n的值可以根据键盘输入的不同而不同,所以,此题可以考虑用new创建string类型的动态数组。
参考答案:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> #include <string> using namespace std; int main() { string *my_string; int n; cin>>n; my_string=new string[n]; for(int i=0;i<n;i++) cin>>my_string[i]; for(i=0;i<n;i++) { if(!my_string[i].find("A",0))//该find函数功能:返回字符串my_string[i]从位置0开始子串“A”第一次出现的位置;此行语句也可用if(my_string[i][0]=='A') cout<<my_string[i]<<endl; } return 0; }

6、教材 同步练习6.1的程序练习的第1题:阅读程序,写出运行结果。一定要自己理解运行结果。

复制代码
1
2
3
4
5
6
7
8
运行结果: 10 5 10 5 15 5

7、教材 同步练习6.2的程序练习的第1题:阅读程序,写出运行结果。一定要自己理解运行结果。
运行结果:

复制代码
1
2
3
4
5
6
7
8
9
10
调用构造函数1. 4 8 调用构造函数2. 4 8 20 调用析构函数 调用析构函数

最后

以上就是年轻金毛最近收集整理的关于大一下 c + + 上机实验总结(十四)的全部内容,更多相关大一下内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部