C++上机实验报告实验四Word文件下载.docx
- 文档编号:16897214
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:20
- 大小:43.88KB
C++上机实验报告实验四Word文件下载.docx
《C++上机实验报告实验四Word文件下载.docx》由会员分享,可在线阅读,更多相关《C++上机实验报告实验四Word文件下载.docx(20页珍藏版)》请在冰豆网上搜索。
Iab6_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()改变类中表示姓名属性的数据成员。
在主程序中定义这个类的对象并对其进行操作。
Iab6_5.cpp。
6•使用上一小题中定义的Employee类定义对象数组emp[5],使用循环语句把数据显示出来。
程序名:
Iab6_6.cpp。
4.思考题
1.如何存储和处理字符串?
(1)可以利用字符数组存储和处理字符串;
(2)利用系统提供的string类存储和处理字符串。
2.头文件<
string.h>
和头文件<
string>
有何区别?
包含头文件<
后,可以使用系统的字符串处理函数,如strcat(连接).strcpy(复
制).strcmp(比较).strlen(求长度).strlwr(转换为小写).strupr(转换为大写)等等;
而包含头文件<
后,则可以定义string类,并且使用系统提供的string类操作符对string
类型的对象进行处理。
3.有几种方法来表示和处理数组元素?
(1)数组下标方法,如a[i]
(2)指针的方法,如int*p=&
a[0]
5.源程序
1.lab6_1.cpp
#include<
iostream>
usingnamespacestd;
inta[3][3];
voidshowTrans()
{
inti,j;
cout<
<
"
Thetranspositionmatrixis:
endl;
for(j=0;
j<
3;
j++)
for(i=0;
i<
i++)
a[i][j];
;
}
endl;
voidinput()
Pleaseinputyour3*3matrix:
intmain()
input();
showTrans();
return0;
2」ab6_2.cpp
int*p[9];
for(j=i;
9;
j=j+3)
*p[j];
inti,n;
for(i=0;
cin»
n;
p[i]=newint(n);
inti;
deletep[i];
3」ab6_3.cpp
//不使用系统自带函数strcpy
chara[20]={"
/0"
},b[20]={"
},c[45]={"
};
inti,j,k;
Inputthefirststring:
cin.getline(a,20,'
\n'
);
Inputthesecondstring:
cin.getline(b,20,'
intm=0,n=0;
//将数组a中的字符串拷贝到数组c中
for(k=0;
k<
45,a[m]!
='
\0'
k++,m++)
c[k]=a[m];
//将数组b中的字符串接着a,拷贝到数组c中
for(;
45,b[n]!
k++,n++)
c[k]=b[n];
c;
4.lab6_4.cpp
cstring>
lnputthefirststring:
cin.getline(a,20,'
lnputthesecondstring:
cin.getline(b,20,'
strings1=a;
strings2=b;
strings3=s1+s2;
s3;
5.Employee.h
#ifndefEmployee』」NCLUDED
#defineEmployee_H」NCLUDED
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」NCLUDED
Employee.cpp
#include"
Employee.h"
Employee:
:
Employee(){}
Employee(charn,chara,charc,intp){
name[O]=n;
name[1]='
\0:
address[O]=a;
address[1]='
city[O]=c;
city[1]='
postcode=p;
~Employee(){}
voidEmployee:
change_name()
Pleaseinputyourchangedname:
cin.getline(name,15,'
change_address()
Pleaseinputyourchangedaddress:
cin.getline(address,25,'
change_city()
Pleaseinputyourchangedcity:
cin.getline(city,10,'
change_postcode()
Pleaseinputyourchangedpostcode:
cin>
>
postcode;
display()
Yourinformationshowsasfollow:
cout<
<
name:
name<
address:
address<
city:
city<
postcode:
postcode<
Employeeperson(1,1,1,1);
person.display();
person.change_name();
person.change_address();
person.change_city();
person.change_postcode();
6.Employee.h
#ifndefEmployee_H」NCLUDED
name[0]=n;
address[0]=a;
city[0]=c;
postcode=p;
cin.getline(name,15,'
cin.getline(address,25,'
cin>
Employee
emp[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)};
5;
emp[i].display();
emp[i].change_name();
emp[i].change_address();
emp[i].change_city();
emp[i].change_postcode();
cin.get();
Theemp"
emp[i].display();
7.
//Date类
classDate
intyear;
intmonth;
intday;
Date();
Date(inty,intm,intd);
Date(Date&
p);
~Date();
voidsetDate();
voidshowDate();
//People类,其中含Date类型的数据
classPeople
charname[11];
charnumber[7];
charsex[3];
Datebirthday;
charid[16];
People();
People(char*n,char*nu,char*s,Dateb,char*i);
People(People&
p);
〜People。
;
voidsetName();
voidsetNumber();
voidsetSex();
voidsetId();
voidshowPeople();
//Date构造函数
Date:
Date(){}
Date(inty,intm,intd)
year=y;
month=m;
day=d;
p)
year=p.year;
month=p.month;
day=p.day;
//析构
inlineDate:
~Date(){}
//Date成员函数,设置出生年月日
voidDate:
setDate()
inty,m,d;
lnputtheyear:
y;
Inputthemonth:
m;
Inputtheday:
d;
//Date内联成员函数,输出年月日
inlinevoidDate:
showDate()
Birthdayis"
year<
年"
month<
月"
day<
日"
//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);
strcpy(name,p.name);
strcpy(number,p.number);
birthday=p.birthday;
strcpy(id,p.id);
//People析构
inlinePeople:
~People(){}
//People成员函数,设置各类数据
voidPeople:
setName()
Pleaseinputtheperson'
sname:
cin.getline(name,11,'
setNumber()
Inputnumber:
cin.getline(number,7,'
setSex()
Inputsex:
cin.getline(sex,3,'
setId()
Inputid:
cin.getline(id,16,'
//People内联成员函数,输出人员信息
inlinevoidPeople:
showPeople()
Name:
Number:
number<
Sex:
sex<
ID:
id<
charspaceA;
//生成3个Date类型的对象
Datedate[3]={Date(0,0,0),Date(0,0,0),Date(0,0,0)};
//生成3个People类型的对象
People
person[3]={People("
0"
"
date[0],"
),People("
date[1],"
date[2],"
)};
//设置这3个对象的各类信息
person[i].setName();
person[i].setNumber();
person[i].setSex();
person[i].setld();
date[i].setDate();
spaceA=getchar();
//输出这3个对象的各类信息
person[i].showPeople();
date[i].showDate();
6.运行结果
1.
Pleaseinputyom*3*3matrix:
123
456
789
Thetranspositionnatrixis:
147
258
369
2.
Pleaseinputyour3*3matrix:
123
Thetpan&
positionmatrixis:
25K
36?
ProcessretuiTied0(0x0》executiontime:
3.
Inputthefirststring:
^bcl23456
Inputthesecondstrinsr-defgh789
kbcl23456def^h789
Processf台turned0<
0x0>
executiontime:
12.967sPressanykeytocontinue・
Votirinfopmationshousasname:
Vouirin£
ornationshousfollau:
nane:
lizhibo
address=shisheCl75
city:
shenyansfpostcode:
1234567
Processreturned
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 上机 实验 报告
