当前位置:文档之家› 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(人员)类。

具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号char id[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>using namespace std;int a[3][3];void showTrans(){int i,j;cout<<"The transposition matrix is:"<<endl; for(j=0;j<3;j++){for(i=0;i<3;i++){cout<<a[i][j];cout<<" ";}cout<<endl;}}void input(){int i,j;cout<<"Please input your 3*3 matrix:"<<endl; for(i=0;i<3;i++){for(j=0;j<3;j++){cin>>a[i][j];}}}int main(){input();showTrans();return 0;}b6_2.cpp#include<iostream>using namespace std;int* p[9];void showTrans(){int i,j;cout<<"The transposition matrix is:"<<endl; for(i=0;i<3;i++){for(j=i;j<9;j=j+3){cout<<*p[j];cout<<" ";}cout<<endl;}}void input(){int i,n;cout<<"Please input your 3*3 matrix:"<<endl; for(i=0;i<9;i++){cin>>n;p[i]=new int(n);}}int main(){input();showTrans();int i;for(i=0;i<9;i++){delete p[i];}return 0;}b6_3.cpp#include<iostream>//不使用系统自带函数strcpyusing namespace std;char a[20]={"/0"},b[20]={"/0"},c[45]={"/0"}; int main(){int i,j,k;cout<<"Input the first string:"<<endl;cin.getline(a,20,'\n');cout<<"Input the second string:"<<endl;cin.getline(b,20,'\n');int m=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;return 0;}b6_4.cpp#include<iostream>#include<cstring>using namespace std;char a[20]={"/0"},b[20]={"/0"};int main(){cout<<"Input the first string:"<<endl; cin.getline(a,20,'\n');cout<<"Input the second string:"<<endl; cin.getline(b,20,'\n');string s1=a;string s2=b;string s3=s1+s2;cout<<s3;return 0;}5.Employee.h#ifndef Employee_H_INCLUDED#define Employee_H_INCLUDEDclass Employee{private:char name[15];char address[25];char city[10];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_INCLUDEDEmployee.cpp#include<iostream>#include"Employee.h"using namespace std;Employee::Employee(){}Employee::Employee(char n,char a,char c,int p) {name[0]=n;name[1]='\0';address[0]=a;address[1]='\0';city[0]=c;city[1]='\0';postcode=p;}Employee::~Employee(){}void Employee::change_name(){cout<<"Please input your changed name:"<<endl;cin.getline(name,15,'\n');}void Employee::change_address(){cout<<"Please input your changed address:"<<endl; cin.getline(address,25,'\n');}void Employee::change_city(){cout<<"Please input your changed city:"<<endl;cin.getline(city,10,'\n');}void Employee::change_postcode(){cout<<"Please input your changed postcode:"<<endl; cin>>postcode;}void Employee::display(){cout<<"Your information shows as follow:"<<endl; cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;}int main(){Employee person(1,1,1,1);person.display();person.change_name();person.change_address();person.change_city();person.change_postcode();person.display();return 0;}6.Employee.h#ifndef Employee_H_INCLUDED#define Employee_H_INCLUDEDclass Employee{private:char name[15];char address[25];char city[10];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_INCLUDEDEmployee.cpp#include<iostream>#include"Employee.h"using namespace std;Employee::Employee(){}Employee::Employee(char n,char a,char c,int p) {name[0]=n;name[1]='\0';address[0]=a;address[1]='\0';city[0]=c;city[1]='\0';postcode=p;}Employee::~Employee(){}void Employee::change_name(){cout<<"Please input your changed name:"<<endl;cin.getline(name,15,'\n');}void Employee::change_address(){cout<<"Please input your changed address:"<<endl; cin.getline(address,25,'\n');}void Employee::change_city(){cout<<"Please input your changed city:"<<endl;cin.getline(city,10,'\n');}void Employee::change_postcode(){cout<<"Please input your changed postcode:"<<endl; cin>>postcode;}void Employee::display(){cout<<"Your information shows as follow:"<<endl;cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;}int main(){Employeeemp[5]={Employee(1,1,1,1),Employee(2,2,2,2),Employee(3,3,3, 3),Employee(4,4,4,4),Employee(5,5,5,5)};int i;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<<"The emp "<<i<<" "; emp[i].display();}return 0;}7.#include<iostream>#include<cstring>using namespace std;//Date类class 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 name[11];char number[7];char sex[3];Date birthday;char id[16];public:People();People(char* n,char* nu,char* 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成员函数,设置出生年月日void Date::setDate(){int y,m,d;cout<<"Input the year:";cin>>y;cout<<"Input the month:";cin>>m;cout<<"Input the day:";cin>>d;year=y;month=m;day=d;}//Date内联成员函数,输出年月日inline void Date::showDate(){cout<<"Birthday is "<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}//People构造函数People::People(){};People::People(char* n,char* nu,char* s,Date b,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析构inline People::~People(){}//People成员函数,设置各类数据void People::setName(){cout<<"Please input the person's name:";cin.getline(name,11,'\n');}void People::setNumber(){cout<<"Input number:";cin.getline(number,7,'\n');}void People::setSex(){cout<<"Input sex:";cin.getline(sex,3,'\n');}void People::setId(){cout<<"Input id:";cin.getline(id,16,'\n');}//People内联成员函数,输出人员信息inline void People::showPeople(){cout<<"Name:"<<name<<endl;cout<<"Number:"<<number<<endl;cout<<"Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;}int main(){int i;char spaceA;//生成3个Date类型的对象Date date[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();}return 0;}6.运行结果1.2.3.4.5. 6.7.7.心得体会通过本次上机课,我对数组的理解进一步加深,并且学会了对字符串数据的组织和处理,能够运用字符串类对字符串进行直接的运算;并且进一步熟悉了标准C++库的使用和指针的使用方法;通过进一步的练习,对Debug的操作更加熟练;而且实践操作了利用指针和new操作在堆区里开辟空间,然后利用delete释放空间,进一步加深了对不同类型存储空间的理解,提高了自己的实际操作能力。

相关主题