C 语言程序设计实验答案 - 数组、指针与字符串解析

发布时间:2018-12-08 00:10:51   来源:文档文库   
字号:

实验06 数组、指针与字符串(4学时)

(第6 数组、指针与字符串

一、实验目的

(1) 学习使用数组数据对象。

(2) 学习字符串数据的组织和处理。

(3) 学习标准C++库的使用。

(4) 掌握指针的使用方法。

(5) 练习通过调试功能观察指针的内容及其所指的对象的内容。

(6) 练习通过动态内存分配实现动态数组,并体会指针在其中的作用。

(7) 分别使用字符数组和标准C++库练习处理字符串的方法。

二、实验任务

6_1(习题6-25)编写并测试3×3矩阵转置函数,使用数组保存3×3矩阵。

6_2(习题6-26)使用动态内存分配生成动态数组来重新完成上题(n阶方阵),使用指针实现函数的功能。

6_3 编程实现两字符串的连接。要求使用字符数组保存字符串,不要使用系统函数。

6_4 使用string类声明字符串对象,重新实现上一小题。

6_5(习题6-27)声明一个Employee类。

其中包括姓名、街道地址、城市和邮编等属性,以及change_name()display()等函数。display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性,实现并测试这个类。

6_6(习题6-27)声明包含5个元素的对象数组,每个元素都是Employee类型的对象。

6_7 修改实验4中的people(人员)类。

具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号char id[16]。其中“出生日期”声明为一个“日期”类内嵌子对象。

用成员函数实现对人员信息的录入和显示。

要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。

在测试程序中声明people类的对象数组,录入数据并显示。

三、实验步骤

1.(编程,习题6-25)编写矩阵转置函数,输入参数为3×3整型数组。

使用循环语句实现矩阵元素的行列对调,注意在循环语句中究竟需要对哪些元素进行操作,编写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<

}

}

2.(编程,习题6-26)编写n×n阶矩阵转置函数,参数为整型指针。

使用指针对数组元素进行操作,在main()函数中使用new操作符分配内存生成动态数组。通过调试功能观察指针的内容及其所指的对象中的内容。程序名:lab6_2.cpp

注:可使用一维动态数组,或二维动态数组。

程序运行结果:

(1) 使用一维动态数组表示方阵。

//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>mat[i*n+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;

}

(2) 使用维动态数组表示方阵。(参考实验提示)

//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;inew int[n];

cout<<"输入"<"×"<"矩阵"<

for(i=0;i

cout<<""<"行:";

for(j=0;j>mat[i][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;

}

3.(编程)实现两字符串的连接。

声明字符数组保存字符串,在程序中提示用户输入两个字符串,实现两个字符串的连接,最后用cout语句显示输出。程序名:lab6_3.cpp

cin实现输入,注意,字符串的结束标志是ASCII0,使用循环语句进行字符串间的字符拷贝。// 字符串的结束标志'\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;

}

4.(编程)使用string类声明字符串对象,实现两字符串的连接。

string类中已重载了运算符“+=”实现字符串的连接,可以使用这个功能。程序名:lab6_4.cpp

程序运行结果:

//lab6_4.cpp

#include //使用C++中的string

#include

using namespace std;

void main(){

string s1,s2;

cout<<"输入字符串s1";

cin>>s1;

cout<<"输入字符串s2";

cin>>s2;

s1+=s2;

cout<<"s1+s2 = "<

}

5.(编程,习题6-27)声明Employee类并应用。

employee.h文件中声明Employee类。Employee类具有姓名、街道地址、城市和邮编等私有数据成员,都可以用字符数组来表示。

成员函数:

构造函数 用来初始化所有成员数组,对字符数组的赋值可以使用字符串拷贝函数strcpy(char *, char *name)

display() 使用cout显示姓名、街道地址、城市和邮编等属性;

change_name() 改变类中表示姓名属性的字符数组类型的数据成员。

在主程序中声明这个类的对象并对其进行操作。程序名:lab6_5.cpp

参考运行结果:

程序运行结果:

//lab6_5employee.h

#include //使用C中的string

#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();

}

6.(编程,习题6-27)使用上一小题中的Employee类声明对象数组emp[5]

使用循环语句把数据显示出来。程序名: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<

}

}

7.(编程)修改实验4中的people(人员)类。

程序名:lab6_7.cpp

参考运行结果:

程序运行结果:

(1) lab6_7.hPeople(人员)类的声明及成员函数的实现

//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<<"姓名:"<"\n编号:"<

cout<<"性别:"<

cout<<"出生日期:"<""

<""<""<

cout<<"身份证号:"<

}

(2) lab6_7.cpp:主函数(输入、输出和拷贝构造函数测试)

//lab6_7.cpp

#include "lab6_7.h"

void main(){

People p;

p.input();

cout<

p.output();

People pp(p);

cout<

pp.output();

}

(3) 结果

、实验提示

步骤2提示

二维数组动态分配空间

示例1 m×n矩阵

#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;inew int[n]; //n

cout<<"输入"<"×"<"矩阵"<

for(i=0;i

cout<<""<"行:";

for(j=0;j>A[i][j];//数组形式

}

cout<<"\n输入的矩阵为:"<

for(i=0;i

for(j=0;j

cout<<" "<

cout<

}

}

示例2 不规范的矩阵

#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<" "<" "<

cout<" "<

}

本文来源:https://www.2haoxitong.net/k/doc/3822d807cbaedd3383c4bb4cf7ec4afe05a1b15b.html

《C 语言程序设计实验答案 - 数组、指针与字符串解析.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式