当前位置:文档之家› C 上机实验报告 实验四

C 上机实验报告 实验四

实验四数组、指针与字符串1.实验目的1.学习使用数组2.学习字符串数据的组织和处理3.学习标准C++库的使用4.掌握指针的使用方法5.练习通过Debug观察指针的内容及其所指的对象的内容6.联系通过动态内存分配实现动态数组,并体会指针在其中的作用7.分别使用字符数组和标准C++库练习处理字符串的方法2.实验要求1.编写并测试3*3矩阵转置函数,使用数组保存3*3矩阵。

2.使用动态内存分配生成动态数组来重新完成上题,使用指针实现函数的功能。

3.编程实现两字符串的连接。

要求使用字符数组保存字符串,不要使用系统函数。

4.使用string类定义字符串对象,重新实现上一小题。

5.定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。

Display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。

实现并测试这个类。

6.定义包含5个元素的对象数组,每个元素都是Employee类型的对象。

7.(选做)修改实验4中的选做实验中的people(人员)类。

具有的属性如下:姓名charname[11]、编号charnumber[7]、性别charsex[3]、生日birthday、身份证号charid[16]。

其中“出生日期”定义为一个“日期”类内嵌对象。

用成员函数实现对人员信息的录入和显示。

要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。

在测试程序中定义people类的对象数组,录入数据并显示。

3.实验内容及实验步骤1.编写矩阵转置函数,输入参数为3*3整形数组,使用循环语句实现矩阵元素的行列对调,注意在循环语句中究竟需要对哪些元素进行操作,编写main()函数实现输入、输出。

程序名:lab6_1.cpp。

2.改写矩阵转置函数,参数为整型指针,使用指针对数组元素进行操作,在main()函数中使用new操作符分配内存生成动态数组。

通过Debug观察指针的内容及其所指的对象中的内容。

程序名:lab6_2.cpp。

3.编程实现两字符串的连接。

定义字符数组保存字符串,在程序中提示用户输入两个字符串,实现两个字符串的连接,最后用cout语句显示输出。

程序名:lab6_3.cpp。

用cin实现输入,注意,字符串的结束标志是ASCII码0,使用循环语句进行字符串间的字符拷贝。

4.使用string类定义字符串对象,编程实现两字符串的连接。

在string类中已重载了运算符“+=”实现字符串的连接,可以使用这个功能。

程序名:lab6_4.cpp。

5.在employee.h文件中定义Employee类。

Employee类具有姓名、街道地址、城市和邮编等私有数据成员,在成员函数中,构造函数用来初始化所有数据成员;display()中使用cout显示姓名、街道地址、城市和邮编等属性,change_name()改变类中表示姓名属性的数据成员。

在主程序中定义这个类的对象并对其进行操作。

程序名:lab6_5.cpp。

6.使用上一小题中定义的Employee类定义对象数组emp[5],使用循环语句把数据显示出来。

程序名:lab6_6.cpp。

4.思考题1.如何存储和处理字符串?(1)可以利用字符数组存储和处理字符串;(2)利用系统提供的string类存储和处理字符串。

2.头文件<string.h>和头文件<string>有何区别?包含头文件<string.h>后,可以使用系统的字符串处理函数,如strcat(连接).strcpy(复制).strcmp(比较).strlen(求长度).strlwr(转换为小写).strupr(转换为大写)等等;而包含头文件<string>后,则可以定义string类,并且使用系统提供的string类操作符对string 类型的对象进行处理。

3.有几种方法来表示和处理数组元素?(1)数组下标方法,如a[i](2)指针的方法,如int*p=&a[0]5.源程序b6_1.cpp#include<iostream>usingnamespacestd;inta[3][3];voidshowTrans(){inti,j;cout<<"Thetranspositionmatrixis:"<<endl;for(j=0;j<3;j++){for(i=0;i<3;i++){cout<<a[i][j];cout<<"";}cout<<endl;}}voidinput(){inti,j;cout<<"Pleaseinputyour3*3matrix:"<<endl;for(i=0;i<3;i++){for(j=0;j<3;j++){cin>>a[i][j];}}}intmain(){input();showTrans();return0;}b6_2.cpp#include<iostream> usingnamespacestd;int*p[9];voidshowTrans(){inti,j;cout<<"Thetranspositionmatrixis:"<<endl; for(i=0;i<3;i++){for(j=i;j<9;j=j+3){cout<<*p[j];cout<<"";}cout<<endl;}}voidinput(){inti,n;cout<<"Pleaseinputyour3*3matrix:"<<endl; for(i=0;i<9;i++){cin>>n;p[i]=newint(n);}}intmain(){input();showTrans();inti;for(i=0;i<9;i++){deletep[i];}return0;}b6_3.cpp#include<iostream>//不使用系统自带函数strcpy usingnamespacestd;chara[20]={"/0"},b[20]={"/0"},c[45]={"/0"}; intmain(){inti,j,k;cout<<"Inputthefirststring:"<<endl;cin.getline(a,20,'\n');cout<<"Inputthesecondstring:"<<endl;cin.getline(b,20,'\n');intm=0,n=0;//将数组a中的字符串拷贝到数组c中for(k=0;k<45,a[m]!='\0';k++,m++){c[k]=a[m];}//将数组b中的字符串接着a,拷贝到数组c中for(;k<45,b[n]!='\0';k++,n++){c[k]=b[n];}cout<<c;return0;}b6_4.cpp#include<iostream>#include<cstring>usingnamespacestd;chara[20]={"/0"},b[20]={"/0"};intmain(){cout<<"Inputthefirststring:"<<endl;cin.getline(a,20,'\n');cout<<"Inputthesecondstring:"<<endl;cin.getline(b,20,'\n');strings1=a;strings2=b;strings3=s1+s2;cout<<s3;return0;}5.Employee.h#ifndefEmployee_H_INCLUDED#defineEmployee_H_INCLUDED classEmployee{private:charname[15];charaddress[25];charcity[10];intpostcode;public:Employee();Employee(charn,chara,charc,intp);~Employee();voidchange_name();voidchange_address();voidchange_city();voidchange_postcode();voiddisplay();};#endif//Employee_H_INCLUDEDEmployee.cpp#include<iostream>#include"Employee.h" usingnamespacestd;Employee::Employee(){}Employee::Employee(charn,chara,charc,intp) {name[0]=n;name[1]='\0';address[0]=a;address[1]='\0';city[0]=c;city[1]='\0';postcode=p;}Employee::~Employee(){} voidEmployee::change_name(){cout<<"Pleaseinputyourchangedname:"<<endl; cin.getline(name,15,'\n');}voidEmployee::change_address(){cout<<"Pleaseinputyourchangedaddress:"<<endl; cin.getline(address,25,'\n');}voidEmployee::change_city(){cout<<"Pleaseinputyourchangedcity:"<<endl; cin.getline(city,10,'\n');}voidEmployee::change_postcode(){cout<<"Pleaseinputyourchangedpostcode:"<<endl; cin>>postcode;}voidEmployee::display(){cout<<"Yourinformationshowsasfollow:"<<endl; cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;}intmain(){Employeeperson(1,1,1,1);person.display();person.change_name();person.change_address();person.change_city();person.change_postcode();person.display();return0;}6.Employee.h#ifndefEmployee_H_INCLUDED#defineEmployee_H_INCLUDED classEmployee{private:charname[15];charaddress[25];charcity[10];intpostcode;public:Employee();Employee(charn,chara,charc,intp);~Employee();voidchange_name();voidchange_address();voidchange_city();voidchange_postcode();voiddisplay();};#endif//Employee_H_INCLUDEDEmployee.cpp#include<iostream>#include"Employee.h" usingnamespacestd;Employee::Employee(){}Employee::Employee(charn,chara,charc,intp){name[0]=n;name[1]='\0';address[0]=a;address[1]='\0';city[0]=c;city[1]='\0';postcode=p;}Employee::~Employee(){}voidEmployee::change_name(){cout<<"Pleaseinputyourchangedname:"<<endl; cin.getline(name,15,'\n');}voidEmployee::change_address(){cout<<"Pleaseinputyourchangedaddress:"<<endl; cin.getline(address,25,'\n');}voidEmployee::change_city(){cout<<"Pleaseinputyourchangedcity:"<<endl; cin.getline(city,10,'\n');}voidEmployee::change_postcode(){cout<<"Pleaseinputyourchangedpostcode:"<<endl; cin>>postcode;}voidEmployee::display(){cout<<"Yourinformationshowsasfollow:"<<endl;cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;}intmain(){Employeeemp[5]={Employee(1,1,1,1),Employee(2,2,2,2),Employee(3,3,3,3),Employee(4,4,4,4),E mployee(5,5,5,5)};inti;for(i=0;i<5;i++){emp[i].display();emp[i].change_name();emp[i].change_address();emp[i].change_city();emp[i].change_postcode();cin.get();}for(i=0;i<5;i++){cout<<"Theemp"<<i<<"";emp[i].display();}return0;}7.#include<iostream>#include<cstring>usingnamespacestd;//Date类classDate{private:intyear;intmonth;intday;public:Date();Date(inty,intm,intd);Date(Date&p);~Date();voidsetDate();voidshowDate();};//People类,其中含Date类型的数据classPeople{private:charname[11];charnumber[7];charsex[3];Datebirthday;charid[16];public:People();People(char*n,char*nu,char*s,Dateb,char*i);People(People&p);~People();voidsetName();voidsetNumber();voidsetSex();voidsetId();voidshowPeople();};//Date构造函数Date::Date(){}Date::Date(inty,intm,intd){year=y;month=m;day=d;}Date::Date(Date&p){year=p.year;month=p.month;day=p.day;}//析构inlineDate::~Date(){}//Date成员函数,设置出生年月日voidDate::setDate(){inty,m,d;cout<<"Inputtheyear:";cin>>y;cout<<"Inputthemonth:";cin>>m;cout<<"Inputtheday:";cin>>d;year=y;month=m;day=d;}//Date内联成员函数,输出年月日inlinevoidDate::showDate(){cout<<"Birthdayis"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}//People构造函数People::People(){};People::People(char*n,char*nu,char*s,Dateb,char*i){strcpy(name,n);strcpy(number,nu);strcpy(sex,s);birthday=b;strcpy(id,i);}People::People(People&p){strcpy(name,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);}//People析构inlinePeople::~People(){}//People成员函数,设置各类数据voidPeople::setName(){cout<<"Pleaseinputtheperson'sname:";cin.getline(name,11,'\n');}voidPeople::setNumber(){cout<<"Inputnumber:";cin.getline(number,7,'\n');}voidPeople::setSex(){cout<<"Inputsex:";cin.getline(sex,3,'\n');}voidPeople::setId(){cout<<"Inputid:";cin.getline(id,16,'\n');}//People内联成员函数,输出人员信息inlinevoidPeople::showPeople(){cout<<"Name:"<<name<<endl;cout<<"Number:"<<number<<endl;cout<<"Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;}intmain(){inti;charspaceA;//生成3个Date类型的对象Datedate[3]={Date(0,0,0),Date(0,0,0),Date(0,0,0)};//生成3个People类型的对象Peopleperson[3]={People("0","0","0",date[0],"0"),People("0","0","0",date[1],"0"),People("0" ,"0","0",date[2],"0")};//设置这3个对象的各类信息for(i=0;i<3;i++){person[i].setName();person[i].setNumber();person[i].setSex();person[i].setId();date[i].setDate();spaceA=getchar();}//输出这3个对象的各类信息for(i=0;i<3;i++){person[i].showPeople();date[i].showDate();}return0;}6.运行结果1.2.3.4.5.6.7.7.心得体会通过本次上机课,我对数组的理解进一步加深,并且学会了对字符串数据的组织和处理,能够运用字符串类对字符串进行直接的运算;并且进一步熟悉了标准C++库的使用和指针的使用方法;通过进一步的练习,对Debug的操作更加熟练;而且实践操作了利用指针和new 操作在堆区里开辟空间,然后利用delete释放空间,进一步加深了对不同类型存储空间的理解,提高了自己的实际操作能力。

相关主题