| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 675 人关注过本帖
标题:关于 班级最高分学生的学号,Total 函数 读取异常 new 开辟 是否 错误 ...
只看楼主 加入收藏
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:9 
关于 班级最高分学生的学号,Total 函数 读取异常 new 开辟 是否 错误 求教
/*
6、【基本题】一个班级有n个学生,n由用户输入,每个学生有学号1-n,
有三门成绩(语、数、外)成绩由随机数生成(0到100之间),每个学生的成绩由动态数组保存。
    要求:
    (1). 取得总分最高的学生的学号。
    (2). 提示用户输入1,2,3来获得语、数、外的平均分。
*/
#include<iostream>
#include<time.h>
#include<math.h>
using namespace std;
class CStudent{
int Math,English,Chinese;
public:
    CStudent()
    {
    //    srand ( time(NULL) );
        Math =rand()%100;
        English =rand()%100;
        Chinese =rand()%100;
    }
    int  Total()
    {

        return (Math +English +Chinese );//报错  
    }
    void  Show()
    {
        cout<<" Math:"<<Math<<"    English:"<<English<<"    Chinese:"<<Chinese <<endl;
    }
};
int GetMaxNum(CStudent * &Grade,int & n )
{
    for(int i=0;i<n;i++)
        {
            (Grade+i)->Show();
            if(i%4==0)
            {
                cout<<endl;
            }
    }
    int i=0,max=(Grade+i)->Total();
    while(i<n)
    {
        if((Grade+i)->Total()<(Grade+++i)->Total())
            max=(Grade+i)->Total();
    }
    return i;
}
void main()
{
    int n;
    cout<<"input the number of  student :"<<endl;
    cin>>n;
    CStudent  * Grade=new CStudent[n] ;
    //a.Show ();
    cout<<GetMaxNum(Grade, n )<<endl;
    delete []Grade;
}
搜索更多相关主题的帖子: include Chinese English 学生 
2013-05-26 12:33
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
我 想 CStudent 类 实现  关于  struct  方法 我会 处理这道题  希望 大家 运行下 我的代码  指教  
我没有 多少 分  谢谢  
原题:
2013-05-26 12:36
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
原题为:【基本题】一个班级有n个学生,n由用户输入,每个学生有学号1-n,
有三门成绩(语、数、外)成绩由随机数生成(0到100之间),每个学生的成绩由动态数组保存。
    要求:
    (1). 取得总分最高的学生的学号。
2013-05-26 12:36
笑傲
Rank: 8Rank: 8
来 自:迪拜
等 级:蝙蝠侠
威 望:5
帖 子:223
专家分:856
注 册:2013-3-9
收藏
得分:20 
这是我在你的代码上修改的:
程序代码:
#include<iostream>
#include<time.h>
#include<math.h>
using namespace std;
class CStudent{
int Math,English,Chinese;
public:
    CStudent()
    {
    //    srand ( time(NULL) );
        Math =rand()%100;
        English =rand()%100;
        Chinese =rand()%100;
    }
    int  Total()
    {

        return (Math +English +Chinese );
    }
    void  Show()
    {
        cout<<" Math:"<<Math<<"    English:"<<English<<"    Chinese:"<<Chinese <<endl;
    }
};
int GetMaxNum(CStudent * &Grade,int &n )
{
    for(int i=0;i<n;i++)
        {
            (Grade+i)->Show();
            if(i%4==0)
            {
                cout<<endl;
            }
    }
    i=0;
    int number = 0;                 //因为是要显示总分最高的人的序号,所以用number记录其序号,注意number比学号小1;
    while(i< n - 1)                      //这里改为i < n - 1 , 因为下面有++i,如果i<n则有可能当i=n-1时继续执行++i,导致越界;
    {
        if((Grade+number)->Total()<(Grade+(++i))->Total())         //这里改(++i),每次用当前总分最高的学生和下一个学生总分比较;
            number = i;                                   //number记录总分高者的序号;
    }
    return number + 1;           //返回最高分的学生的学号;
}
void main()
{
    int n;
    cout<<"input the number of  student :"<<endl;
    cin>>n;
    srand(unsigned(time(NULL)));
    CStudent  * Grade=new CStudent[n] ;
    //a.Show ();
    cout<<GetMaxNum(Grade, n )<<endl;
    delete []Grade;
}

练就一身本领,只为笑傲江湖!
2013-05-26 15:15
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
你的i重定义了,VC不能这样,我也不知道为什么,一个变量不能定义两次,即使 局部变量也不可以。
 i=0;
int max=(Grade+i)->Total();

Maybe
2013-05-26 17:37
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
回复 4楼 笑傲
我的 是  VS    2012  可以 这个 没错误  谢谢
2013-05-26 18:10
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
回复 4楼 笑傲
谢谢   我也  改过来 现在 我又 出毛病了 有时间看下把
2013-05-26 18:11
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
/*
6、【基本题】一个班级有n个学生,n由用户输入,每个学生有学号1-n,
有三门成绩(语、数、外)成绩由随机数生成(0到100之间),每个学生的成绩由动态数组保存。
    要求:
    (1). 取得总分最高的学生的学号。
    (2). 提示用户输入1,2,3来获得语、数、外的平均分。
*/
#include<iostream>
#include<time.h>
#include<math.h>
using namespace std;
class CStudent{
int Math,English,Chinese;
public:
    CStudent()
    {   
        Math =rand()%100;
        English =rand()%100;
        Chinese =rand()%100;
    }
    int GetMath()
    {
        return Math;
    }
    int GetEnglish()
    {
        return English;
    }
    int GetChinese()
    {
        return Chinese;
    }
    int  Total()
    {
        return (Math +English +Chinese );//原先报错来着,现在 没了 不知道为啥 嘿嘿   
    }
    void  Show()
    {
        cout<<" Math:"<<Math<<"    English:"<<English<<"    Chinese:"<<Chinese ;
    }
};
void  UserTips(CStudent * &Grade,int & n)
{
    int AverEnglish=0,AverMath=0,AverChinese=0;
    for(int i=0;i<n;i++)
    {
        AverEnglish+=(Grade +i)->GetEnglish() ;
        AverMath+=(Grade +i)->GetMath() ;
        AverChinese+=(Grade +i)->GetChinese() ;
    }
    AverChinese =AverChinese /n;
    AverEnglish =AverEnglish /n;
    AverMath =AverMath /n;
    cout<<"        用户须知:"<<endl;
    cout<<"1        代表英语平均成绩"<<endl;
    cout<<"2        代表数学平均成绩"<<endl;
    cout<<"3        代表语文平均成绩"<<endl;
    int num;cin>>num;
    switch(num)
    {
    case 1:cout<<"    the average of English :"<<AverEnglish <<endl; ;break;
    case 2:cout<<"    the average of Math :"<<AverMath<<endl;;break;
    case 3:cout<<"    the average of Chinese :"<<AverChinese<<endl;;break;
    default: cout<<"input  error!"<<endl;break;
    }
}
int GetMaxNum(CStudent * &Grade,int & n )
{

    for(    int i=0;i<n;i++)
        {
            (Grade+i)->Show();
            cout<<"    total    :"<<(Grade+i)->Total() <<endl;
            if((i+1)%4==0)
            {
                cout<<endl;
            }
    }
    int j=0,MaxIndex=0,HighestScore=Grade->Total ();
    for(;j<n;j++)
    {
        if(HighestScore<(Grade+j+1)->Total ())
        {
            HighestScore=(Grade+j+1)->Total ();
            MaxIndex=j;
        }        
    }
    return MaxIndex+1;//??why  the answer  is wrong 。cannot  diplay  rigthly...
}
void main()
{   
    int n;
    cout<<"input the number of  student :"<<endl;
    cin>>n;
    CStudent  * Grade=new CStudent[n] ;
    cout<<"the student num of the highest score : "<<GetMaxNum(Grade, n )<<endl;
    UserTips(Grade, n);
    delete []Grade;
    Grade=NULL;
}
不能正却 显示 最高分的学号   ,比大小 代码 现在 看不出来  大家 给 找下把
2013-05-26 18:12
笑傲
Rank: 8Rank: 8
来 自:迪拜
等 级:蝙蝠侠
威 望:5
帖 子:223
专家分:856
注 册:2013-3-9
收藏
得分:0 
回复 8楼 haoyasen
修改后的代码:
程序代码:
#include<iostream>
#include<time.h>
#include<math.h>
using namespace std;
class CStudent{
int Math,English,Chinese;
public:
    CStudent()
    {    
        Math =rand()%100;
        English =rand()%100;
        Chinese =rand()%100;
    }
    int GetMath()
    {
        return Math;
    }
    int GetEnglish()
    {
        return English;
    }
    int GetChinese()
    {
        return Chinese;
    }
    int  Total()
    {
        return (Math +English +Chinese );//原先报错来着,现在 没了 不知道为啥 嘿嘿   
    }
    void  Show()
    {
        cout<<" Math:"<<Math<<"    English:"<<English<<"    Chinese:"<<Chinese ;
    }
};
void  UserTips(CStudent * &Grade,int & n)
{
    int AverEnglish=0,AverMath=0,AverChinese=0;
    for(int i=0;i<n;i++)
    {
        AverEnglish+=(Grade +i)->GetEnglish() ;
        AverMath+=(Grade +i)->GetMath() ;
        AverChinese+=(Grade +i)->GetChinese() ;
    }
    AverChinese =AverChinese /n;
    AverEnglish =AverEnglish /n;
    AverMath =AverMath /n;
    cout<<"        用户须知:"<<endl;
    cout<<"1        代表英语平均成绩"<<endl;
    cout<<"2        代表数学平均成绩"<<endl;
    cout<<"3        代表语文平均成绩"<<endl;
    int num;cin>>num;
    switch(num)
    {
    case 1:cout<<"    the average of English :"<<AverEnglish <<endl; ;break;
    case 2:cout<<"    the average of Math :"<<AverMath<<endl;;break;
    case 3:cout<<"    the average of Chinese :"<<AverChinese<<endl;;break;
    default: cout<<"input  error!"<<endl;break;
    }
}
int GetMaxNum(CStudent * &Grade,int & n )
{

    for(    int i=0;i<n;i++)
        {
            (Grade+i)->Show();
            cout<<"    total    :"<<(Grade+i)->Total() <<endl;
            if((i+1)%4==0)
            {
                cout<<endl;
            }
    }
    int j=0,MaxIndex=0,HighestScore=Grade->Total ();
    for(;j<n - 1; j++)                   //我想说你根本没看我的代码,这里是n-1,n的话可能会越界,下面有个j+1,
    {                                    //如果是n的话,j可以取n-1,则j+1是n,而(Grade + n)是没有的,
        if(HighestScore<(Grade+j+1)->Total ())
        {
            HighestScore=(Grade+j+1)->Total ();
            MaxIndex=j + 1;                //这里是j+1,上面的比较知道总分高的学生的序号应该是j+1(自己好好想想)
        }        
    }
    return MaxIndex+1;//还是那句话,这里的学号比序号大一,因为序号从0开始,学号从1开始;所以+1;
}
void main()
{    
    int n;
    cout<<"input the number of  student :"<<endl;
    cin>>n;
    srand(unsigned(time(NULL)));            //这里设置种子,否则不觉得每次的结果都是一样么?
    CStudent  * Grade=new CStudent[n] ;
    cout<<"the student num of the highest score : "<<GetMaxNum(Grade, n )<<endl;
    UserTips(Grade, n);
    delete []Grade;
    Grade=NULL;
}


[ 本帖最后由 笑傲 于 2013-5-26 19:40 编辑 ]

练就一身本领,只为笑傲江湖!
2013-05-26 19:38
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
回复 9楼 笑傲
十分感谢  
2013-05-26 22:27
快速回复:关于 班级最高分学生的学号,Total 函数 读取异常 new 开辟 是否 ...
数据加载中...
 
   



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

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