C++上机实验报告实验四_第1页
C++上机实验报告实验四_第2页
C++上机实验报告实验四_第3页
C++上机实验报告实验四_第4页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验四数组、指针与字符串1. 实验目的1.学习使用数组2.学习字符串数据的组织和处理3.学习标准C+库的使用4.掌握指针的使用方法5.练习通过Debug 观察指针的内容及其所指的对象的内容6.联系通过动态内存分配实现动态数组,并体会指针在其中的作用7.分别使用字符数组和标准C+库练习处理字符串的方法2. 实验要求1.编写并测试3*3 矩阵转置函数,使用数组保存3*3 矩阵。2.使用动态内存分配生成动态数组来重新完成上题,使用指针实现函数的功能。3.编程实现两字符串的连接。要求使用字符数组保存字符串,不要使用系统函数。4.使用 string 类定义字符串对象,重新实现上一小题。5.定义一个Emp

2、loyee 类,其中包括姓名、 街道地址、 城市和邮编等属性,以及 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 观察指针的内容及其所指的对象中的内容。程序名

4、: lab6_2.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.使用上一小题中定义的程序名: lab6_6.cpp 。Employee类定义对象数组emp5,使用循环语句把数据显示出来。4.思考题1.如何存储和处理字符串?(1)可以利用字符数组存储和处理字符串;(2)利用系统提供的string 类存储和处理字符串。2.头文件 <string.h>和头文件 <str

6、ing> 有何区别?包含头文件<string.h> 后,可以使用系统的字符串处理函数,如strcat(连接) .strcpy (复制) .strcmp (比较) .strlen (求长度) .strlwr (转换为小写).strupr (转换为大写)等等;而包含头文件 <string> 后,则可以定义string 类,并且使用系统提供的string 类操作符对string类型的对象进行处理。3.有几种方法来表示和处理数组元素?( 1)数组下标方法,如 ai( 2)指针的方法,如 int* p=&a05.源程序1.lab6_1.cpp#include<

7、iostream>using namespace std;int a33;void showTrans()int i,j;cout<<"The transposition matrix is:"<<endl;for(j=0;j<3;j+)for(i=0;i<3;i+)cout<<aij;cout<<" "cout<<endl;void input()int i,j;cout<<"Please input your 3*3 matrix:"<

8、<endl;for(i=0;i<3;i+)for(j=0;j<3;j+)cin>>aij;int main()input();showTrans();return 0;2.lab6_2.cpp#include<iostream>using namespace std;int* p9;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<&

9、lt;*pj;cout<<" "cout<<endl;void input()int i,n;cout<<"Please input your 3*3 matrix:"<<endl;for(i=0;i<9;i+)cin>>n;pi=new int(n);int main()input();showTrans();int i;for(i=0;i<9;i+)delete pi;return 0;3.lab6_3.cpp#include<iostream>/ 不使用系统自带函数

10、 strcpy using namespace std;char a20="/0",b20="/0",c45="/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

11、=0;/ 将数组 a 中的字符串拷贝到数组 for(k=0;k<45,am!='0'k+,m+) c 中ck=am;/ 将数组 b 中的字符串接着 a,拷贝到数组 c 中for(;k<45,bn!='0'k+,n+)ck=bn;cout<<c;return 0;4. lab6_4.cpp#include<iostream>#include<cstring> using namespace std;char a20="/0",b20="/0" int main()cout<

12、;<"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_INC

13、LUDEDclass Employeeprivate: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_INCLUDEDEmployee.cpp#include<iostr

14、eam>#include"Employee.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()cout<<"Please input your chang

15、ed 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.getl

16、ine(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&

17、lt;<"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.chan

18、ge_postcode();person.display();return 0;6.Employee.h#ifndef Employee_H_INCLUDED#define Employee_H_INCLUDEDclass Employeeprivate: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

19、ity();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)name0=n;name1='0'address0=a;address1='0'city0=c;city1='0&#

20、39;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');vo

21、id 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 infor

22、mation shows as follow:"<<endl;cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;int main()Employeeemp5=Emplo

23、yee(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+)empi.display();empi.change_name();empi.change_address();empi.change_city();empi.change_postcode();cin.get();for(i=0;i<5;i+)cout<<"The emp "<<i<<" "emp

24、i.display();return 0;7.#include<iostream>#include<cstring>using namespace std;/Date 类class Dateprivate: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 Peopleprivate:char name11;char numb

25、er7;char sex3;Date birthday;char id16;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 &

26、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 内联成员函数,输出年

27、月日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);str

28、cpy(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

29、');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<<&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论