}
}
voidinput()
{
inti,j;
cout<<"Pleaseinputyour3*3matrix:
"<for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
intmain()
{
input();
showTrans();
return0;
}
2.lab6_2.cpp
#include
usingnamespacestd;
int*p[9];
voidshowTrans()
{
inti,j;
cout<<"Thetranspositionmatrixis:
"<for(i=0;i<3;i++)
{
for(j=i;j<9;j=j+3)
{
cout<<*p[j];
cout<<"";
}
cout<}
}
voidinput()
{
inti,n;
cout<<"Pleaseinputyour3*3matrix:
"<for(i=0;i<9;i++)
{
cin>>n;
p[i]=newint(n);
}
}
intmain()
{
input();
showTrans();
inti;
for(i=0;i<9;i++)
{
deletep[i];
}
return0;
}
3.lab6_3.cpp
#include
//不使用系统自带函数strcpy
usingnamespacestd;
chara[20]={"/0"},b[20]={"/0"},c[45]={"/0"};
intmain()
{
inti,j,k;
cout<<"Inputthefirststring:
"<cin.getline(a,20,'\n');
cout<<"Inputthesecondstring:
"<cin.getline(b,20,'\n');
intm=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<return0;
}
4.lab6_4.cpp
#include
#include
usingnamespacestd;
chara[20]={"/0"},b[20]={"/0"};
intmain()
{
cout<<"Inputthefirststring:
"<cin.getline(a,20,'\n');
cout<<"Inputthesecondstring:
"<cin.getline(b,20,'\n');
strings1=a;
strings2=b;
strings3=s1+s2;
cout<return0;
}
5.Employee.h
#ifndefEmployee_H_INCLUDED
#defineEmployee_H_INCLUDED
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_INCLUDED
Employee.cpp
#include
#include"Employee.h"
usingnamespacestd;
Employee:
:
Employee(){}
Employee:
:
Employee(charn,chara,charc,intp)
{
name[0]=n;
name[1]='\0';
address[0]=a;
address[1]='\0';
city[0]=c;
city[1]='\0';
postcode=p;
}
Employee:
:
~Employee(){}
voidEmployee:
:
change_name()
{
cout<<"Pleaseinputyourchangedname:
"<cin.getline(name,15,'\n');
}
voidEmployee:
:
change_address()
{
cout<<"Pleaseinputyourchangedaddress:
"<cin.getline(address,25,'\n');
}
voidEmployee:
:
change_city()
{
cout<<"Pleaseinputyourchangedcity:
"<cin.getline(city,10,'\n');
}
voidEmployee:
:
change_postcode()
{
cout<<"Pleaseinputyourchangedpostcode:
"<cin>>postcode;
}
voidEmployee:
:
display()
{
cout<<"Yourinformationshowsasfollow:
"<cout<<"name:
"<cout<<"address:
"<
cout<<"city:
"<cout<<"postcode:
"<}
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_INCLUDED
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_INCLUDED
Employee.cpp
#include
#include"Employee.h"
usingnamespacestd;
Employee:
:
Employee(){}
Employee:
:
Employee(charn,chara,charc,intp)
{
name[0]=n;
name[1]='\0';
address[0]=a;
address[1]='\0';
city[0]=c;
city[1]='\0';
postcode=p;
}
Employee:
:
~Employee(){}
voidEmployee:
:
change_name()
{
cout<<"Pleaseinputyourchangedname:
"<cin.getline(name,15,'\n');
}
voidEmployee:
:
change_address()
{
cout<<"Pleaseinputyourchangedaddress:
"<cin.getline(address,25,'\n');
}
voidEmployee:
:
change_city()
{
cout<<"Pleaseinputyourchangedcity:
"<cin.getline(city,10,'\n');
}
voidEmployee:
:
change_postcode()
{
cout<<"Pleaseinputyourchangedpostcode:
"<cin>>postcode;
}
voidEmployee:
:
display()
{
cout<<"Yourinformationshowsasfollow:
"<cout<<"name:
"<cout<<"address:
"<
cout<<"city:
"<cout<<"postcode:
"<}
intmain()
{
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)};
inti;
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<<"Theemp"<
emp[i].display();
}
return0;
}
7.
#include
#include
usingnamespacestd;
//Date类
classDate
{
private:
intyear;
intmonth;
intday;
public:
Date();
Date(inty,intm,intd);
Date(Date&p);
~Date();
voidsetDate();
voidshowDate();
};
//People类,其中含Date类型的数据
classPeople
{
private:
charname[11];
charnumber[7];
charsex[3];
Datebirthday;
charid[16];
public:
People();
People(char*n,char*nu,char*s,Dateb,char*i);
People(People&p);
~People();
voidsetName();
voidsetNumber();
voidsetSex();
voidsetId();
voidshowPeople();
};
//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<<"Inputtheday:
";
cin>>d;
year=y;
month=m;
day=d;
}
//Date内联成员函数,输出年月日
inlinevoidDate:
:
showDate()
{
cout<<"Birthdayis"<}
//People构造函数
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);
}
People:
:
People(People&p)
{
strcpy(name,p.name);
strcpy(number,p.number);
birthday=p.birthday;
strcpy(id,p.id);
}
//People析构
inlinePeople:
:
~People(){}
//People成员函数,设置各类数据
voidPeople:
:
setName()
{
cout<<"Pleaseinputtheperson'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(id,16,'\n');
}
//People内联成员函数,输出人员信息
inlinevoidPeople:
:
showPeople()
{
cout<<"Name:
"<cout<<"Number:
"<cout<<"Sex:
"<cout<<"ID:
"<}
intmain()
{
inti;
charspaceA;
//生成3个Date类型的对象
Datedate[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();
}
return0;
}
6.运行结果
1.
2.
3.
4.
5
.
6.
7.
7.心得体会
通过本次上机课,我对数组的理解进一步加深,并且学会了对字符串数据的组织和处理,能够运用字符串类对字符串进行直接的运算;并且进一步熟悉了标准C++库的使用和指针的使用方法;通过进一步的练习,对Debug的操作更加熟练;而且实践操作了利用指针和new操作在堆区里开辟空间,然后利用delete释放空间,进一步加深了对不同类型存储空间的理解,提高了自己的实际操作能力。