c++实验答案 下载本文

内容发布更新时间 : 2024/5/12 9:01:57星期一 下面是文章的全部内容请认真阅读。

c[1]=20+20i c[2]=30+30i c[3]=40+40i c[4]=50+50i sum=150+150i (5)

# include # include struct Rectangle { float x1,y1;

float length,width; };

void Area(Rectangle a) { float wlength,area;

wlength=(a.length+a.width)*2; area=a.length*a.width;

cout<<\ cout<<\ cout<<\ }

void main(void) { Rectangle Rect;

cout<<\ cin>>Rect.x1>>Rect.y1;

cout<<\ cin>>Rect.length>>Rect.width; Area(Rect); }

程序运行结果:

Ipunt Rect(x1,y1):100 100

Ipunt Rectangle length and width:200 50 Rectangle left-top point: 100 100 whole length= 500 Area= 10000

Press any key to continue

实验十

1.实验目的

(1)理解链表的概念及使用链表的优点。

(2)学会链表的建立、查询、输出、删除、排序等操作。 (3)初步学会用链表处理职工工资等实际问题。 2.实验要求

(1)编写实验程序;

(2)在VC++运行环境中,输入源程序; (3)编译运行源程序;

(4)输入测试数据进行程序测试; (5)写出运行结果。 3.实验内容

(1)建立一个描述职工工资的无序链表,各结点内容如表8.4所示。计算出各职工的实发工资,并输出链表中各职工结点的内容。最后删除链表,回收链表占用空间。建立无序链表、计算实发工资(实发工资=应发工资-税金)、输出链表、删除链表各用一个函数实现。在主函数中调用四个函数完成上述操作。 表8.4 职工工资表

no(工号)name[8] (姓名)dsalary (应发工资) tax(税金)fsalary (实发工资) 1001Zhang190085 1002Wang180080 1003Li170070 1004Zhou200090

(2)在实验(1)的基础上,再编写能删除指定工号结点的函数,能在指定工号结点前插入新职工结点的函数。在主函数中输入要删除与插入结点的工号,并调用删除与插入函数删除与插入指定结点。插入新职工的信息在插入函数内输入。

(3)建立一个描述职工工资的有序链表,各结点内容如表8.4所示,输入职工信息时自动计算实发工资,链表按实发工资升序排列。输出有序链表各结点内容,最后删除链表。 4.解答参考 (1)

# include # include struct employee { int no;

char name[8]; float salary,tax; float fsalary; node *next; };

node * Create(void )

{ int no; //定义输入职工工号的临时变量no

node *head,*pn,*pt; //定义链表头指针、新结点指针、尾指针head、pn、pt。 head=0; //链表头指针赋0,表示链表为空。 cout<<\产生无序链表,请输入工号、姓名、应发工资、税金, 以工号为-1结束:\

cin>>no; //输入职工工号

while (no!= -1) //工号为-1时结束输入

{ pn= new node; //动态分配新结点内存空间,并将结点地址赋给pn。 pn->no=no; //将职工工号输入新结点

cin>>pn->name ; //职工姓名输入新结点 cin>>pn->salary ; //应发工资输入新结点 cin>>pn->tax ; //税金输入新结点 if (head==0) //若链表为空

{ head=pn; //则将新结点地址由pn赋给头指针head与尾指针pt

pt=pn; //使新结点加入到链首 }

else //否则链表非空

{ pt->next=pn; //将新结点地址由pn赋给链尾的next指针与尾指针pt

pt=pn; //使新结点加入到链尾 }

cin >>no; //输入职工工号 }

pt->next=0; //链尾指针变量赋0 return (head); //返回链表的头指针 }

void Calcu(node *head) { node *p; p=head;

while (p!=0 )

{ p->fsalary=p->salary-p->tax; p=p->next; } }

void Print(const node *head) { const node *p; p=head;

cout<<\输出链表中各结点值:\ while (p!=0 )

{ cout<no<<'\\t'<name<<'\\t'<salary <<'\\t'<tax<<'\\t'<fsalary<<'\\t'<next; } }

void Delchain(node * head) { node * p;

p=head; //链表头指针赋给p

while (head) //当链表非空时删除结点

{ head=p->next; //将链表下一个结点指针赋给head delete p; //删除链表第一个结点

p=head; //再将头指针赋给p } }

void main(void) //主函数 { node * head;

head=Create(); //产生无序链表 Calcu(head);

Print(head); //输出无序链表 Delchain(head); //删除整个链表 }

程序运行结果:

产生无序链表,请输入工号、姓名、应发工资,税金,以工号为-1结束: 1001 zhang 1900 85 1002 wang 1800 80 1003 li 1700 70 1004 zhou 2000 90 -1

输出链表中各结点值:

1001 zhang 1900 85 1815 1002 wang 1800 80 1720 1003 li 1700 70 1655 1004 zhou 2000 90 1910 (2)

# include # include struct node { int no;

char name[8]; float salary,tax; float fsalary; node *next; };

node * Create(void )

{ int no; //定义输入职工工号的临时变量no

node *head,*pn,*pt; //定义链表头指针、新结点指针、尾指针head、pn、pt。 head=0; //链表头指针赋0,表示链表为空。 cout<<\产生无序链表,请输入工号、姓名、应发工资、税金,以工号为-1结束:\ cin>>no; //输入职工工号

while (no!= -1) //成绩为-1时结束输入

{ pn= new node; //动态分配新结点内存空间,并将结点地址赋给pn。 pn->no=no; //将职工工号输入新结点 cin>>pn->name ; //职工姓名输入新结点

cin>>pn->salary ; //应发工资输入新结点 cin>>pn->tax ; //税金输入新结点 if (head==0) //若链表为空

{ head=pn; //则将新结点地址由pn赋给头指针head与尾指针pt

pt=pn; //使新结点加入到链首 }

else //否则链表非空

{ pt->next=pn; //将新结点地址由pn赋给链尾的next指针与尾指针pt

pt=pn; //使新结点加入到链尾 }

cin >>no; //输入职工工号 }

pt->next=0; //链尾指针变量赋0 return (head); //返回链表的头指针 }

void Calcu(node *head) { node *p; p=head;

while (p!=0 )

{ p->fsalary=p->salary-p->tax; p=p->next; } }

void Print(const node *head) { const node *p; p=head;

cout<<\输出链表中各结点值:\ while (p!=0 )

{ cout<no<<'\\t'<name<<'\\t'<salary <<'\\t'<tax<<'\\t'<fsalary<<'\\t'<next; } }

void Delchain(node * head) { node * p;

p=head; //链表头指针赋给p

while (head) //当链表非空时删除结点

{ head=p->next; //将链表下一个结点指针赋给head delete p; //删除链表第一个结点 p=head; //再将头指针赋给p