第12周作业(第八章)

时间:2022-11-22 09:18:50

课后习题:

1.0

#include<iostream>  

using namespace std;
struct
{
int xuehao;
char* name;
int math_score;

}stu1;

void print()
{
cout<<stu1.xuehao<<" "<<stu1.name<<" "<<stu1.math_score<<endl;

}
void main()
{

stu1.xuehao=111;
stu1.name="小明";
stu1.math_score=98;


print();


}

第12周作业(第八章)



2.0


#include<iostream>  

using namespace std;
struct student
{
int xuehao;
char* name;
int math_score;
int english_score;
int chinese_score;

};
void print(student *ps)
{
cout<<"学号是:"<<ps->xuehao<<" "<<"姓名:"<<ps->name<<" "<<endl;
cout<<"数学:"<<ps->math_score<<" "<<"英语:"<<ps->english_score<<" "<<"语文:"<<ps->chinese_score<<endl;


}



void main()
{
student stu[3]={{111,"小明",95,96,97},{112,"小红",86,87,89},{113,"小坤",88,89,87}};
int i;
for( i=0;i<3;i++)

print(&stu[i]);




}

第12周作业(第八章)

结构指针做形参。

3.0

#include<iostream.h>
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;
}

第12周作业(第八章)

4.0

#include<iostream>  

using namespace std;

struct student

{
int bianhao;
int jiben;
int zhiwu;
char name[15];

};
void main()
{
student stu[6]={{001,1100,1200,"小明"},{002,1200,1300,"小红"},{003,600,800,"小花"},{004,1400,1500,"小李"},{005,1500,1600,"小陈"},{006,1600,1700,"小坤"}};
student temp;
int i;
for( i=1;i<6;i++)
if((stu[0].jiben+stu[0].zhiwu)>=(stu[i].jiben+stu[i].zhiwu))

stu[0]=stu[i];


cout<<"编号"<<" 基本工资"<<" 职务工资"<<" 名字"<<endl;
cout<<stu[0].bianhao<<" "<<stu[0].jiben<<" "<<stu[0].zhiwu<<" "<<stu[0].name<<endl;
}

第12周作业(第八章)

课本:

#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;
op1=en(++i);


}
return 0;

}


第12周作业(第八章)


#include<iostream>   
using namespace std;
struct student
{
int num;
float score;
student *next;

};
student*creat();
void print(student *head);
student*del(student*head,int num);
student *insert(student *head,student*stud);
int n=0;
int main()
{
student*head=creat();
cout<<"新建的链表为:"<<endl;
<<"学号 成绩"<<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;



}

3.0

#include<iostream>   
using namespace std;
struct student
{
int num;
char name[20];
float score;


};
int main()
{
struct stu[3]={{1001,"liu jin",75},{1002,"zhang ming ",88},{1003,"he hua",66}};
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;


}

4.0

#include<iostream>   
using namespace std;
struct student
{
int num;
char name[20];
float score;


};
void main()
{
student stu[3]={{1001,"liu jin",88},{1002,"hu jin",99},{1003,"hua wei",97}};
student*s=stu;
cout<<"num"<<" name"<<" score"<<endl;
for(;s<stu+3;s++)
cout<<s->num<<" "<<s->name<<" "<<s->score<<endl;



}

第12周作业(第八章)