| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 829 人关注过本帖
标题:莫名的错误,求指点!
只看楼主 加入收藏
staythink
Rank: 2
等 级:论坛游民
帖 子:42
专家分:50
注 册:2010-7-26
收藏
 问题点数:0 回复次数:10 
莫名的错误,求指点!
程序代码:
#include<stdio.h>
#define ARR_SIZE 40
void FindMax(int *maxscore,char *maxnum,int score[],char num[],int n);
void main()
{
    int n,score[ARR_SIZE],i,maxscore;
    char num[ARR_SIZE],maxnum;
    printf("please input the total number:\n");
    scanf("%d",&n);
    printf("please input the number and score:\n");
    for(i=0;i<n;i++)
    {
        scanf("%s %d",num,score);
    }
    FindMax(&maxscore,&maxnum,score,num,n);
    printf("该班上分数最高的学生学号为:%s\n",maxnum);
    printf("该班上分数最高的学生成绩为:%d\n",maxscore);

}

void FindMax(int *maxscore,char *maxnum,int score[],char num[],int n)
{
    int i;
    *maxscore=score[0];
    *maxnum=num[0];
    for(i=1;i<n;i++)
    {
        if(score[i]>*maxscore)
        {
            *maxscore=score[i];
            *maxnum=num[i];
        }
    }
}
这个代码主要是想实现这个功能:从键盘输入一个班学生一门课的成绩,用函数编程实现打印最高分及其学号。
输入:
E08620207 88
E08620208 90
E08620209 77
搜索更多相关主题的帖子: color 
2010-07-29 16:57
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
收藏
得分:0 
~\(≧▽≦)/~,你应该用链表。。。

看错,你用了数组啊,
你的scanf貌似有问题耶。

[ 本帖最后由 Rexfield 于 2010-7-29 19:44 编辑 ]

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
2010-07-29 19:43
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
收藏
得分:0 
呼,搞定了。真累。。。。

#include <stdio.h>
#include <string.h>
#define ARR_SIZE 40

void FindMax(int *maxscore,char *maxnum,int score[],char *num[],int n);

int main(int argc,char *argv[])
{
    int n,score[ARR_SIZE],i,*maxscore=(int*)malloc(4);
    char *num[ARR_SIZE],*maxnum=(char*)malloc(20);
    memset(maxnum,'\0',20);
    printf("please input the total number:\n");
    scanf("%d",&n);
    printf("please input the number and score:\n");
    for(i=0;i<n;i++)
    {
        num[i]=(char*)malloc(20);
        scanf("%s %d",num[i],&score[i]);

    }
    FindMax(maxscore,maxnum,score,num,n);
    printf("该班上分数最高的学生学号为:%s\n",maxnum);
    printf("该班上分数最高的学生成绩为:%d\n",*maxscore);
    return 0;
}

void FindMax(int *maxscore,char *maxnum,int score[],char *num[],int n)
{
    int i;
    *maxscore=score[0];
    strcpy(maxnum,num[0]);
    for(i=0;i<n;i++)
    {
        //printf("%d/%d:%s,%d\n",i,n,num[i],score[i]);
        if(score[i]>*maxscore)
        {
            *maxscore=score[i];
            strcpy(maxnum,num[i]);
        }
    }
}


[ 本帖最后由 Rexfield 于 2010-7-29 20:42 编辑 ]

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
2010-07-29 19:56
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
收藏
得分:0 
gcc编译通过,达到预期目的。

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
2010-07-29 20:42
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:0 
    for(i=0;i<n;i++)
    {
        scanf("%s %d",num,score);                //scanf里面的num和score都是数组的地址,你存一个学号在num中没问题,可是你有多个num就不对了,\
                                                   另外score改成&score[i]
    }
改成:
#include<stdio.h>
#include<string.h>

#define ARR_SIZE 40
#define NUMLEN    20

void FindMax(int *maxscore,char **maxnum,int score[],char num[][NUMLEN],int n);

void main()
{
    int n,score[ARR_SIZE],i,maxscore;
    char num[ARR_SIZE][NUMLEN];
    char *maxnum  = NULL;
   

    memset(score , 0 , sizeof(score));
    memset(num , 0 , sizeof(num));
   
    printf("please input the total number:\n");
    scanf("%d",&n);
   
    for(i = 0 ; i < n ; i++)
    {
        printf("please NO.%d input the number and score:\n",i+1);
        scanf("%s %d",num[i],&score[i]);
    }
    FindMax(&maxscore,&maxnum,score,num,n);
   
    printf("该班上分数最高的学生学号为:%s\n",maxnum);
    printf("该班上分数最高的学生成绩为:%d\n",maxscore);

}

void FindMax(int *maxscore,char **maxnum,int score[],char num[][NUMLEN],int n)
{
    int i;
    *maxscore = score[0];
    *maxnum = num[0];
    for(i = 1 ; i < n ; i++)
    {
        if(score[i] > *maxscore)
        {
            *maxscore = score[i];
             *maxnum = num[i];
        }
    }

}

2010-07-29 20:43
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:0 
学号必须用二维数组,因为每个同学的学号必然是一个字符串,而字符串要用一位数组存放,
另外有多个学生,所以要二维数组才行。
maxnum用指针是用来指向找到的分数最高的同学的学号的首地址的,
FindMax中用二级指针是因为,如果使用一级指针的话,FIndMax返回后,
主函数中maxnum还是为空,所以采用传地址的方式。
2010-07-29 20:49
mujiaha
Rank: 2
等 级:论坛游民
帖 子:37
专家分:65
注 册:2010-7-27
收藏
得分:0 
char num[ARR_SIZE],这个应该定义为二维数组,用来存放名字。scanf("%s %d",num,score);应该用&score[i];这个函数FindMax有有问题,你这个函数没返回值,除非你吧  printf("该班上分数最高的学生学号为:%s\n",maxnum);
             printf("该班上分数最高的学生成绩为:%d\n",maxscore);这2个语句放到函数FindMax里面去;我现在要回去了,如果明天没高手解答的话,我去弄弄,我也不太会C语言,哈哈。如果用结构体的话就好做多了;这个是我的观点,不对的请指点,别批评啊
2010-07-29 21:01
staythink
Rank: 2
等 级:论坛游民
帖 子:42
专家分:50
注 册:2010-7-26
收藏
得分:0 
回复 3楼 Rexfield
兄台,这个。。。我得仔细看看,malloc()我是第一次看过。。。刚学C语言不久

be a progammer,instead of a coder~
2010-07-29 23:33
staythink
Rank: 2
等 级:论坛游民
帖 子:42
专家分:50
注 册:2010-7-26
收藏
得分:0 
回复 7楼 mujiaha
我还有一个疑问,原题中的学号是用数字表示的。。。我只是想改成我们常用的学号形式,就是字母+学号。。。
原来的代码中只是改了少部分,就可以实现的啊。。
代码如下:
#include<stdio.h>
#define ARR_SIZE 40
void FindMax(int *maxscore,long *maxnum,int score[],long num[],int n);
void main()
{
    int n,score[ARR_SIZE],i,maxscore;
    long num[ARR_SIZE],maxnum;
    printf("plealde input the total number:\n");
    scanf("%d",&n);
    printf("plealde input the number and score:\n");
    for(i=0;i<n;i++)
    {
        scanf("%ld %d",&num[i],&score[i]);
    }
    FindMax(&maxscore,&maxnum,score,num,n);
    printf("该班上分数最高的学生学号为:%ld\n",maxnum);
    printf("该班上分数最高的学生成绩为:%d\n",maxscore);

}

void FindMax(int *maxscore,long *maxnum,int score[],long num[],int n)
{
    int i;
    *maxscore=score[0];
    *maxnum=num[0];
    for(i=1;i<n;i++)
    {
        if(score[i]>*maxscore)
        {
            *maxscore=score[i];
            *maxnum=num[i];
        }
    }
}
整段代码中改动的只有:
char---long;
%s---%ld;

就可以实现的。。。所以,我就不大明白了。。。字符串难道与单纯的数字差别很大?
图片附件: 游客没有浏览图片的权限,请 登录注册

be a progammer,instead of a coder~
2010-07-29 23:40
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
收藏
得分:0 
是啊,差别很大的。整数只占4位空间,可以不用指针。而字符串必须使用指针,因为它应该指向一个字串的开头并以'\0'结尾。字符串的使用长度通常是不定的,所以必须使用malloc来分配内存空间,用memset来初始化空间。
当然,如果你用了CString的话,差别就很小了。

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
2010-07-30 08:00
快速回复:莫名的错误,求指点!
数据加载中...
 
   



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

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