鱼C论坛

 找回密码
 立即注册
查看: 2262|回复: 1

请问为什么输入1或2或其他指定的字符串后不能得到预期的结果呢?谢谢了

[复制链接]
发表于 2019-2-6 22:14:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述
定义结构体,存储学生学号和三门课成绩及平均分,初始化成绩如下{{1,90,80,70},{2,85,75,95},{3,88,84,65}},输入学号,输出平均分,输入max,输出最高分id。

输入描述
输入学号或max

输出描述
输出平均分或最高分id

样例输入
//样例[1]
1

//样例[2]
max

//样例[3]
4

样例输出
//样例[1]
80

//样例[2]
2

//样例[3]
0
#include<stdio.h>
#include<string.h>
struct student
{
        int num;
        int g[3];
        int aver;
}stu[3]={{1,90,80,70},{2,85,75,95},{3,88,84,65}};
int main()
{
        int i,j,max,t;
        char a[10],(*p)[10];
        for(i=0;i<3;i++)
                stu[i].aver=0.0;
        for(i=0;i<3;i++)
                {
                        for(j=0;j<3;j++)                
                        stu[i].aver+=stu[i].g[j];
                        stu[i].aver=stu[i].aver/3.0;
                }
                        if(stu[0].aver>stu[1].aver)
                        {
                                max=stu[0].aver;t=1;
                        }
                        else
                        {
                                max=stu[1].aver;t=2;
                        }        
                        if(stu[2].aver>max)
                        {
                                max=stu[2].aver;t=3;
                        }
                        gets(a);
                        p=&a;
                        if(*p=="1")
                        printf("%d\n",stu[0].aver);
                        else if(*p=="2")
                        printf("%d\n",stu[1].aver);
                        else if(*p=="3")
                        printf("%d\n",stu[2].aver);
                        else if(*p=="max")
                        printf("%d\n",t);
                        else printf("0");                
                        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-7 15:20:14 | 显示全部楼层
先请原谅我爆出口。这狗屎一样的代码,代码不整洁,逻辑不清晰。
本来想在原来的基础上改改,看了10几分钟实在看不下去。

于是按题目写了如下代码(参考):
#include<stdio.h>
#include<string.h>

struct Score
{
        int chinese;
        int math;
        int english;
};

typedef struct Student
{
        int num;
    struct Score score;
    double aver;
} St;


int main()
{
        St stu[3] = {{1,90,80,70}, {2,85,75,95}, {3,88,84,65}};
        int i, id;
        double max = 0;        
        char s[10]={'\0'};

        for(i=0; i<3; i++)
        {
                stu[i].aver = (stu[i].score.chinese + stu[i].score.math + stu[i].score.english) / 3;

                if (stu[i].aver > max) // 此处便可获取最高分ID
                {
                        max = stu[i].aver;
                        id = stu[i].num;
                }
        }
        
        printf("请输入学号/max:");

        gets(s);

        if( strcmp(s,"1")==0 )
        {
                printf("%f\n", stu[0].aver);
        }
        else if( strcmp(s,"2")==0 )
        {
                printf("%f\n", stu[1].aver);
        }
        else if( strcmp(s,"3")==0 )
        {
                printf("%f\n", stu[2].aver);
        }
        else if( strcmp(s,"max")==0 )
        {
                printf("最高分ID是:%d\n", id);
        }
        else 
        {
                printf("0"); 
        }

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 08:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表