第八章作业Part1

来源:互联网 发布:2015文化产业数据 编辑:程序博客网 时间:2024/05/17 07:50

1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序

例一

#include<iostream>using namespace std;struct date{int month;int day;int year;};struct student{int num;char name[20];struct date birthday;char addr[30];};int main(){student stu1;stu1.num=1001;stu1.birthday.month=8;stu1.birthday.day=20;stu1.birthday.year=1980;cout<<stu1.num<<"  ";cout<<stu1.birthday.month<<"  ";cout<<stu1.birthday.day<<"  ";cout<<stu1.birthday.year<<endl;return 0;}

例二

#include<iostream>using namespace std;int main(){struct{int num;int age;}stu1,stu2;stu1.num=1001;stu1.age=20;stu2=stu1;cout<<stu2.num<<endl;cout<<stu2.age<<endl;return 0;}

例三
#include<iostream>using namespace std;struct student{int num;char name[20];float score;};int main(){student stu[3]={{1001,"liu jin",75},{1002,"li lan",82},{1003,"ma kai",80}};student temp;for(int i=1;i<3;i++)for(int j=0;j<=2-i;j++)if(stu[j].score<stu[j+1].score){temp=stu[j];stu[j]=stu[j+1];stu[j+1]=temp;}cout<<"Num"<<"       Name"<<"   Score"<<endl;for(int k=0;k<3;k++)cout<<stu[k].num<<'\t'<<stu[k].name<<'\t\t'<<stu[k].score<<endl;return 0;}


例四

#include<iostream>using namespace std;struct student{int num;char name[20];float score;};int main(){student stu[3]={{1001,"liu jin",75},{1002,"li lan",82},{1003,"ma kai",80}};student *s=stu;cout<<"Num"<<"\tName"<<"\t\tScore"<<endl;for(;s<stu+3;s++)     //s小于stu首地址右移3位cout<<s->num<<"\t"<<s->name<<"\t\t"<<s->score<<endl;return 0;}

例五

#include<iostream>using namespace std;struct student{int num;char name[20];float score;};void print(student *ps){cout<<ps->num<<" "<<ps->name<<"  "<<ps->score<<endl;}int main(){student stu[3]={{1001,"liu jin",75},{1002,"li lan",82},{1003,"ma kai",80}};for(int i=0;i<3;i++)print(&stu[i]);return 0;}

例六

#include<iostream>using namespace std;struct student{long num;float score;student *next;};int n=0;student *creat()//建立链表{student *head,*p1,*p2;head=NULL;p1=new(student);//开辟动态内存存储空间存放新结点p2=p1;cout<<"请输入学生学号和成绩,当学号为0时,停止输入"<<endl;cin>>p1->num>>p1->score;while(p1->num!=0){n++;if(n==1)head=p1;//第一个结点赋给头结点else{p2->next=p1;//往下链接新建立的结点p2=p1;}p1=new(student);//继续开辟动态内存存储空间存放新结点cin>>p1->num>>p1->score;}delete p1;//停止建立新结点p2->next=NULL;return head;}void print(student *head)//输出链表{student *p;p=head;if(p==NULL)return;do{cout<<p->num<<"    "<<p->score<<endl;p=p->next;}while(p!=NULL);}student *del(student *head,int num)//删除结点{student *p1,*p2;if(head==NULL)//如果原先链表为空{cout<<"list NULL"<<endl;return head;}p1=head;while(num!=p1->num && p1->next!=NULL)//找不到相应的结点就往下继续搜索{p2=p1;p1=p1->next;}if(num==p1->num)//找到结点{if(p1==head)//结点是第一位head=p1->next;else//结点不是第一位p2->next=p1->next;cout<<"delete: "<<num<<endl;n--;}elsecout<<num<<"not been found"<<endl;return head;}student *insert(student *head,student *stud)//增加结点{student *p0,*p1,*p2;p1=head;p0=stud;if(head==NULL)//若原本的链表为空{head=p0;p0->next=NULL;}elsewhile((p0->num>p1->num)&&(p1->next!=NULL))//当p0所指向的数字大于p1所指向的数字,继续往下搜索{p2=p1;p1=p1->next;}if(p0->num<=p1->num)//找到插入点{if(head==p1)//如果插入点是首位head=p0;else//插入点在中间p2->next=p0;p0->next=p1;}else//插入点在末尾{p1->next=p0;p0->next=NULL;}n++;return head;}int main(){student *head=creat();cout<<"新建的链表为:"<<endl<<"学号\t成绩"<<endl;print(head);int num;cout<<"请输入要删除的学号:";cin>>num;head=del(head,num);cout<<"目前的链表为: "<<endl;print(head);student *pt=new(student);cout<<"请输入要插入学生的学号和成绩: ";cin>>pt->num>>pt->score;head=insert(head,pt);cout<<"目前的链表为:"<<endl;print(head);return 0;}

例七

/***********使用共用体将一个整数转换成为对应的ASCII码字符*****************/#include<iostream>using namespace std;union pw{int i;char ch[2];};int main(){cout<<"请输入一个整数,若大于127则退出"<<endl;pw password;while(1){cin>>password.i;if(password.i>127) break;cout<<password.i<<"对应的字符为:"<<password.ch<<endl;}return 0;}

例八
#include<iostream>using namespace std;int main(){enum en{plus,minus,times}op1;int x,y;cout<<"请输入两个数";cin>>x>>y;op1=plus;while(op1<=times){switch(op1){case plus:cout<<x<<"+"<<y<<"="<<x+y<<endl;break;case minus:cout<<x<<"-"<<y<<"="<<x-y<<endl;break;case times:cout<<x<<"*"<<y<<"="<<x*y<<endl;break;}int i=(int)op1;//强制类型转换为int型,因为枚举类型变量不能自增op1=en(++i);}return 0;}


0 0
原创粉丝点击