| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1648 人关注过本帖
标题:c++中对类的对象能使用循环吗?求解
只看楼主 加入收藏
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
结帖率:93.75%
收藏
已结贴  问题点数:40 回复次数:9 
c++中对类的对象能使用循环吗?求解
c++中对类的对象能使用循环吗?求解
有这样一道题
一班有10名学生,一学期有5门课程,编写程序实现如下功能:
(1)录入每名学生的各科成绩
(2)求出每名学生的平均成绩和总分
(3)求每门课程的平均分、及格率、最高成绩和最低成绩
(4)输出总成绩前5名的学生学号、各科成绩和总分
(5)输出各科补考学生的学号和成绩
我目前只做了前两问,由于第三问不仅涉及到对每名学生的平均分,还有对每门课程的平均分,信息量比较大,一个一个列出来是最笨的方法,应该没人会这么干
我打算在类中设置一个数组,这样提取数据的时候比较方便,对学生的处理使用for循环,也就是对类中的对象使用for循环,我不会弄,谁能给看看?或者谁有更好的思想?
ps:我是编程新手,c++不咋地!嘿嘿!
下面代码奉上,很少,很简单!希望各位看客不要介意!
程序代码:
#include <iostream>
using namespace std;
int i;
class student
{public:
student(int n,float s,float sci,float m,float a,float mu,float c):num(n),score(s),science(sci),math(m),art(a),music(mu),computer(c){}     //定义构造函数
 student(){}; //定义无参的构造函数
 float total();
void lvru();
static float Saverage();     //学生平均
private:
    int num;
    float score;
    float science;
    float math;
    float art;
    float music;
    float computer;
    static float sum;
   
};
                        

 float student::total()
{
    sum=science+math+art+music+computer;
    cout<<"五科的总成绩为"<<sum<<endl;
   return 0;

 }

void student::lvru()
{   cout<<"学号:"<<endl;
    cin>>num;
    cout<<"科学成绩为:";
    cin>>science;
    cout<<"数学成绩为:";
    cin>>math;
    cout<<"艺术成绩为:";
    cin>>art;
    cout<<"音乐成绩为:";
    cin>>music;
    cout<<"计算机成绩为:";
    cin>>computer;
}
float student::Saverage()
{
    return ( float (sum/5));
}


 float student::sum=0;
int main()
{ 
    student stu1,stu2,stu3,stu4,stu5,stu6,stu7,stu8,stu9,stu10,stui;
    cout<<"请输入十名学生的信息:"<<endl;
    stu1.lvru();stu2.lvru();stu3.lvru();stu4.lvru();stu5.lvru();
    stu6.lvru();stu7.lvru();stu7.lvru();stu8.lvru();stu9.lvru();stu10.lvru();
    cout<<"十名学生平均成绩为:"<<endl;
    for(int i=1;i<=10;i++)
    {
        cout<<stui.num<<stui.Saverage()<<endl;    //编译时出错,stui不通过
    }
        return 0;
}
搜索更多相关主题的帖子: 编写程序 总成绩 课程 
2012-05-23 17:12
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
收藏
得分:20 
你那个编译出错时因为num是私有变量不能外部访问。
程序代码:
#include <iostream>
using namespace std;
int i;
class student
{public:
student(int n,float s,float sci,float m,float a,float mu,float c):num(n),score(s),science(sci),math(m),art(a),music(mu),computer(c){}     //定义构造函数
student(){}; //定义无参的构造函数
float total();
void lvru();
float getnum(){return num;}
static float Saverage();     //学生平均
private:
    int num;
    float score;
    float science;
    float math;
    float art;
    float music;
    float computer;
    static float sum;
  

};
                       

float student::total()
{
    sum=science+math+art+music+computer;
    cout<<"五科的总成绩为"<<sum<<endl;
   return 0;
}

void student::lvru()
{   cout<<"学号:"<<endl;
    cin>>num;
    cout<<"科学成绩为:";
    cin>>science;
    cout<<"数学成绩为:";
    cin>>math;
    cout<<"艺术成绩为:";
    cin>>art;
    cout<<"音乐成绩为:";
    cin>>music;
    cout<<"计算机成绩为:";
    cin>>computer;
}
float student::Saverage()
{
    return ( float (sum/5));
}

float student::sum=0;
int main()
{
    student stud[3];
    cout<<"请输入三名学生的信息:"<<endl;
    for (int i=0;i<3;i++)
    {
        stud[i].lvru();
    }
    cout<<"三名学生平均成绩为:"<<endl;
    for(int j=0;j<3;j++)
    {
        stud[j].total();
        cout<<stud[j].getnum()<<" "<<stud[j].Saverage()<<endl;
    }
        return 0;
}

2012-05-23 19:06
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 2楼 lknight
我真的好粗心啊!!这次长记性了!O(∩_∩)O谢谢!
下边还有两问,对于这道题,我的思想对吗?或者,你能想出一个更为简洁的思路吗?
2012-05-23 22:40
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
C++中你可以使用vector,或是list来装你的对象。

我们都在路上。。。。。
2012-05-24 14:46
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
收藏
得分:0 
多维元素操作感觉还是数据库给力,要是自己写,我觉得还是再添加几个变量,每个变量存放所有学生的某一门课成绩,,感觉我这方法真不咋地。。。
2012-05-24 18:42
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
将对象放在容器中就行了。
2012-05-25 09:09
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 4楼 hellovfp
关于你说的vector我已经查过了资料了,但由于我们原来没讲过,对于具体的编写还是不会,你能帮我把这个程序改成使用vector吗?谢谢啦!
2012-05-26 14:24
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:20 
回复 7楼 跳跳鱼
只是一个示例,并未完成所有功能:

程序代码:
/*
一班有10名学生,一学期有5门课程,编写程序实现如下功能:
(1)录入每名学生的各科成绩
(2)求出每名学生的平均成绩和总分
(3)求每门课程的平均分、及格率、最高成绩和最低成绩
(4)输出总成绩前5名的学生学号、各科成绩和总分
(5)输出各科补考学生的学号和成绩
*/

#include <iostream>
#include <vector>

using namespace std;
enum score_name{science, math, art, music, computer};

class student
{
    
    public:
        student(){}
        student(int n, float sci, float m, float a, float mu, float c)
            :num(n)
        {
            score[science]    = sci;
            score[math]        = m; 
            score[art]        = a;
            score[music]    = mu; 
            score[computer] = c;
        }
        int get_number();
        float get_total();        //求取学生个人总成绩
        float get_average();    //求取学生个人平均成绩
        float get_score_by_name(enum score_name); //根据学科取单门成绩
    private:
        int num;
        float score[5];
};

int student::get_number()
{
    return num;
}

float student::get_total()
{
    int i, sum = 0;
    for(i = 0; i < 5; i++)
    {
        sum += score[i];
    }
    return sum;
}

float student::get_average()
{
    return get_total() / 5;
}

float student::get_score_by_name(enum score_name sn)
{
    return score[sn];
}

class Manage
{
    public:
        void entry(); //录入
        void output();    //输出每个人的总成绩和平均成绩
        float get_course_average(enum score_name); //求取所有学生单门平均成绩
    private:
        vector<student> stus;
};

void Manage::entry()
{
    int num = 0;
    float score[5] = {0};

    cout << "学号:" <<endl;
    cin >> num;
    cout<<"科学成绩为:";
    cin >> score[0];
    cout<<"数学成绩为:";
    cin >> score[1];
    cout<<"艺术成绩为:";
    cin >> score[2];
    cout<<"音乐成绩为:";
    cin >> score[3];
    cout<<"计算机成绩为:";
    cin >> score[4];

    student s(num, score[0], score[1], score[2], score[3], score[4]);
    stus.push_back(s);
}

void Manage::output()
{
    vector<student>::iterator itor;

    for(itor = stus.begin(); itor != stus.end(); ++itor)
    {
        cout << "学号:" << itor->get_number() << endl;
        cout << "总成绩:" << itor->get_total() << endl;
        cout << "平均成绩:" << itor->get_average() << endl;
        cout << endl;
    }
}

float Manage::get_course_average(enum score_name sn)
{
    int sum = 0;
    vector<student>::iterator itor;
    
    for(itor = stus.begin(); itor != stus.end(); ++itor)
    {
        sum += itor->get_score_by_name(sn);
    }
    return sum / stus.size();
}

int main()
{
    int i;
    Manage m;
    const int stu_num = 3;

    for(i = 0; i < stu_num; i++)
    {
        m.entry();    
    }
    cout << "-----------------------------------" << endl;

    m.output();

    cout << "-----------------------------------" << endl;

    cout << "每门平均成绩:" << endl;
    cout << "科学:" << m.get_course_average(science) << endl;
    cout << "数学:" << m.get_course_average(math) << endl;
    cout << "艺术:" << m.get_course_average(art) << endl;

    return 0;
}

我们都在路上。。。。。
2012-05-27 11:44
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 8楼 hellovfp
大爱啊!原来我纠结的头发都快白了,,,幸亏你拯救了我!真是好人啊!O(∩_∩)O哈哈~
2012-05-27 17:36
keli55
Rank: 1
等 级:新手上路
帖 子:8
专家分:3
注 册:2010-4-18
收藏
得分:0 
如果把学习成绩做成一个struct这样可以吗?包含在学生类里面,我也是新手,以前的C可能用得太多了,
2012-06-06 21:32
快速回复:c++中对类的对象能使用循环吗?求解
数据加载中...
 
   



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

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