P226页
练习9-1,9-2,9-3
#include<stdio.h>//练习9-1
struct plural{
double real;
double imag;
};
int main(void)
{
struct plural number;
printf("实部,虚部:");
scanf("%.2lf%.2lf",&number.real,&number.imag);
// printf("%lf",number.imag);
// printf("此复数为;%.2lf+%.2lfi",number.real,number.imag);
return 0;
}
#include<stdio.h>//练习9-2
struct birthday{
int year;
int month;
int day;
};
struct student
{
int num;
char name[10];
int math,english,computer;
struct birthday bir;
double average;
};
int main(void)
{
struct student s,max;
int n,i,j;
printf("Enter n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("请输入学号;");
scanf("%d",&s.num);
printf("请输入姓名;");
scanf("%s",s.name);
printf("请输入数学,英语,计算机成绩:");
scanf("%d%d%d",&s.math,&s.english,&s.computer);
printf("请输入出生年份;");
scanf("%d",&s.bir.year);
printf("请输入出生月份:");
scanf("%d",&s.bir.month);
printf("请输入出生日期:");
scanf("%d",&s.bir.day);
s.average=(s.computer+s.english+s.math)/3.0;
if(i==1)//第一个平均分
{
max=s;//第一个默认最大值
} else if(s.average>max.average)//成员比较
{
max=s;//整体赋值
}
}
printf("学号\t姓名\t数学\t英语\t计算机\t平均分\t年—月—日\n");
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\t%2d-%2d-%2d\n",max.num,max.name,max.math,max.english,max.computer,max.average,max.bir.year,max.bir.month,max.bir.day);
return 0;
}
#include<stdio.h>//练习9-3
#define N 50
struct student
{
int num;
char name[10];
int math,english,computer;
double average;
};
int main(void)
{
struct student s[N];
int n,i,j;
printf("Enter n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("请输入学号;");
scanf("%d",&s[i].num);
printf("请输入姓名;");
scanf("%s",s[i].name);
printf("请输入数学,英语,计算机成绩:");
scanf("%d%d%d",&s[i].math,&s[i].english,&s[i].computer);
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
}
printf("学号\t姓名\t数学\t英语\t计算机\t平均分\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",s[i].num,s[i].name,s[i].math,s[i].english,s[i].computer,s[i].average);
}
return 0;
}
P228页
练习9-4,9-5
#include<stdio.h>//练习9-4
struct student{
int num;
char name[10];
int math,english,computer;
};
int main(void)
{
struct student stu[5]={{101,"zhang",86,98,88},{103,"li",65,67,68},{106,"wang",55,54,58},{102,"qian",65,62,64},{109,"sun",86,49,58}};
int i;
printf("学号\t姓名\t数学\t英语\t计算机\n");
for(i=0;i<5;i++)
{
printf("%d\t%s\t%d\t%d\t%d\n",stu[i].num,stu[i].name,stu[i].math,stu[i].english,stu[i].computer);
}
return 0;
}
#include<stdio.h>//练习9-5
struct student
{
int num;
char name[10];
int math,english,computer;
double average;
};
int main(void)
{
struct student s[10];
int i,max=0,min=0;
for(i=0;i<10;i++)
{
printf("请输入学号;");
scanf("%d",&s[i].num);
printf("请输入姓名;");
scanf("%s",s[i].name);
printf("请输入数学,英语,计算机成绩:");
scanf("%d%d%d",&s[i].math,&s[i].english,&s[i].computer);
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
if(s[i].average>s[max].average){
max=i;
} else if(s[i].average<s[min].average){
min=i;
}
}
printf("平均分最高和最低的学生信息为;\n");
printf("学号\t姓名\t数学\t英语\t计算机\t平均分\n");
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",s[max].num,s[max].name,s[max].math,s[max].english,s[max].computer,s[max].average);
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",s[min].num,s[min].name,s[min].math,s[min].english,s[min].computer,s[min].average);
return 0;
}
P232页
练习9-6,9-7
#include<stdio.h>//练习9-6
struct student
{
int num;
char name[10];
int math,english,computer;
double average;
};
int main(void)
{
struct student stu,*p;
p=&stu;
printf("请输入学号;");
scanf("%d",&(*p).num);
printf("请输入姓名;");
scanf("%s",(*p).name);
printf("请输入数学,英语,计算机成绩:");
scanf("%d%d%d",&(*p).math,&(*p).english,&(*p).computer);
(*p).average=((*p).computer+(*p).english+(*p).math)/3.0;
printf("学号\t姓名\t数学\t英语\t计算机\t平均分\n");
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",p->num,p->name,p->math,p->english,p->computer,p->average);
return 0;
}
#include<stdio.h>//练习9-7
#define N 50
struct student
{
int num;
char name[10];
int math,english,computer;
double average;
};
void alter(struct student s[],int n,int num,int math)
{
int i,k;
char ch;
for(i=0;i<n;i++)
{
if(num==s[i].num){
break;
}
}
if(i<n){
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",s[i].num,s[i].name,math,s[i].english,s[i].computer,(s[i].computer+s[i].english+math)/3.0);
printf("请再次确认:y or n\n");
ch=getchar();
if(ch=='y'){
s[i].math=math;
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
puts("修改成功");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%d\t%d\t%.2lf\n",s[i].num,s[i].name,s[i].math,s[i].english,s[i].computer,s[i].average);
}
}
}
else printf("没有找到");
}
int main(void)
{
struct student s[N],x;
int n,i,j,k,num,math;
printf("Enter n(n<50):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("请输入学号;");
scanf("%d",&s[i].num);
printf("请输入姓名;");
scanf("%s",s[i].name);
printf("请输入数学,英语,计算机成绩:");
scanf("%d%d%d",&s[i].math,&s[i].english,&s[i].computer);
s[i].average=(s[i].computer+s[i].english+s[i].math)/3.0;
}
printf("请输入待修改学生的学号:");
scanf("%d",&num);
printf("请输入待修改数学成绩;");
scanf("%d",&math);
getchar();//接受输入数学成绩后面的那个回车,避免影响确认信息
alter(s,n,num,math);//只可以将要修改的成员传过去,不可以将一整个结构体变量传过去
return 0;
}
|