标题:曦瓜 15:26:55 就是想做一个录入3名同学的成绩的登记程序 ,然后等级评价这一 ...
只看楼主
西瓜瓤
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2022-5-28
结帖率:14.29%
已结贴  问题点数:10 回复次数:2 
曦瓜 15:26:55 就是想做一个录入3名同学的成绩的登记程序 ,然后等级评价这一块始终无法和班级姓名学号一样输出
#include "stdio.h"
#include "stdlib.h"
struct courses
{
    int programming;
    int database;
    int network;
    double score;
};
struct student
{
    int num;       //学号
    char name[10]; //姓名
    int sum;       //总分
    char sumlevel;
    struct courses sc[3];
};
void sumScore(struct student students[])
{
    int i;
    for (i = 0; i < 3; i++)
       {
         students[i].sum=students[i].sc[0].programming+students[i].sc[1].network+students[i].sc[2].database;
       }
}
void sumLevel(struct student students[])
{
   int i;
   char sumlevel;
   for (i = 0; i < 3; i++)
             {
             sumlevel=(int)( students[i].sum/50);
             }
         scanf("%lf",& students[i].sum);
         if ( students[i].sum>300|| students[i].sum<0)
            {
           printf("成绩输入错误!\n");
            }
        else
            {
             sumlevel=(int)( students[i].sum/50);
             switch(sumlevel)
                   {
                    case 5: printf("优\n");break;
                    case 4: printf("优\n");break;
                    case 3: printf("良\n");break;
                    case 2: printf("中\n");break;
                    case 1: printf("差\n");break;
                    default: printf("劣\n");break;
                   }
            }
}
int main()
{
    struct student students[5];//3名同学
    int i;
    for (i = 0; i < 3; i++)
       {
            printf("num:");
            scanf("%d",&students[i].num);
            printf("name:");
            scanf("%s",students[i].name);
            printf("programming score:");
            scanf("%d",&students[i].sc[0].programming);
            printf("network score:");
            scanf("%d",&students[i].sc[1].network);
            printf("database score:");
            scanf("%d",&students[i].sc[2].database);
        }
    sumScore(students);
    sumLevel(students);
    printf("num\tname\tsum\n");
    for (i = 0; i < 3; i++)
       {
        printf("%d\t%s\t%d\t%s\n",students[i].num,students[i].name,students[i].sum,students[i].sumlevel);
       }
      
    return 0;
}
搜索更多相关主题的帖子: struct int printf scanf sum 
2022-05-29 16:56
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
为什么不贴题目要求?何况你自己还知道代码是错误的,那怎么从错误的代码推导出正确的要求?

             sumlevel=(int)( students[i].sum/50);
             switch(sumlevel)
                   {
                    case 5: printf("优\n");break;
                    case 4: printf("优\n");break;
                    case 3: printf("良\n");break;
                    case 2: printf("中\n");break;
                    case 1: printf("差\n");break;
                    default: printf("劣\n");break;
                   }
我完全看不懂(比如三科全100分,那按你的代码就应该是“劣”了?),就将它改掉了。

程序代码:
#include <stdio.h>

struct student
{
    int num;       // 学号
    char name[10]; // 姓名
    int sc_programming;
    int sc_database;
    int sc_network;
};

int main( void )
{
    struct student students[3]; // 3名同学

    const size_t n = sizeof(students)/sizeof(*students);
    for( size_t i=0; i!=n; ++i )
    {
        printf( "num: " );
        scanf( "%d", &students[i].num );
        printf( "name: " );
        scanf( "%s", students[i].name );
        printf( "programming score: " );
        scanf( "%d", &students[i].sc_programming );
        printf( "network score: " );
        scanf( "%d", &students[i].sc_network );
        printf( "database score: ");
        scanf( "%d", &students[i].sc_database );
    }

    printf( "num\tname\tsum\n" );
    for( size_t i=0; i!=n; ++i )
    {
        if( students[i].sc_programming<0 || students[i].sc_programming>100
            || students[i].sc_network<0 || students[i].sc_network>100
            || students[i].sc_database<0 || students[i].sc_database>100 )
        {
            puts( "成绩输入错误!");
        }
        else
        {
            int sum = students[i].sc_programming + students[i].sc_network + students[i].sc_database;
            const char* level;
            if( sum >= 3*90 ) level = "";
            else if( sum >= 3*80 ) level = "";
            else if( sum >= 3*70 ) level = "";
            else if( sum >= 3*60 ) level = "";
            else level = "";
            printf( "%d\t%s\t%d\t%s\n", students[i].num, students[i].name, sum, level );
        }
    }
}
2022-05-30 08:48
do8do8do8
Rank: 10Rank: 10Rank: 10
来 自:沙滩
等 级:贵宾
威 望:17
帖 子:366
专家分:1845
注 册:2010-7-2
得分:5 
经过分析后总结如下:
阁下数据结构冗余,过程控制混乱
请根据在下的小小分析,调整代码

结构化编程自顶向下     数据+过程控制
现(分析阁下的代码)
一、数据
   struct courses   //课程信息
  {
    int programming;//程序设计课程
    int database;   //数据库课程
    int network;    //网络课程
    double score;   //分数?
};
struct student    //学生信息
{
    int num;       //学号
    char name[10]; //姓名
    int sum;       //总分
    char sumlevel;  //评级?
    struct courses sc[3];//课程信息 结构体,其实不必要用数组,直接struct courses sc;即可
};
二、过程控制
int main()
{  
    ........
    sumScore(students);//把学生3门课程的成绩求和,并赋值给学生信息里的总分sum,
    sumLevel(students);//这个函数就有点难分析,现以下做详细分析,请往下移动目光
    .......
    return 0;
}
三、分析 sumLevel(students)函数
void sumLevel(struct student students[])
{
   int i;
   char sumlevel;
   for (i = 0; i < 3; i++) //表面一看是循环三次求值都赋值给局部变量sumlevel,可是为何要循环3次,而且3次都要赋值给同一个变量,不如干脆点,计算最后一个不就行了?不解之谜1
             {
             sumlevel=(int)( students[i].sum/50);
             }
         scanf("%lf",& students[i].sum);   //此时  i应该为3了,那么students[3]是没有学生信息的,难道这只是为了 保存 输入的值?不解之谜2
         if ( students[i].sum>300|| students[i].sum<0)  //判断输入的值是否在(0--300)之内
            {
           printf("成绩输入错误!\n");
            }
        else
            {
             sumlevel=(int)( students[i].sum/50);  //这里是将students[3]再除以50,似乎students[3]一直都在用
             switch(sumlevel)                     //这个就简单了,switch选择,以下忽略
                   {
                    case 5: printf("优\n");break;
                    case 4: printf("优\n");break;
                    case 3: printf("良\n");break;
                    case 2: printf("中\n");break;
                    case 1: printf("差\n");break;
                    default: printf("劣\n");break;
                   }
            }
}

学C语言从底层开始,学编程从问题开始,一日学会C!!!
2022-05-31 09:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509205-1-1.html




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

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