首页 > 代码库 > 第四次作业

第四次作业

#include <stdio.h>
int main()
{
    struct Student 
    {
         int num;
        char name[20];
        char sex;
        char addr[20];
    } a = { 1, "这种重中之重", M, "123 Beijng Road" };
    printf("NO.:%1d\nname:%s\nsex:%c\naddress:%s\n", a.num ,a.name,a.sex,a.addr);
    return 0;
}


NO.:1
name:这种重中之重
sex:M
address:123 Beijng Road

进程退出返回值 0 (0x0)   运行时间 : 0.750 秒
按任意键继续...

 

 

#include<stdio.h>
main()
{struct Student
{int num;
char name[20];
float score;
}student1,student2;
scanf("%d,%s,%f",&student1.num,&student1.name,&student1.score);
scanf("%d,%s,%f",&student2.num,&student2.name,&student2.score);
printf("The higher score is:\n");
if(student1.score>student2.score)
printf("%d,%s,%f",student1.num,student1.name,student1.score);
else if(student1.score<student2.score)
printf("%d,%s,%f",student2.num,student2.name,student2.score);
else
{printf("%d,%s,%f",student1.num,student1.name,student1.score);
printf("%d,%s,%f",student2.num,student2.name,student2.score);
}
return 0;
}

1 w 85

2 p 96
The higher score is:
2  p   96.00
 
--------------------------------
Process exited after 0.2245 seconds with return value 13
请按任意键继续. . .
 
 
 
#include<string.h>
#include<stdio.h>
struct Person
{char name[20];
int count;
 } leader[3]={"li",0,"zhang",0,"Sun",0};
 int main()
 {int i,j;
 char leader_name[20];
 for(i=1;i<=10;i++)
 {scanf("%s",leader_name);
 for(j=0;j<3;j++)
 if(strcmp(leader_name,leader[j].name)==0)leader[j].count++;
 }
 printf("\nResult:\n");
 for(i=0;i<3;i++)
 printf("%5s:%d\n",leader[i].name,leader[i].count);
 return 0;
  }

Li

Zhang
Li
LI
Zhang
Li
Sun
Sun
Li
Li
 
Result:
   li:0
zhang:0
  Sun:2
 
--------------------------------
Process exited after 0.345 seconds with return value 0
请按任意键继续. . .
 
 
 
#include<stdio.h>
#include<stdlib.h>
struct Student//声明结构体类型 struct person
{
    int num;
    char name[20];
    float score;
};
int main()
{
    struct Student stu[5]={{10101,"zhang",78},{10103,"wang",98},{10106,"li",86},{10108,"ling",73},{10110,"sun",100}};//定义结构体数组并初始化
    struct Student temp;
    const int n=5;
    int i,j,k;//定义常变量n
    printf("the order is:\n");
    for(i=0;i<n-1;i++)
    {k=i;
    for(j=i+1;j<n;j++)
    if(stu[j].score>stu[k].score)
    k=j;
    temp=stu[k];stu[k]=stu[i];stu[i]=temp;  //stu[k]和stu[i]元素互换
    }
    for(i=0;i<n;i++)
    printf("%d,%s,%d",stu[i].num,stu[i].name,stu[i].score);
    printf("\n");
    return 0;
}

the order is:

10110,sun,010103,wang,010106,li,010101,zhang,010108,ling,0
 
--------------------------------
Process exited after 3.567seconds with return value 0
请按任意键继续. . .
 
 
#include<stdio.h>
#include<string.h>
main()
{struct Student
{long num;
char name[20];
char sex;
float score;
};
struct Student stu_1;
struct Student *p;
p=&stu_1;//p指向stu—1
stu_1.num=10101;
strcpy(stu_1.name,"li lin");
stu_1.sex=‘M‘;
stu_1.score=89;
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
}

NO.:10101

name:li lin
sex:M
score:0
NO.:10101
name:li lin
sex:M
score:0
 
--------------------------------
Process exited after 2.478 seconds with return value 0
请按任意键继续. . .
 
 
#include <stdio.h>
struct student
{
    int num;
    char name[20];
    char sex;
    int age;
};
struct student stu[3] = { { 10101, "Li Lin", M, 18 }, { 10102, "Zhang Fang", M, 19 }, { 10103, "Wang Min", F, 20 } };
int main()
{
    struct student *p;
    p = stu;
    printf("No.   Name                sex age\n");
    for (; p < stu + 3; p++)
        printf("%5d %-20s%2c%4d\n", p->num, p->name, p->sex, p->age);
}

No.   Name                sex age

10101 Li Lin               M  18
10102 Zhang Fang           M  19
10103 Wang Min             F  20
 
--------------------------------
Process exited after 2.256 seconds with return value 4206752
请按任意键继续. . .
#include<stdio.h>
struct student
{
    int num;
    char name[20];
    float score[3];
    float aver;
};
void input(struct student stu[])
{
    int i;
    printf("请输入各学生的信息:学号,姓名,三门课成绩:\n");
    for (i = 0; i < 3; i++){
        scanf("%d%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
        stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
    }
}
struct student max(struct student stu[])
{
    int i, m = 0;
    for (i = 1; i<3; i++)
        if (stu[i].aver>stu[m].aver) m = i;
    return stu[m];
}
void print(struct student stud)
{
    printf("\n成绩最高的学生是:\n");
    printf("学号:%d\n姓名: %s\n三门课成绩: %5.1f ,%5.1f,%5.1f\n平均成绩:%5.1f\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver);
}
int main()
{
    //void input (struct student stu[]);
    //struct student max(struct student stu[]);
    //void print(struct student stu);
    struct student stu[3], *p = stu;
    input(p);
    print(max(p));
    return 0;
}

请输入各学生的信息:学号,姓名,三门课成绩:
60,周天豪,60,60,60

技术分享

第四次作业