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

下载本文档

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

文档简介

1、验四数组、指针与字符串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 (人员)类。具有的属性如下:姓名 charname11、编号 charnumber7、性别 charsex3、生日 birthday、身份证号 charid16。其中“出生日期”定义为一个“日期”类内嵌对象。

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 .头文件<string.h>和头文件<string>有何区另1J?包含头文件<

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

7、d;inta33;voidshowTrans() inti,j;cout<<"Thetranspositionmatrixis:"<<endl;for(j=0;j<3;j+) for(i=0;i<3;i+) cout<<aij; cout<<"" cout<<endl; voidinput() inti,j;cout<<"Pleaseinputyour3*3matrix:"<<endl;for(i=0;i<3;i+) for(j=0;

8、j<3;j+) cin>>aij; intmain() input();showTrans();return0;2.lab6_2.cpp#include<iostream>usingnamespacestd;int*p9;voidshowTrans()inti,j;cout<<"Thetranspositionmatrixis:"<<endl;for(i=0;i<3;i+)for(j=i;j<9;j=j+3)cout<<*pj;cout<<""cout<<

9、;endl;voidinput()inti,n;cout<<"Pleaseinputyour3*3matrix:"<<endl;for(i=0;i<9;i+)cin>>n;pi=newint(n);intmain()input();showTrans();inti;for(i=0;i<9;i+)deletepi;return0;3.lab6_3.cpp#include<iostream>/ 不使用系统自带函数strcpyusingnamespacestd;chara20="/ 0",b20=&q

10、uot;/ 0",c45="/ 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,am!='0'k+,m+)ck=a

11、m;将数组b中的字符串接着 a,拷贝到数组c中 for(;k<45,bn!='0'k+,n+) ck=bn;cout<<c;return0;4. lab6_4.cpp#include<iostream>#include<cstring> usingnamespacestd;chara20="/ 0",b20="/ 0"intmain()cout<<"Inputthefirststring:"<<endl;cin.getline(a,20,'n&#

12、39;);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 classEmployeeprivate:charname15;charaddress25;charcity10;intpostcode;public:Emplo

13、yee();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,cha

14、ra,charc,intp)name0=n;name1='0'address0=a;address1='0'city0=c;city1='0'postcode=p;Employee:Employee() voidEmployee:change_name() cout<<"Pleaseinputyourchangedname:"<<endl;cin.getline(name,15,'n');voidEmployee:change_address()cout<<"Ple

15、aseinputyourchangedaddress:"<<endl;cin.getline(address,25,'n');voidEmployee:change_city()cout<<"Pleaseinputyourchangedcity:"<<endl;cin.getline(city,10,'n');voidEmployee:change_postcode()cout<<"Pleaseinputyourchangedpostcode:"<<en

16、dl; cin>>postcode;voidEmployee:display()cout<<"Yourinformationshowsasfollow:"<<endl;cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"city:"<<city<<endl;cout<<"po

17、stcode:"<<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_INCLUDEDclassEmployeeprivate:charnam

18、e15;charaddress25;charcity10;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"usingnamespa

19、cestd;Employee:Employee()Employee:Employee(charn,chara,charc,intp)name0=n;name1='0'address0=a;address1='0'city0=c;city1='0'postcode=p;Employee:Employee() voidEmployee:change_name() cout<<"Pleaseinputyourchangedname:"<<endl;cin.getline(name,15,'n'

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

21、"Pleaseinputyourchangedpostcode:"<<endl; cin>>postcode;voidEmployee:display()cout<<"Yourinformationshowsasfollow:"<<endl;cout<<"name:"<<name<<endl;cout<<"address:"<<address<<endl;cout<<"cit

22、y:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;intmain()Employeeemp5=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+)empi.display();empi.change_name();empi.change_address();empi.change_c

23、ity();empi.change_postcode();cin.get();for(i=0;i<5;i+)cout<<"Theemp"<<i<<""empi.display();return0;7.#include<iostream>#include<cstring> usingnamespacestd;/Date 类classDateprivate:intyear;intmonth;intday;public:Date();Date(inty,intm,intd);Date(Date&

24、amp;p);Date();voidsetDate();voidshowDate();/People 类,其中含Date 类型的数据classPeopleprivate:charname11;charnumber7;charsex3;Datebirthday;charid16;public:People();People(char*n,char*nu,char*s,Dateb,char*i);People(People&p);People();voidsetName();voidsetNumber();voidsetSex();voidsetId();voidshowPeople();

25、/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<&

26、lt;"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,c

27、har*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<<"Pleasei

28、nputtheperson'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

29、(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 类型的对象Datedate3=Date(0,0,0),Date(0,0,0),Date(0,0,0);/ 生成 3 个 People 类型的对象Peopleperson3=People("0","0",&q

温馨提示

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

评论

0/150

提交评论