8-1
#include <iostream>
using namespace std;
struct date
{
int year;
int month;
int day;
};
struct
{
int num;
char name[20];
date birthday;
char addr[30];
}student={1001,"HMW",{1992,01,07},"shangtou"};
int main()
{
cout<<student.num<<endl;
cout<<student.name<<endl;
cout<<student.birthday.year<<student.birthday.month<<student.birthday.day<<endl;
cout<<student.addr<<endl;
return 0;
}
8-2
#include <iostream>
using namespace std;
struct date
{
int year;
int month;
int day;
}a={1,2,3};
date b;
int main()
{
b=a;
cout<<b.year<<endl;
cout<<b.month<<endl;
cout<<b.day<<endl;
return 0;
}
8-3
#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<<" "<<stu[k].name<<" "<<stu[k].score<<endl;
return 0;
}
8-4
#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 *p=stu;
cout<<"Num"<<"\tName"<<"\t\tScore"<<endl;
for(;p<stu+3;p++)
cout<<(*p).num<<"\t"<<(*p).name<<"\t\t"<<(*p).score<<endl;
return 0;
}
8-5
#include<iostream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
void print(student *p)
{
cout<<(*p).num<<"\t"<<(*p).name<<"\t\t"<<(*p).score<<endl;
}
int main()
{
student stu[3]={{1001,"Liu jin",75},{1002,"Li Lan",82},{1003,"Ma Kai",80}};
cout<<"Num"<<"\tName"<<"\t\tScore"<<endl;
for(int i=0;i<3;i++)
{
print(&stu[i]);
}
return 0;
}
8-10
#include<iostream>
using namespace std;
struct student
{
int 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--;
}
else
cout<<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=p1->next;
}
else
while((p0->num>p1->num)&&(p1->next!=NULL))
{
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;
}
8-11
#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;
}
8-12
#include<iostream>
using namespace std;
int main()
{
enum en{plus,minus,times}p;
int x,y;
cout<<"请输入两个数:"<<endl;
cin>>x>>y;
p=plus;
while(p<=times)
{
switch(p)
{
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)p;
p=en(++i);
}
return 0;
}
习题1
#include<iostream>
using namespace std;
struct student
{
int num;
char ch[10];
int score1;
int score2;
int score3;
}me={1122,"黄明武",97,97,97};
void print();
int main()
{
print();
return 0;
}
void print()
{
cout<<me.num<<"\t";
cout<<me.ch<<"\t";
cout<<me.score1<<"\t";
cout<<me.score2 <<"\t";
cout<<me.score3<<"\t"<<endl;
}
习题2
#include<iostream>
using namespace std;
struct student
{
char aa[20];
char ch[20];
int mathscore;
int englishiscoer;
int chinesescore;
};
void print(student *b)
{
cout<<"学号"<<"\t\t"<<"姓名"<<"\t"<<"数学成绩"<<"\t"<<"英语成绩"<<"\t"<<"语文成绩"<<endl;
cout<<b->aa<<"\t"<<b->ch <<"\t"<<b->mathscore<<"\t\t"<<b->englishiscoer<<"\t\t"<<b->chinesescore<<endl;
}
int main()
{
student a[1]={"201211632207","黄明武",97,98,97};
print(a);
return 0;
}
习题3
#include<iostream>
using namespace std;
union change
{
int a;
char b;
}asc;
int main ()
{
cout<<"请输入要转换成ASCII码的整数:"<<endl;
cin>>asc.a;
cout<<"对应的ASCII码为:"<<asc.b <<endl;
return 0;
}
习题4
#include <iostream>
using namespace std;
struct worker
{
long num;
char name[20];
double basic_wage;
double job_wage;
};
int main()
{
worker work[10];
for(int i=0;i<10;i++)
{
cout<<"Enter the num:";
cin>>work[i].num;
cin.get();
cout<<"Enter the name:";
cin.get(work[i].name,20);
cin.get();
cout<<"Enter the basic wages:";
cin>>work[i].basic_wage;
cin.get();
cout<<"Enter the job wages:";
cin>>work[i].job_wage;
cin.get();
}
double sum_wage[10];
double min=work[0].basic_wage+work[0].job_wage;
int n=0;
for(int i=1;i<10;i++)
{
sum_wage[i]=work[i].basic_wage+work[i].job_wage;
if(sum_wage[i]<min)
{
min=sum_wage[i];
n=i;
}
}
cout<<"The minimum wage workers:"<<endl;
cout<<"Name:"<<work[n].name<<endl;
cout<<"Num:"<<work[n].num<<endl;
cout<<"Basic wages:"<<work[n].basic_wage<<endl;
cout<<"Job wages:"<<work[n].job_wage<<endl;
return 0;
}
5
#include <iostream>
using namespace std;
struct time
{
int year;
int month;
int day;
};
int main()
{
cout<<"请输入一个时间(年,月,日):";
time a_time;
cin>>a_time.year>>a_time.month>>a_time.day;
int day_year;
if((a_time.year%4==0&&a_time.year%100!=0)||a_time.year%400==0)
{
switch(a_time.month)
{
case 1:day_year=a_time.day;break;
case 2:day_year=31+a_time.day;break;
case 3:day_year=60+a_time.day;break;
case 4:day_year=91+a_time.day;break;
case 5:day_year=121+a_time.day;break;
case 6:day_year=152+a_time.day;break;
case 7:day_year=182+a_time.day;break;
case 8:day_year=213+a_time.day;break;
case 9:day_year=244+a_time.day;break;
case 10:day_year=274+a_time.day;break;
case 11:day_year=305+a_time.day;break;
case 12:day_year=335+a_time.day;break;
}
}
else
{
switch(a_time.month)
{
case 1:day_year=a_time.day;break;
case 2:day_year=31+a_time.day;break;
case 3:day_year=59+a_time.day;break;
case 4:day_year=90+a_time.day;break;
case 5:day_year=120+a_time.day;break;
case 6:day_year=151+a_time.day;break;
case 7:day_year=181+a_time.day;break;
case 8:day_year=212+a_time.day;break;
case 9:day_year=243+a_time.day;break;
case 10:day_year=273+a_time.day;break;
case 11:day_year=304+a_time.day;break;
case 12:day_year=334+a_time.day;break;
}
}
cout<<"该日是本年中第 "<<day_year<<" 天"<<endl;
return 0;
}
6
#include <iostream>
using namespace std;
struct student
{
long num;
float score;
student *next;
};
student *creat(int &n);
void show(student *head);
student *merger(student *a,student *b);
student *sort(student *a);
int main()
{
student *one,*two;
int n1=0,n2=0;
cout<<"请输入第一个链表:"<<endl;
one=creat(n1);
cout<<"请输入第二个链表:"<<endl;
two=creat(n2);
cout<<"第一个链表:"<<endl;
show(one);
cout<<"第二个链表:"<<endl;
show(two);
cout<<endl;
one=merger(one,two);
one=sort(one);
cout<<"处理后:"<<endl;
show(one);
return 0;
}
student *creat(int &n)
{
student *head,*p1,*p2;
head=NULL;
p1=new(student);
p2=p1;
cout<<"请输入,当学号为0时,停止输入"<<endl;
cout<<"请输入第 "<<n+1<<" 个学生的学号:";
cin>>p1->num;
cout<<"请输入其成绩:";
cin>>p1->score;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=new(student);
cout<<"请输入第 "<<n+1<<" 个学生的学号:";
cin>>p1->num;
cout<<"请输入其成绩:";
cin>>p1->score;
}
delete p1;
p2->next=NULL;
return head;
}
void show(student *head)
{
student *p;
p=head;
if(p==NULL)return;
cout<<"学号\t成绩\t"<<endl;
do
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
student *merger(student *a,student *b)
{
student *p;
p=a;
while(p->next!=NULL)
p=p->next;
if(b==NULL)return a;
p->next=b;
return a;
}
student *sort(student *a)
{
struct student *first; /*排列后有序链的表头指针*/
struct student *tail; /*排列后有序链的表尾指针*/
struct student *p_min; /*保留键值更小的节点的前驱节点的指针*/
struct student *min; /*存储最小节点*/
struct student *p; /*当前比较的节点*/
first=NULL;
while(a!=NULL)
{
for(p=a,min=a;p->next!=NULL;p=p->next)
{
if(p->next->num<min->num)
{
p_min=p;
min=p->next;
}
}
if(first==NULL)
{
first=min;
tail=min;
}
else
{
tail->next=min;
tail=min;
}
if(min==a)
{
a=a->next;
}
else
{
p_min->next=min->next;
}
}
if(first!=NULL)
{
tail->next=NULL;
}
a=first;
return a;
}
10
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Grade
{
int s1,s2,s3;
float aver;
};
struct StudentRec
{
int num;
string name;
Grade s;
};
typedef struct StudentRec STUDENT;
void inputstu(STUDENT stu[],int);
void sort(STUDENT stu[],int);
int main()
{
STUDENT stu[10];
int i;
inputstu(stu,10);
sort(stu,10);
cout<<"按照平均成绩排序后的学生信息如下:"<<endl;
cout<<"学号 姓名 数学 英语 语文"<<endl;
for(i=0;i<10;i++)
{
cout<<stu[i].num<<'\t'<<stu[i].name<<'\t'
<<stu[i].s.s1<<'\t'<<stu[i].s.s2<<'\t'
<<stu[i].s.s3<<'\t'<<'\t'<<stu[i].s.aver<<endl;
}
return 0;
}
void inputstu(STUDENT stu[],int n)
{
cout<<"请输入"<<n
<<"个学生的学号、姓名以及数学、英语、语文三门课程的成绩"<<endl;
cout<<"学号 姓名 数学 英语 语文"<<endl;
for(int i=0;i<n;i++)
{
cin>>stu[i].num>>stu[i].name
>>stu[i].s.s1>>stu[i].s.s2>>stu[i].s.s3;
stu[i].s.aver=(stu[i].s.s1+stu[i].s.s2+stu[i].s.s3)/3;
}
}
void sort(STUDENT stu[],int n)
{
STUDENT newstu;
for(int i=0;i<n-1;i++)
for(int j=0;j<n-1-i;j++)
{
if(stu[j].s.aver>stu[j+1].s.aver)
{
newstu=stu[j];
stu[j]=stu[j+1];
stu[j+1]=newstu;
}
}
}