c++中数组上机实验报告.doc
第六次上机实验报告姓名:杨天野班级:计算机1204学号:实验一:题目:(1)编写并测试3*3矩阵转置函数,使用数组保存3*3矩阵。源程序:#include<iostream>using namespace std;int main()int a133=1,2,3,4,5,6,7,8,9;int a233;cout<<"原数列为:"<<endl;for(int i=0;i<3;i+)for(int j=0;j<3;j+)cout<<a1ij;cout<<endl;cout<<"转置后的数列为:"<<endl;for(int m=0;m<3;m+)for(int n=0;n<3;n+)a2mn=a1nm;cout<<a2mn;cout<<endl;return 0;截图:实验二:题目:(2)使用动态内存分配生成动态数组来重新完成上题,使用指针实现函数的功能。源代码:#include<iostream>#include<cmath>using namespace std;int main()int *p=new int;int i,j,t,n;cout<<"请输入数列元素的个数"<<endl;cin>>n;cout<<"原数列"<<endl;for(i=1;i<=n;i+)cin>>*(p+i);cout<<"转置后的数列"<<endl;t=sqrt(n);for(i=1;i<=t;i+)for(j=0;j<t;j+) cout<<*(p+t*j+i)<<" "cout<<endl;return 0;截图:实验三:题目(3)编程实现两字符串的连接。要求使用字符数组保存字符串,不要使用系统函数。源程序:#include<iostream>using namespace std;int main()char a="hello" char b="boys and girls",c100; int m=0,n=0,p=0; while(am!='0')cm=am;m+; cm+n=' ' while(bn!='0')cm+n+1=bn;n+;cout<<a<<endl;cout<<b<<endl; cout<<"连接后"<<endl; while(p<m+n+1) cout<<cp;p+; cout<<endl; return 0;截屏:实验四:题目:(4)使用string类定义字符串对象,重新实现上一小题。源代码:#include<iostream>#include<string>using namespace std;int main() string a1,a2; cout<<"请输入a1:"cin>>a1; cout<<"请输入a2:"cin>>a2; cout<<"连接后字符串:" a1=a1+a2; cout<<a1<<endl; return 0;截屏:实验五:题目:(5)定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。Display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。实现并测试这个类。源代码:#include<iostream>#include<string>using namespace std;class employeeprivate:string name,address,city,code;public:employee(string m,string a,string c,string o)name=m;address=a;city=c;code=o;cout<<"构造了一个函数"<<endl;void changename()string b; cin>>b; name=b;void display() cout<<"名字是:"<<name<<endl;cout<<"地址是:"<<address<<endl;cout<<"城市是:"<<city<<endl;cout<<"邮编是:"<<code<<endl;int main()employee a("杨天野","东北大学","辽宁沈阳",""); a.display(); a.changename(); a.display(); return 0;截屏:实验六:题目:(6)定义包含5个元素的对象数组,每个元素都是Employee类型的对象。源程序:#include<iostream>#include<string>using namespace std;class employeeprivate:string name,address,city,code;public:employee(string n,string m,string c,string o)name=n;address=m;city=c;code=o;cout<<"构造了一个函数"<<endl;void display() cout<<"名字是:"<<name<<endl;cout<<"地址是:"<<address<<endl;cout<<"城市是:"<<city<<endl;cout<<"邮编是:"<<code<<endl;int main() int i;employee a5=employee("杨天野","东北大学","辽宁沈阳",""), employee("杨玉坤","东北大学","辽宁沈阳",""), employee("陈勇","东北大学","辽宁沈阳",""), employee("李雪健","东北大学","辽宁沈阳",""), employee("徐凯","东北大学","辽宁沈阳",""); for (i=0;i<5;i+)ai.display();return 0;截图:实验心得:通过一个下午的编写,我终于进一步的理解和掌握了指针和数组之间的关系。并且能够进一步的编写一些我原来认为很难得程序了,感谢在这期间老师和同学的帮助!