| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 374 人关注过本帖
标题:在子函数中输出的结果都不对,且有的直击跳过操作
只看楼主 加入收藏
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:2 
在子函数中输出的结果都不对,且有的直击跳过操作
#include <stdio.h>
#include <stdlib.h>
    struct Student{
        int ID;
        int class;
        char name[10];
        float score[3];
        float sum;
    }stu[3];


    void print(void)
        {
            printf("1.input\n");
            printf("2.delete\n");
            printf("3.select\n");
            printf("4.order\n");
            printf("5.output\n");
            printf("6.quit\n");
            printf("please input your option\n");
        }

     void add(struct Student stu[],int j)
    {
        int i;
        for(i=0;i<j+1;i++)
        {
            stu[i].sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
        }
    }

   void input(struct Student stu[],int j)
    {
        int i,n;
        char ch[10],ch1[10]={"yes"},ch2[10]={"no"};
        scanf("%d%d%s",&stu[j].ID,&stu[j].class,stu[j].name);
        for(i=0;i<3;i++)
            scanf("%f",&stu[j].score[i]);
        printf("continue?");
        scanf("%s",ch);
        if(strcmp(ch,ch1)==0) input(stu,j++);
        if(strcmp(ch,ch2)==0)
        {

            add (stu,j);
              print();
        }

    }



    void delete(struct Student stu[])
    {
        char ch1[10]={"yes"},ch2[10]={"no"},ch3[10];
        int id,i,j;
        scanf("%d",&id);
        for(i=0;i<3;i++)
        {
           if(id!=stu[i].ID)
           {
               printf("%d,%d,%s,",stu[i].ID,stu[i].class,stu[i].name);
               for(j=0;j<3;j++)
               {
                printf("%.1,",stu[i].score[j]);  //出现bug 从这里开始输出错误
               }
               printf("%.1",stu[i].sum);
           }
        }
         printf("continue?");
         scanf("%s",ch3);
        if(strcmp(ch3,ch1)==0) delete(stu);
        if(strcmp(ch3,ch2)==0) print();


    }

      void select(struct Student stu[])
    {
        char ch[10],ch1[10]={"yes"},ch2[10]={"no"},ch3[10];
        int id,cla,i,j,flag=0;
        scanf("%d",&id);
        for(i=0;i<3;i++)
        {
           if(id==stu[i].ID)    //根本就没有输出
           {
               printf("%d,%d,%s,",stu[i].ID,stu[i].class,stu[i].name);
               for(j=0;j<3;j++)
                   {
                     printf("%.1",stu[i].score[j]);
                   }
               printf("%.1",stu[i].sum);
               flag++;
           }
        }
        if(flag!=0) printf("there is no eligible student");
        printf("continue?");
        scanf("%s",ch);
        if(strcmp(ch,ch1)==0)
        {
            scanf("%d",&cla);
             for(i=0;i<3;i++)
         {
           if(cla==stu[i].class)    //根本就没有输出
           {
               printf("%d,%d,%s,",stu[i].ID,stu[i].class,stu[i].name);
               for(j=0;j<3;j++)
                   {
                     printf("%.1",stu[i].score[j]);
                   }
               printf("%.1",stu[i].sum);

           }
         }
         printf("continue?");
       scanf("%s",ch3);
       if(strcmp(ch3,ch2)==0) print();
        }
        if(strcmp(ch,ch2)==0) print();
    }

      void order(struct Student stu[])
    {
        int i,j;
        struct Student temp;

        for(i=0;i<2;i++)        // 压根就没有输出
            for(j=0;j<2-i;j++)
            {
                if(stu[j].class>stu[j+1].class)
                  {
                    temp=stu[j];stu[j]=stu[j+1];stu[j+1]=temp;
                  }

                if(stu[j].class==stu[j+1].class)
                  {
                     if(stu[j].sum>stu[j+1].sum)
                         {
                            temp=stu[j];stu[j]=stu[j+1];stu[j+1]=temp;
                         }
                  }
            }
print();

}

      void output(struct Student stu[])
      {

        int i,j;
         for(i=0;i<3;i++)
        {
           printf("%d,%d,%s,",stu[i].ID,stu[i].class,stu[i].name);
           for(j=0;j<3;j++)
           {
               printf("%.1",stu[i].score[j]);
           }
           printf("%.1\n",stu[i].sum);   //输出还是错误的
        }
        print();
      }

      void match(int n)
        {
        switch(n)
          {
              case 1:input(stu,0); break;
              case 2:delete(stu);break;
              case 3:select(stu);break;
              case 4:order(stu);break;
              case 5:output(stu);break;
              case 6:break;
          }
        }

    int main()
        {
            int a[6];
            print();
            scanf("%d",&a[0]);
           if(a[0]!=6)
             {
                 match(a[0]);
                 scanf("%d",&a[1]);
                 if(a[1]!=6)
                      {match(a[1]);
                       scanf("%d",&a[2]);
                       if(a[2]!=6)
                           {
                              match(a[2]);
                              scanf("%d",&a[3]);
                                if(a[3]!=6)
                                   {
                                     match(a[3]);
                                     scanf("%d",&a[4]);
                                       if(a[4]!=6)
                                          {
                                            match(a[4]);
                                            scanf("%d",&a[5]);
                                             if(a[5]!=6)
                                             {
                                               match(a[5]);
                                             }
                                         }
                                   }
                             }

                         }
             }
            return 0;
      }


图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册



[此贴子已经被作者于2015-11-9 22:35编辑过]

2015-11-09 22:30
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
就是2,3,4,5的输出都是断断续续的。要么全部跳过操作,要么不输出成绩与总和。
是逻辑哪错了吗?求解,求助!
2015-11-09 22:45
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:14 
错误太多了啊  

vs 下根本就无法编译通过  就别说运行了  

DO IT YOURSELF !
2015-11-10 08:57
快速回复:在子函数中输出的结果都不对,且有的直击跳过操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056870 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved