1、C上机实验报告 实验四精品文档 实验四数组、指针与字符串 1. 实验目的 1.学习使用数组 2.学习字符串数据的组织和处理 3.学习标准C+库的使用 4.掌握指针的使用方法 5.练习通过Debug观察指针的内容及其所指的对象的内容 6.联系通过动态内存分配实现动态数组,并体会指针在其中的作用 7.分别使用字符数组和标准C+库练习处理字符串的方法 2. 实验要求 1.编写并测试3*3矩阵转置函数,使用数组保存3*3矩阵。 2.使用动态内存分配生成动态数组来重新完成上题,使用指针实现函数的功能。 3.编程实现两字符串的连接。要求使用字符数组保存字符串,不要使用系统函数。 4.使用string类定义
2、字符串对象,重新实现上一小题。 5.定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。Display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。实现并测试这个类。 6.定义包含5个元素的对象数组,每个元素都是Employee类型的对象。 7. (选做)修改实验4中的选做实验中的people(人员)类。具有的属性如下:姓名char name11、编号char number7、性别char sex3、生日birthday、身份证号char id16。其中“出生日期”定义为一个“
3、日期”类内嵌对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。在测试程序中定义people类的对象数组,录入数据并显示。 3. 实验内容及实验步骤 1.编写矩阵转置函数,输入参数为3*3整形数组,使用循环语句实现矩阵元素的行列对调,注意在循环语句中究竟需要对哪些元素进行操作,编写main()函数实现输入、输出。程序名:lab6_1.cpp。 2.改写矩阵转置函数,参数为整型指针,使用指针对数组元素进行操作,在main()函数中使用new操作符分配内存生成动态数组。通过Debug观察指针的内容及其所指的对象中的内容。程序名:lab6_2.
4、cpp。 3.编程实现两字符串的连接。定义字符数组保存字符串,在程序中提示用户输入两个字符串,实现两个字符串的连接,最后用cout语句显示输出。程序名:lab6_3.cpp。用cin实现输入,注意,字符串的结束标志是ASCII码0,使用循环语句进行字符串间的字符拷贝。 4.使用string类定义字符串对象,编程实现两字符串的连接。在string类中已重载了运算符“+=”实现字符串的连接,可以使用这个功能。程序名:lab6_4.cpp。 5.在employee.h文件中定义Employee类。Employee类具有姓名、街道地址、城市和邮编等私有数据成员,在成员函数中,构造函数用来初始化所有数据
5、成员;display()中使用cout显示. 精品文档 姓名、街道地址、城市和邮编等属性,change_name()改变类中表示姓名属性的数据成员。在主程序中定义这个类的对象并对其进行操作。程序名:lab6_5.cpp。 6.使用上一小题中定义的Employee类定义对象数组emp5,使用循环语句把数据显示出来。程序名:lab6_6.cpp。 4.思考题 1.如何存储和处理字符串? (1)可以利用字符数组存储和处理字符串; (2)利用系统提供的string类存储和处理字符串。 2.头文件和头文件有何区别? 包含头文件后,可以使用系统的字符串处理函数,如strcat(连接).strcpy(复制)
6、.strcmp(比较).strlen(求长度).strlwr(转换为小写).strupr(转换为大写)等等; 而包含头文件后,则可以定义string类,并且使用系统提供的string类操作符对string类型的对象进行处理。 3.有几种方法来表示和处理数组元素? (1)数组下标方法,如ai (2)指针的方法,如int* p=&a0 5.源程序 1.lab6_1.cpp #include using namespace std; int a33; void showTrans() int i,j; coutThe transposition matrix is:endl; for(j=0;j3;
7、j+) for(i=0;i3;i+) coutaij; cout ; coutendl; void input() int i,j; coutPlease input your 3*3 matrix:endl; for(i=0;i3;i+) for(j=0;jaij; int main() input(); showTrans(); return 0; 2.lab6_2.cpp #include using namespace std; int* p9; void showTrans() int i,j; coutThe transposition matrix is:endl; for(i=
8、0;i3;i+) for(j=i;j9;j=j+3) cout*pj; cout ; coutendl; void input() int i,n; coutPlease input your 3*3 matrix:endl; for(i=0;in; pi=new int(n); int main() input(); showTrans(); . 精品文档 int i; for(i=0;i9;i+) delete pi; return 0; 3.lab6_3.cpp #include /不使用系统自带函数strcpy using namespace std; char a20=/0,b20=
9、/0,c45=/0; int main() int i,j,k; coutInput the first string:endl; cin.getline(a,20,n); coutInput the second string:endl; cin.getline(b,20,n); int m=0,n=0; /将数组a中的字符串拷贝到数组c中 for(k=0;k45,am!=0;k+,m+) ck=am; /将数组b中的字符串接着a,拷贝到数组c中 for(;k45,bn!=0;k+,n+) ck=bn; coutc; return 0; 4. lab6_4.cpp #include #inc
10、lude using namespace std; char a20=/0,b20=/0; int main() . 精品文档 coutInput the first string:endl; cin.getline(a,20,n); coutInput the second string:endl; cin.getline(b,20,n); string s1=a; string s2=b; string s3=s1+s2; couts3; return 0; 5. Employee.h #ifndef Employee_H_INCLUDED #define Employee_H_INCLU
11、DED class Employee private: char name15; char address25; char city10; int postcode; public: Employee(); Employee(char n,char a,char c,int p); Employee(); void change_name(); void change_address(); void change_city(); void change_postcode(); void display(); ; #endif / Employee_H_INCLUDED Employee.cpp
12、 #include #includeEmployee.h using namespace std; Employee:Employee() Employee:Employee(char n,char a,char c,int p) . 精品文档 name0=n; name1=0; address0=a; address1=0; city0=c; city1=0; postcode=p; Employee:Employee() void Employee:change_name() coutPlease input your changed name:endl; cin.getline(name
13、,15,n); void Employee:change_address() coutPlease input your changed address:endl; cin.getline(address,25,n); void Employee:change_city() coutPlease input your changed city:endl; cin.getline(city,10,n); void Employee:change_postcode() coutPlease input your changed postcode:postcode; void Employee:di
14、splay() coutYour information shows as follow:endl; coutame:nameendl; coutaddress:addressendl; coutcity:cityendl; coutpostcode:postcodeendl; int main() Employee person(1,1,1,1); person.display(); person.change_name(); person.change_address(); person.change_city(); . 精品文档 person.change_postcode(); per
15、son.display(); return 0; 6.Employee.h #ifndef Employee_H_INCLUDED #define Employee_H_INCLUDED class Employee private: char name15; char address25; char city10; int postcode; public: Employee(); Employee(char n,char a,char c,int p); Employee(); void change_name(); void change_address(); void change_c
16、ity(); void change_postcode(); void display(); ; #endif / Employee_H_INCLUDED Employee.cpp #include #includeEmployee.h using namespace std; Employee:Employee() Employee:Employee(char n,char a,char c,int p) name0=n; name1=0; address0=a; address1=0; city0=c; . 精品文档 city1=0; postcode=p; Employee:Employ
17、ee() void Employee:change_name() coutPlease input your changed name:endl; cin.getline(name,15,n); void Employee:change_address() coutPlease input your changed address:endl; cin.getline(address,25,n); void Employee:change_city() coutPlease input your changed city:endl; cin.getline(city,10,n); void Em
18、ployee:change_postcode() coutPlease input your changed postcode:postcode; void Employee:display() coutYour information shows as follow:endl; coutame:nameendl; coutaddress:addressendl; coutcity:cityendl; coutpostcode:postcodeendl; int main() Employee emp5=Employee(1,1,1,1),Employee(2,2,2,2),Employee(
19、3,3,3,3),Employee(4,4,4,4),Employee(5,5,5,5); int i; for(i=0;i5;i+) empi.display(); empi.change_name(); empi.change_address(); empi.change_city(); . 精品文档 empi.change_postcode(); cin.get(); for(i=0;i5;i+) coutThe emp i ; empi.display(); return 0; 7. #include #include using namespace std; /Date类 class
20、 Date private: int year; int month; int day; public: Date(); Date(int y,int m,int d); Date(Date &p); Date(); void setDate(); void showDate(); ; /People类,其中含Date类型的数据 class People private: char name11; char number7; char sex3; Date birthday; char id16; public: People(); People(char* n,char* nu,char*
21、s,Date b,char* i); People(People &p); . 精品文档 People(); void setName(); void setNumber(); void setSex(); void setId(); void showPeople(); ; /Date构造函数 Date:Date() Date:Date(int y,int m,int d) year=y; month=m; day=d; Date:Date(Date &p) year=p.year; month=p.month; day=p.day; /析构 inline Date:Date() /Date
22、成员函数,设置出生年月日 void Date:setDate() int y,m,d; couty; coutm; coutd; year=y; month=m; day=d; /Date内联成员函数,输出年月日 inline void Date:showDate() 潣瑵?物桴慤?獩尠?敹牡?年?洼湯桴?月?搼祡?日endl; /People构造函数 People:People(); . 精品文档 People:People(char* n,char* nu,char* s,Date b,char* i) strcpy(name,n); strcpy(number,nu); strcpy(s
23、ex,s); birthday=b; strcpy(id,i); People:People(People &p) strcpy(name,p.name); strcpy(number,p.number); birthday=p.birthday; strcpy(id,p.id); /People析构 inline People:People() /People成员函数,设置各类数据 void People:setName() coutPlease input the persons name:; cin.getline(name,11,n); void People:setNumber()
24、coutInput number:; cin.getline(number,7,n); void People:setSex() coutInput sex:; cin.getline(sex,3,n); void People:setId() coutInput id:; cin.getline(id,16,n); /People内联成员函数,输出人员信息 inline void People:showPeople() . 精品文档 coutName:nameendl; coutNumber:numberendl; coutSex:sexendl; coutID:idendl; int ma
25、in() int i; char spaceA; /生成3个Date类型的对象 Date date3=Date(0,0,0),Date(0,0,0),Date(0,0,0); /生成3个People类型的对象 People person3=People(e2, /设置这3个对象的各类信息 for(i=0;i3;i+) personi.setName(); personi.setNumber(); personi.setSex(); personi.setId(); datei.setDate(); spaceA=getchar(); /输出这3个对象的各类信息 for(i=0;i3;i+) personi.showPeople(); datei.showDate(); return 0; 运行结果 6. 1. . 精品文档 2. 3. 4. 5 . 精品文档 .6. 7. . 精品文档 7. 心得体