抄写例题

例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,a.addr); return 0; }

 NO.:10101
name:Li Lin
sex:M
address:123 Beijing Road

--------------------------------
Process exited after 0.02924 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",student1.num,student1.name,student1.score);
    else if(student1.score<student2.score)
    printf("%d   %s   %6.2f",student2.num,student2.name,student2.score);
    else
    {
        printf("%d   %s   %6.2f",student1.num,student1.name,student1.score);
        printf("%d   %s   %6.2f",student2.num,student2.name,student2.score);
    }
    return 0;
}
复制代码

10101 Wang 89
10103 Ling 90
The higher score is:
10103   Ling    90.00
--------------------------------
Process exited after 12.16 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(int 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
Li
Sun
Zhang
Zhabg
Sun
Li
Sun
Zhang
Li

Result
   Li:4
Zhang:2
  Sun:3

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

 

  例9.4

复制代码

复制代码
#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.2451 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 stu_1;
    struct Student *p;
    p=&stu_1;
    stu_1.num=10101;
    strcpy(stu_1.name,"Li Lin");
    stu_1.sex='M';
    stu_1.score=89.5;
    printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",
            stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
    printf("\nNo.:%ld\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.1452 seconds with return value 0
请按任意键继续. . .



总结:我觉得C语言基础不好的话就应该多抄写,慢慢熟悉就好了。
posted @ 2017-04-11 19:21  2016024241李海龙  阅读(173)  评论(0编辑  收藏  举报