实验06 数组、指针与字符串(4学时)
(第6章 数组、指针与字符串)
(1) 学习使用数组数据对象。
(2) 学习字符串数据的组织和处理。
(3) 学习标准C++库的使用。
(4) 掌握指针的使用方法。
(5) 练习通过调试功能观察指针的内容及其所指的对象的内容。
(6) 练习通过动态内存分配实现动态数组,并体会指针在其中的作用。
(7) 分别使用字符数组和标准C++库练习处理字符串的方法。
其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性,实现并测试这个类。
具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号char id[16]。其中“出生日期”声明为一个“日期”类内嵌子对象。
用成员函数实现对人员信息的录入和显示。
要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。
在测试程序中声明people类的对象数组,录入数据并显示。
使用循环语句实现矩阵元素的行列对调,注意在循环语句中究竟需要对哪些元素进行操作,编写main()函数实现输入输出。程序名:lab6_1.cpp。
参考运行结果:
//lab6_1.cpp #include using namespace std; void move(int matrix[][3]){ int temp; for(int i=0;i<3;i++) for(int j=0;j temp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } } void main(){ int i,j,mat[3][3]; cout<<"输入3×3矩阵"< for(i=0;i<3;i++){ cout<<"第"<"行:"; for(j=0;j<3;j++) cin>>mat[i][j]; } cout<<"\n输入的3×3矩阵为:"< for(i=0;i<3;i++){ for(j=0;j<3;j++) cout< cout< } move(mat); cout<<"转置后的矩阵为:"< for(i=0;i<3;i++){ for(j=0;j<3;j++) cout<<*(*(mat+i)+j)<<" ";//指针形式 cout< } } |
使用指针对数组元素进行操作,在main()函数中使用new操作符分配内存生成动态数组。通过调试功能观察指针的内容及其所指的对象中的内容。程序名:lab6_2.cpp。
注:可使用一维动态数组,或二维动态数组。
//lab6_2.cpp #include #include using namespace std; void move(int *matrix,int n){//矩阵为整型指针,对应一维数组 int temp; for(int i=0;i for(int j=0;j temp=matrix[i*n+j]; matrix[i*n+j]=matrix[j*n+i]; matrix[j*n+i]=temp; } } void main(){ int i,j,n,*mat; cout<<"输入方阵的阶数:"; cin>>n; mat=new int[n*n]; cout<<"输入"< for(i=0;i cout<<"第"<"行:"; for(j=0;j } cout<<"\n输入的矩阵为:"< for(i=0;i for(j=0;j cout< cout< } move(mat,n); cout<<"转置后的矩阵为:"< for(i=0;i for(j=0;j cout< cout< } delete []mat; } |
//lab6_2.cpp #include #include using namespace std; void move(int **matrix,int n){//矩阵为指向整型指针的指针,对应二维数组 int temp; for(int i=0;i for(int j=0;j temp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } } void main(){ int i,j,n,**mat; cout<<"输入方阵的阶数:"; cin>>n; mat=new int*[n]; for(i=0;i cout<<"输入"< for(i=0;i cout<<"第"<"行:"; for(j=0;j } cout<<"\n输入的矩阵为:"< for(i=0;i for(j=0;j cout< cout< } move(mat,n); cout<<"转置后的矩阵为:"< for(i=0;i for(j=0;j cout< cout< } delete []mat; } |
声明字符数组保存字符串,在程序中提示用户输入两个字符串,实现两个字符串的连接,最后用cout语句显示输出。程序名:lab6_3.cpp。
用cin实现输入,注意,字符串的结束标志是ASCII码0,使用循环语句进行字符串间的字符拷贝。// 字符串的结束标志'\0'
参考运行结果:
//lab6_3.cpp #include using namespace std; void strLL(char *a,char *b,char *c); void main(){ int i; char a[10],b[10],c[20]; cout<<"输入一个字符串a:"; cin>>a; cout<<"显示内存中的字符串a:"; for(i=0;i<10;i++){ if(a[i]) cout<//a[i]不为0 else{ cout<<"\'\\0\'";//转义 break; } } cout< cout<<"\n输入另一个字符串b:"; cin>>b; cout<<"显示内存中的字符串b:"; cout<"\'\\0\'"< strLL(a,b,c); cout<<"\n显示内存中的连接字符串c:"; for(i=0;i<20;i++){ if(c[i]) cout< else{ cout<<"\'\\0\'"; break; } } cout< } void strLL(char *a,char *b,char *c){ int i,j; for(i=0;a[i];i++) c[i]=a[i];//循环直到a[i]为0 for(j=0;b[j];i++,j++) c[i]=b[j]; c[i]=0; } |
在string类中已重载了运算符“+=”实现字符串的连接,可以使用这个功能。程序名:lab6_4.cpp。
//lab6_4.cpp #include #include using namespace std; void main(){ string s1,s2; cout<<"输入字符串s1:"; cin>>s1; cout<<"输入字符串s2:"; cin>>s2; s1+=s2; cout<<"s1+s2 = "< } |
在employee.h文件中声明Employee类。Employee类具有姓名、街道地址、城市和邮编等私有数据成员,都可以用字符数组来表示。
成员函数:
构造函数 用来初始化所有成员数组,对字符数组的赋值可以使用字符串拷贝函数strcpy(char *, char *name);
display() 使用cout显示姓名、街道地址、城市和邮编等属性;
change_name() 改变类中表示姓名属性的字符数组类型的数据成员。
在主程序中声明这个类的对象并对其进行操作。程序名:lab6_5.cpp。
参考运行结果:
//lab6_5employee.h #include #include using namespace std; class Employee{ private: char name[10],street[20],city[10],mail[7];//姓名、街道、城市、邮编 public: Employee(char *n,char *s,char *c,char *m); void display(); void change_name(char *n); }; Employee::Employee(char *n,char *s,char *c,char *m){ strcpy(name,n); strcpy(street,s); strcpy(city,c); strcpy(mail,m); } void Employee::display(){ cout<<"姓名:"< cout<<"街道:"< cout<<"城市:"< cout<<"邮编:"< } void Employee::change_name(char *n){ strcpy(name,n); } |
//lab6_5.cpp #include "lab6_5employee.h" void main(){ Employee emp("张山","海甸三西路13号","海口市","570228"); emp.display(); emp.change_name("李世"); cout< emp.display(); } |
使用循环语句把数据显示出来。程序名:lab6_6.cpp。
参考运行结果:
//lab6_6.cpp #include "lab6_5employee.h" #include using namespace std; void main(){ Employee emp[5]={ Employee("n1","s1","c1","m1"), Employee("n2","s2","c2","m2"), Employee("n3","s3","c3","m3"), Employee("n4","s4","c4","m4"), Employee("n5","s5","c5","m5") }; for(int i=0;i<5;i++){ emp[i].display(); cout< } } |
程序名:lab6_7.cpp。
参考运行结果:
//lab6_7.h #include #include using namespace std; class Date{ private: int yy,mm,dd; public: Date(){ } Date(Date &d) : yy(d.yy),mm(d.mm),dd(d.dd){}//增添 ~Date(){ } int Getyy()const{ return yy; } int Getmm()const{ return mm; } int Getdd()const{ return dd; } void Setyy(int y){ yy=y; } void Setmm(int m){ mm=m; } void Setdd(int d){ dd=d; } }; class People{ private: char name[11]; //姓名,增添 char number[7]; //编号,改为数组 char sex[3]; //男或女,改为数组 Date birthday; //出生日期 char id[16]; //身份证号,改为数组 public: People(){} People(People &p); ~People(){} void Getname(char na[]){ strcpy(na,name); } void Getnumber(char nu[]){ strcpy(nu,number); } void Getsex(char se[]){strcpy(se,sex); } int Getbirthyy()const{ return birthday.Getyy(); } int Getbirthmm()const{ return birthday.Getmm(); } int Getbirthdd()const{ return birthday.Getdd(); } void Getid(char d[]){ strcpy(d,id); } void Setname(char na[]){ strcpy(name,na); } void Setnumber(char nu[]){ strcpy(number,nu); } void Setsex(char se[]){strcpy(sex,se); } void Setbirthyy(int y){ birthday.Setyy(y); } void Setbirthmm(int m){ birthday.Setmm(m); } void Setbirthdd(int d){ birthday.Setdd(d); } void Setid(char d[]){ strcpy(id,d); } void input(); void output(); }; People::People(People &p) :birthday(p.birthday) { strcpy(name,p.name); strcpy(number,p.number); strcpy(sex,p.sex); strcpy(id,p.id); } inline void People::input() { cout<<"姓名:"; cin>>name; cout<<"编号:"; cin>>number; cout<<"性别(男/女):"; cin>>sex; cout<<"出生日期(年 月 日):"; int y,m,d; cin>>y>>m>>d; Setbirthyy(y); Setbirthmm(m); Setbirthdd(d); cout<<"身份证号:"; cin>>id; } inline void People::output() { cout<<"姓名:"< cout<<"性别:"< cout<<"出生日期:"< < cout<<"身份证号:"< } |
//lab6_7.cpp #include "lab6_7.h" void main(){ People p; p.input(); cout< p.output(); People pp(p); cout< pp.output(); } |
#include<iostream> using namespace std; void main(){ int i,j,m,n,**A; cout<<"输入矩阵的行数m和列数n:"; cin>>m>>n; A=new int*[m]; //m行 for(i=0;i cout<<"输入"< for(i=0;i cout<<"第"<"行:"; for(j=0;j } cout<<"\n输入的矩阵为:"< for(i=0;i for(j=0;j cout< } } |
#include<iostream> using namespace std; void main(){ int **a=new int*[2]; a[0]=new int[3]; a[1]=new int[2]; a[0][0]=11; a[0][1]=12; a[0][2]=13; a[1][0]=21; a[1][1]=22; cout<" "< } |
本文来源:https://www.2haoxitong.net/k/doc/3822d807cbaedd3383c4bb4cf7ec4afe05a1b15b.html
文档为doc格式