| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 776 人关注过本帖
标题:结构体中从键盘输入数据格式的问题
只看楼主 加入收藏
不懂C编程
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2009-10-17
结帖率:100%
收藏
 问题点数:0 回复次数:1 
结构体中从键盘输入数据格式的问题

有四个学生,每个学生的信息包括学号,姓名和成绩。要求调用函数找出成绩最高者的姓名和成绩。
#include"stdio.h"
struct student
{
    int num;
    char name[20];
    float score;
};
int find(struct student *p,int n)
{
    int i;
    int temp;
    float max;
    for(max=p->score,i=1;i<n;i++)
    {
        if((p+i)->score>max)
        {
            max=(p+i)->score;
            temp=i;
        }
    }
    return temp;
}
void main()
{
    struct student stu[4];
    int i;
    float score;
    struct student *p;
    int temp=0;
        printf("请输入4个学生的信息:\n");
    for(i=0;i<4;i++)
        scanf("%d,%s,%f",&stu[i].num,stu[i].name,&stu[i].score);
    temp=find(stu,4);
    p=stu+temp;
    printf("the maximum score:\n");
    printf("no:%d\n name:%s\n score:%4.1f\n",p->num,p->name,p->score);
}
调试没有任何错误和警告,但输入方式是如何的呢/偶输入以下
120,liming,90 121,lipeng,50 122,kuku,64 123,haha,85
但是这样输入格式或方法不对,偶输入以下的:
120,liming,90,121,lipeng,50,122,kuku,64,123,haha,85
也是不对的,请问,正确的输入格式是什么?请写下来.
搜索更多相关主题的帖子: 结构体 输入 格式 数据 键盘 
2009-10-18 18:49
mfh
Rank: 6Rank: 6
等 级:侠之大者
帖 子:179
专家分:411
注 册:2009-5-31
收藏
得分:0 
#include"stdio.h"
struct student
{
    int num;  
    char name[20];
    float score;
};
int find(struct student *p,int n)
{
    int i;
    int temp=0;
    float max;
    for(max=p->score,i=1;i<n;i++)
    {
        if((p+i)->score>max)
        {
            max=(p+i)->score;
            temp=i;
        }
    }
    return temp;
}
void main()
{
    struct student stu[2];
    int i;
    float score;
    struct student *p;
    int temp=0;
        printf("请输入4个学生的信息:\n");
    for(i=0;i<2;i++)
        scanf("%d,%f,%s",&stu[i].num,&stu[i].score,stu[i].name); //同志建议不要这样子给结构体赋值
    temp=find(stu,2);
    p=stu+temp;
    printf("the maximum score:\n");
    printf("no:%d\n name:%s\n score:%4.1f\n",p->num,p->name,p->score);
}
现在可以了!!!!!!!!!!!
有事QQ153612021
2009-10-18 19:25
快速回复:结构体中从键盘输入数据格式的问题
数据加载中...
 
   



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

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