例题抄写作业

例9.1 把一个学生的信息放在一个结构体变量中,然后输出这个学生的信息

#include<stdio.h>
int main()
{
    struct stu
    {
        long int num;
        char name[20];
        char sex[3];
        char addr[20];
    }a={1010,"张鑫家","男","qqq"};
    printf("No:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr);
 }

运行结果:

No:1010
name:张鑫家
sex:8
address:qqq

--------------------------------
Process exited after 0.1287 seconds with return value 0
请按任意键继续. . .

没有出现问题

例9.2 输入两个学生的学号,姓名和成绩,输出成绩较高的学生的学号,姓名和成绩。

#include <stdio.h>
int 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  %6.2f\n", student1.num, student1.name, student1.score);
    else if (student1.score < student2.score)
        printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
    else
    {
        printf("%d  %s  %6.2f\n", student1.num, student1.name, student1.score);
        printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
    }
}

运行结果:

01 wang 89
02 ling 64
The higher score is:
1 wang 89.00

--------------------------------
Process exited after 34.9 seconds with return value 0
请按任意键继续. . .

没有出现问题

例9.3 有三个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入备选人的名字,然后输出各人的得票结果

#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);
}

运行结果:

li
zhang
sun
zhang
zhang
li
sun
li
li
li

Result:
li:5
zhang:3
sun:2

--------------------------------
Process exited after 16.26 seconds with return value 0
请按任意键继续. . .

没有出现问题

例9.4 有n个学生的信息,要求按照成绩的高低顺序输出各学生的信息

#include <stdio.h>
struct student
{
int num;
char name[20];
float score;
};
int main()
{
struct student stu[5]={{ 10101,"zhang",78},{10103,"wang",98.5},{10106,"li",86},{ 10108,"ling",73.5},{ 10110,"sun",100}};
struct student temp;
const int n=5;
int i,j,k;
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;
}
for(i=0;i<n;i++)
printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
}

运行结果:

The order is:
10110 sun 100.00
10103 wang 98.50
10106 li 86.00
10101 zhang 78.00
10108 ling 73.50

--------------------------------
Process exited after 0.2378 seconds with return value 0
请按任意键继续. . .

.

没有出现问题

例9.5 通过指向结构体变量的指针变量输出结构体变量中成员的信息

#include <stdio.h>
#include <string.h>
int main()
{
    struct student
    {
        long num;
        char name[20];
        char sex;
        float score;
    };
    struct student stu1;
    struct student *p;
    p=&stu1;
    stu1.num=10101;
    strcpy(stu1.name, "Li Lin");
    stu1.sex=‘M‘;
    stu1.score=89.5;
    printf("No.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score);
    printf("\nNo.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
    return 0;
}

输出结果:

No.:10101
name:Li Lin
sex:M
score: 89.5

No.:10101
name:Li Lin
sex:M
score: 89.5

--------------------------------
Process exited after 0.1261 seconds with return value 0
请按任意键继续. . .

没有出现问题

例9.6 有三个学生的信息,放在结构体数组中,要求输出全部学生的信息

#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;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 0.1379 seconds with return value 0
请按任意键继续. . .

出现一些输入上的小问题,发现后改正。

例9.7 有n个结构体变量,内含学生学号,姓名和三门课程的成绩,要求输出平均成绩最高的学生的信息

#include<stdio.h>
#define N 3
struct Student
{
    int num;
    char name[20];
    float score[3];
    float aver;
};
int main()
{void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stu);
struct Student stu[N],*p=stu;
input(p);
print(max(p));
return 0;
}
void input(struct Student stu[])
{int i;
printf("??ê?è??÷?§éúμ?D??¢£o?§o?£?D???£?èy????3é?¨£o\n");
for(i=0;i<N;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=0;i<N;i++)
if(stu[i].aver>stu[m].aver)m=i;
return stu[m];
}
void print(struct Student stud)
{printf("\n3é?¨×?o?μ??§éúê?£o\n");
printf("?§o?£o%d\D???£o%s\nèy????3é?¨£o%5.1f,%5.1f,%5.1f\n???ù3é?¨£o%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}

运行结果:

请输入各学生的信息:学号,姓名,三门课成绩:
01 li 76 87 76
02 zhang 98 87 78
03 liu 65 87 98

成绩最好的学生是:
学号:2姓名:zhang
三门课成绩: 98.0, 87.0, 78.0
平均成绩: 87.67

--------------------------------
Process exited after 31.16 seconds with return value 0
请按任意键继续. . .

这道题抄写的时候因为这道题太长加上太长时间抄写出现了许多小错误,发现之后一点点改正

上传截图:

总结:例题抄写虽然累,但还是学到了许多东西,程序的输入也更加熟练,输入的速度也提高了一些。

时间: 2024-12-04 10:20:58

例题抄写作业的相关文章

抄写作业

9.#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;

C++课程第一周小结

本周C++课程共布置了三个作业,分别是: 1.编程求两数的和并输出.要求:从键盘输入两个整数,求出两数的和,并在屏幕输出. 2.编程输出 a.文字:hello,gdufs! b.一个有字母或字符组成的自定义图案. 代码:https://github.com/524868939/Homework 程序分析:本周作业都挺简单,作业1是在课本上有相似例题,作业2.a在老师文档里有示例,作业2.b仅需在作业2.a的基础上变动. 过程:虽然作业简单,但是也遇到问题,第一个问题是完成作业1时,我虽参照了课本

20165334 学习基础与c语言学习心得

学习基础和c语言学习心得 一.技能学习 ??我觉得在男生中,我做饭应该算不错的.我是小学四年级开始学做饭,从刚开始的学煮饭,到后来的蒸馒头,到最后的炒家常菜真的很有成就感.首先,我觉得做任何事情兴趣很重要,因为小时候喜欢美食,所以特别喜欢做东西给别人吃:其次我觉得别人的鼓励也非常重要,它会是你坚持下去的动力.因为受到爸爸对我做饭的鼓励,做晚饭从一开始的西红柿鸡蛋面到后来的三菜一汤,虽然做饭花的时间越来越多,但自己的成就感也越来越强.看着别人吃自己做的东西,自己感到很开心.所以这和娄老师下面这段话

抄写例题作业

例题9.1 #include<stdio.h> int main() { struct Student { long int num; char name[20]; char sex; char addr[20]; }a={10101,"Li Lin",'M',"123 Beijing Road"}; printf("NO.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,

抄写例题作业:

#in clude<stdio.h> #include<stdlib.h> main() { struct student {int num; char name[20]; char sex; int age; float score; char addr[30]; }; }运行结果 -------------------------------- Process exited after 0.01458 seconds with return value 0 请按任意键继续 #i

抄写例题作业1

例9.1: #include<stdio.h> int main() {struct Student //声明结构体类型struct Student { long int num; //以下四行为结构体的成员 char name[20]; char sex; char addr[20]; }w={01,"Yu Jinchi",'M',"Beijing Road"}; //定义结构体变量a并初始化 printf("NO.:%ld\nname:%s

抄写第九章例题

例9.1 #include<stdio.h> #include<stdlib.h> main() {struct Student {long int num; char name[20]; char sex; char addr[20]; }a={10101,"li lin",'M',"123 beijing Road"}; printf("NO.;%ld\nname:%s\nsex:%c\naddress:%s\n",a

例题作业(9.1-9.7)

例题9.1 #include<stdio.h> struct student { int num; char *name; char sex; char *address; }stu_1={521,"ghfgh",'M',"hgghgf"}; int main() {printf("%d,%s,%c,%s",stu_1.num,stu_1.name,stu_1.sex,stu_1.address); return 0; } 521,g

例题作业

1.#include<stdio.h> #include<stdlib.h> main() { struct student {int num; char name[20]; char sex; int age; float score; char addr[30]; }; } 2.#include<stdio.h> #include<stdlib.h> main() { struct Date { int month; int day; int year;