2017实验9 结构程序设计
2017-12-19 本文已影响0人
林之禾
6-1 计算两个复数之积(15 分)
struct complex multiply(struct complex x, struct complex y){
struct complex z;
z.real = x.real * y.real - x.imag * y.imag;
z.imag = x.real * y.imag + x.imag * y.real;
return z;
}
6-2 按等级统计学生成绩(20 分)
int set_grade( struct student *p, int n ){
int count=0;
for(int i=0;i<n;i++,p++){
if(p->score<=100&&p->score>=85){
p->grade='A';
}else if(p->score<=84&&p->score>=70){
p->grade='B';
}else if(p->score<=69&&p->score>=60){
p->grade='C';
}else if(p->score<60){
p->grade='D';
count++;
}
}
return count;
}
7-1 计算职工工资(15 分)
#include<stdio.h>
struct staff{
char name[11];
double jbgz;
double fdgz;
double zc;
double sfgz;
};
void input(struct staff s[],int n);
void output(struct staff s[],int n);
int main(void){
int n;
scanf("%d",&n);
struct staff s[n];
input(s,n);
output(s,n);
return 0;
}
void input(struct staff s[],int n){
for(int i=0;i<n;i++){
scanf("%s %lf %lf %lf",s[i].name,&s[i].jbgz,&s[i].fdgz,&s[i].zc);
}
}
void output(struct staff s[],int n){
for(int i=0;i<n;i++){
s[i].sfgz=s[i].jbgz+s[i].fdgz-s[i].zc;
printf("%s %.2lf\n",s[i].name,s[i].sfgz);
}
}
7-2 时间换算(15 分)
#include<stdio.h>
struct time{
int hh;
int mm;
int ss;
int s_ss;
};
int main(void){
struct time t;
scanf("%d:%d:%d",&t.hh,&t.mm,&t.ss);
scanf("%d",&t.s_ss);
if(t.ss+t.s_ss<60){
t.ss+=t.s_ss;
}else if(t.ss+t.s_ss>=60){
t.ss=t.ss+t.s_ss-60;
if(t.mm==59){
t.mm=0;
if(t.hh==23){
t.hh=0;
}else{
t.hh+=1;
}
}else{
t.mm+=1;
}
}
printf("%02d:%02d:%02d",t.hh,t.mm,t.ss);
return 0;
}
7-3 计算平均成绩(15 分)
#include<stdio.h>
struct students{
char xh[6];
char name[11];
int score;
};
void input(struct students s[],int n);
void output(struct students s[],int n);
int main(void){
int n;
scanf("%d",&n);
struct students s[n];
input(s,n);
output(s,n);
return 0;
}
void input(struct students s[],int n){
for(int i=0;i<n;i++){
scanf("%s %s %d",s[i].xh,s[i].name,&s[i].score);
}
}
void output(struct students s[],int n){
double avg=0,sum=0;
for(int i=0;i<n;i++){
sum+=s[i].score;
}
avg=sum/n;
printf("%.2lf\n",avg);
for(int i=0;i<n;i++){
if(s[i].score<avg){
printf("%s %s\n",s[i].name,s[i].xh);
}
}
}
7-4 查找书籍(20 分)
#include<stdio.h>
#include<string.h>
struct book{
char book_name[31];
double cost;
};
int main(void){
int n;
scanf("%d",&n);
struct book b[9];
int max_index=0,min_index=0;
for(int i=0;i<n;i++){
getchar();
gets(b[i].book_name);
scanf("%lf",&b[i].cost);
if(b[max_index].cost<b[i].cost){
max_index=i;
}
if(b[min_index].cost>b[i].cost){
min_index=i;
}
}
printf("%.2f, %s\n",b[max_index].cost,b[max_index].book_name);
printf("%.2f, %s",b[min_index].cost,b[min_index].book_name);
return 0;
}
7-5 有理数比较(10 分)
#include<stdio.h>
#include<math.h>
struct xl{
double x;
double y;
};
int main(void){
struct xl v1;
struct xl v2;
double x,y;
scanf("%lf/%lf",&v1.x,&v1.y);
scanf("%lf/%lf",&v2.x,&v2.y);
if(v1.x/v1.y>v2.x/v2.y){
printf("%.0lf/%.0lf > %.0lf/%.0lf",v1.x,v1.y,v2.x,v2.y);
}else if(v1.x/v1.y==v2.x/v2.y){
printf("%.0lf/%.0lf = %.0lf/%.0lf",v1.x,v1.y,v2.x,v2.y);
} else if(v1.x/v1.y<v2.x/v2.y){
printf("%.0lf/%.0lf < %.0lf/%.0lf",v1.x,v1.y,v2.x,v2.y);
}
return 0;
}
7-6 平面向量加法(15 分)
#include<stdio.h>
#include<math.h>
struct xl{
double x;
double y;
};
int main(void){
struct xl v1;
struct xl v2;
double x,y;
scanf("%lf%lf",&v1.x,&v1.y);
scanf("%lf%lf",&v2.x,&v2.y);
x=v1.x+v2.x;
y=v1.y+v2.y;
if(x > -0.05 && x < 0.05) {
x = 0.0;
}
if(y > -0.05 && y < 0.05) {
y = 0.0;
}
printf("(%.1lf, %.1lf)",x,y);
return 0;
}