zoukankan      html  css  js  c++  java
  • 例题作业(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,ghfgh,M,hgghgf
    --------------------------------
    Process exited after 0.2441 seconds with return value 0
    请按任意键继续. . .

    总结:
    这个程序是个普通的定义结构体并初始化,再输出,运行时没用啥错误

    例题9.2

    #include<stdio.h>
    struct Student 
    {
        int num;
        char name[20];
        int score;
    }stu_1,stu_2;
    
    
    
    int main()
    {
        printf("请输入学生1的信息:
    ");
        scanf("%d %s %d",&stu_1.num,stu_1.name,&stu_1.score);
        printf("%d,%s,%d
    ",stu_1.num,stu_1.name,stu_1.score);
        printf("请输入学生2的信息:
    ");
        scanf("%d %s %d",&stu_2.num,stu_2.name,&stu_2.score);
        printf("%d,%s,%d
    ",stu_2.num,stu_2.name,stu_2.score);
        printf("成绩最高同学信息为;
    ");
        
        
        
        if(stu_1.score<stu_2.score)
        printf("%d,%s,%d
    ",stu_2.num,stu_2.name,stu_2.score);
        else
        {
            if(stu_1.score>stu_2.score)
            printf("%d,%s,%d
    ",stu_1.num,stu_1.name,stu_1.score);
            else
            {
                printf("%d,%s,%d
    ",stu_1.num,stu_1.name,stu_1.score);
                printf("%d,%s,%d
    ",stu_2.num,stu_2.name,stu_2.score);
            }
        }
        return 0;
     } 
    请输入学生1的信息:
    123 nhj 456
    123,nhj,456
    请输入学生2的信息:
    456 ggh 789
    456,ggh,789
    成绩最高同学信息为;
    456,ggh,789
    
    --------------------------------
    Process exited after 33.86 seconds with return value 0
    请按任意键继续. . .

    总结:
    定义名字类型为指针类型 ,但 程序不能直接给指针赋值,然后将指针类型换成数组类型;赋值的问题解决了
    但是程序虽然编译正常,但是程序运行后在学生分数后面又多了个0;经过上网查询知道%s和%d连续用时不能
    在中间加逗号,因为程序在读入%s类型时不读到空格或空行不会停止,%d读入整型数据就会停止,所以程序
    运行时输入的姓名和成绩被当作一个字符串读入,然后学生成绩就被初始化为0,所以将逗号换成空格
    程序运行成功

    例题9.3

    #include<stdio.h>
    #include<string.h>
    struct Person
    {
        char name[20];
        int count;
        
    }leader[3]={{"wang",0},{"xiang",0},{"tao",0}}; 
    int main()
    {
        char xuan[20];
        int i,m;
        for(i=0;i<5;i++)
        {
            printf("请输入姓名:
    ");
            scanf("%s",xuan);
            for(m=0;m<3;m++)
            {
                if(strcmp(xuan,leader[m].name)==0)
                leader[m].count++;
                
                
           }
        }
    printf("投票的结果为:
    ");
    for(i=0;i<3;i++)
    printf("%s:%d
    ",leader[i].name,leader[i].count);
        
    return 0;    
        
    }
    请输入姓名:
    wang
    请输入姓名:
    xiang
    请输入姓名:
    tao
    请输入姓名:
    tao
    请输入姓名:
    xiang
    投票的结果为:
    wang:1
    xiang:2
    tao:2
    
    --------------------------------
    Process exited after 31.64 seconds with return value 0
    请按任意键继续. . .

    总结:
    一开始出现错误,输入候选人姓名,程序输出“无该候选人”两遍,一开始我的程序中
    在if后面加了else{printf("无该候选人");} 想输出没有输入候选人的情况,结果无论输入
    何姓名总会出现“无该候选人” ,但将else注释掉程序正常运行,后来想了想,for循环、
    每循环一次都要执行一遍下面内容,所以我输入正确的姓名会输出两次 “无该候选人”
    输入错误的候选人输出三次 “无该候选人”

    例题9.4

    #include<stdio.h>
    struct Student
    {
        int num;
        char name[20];
        int score;
    }stu[6];
    
    
    int main()
    {
        int  i,m;
        struct Student stu[6]={{123,"fgh",455},{789,"gdfg",456},{147,"bfh",558},{214,"jgj",369},{587,"gdf",568},{0,"",0}};
        for(m=0;m<4;m++)
        {
            for(i=0;i<4-m;i++)
            {
            if(stu[i].score<stu[i+1].score)
            {
                stu[5]=stu[i];
                stu[i]=stu[i+1];
                stu[i+1]=stu[5];
            }
            }
            
        }
        printf("成绩由高到低:
    ");
      for(i=0;i<5;i++)
      //stu[1]=stu[2];
        printf("%d	%s	%d
    ",stu[i].num,stu[i].name,stu[i].score); 
    
        
        return 0;
    }
    成绩由高到低:
    587     gdf     568
    147     bfh     558
    789     gdfg    456
    123     fgh     455
    214     jgj     369
    
    --------------------------------
    Process exited after 0.3383 seconds with return value 0
    请按任意键继续. . .

    总结:
    编写程序时需要用到起泡法,我复习了一遍起泡法后编的程序,开始的时候犯了
    一个非常低级的错误,我定义了结构体数组有6个元素,下面循环体中第六个元素
    竟然用stu[6]表示,导致程序运行时被系统阻止,请教同学才看出这个错误
    感觉这种错误编译时程序发现不了,自己很难检查到,所以一定要细心

    例题9.5

    #include<stdio.h>
    struct Student
    {
        int num;
        char name[20];
        char sex;
        int score;    
    }stu_1={456,"fghh",'M',789};
    int main()
    {
        struct Student *p;
        p=&stu_1;
        printf("学生信息:%d,%s,%c,%d
    ",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
        printf("学生信息:%d,%s,%c,%d
    ",(*p).num,(*p).name,(*p).sex,(*p).score);
        printf("学生信息:%d,%s,%c,%d
    ",p->num,p->name,p->sex,p->score);
        
        return 0;
     } 
    学生信息:456,fghh,M,789
    学生信息:456,fghh,M,789
    学生信息:456,fghh,M,789
    
    --------------------------------
    Process exited after 0.3013 seconds with return value 0
    请按任意键继续. . .

    总结:
    刚写程序还有点蒙,直接用*p输出学生信息,用*p.stu_1,就想输出学生的信息
    输出一堆乱东西,一直感觉不对,但还是看了好一会才看到错误的地方

    例题9.6

    #include<stdio.h>
    struct Student
    {
        int num;
        char name[20];
        char sex;
        int score;    
    }stu[3]={{456,"fghh",'M',789},{258,"gggh",'N',369},{147,"cde",'R',357}};
    int main()
    {
        int i;
        struct Student *p;
        for(i=0;i<3;i++)
        printf("学生信息1:%d	%s	%c	%d
    ",stu[i].num,stu[i].name,stu[i].sex,stu[i].score);
        for(p=stu;p<stu+3;p++)
        printf("学生信息2:%d	%s	%c	%d
    ",(*p).num,(*p).name,(*p).sex,(*p).score);
        for(p=stu;p<stu+3;p++)
        printf("学生信息3:%d	%s	%c	%d
    ",p->num,p->name,p->sex,p->score);
        
        
        return 0;
    }
    学生信息1:456  fghh    M       789
    学生信息1:258  gggh    N       369
    学生信息1:147  cde     R       357
    学生信息2:456  fghh    M       789
    学生信息2:258  gggh    N       369
    学生信息2:147  cde     R       357
    学生信息3:456  fghh    M       789
    学生信息3:258  gggh    N       369
    学生信息3:147  cde     R       357
    
    --------------------------------
    Process exited after 0.2881 seconds with return value 0
    请按任意键继续. . .

    总结:
    这个例题属于基本的定义结构体并初始化,输出变量信息,用三种方法输出,程序没有错误

    例题9.7

    #include<stdio.h>
    int main()
    {
        void input();
        input();
        return 0;
     } 
     
     void input()
     {
         
         int  max(int *p,int y);
         int i,sum,k;
         int aver[5];
         struct Student
         {
         
             int num;
            char name[20];
            int Cscore;
            int Escore;
            int Mscore;
            
          }stu[5];
        printf("请输入学生信息:
    ");
        for(i=0;i<5;i++)
        {
        scanf("%d %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].Cscore,&stu[i].Escore,&stu[i].Mscore);
        printf("%d	%s	%d	%d	%d
    ",stu[i].num,stu[i].name,stu[i].Cscore,stu[i].Escore,stu[i].Mscore);
        printf("该学生各科平均数
    ");
        sum=stu[i].Cscore+stu[i].Escore+stu[i].Mscore;
        printf("%d
    ",sum/3);
        aver[i]=(sum/3);
        }
       for(i=0;i<5;i++)
       printf("%d,",aver[i]);
       printf("
    ");
       k=max(aver,5);
       printf("k=%d
    ",k);
       printf("成绩最高的学生信息为:
    ");
       printf("%d	%s	%d	%d	%d
    ",stu[k].num,stu[k].name,stu[k].Cscore,stu[k].Escore,stu[k].Mscore);
     }
     int  max(int *p,int y)
     {
         int t,f;
         f=(*p);
         for(t=0;t<y;t++)
         {
        if(*(p+t)>f)
         f=*(p+t);
        }
        printf("%d
    ",f);
        for(t=0;t<y;t++)
        {
            if(f==*(p+t))
            break;  
        }
        printf("%d
    ",t);
        return t;
         
     }
     
    请输入学生信息:
    123 jhj 1 2 3
    123     jhj     1       2       3
    该学生各科平均数
    2
    456 njj 4 5 6
    456     njj     4       5       6
    该学生各科平均数
    5
    789 juii 7 8 9
    789     juii    7       8       9
    该学生各科平均数
    8
    147 jkl 1 4 7
    147     jkl     1       4       7
    该学生各科平均数
    4
    258 jhh 2 5 8
    258     jhh     2       5       8
    该学生各科平均数
    5
    2,5,8,4,5,
    8
    2
    k=2
    成绩最高的学生信息为:
    789     juii    7       8       9
    
    --------------------------------
    Process exited after 36.06 seconds with return value 0
    请按任意键继续. . .

    总结:
    这个程序是将三名学生的信息输入,求出个学生的平均数,
    找出最大值 ,并输出成绩最高的信息我为找出成绩最高的
    同学,将所有同学的平均数放到一个数组中,通过for循环
    将最大值赋值给变量f再通过for循环找出最大值是哪个同学
    (即是返回值t)然后在调用函数中输出成绩最高同学信息
    调用函数max时调用函数input的调用函数,即调用函数的调
    用函数 ,编程时将if(f==*(p+t)) 中少写了一个等于号导致
    没输出想要的结果,编译不报错,找出这个错误还是花了不
    少力气的

  • 相关阅读:
    Kali视频学习21-25
    20159315《网络攻防实践》第六周学习总结
    Kali视频学习16-20
    20159315《网络攻防实践》第五周学习总结
    一个PE文件的逆向分析
    一个好玩的CTF题
    对于安卓锁屏中知识点小结
    安卓系统安全措施
    安卓防逆向、防动态分析、渗透测试及加固
    安卓组件漏洞防护注意事项
  • 原文地址:https://www.cnblogs.com/abtious/p/6685431.html
Copyright © 2011-2022 走看看