| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1002 人关注过本帖
标题:用class写程式的问题
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:9 
用class写程式的问题
我现在要写一个程式, 要求输入3个学生的姓和名(分开来输入姓,名), 然后输入这3个人的3个成绩, 然后算出平均分, 输出 是三个人的名字, 三个人的成绩, 三个人的平均分数, 还要输出谁的分数最高, 谁的最低, 谁的在中间

输出应该像这样:

  90, 90, 90,  平均:90   最高
  80, 80, 80,  平均:80   中间
  70, 70, 70,  平均:70   最低

但是前提一定要是用class来编写

这个程式我差不多写完了, 但是最后比谁高谁低的,我是先把他们的平均分按从小到大排了, 但是现在排列出了点问题,大家帮忙找一找我的错误。(在最下面)

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

class personType
{
public:
       void print() const;
       void printTwo() const;
       void printThree() const;
       void setNameOne (string first, string last);
       void setScore (int one, int two, int three);
       string getFirstNameOne() const;
       string getLastNameOne() const;
       int OnegetFirstScore() const;
       int OnegetSecondScore() const;
       int OnegetThirdScore() const;
       string personTypeOne (string first="", string last="");
       int personOneScore (int one, int two, int three);
       double getaveOne();
       void setNameTwo (string first_two, string last_two);
       void setScoreTwo (int one_two, int two_two, int three_two);
       string getFirstNameTwo() const;
       string getLastNameTwo() const;
       int TwogetFirstScore() const;
       int TwogetSecondScore() const;
       int TwogetThirdScore() const;
       string personTypeTwo (string first_two="", string last_two="");
       int personTwoScore (int one_two, int two_two, int three_two);
       double getaveTwo();
       void setNameThree (string first_three, string last_three);
       void setScoreThree (int one_three, int two_three, int three_three);
       string getFirstNameThree() const;
       string getLastNameThree() const;
       int ThreegetFirstScore() const;
       int ThreegetSecondScore() const;
       int ThreegetThirdScore() const;
       string personTypeThree (string first_three="", string last_three="");
       int personThreeScore (int one_three, int two_three, int three_three);
       double getaveThree();
private:
        string OnefirstName;
        string OnelastName;
        string TwofirstName;
        string TwolastName;
        string ThreefirstName;
        string ThreelastName;
        int firstScore;
        int secondScore;
        int thirdScore;
        int twofirstScore;
        int twosecondScore;
        int twothirdScore;
        int threefirstScore;
        int threesecondScore;
        int threethirdScore;
};

void personType::print() const
{
     cout<<OnefirstName<<""<<OnelastName;
}

void personType::printTwo() const
{
    cout<<TwofirstName<<""<<TwolastName;
}

void personType::printThree() const
{
    cout<<ThreefirstName<<""<<ThreelastName;
}

void personType::setNameOne(string first, string last)
{
     OnefirstName=first;
     OnelastName=last;
}

void personType::setScore (int one, int two, int three)
{
    firstScore=one;
    secondScore=two;
    thirdScore=three;
}
     
string personType::getFirstNameOne() const
{
       return OnefirstName;
}

string personType::getLastNameOne() const
{
       return OnelastName;
}

int personType::OnegetFirstScore() const
{
    return firstScore;
}
int personType::OnegetSecondScore() const
{
    return secondScore;
}
int personType::OnegetThirdScore() const
{
    return thirdScore;
}

string personType::personTypeOne(string first, string last)
{
      OnefirstName=first;
      OnelastName=last;
}

int personType::personOneScore (int one, int two, int three)
{
    firstScore=one;
    secondScore=two;
    thirdScore=three;
}

double personType::getaveOne()
{
    return (firstScore+secondScore+thirdScore)/3;
}

void personType::setNameTwo(string first_two, string last_two)
{
     TwofirstName=first_two;
     TwolastName=last_two;
}

void personType::setScoreTwo (int one_two, int two_two, int three_two)
{
    twofirstScore=one_two;
    twosecondScore=two_two;
     twothirdScore=three_two;
}
string personType::getFirstNameTwo() const

{
       return TwofirstName;
}

string personType::getLastNameTwo() const
{
       return TwolastName;
}

int personType::TwogetFirstScore() const
{
    return twofirstScore;
}
int personType::TwogetSecondScore() const
{
    return twosecondScore;
}
int personType::TwogetThirdScore() const
{
    return twothirdScore;
}

string personType::personTypeTwo(string first_two, string last_two)
{
      TwofirstName=first_two;
      TwolastName=last_two;
}

int personType::personTwoScore (int one_two, int two_two, int three_two)
{
    twofirstScore=one_two;
    twosecondScore=two_two;
    twothirdScore=three_two;
}

double personType::getaveTwo()
{
    return (twofirstScore+twosecondScore+twothirdScore)/3;
}

void personType::setNameThree(string first_three, string last_three)
{
     ThreefirstName=first_three;
     ThreelastName=last_three;
}

void personType::setScoreThree (int one_three, int two_three, int three_three)
{
    threefirstScore=one_three;
    threesecondScore=two_three;
     threethirdScore=three_three;
}
string personType::getFirstNameThree() const

{
       return ThreefirstName;
}

string personType::getLastNameThree() const
{
       return ThreelastName;
}

int personType::ThreegetFirstScore() const
{
    return threefirstScore;
}
int personType::ThreegetSecondScore() const
{
    return threesecondScore;
}
int personType::ThreegetThirdScore() const
{
    return threethirdScore;
}

string personType::personTypeThree(string first_three, string last_three)
{
      ThreefirstName=first_three;
      ThreelastName=last_three;
}

int personType::personThreeScore (int one_three, int two_three, int three_three)
{
    threefirstScore=one_three;
    threesecondScore=two_three;
     threethirdScore=three_three;
}

double personType::getaveThree()
{
    return (threefirstScore+threesecondScore+threethirdScore)/3;
}

int main()
{
    personType name;
    string l, f, l_two, f_two, l_three, f_three;
    int one, two, three, one_two, two_two, three_two, one_three, two_three;
    int a[3], three_three, i, aterm, count=0;
   
    cout<<"student #1.\n";
    cout<<"input first name.\n";
    cin>>f;
    cout<<"input last name.\n";
    cin>>l;
    cout<<endl;
    cout<<"enter 3 scores for student #1.\n";
    cin>>one>>two>>three;
    name.setNameOne(f, l);
    name.setScore(one, two, three);
    cout<<endl;

    cout<<"student #2.\n";
    cout<<"input first name.\n";
    cin>>f_two;
    cout<<"input last name.\n";
    cin>>l_two;
    cout<<endl;
    cout<<"enter 3 scores for student #2.\n";
    cin>>one_two>>two_two>>three_two;
    name.setNameTwo(f_two, l_two);
    name.setScoreTwo(one_two, two_two, three_two);
    cout<<endl;

    cout<<"student #3.\n";
    cout<<"input first name.\n";
    cin>>f_three;
    cout<<"input last name.\n";
    cin>>l_three;
    cout<<endl;
    cout<<"enter 3 scores for student #3.\n";
    cin>>one_three>>two_three>>three_three;
    name.setNameThree(f_three, l_three);
    name.setScoreThree(one_three, two_three, three_three);
    cout<<endl;

    cout<<"------------------------------\n";
    cout<<"the name for student #1 is:\n";
    cout<<name.getLastNameOne()<<", "<<name.getFirstNameOne()<<endl;
    cout<<"the 3 scores for student #1 is:\n";
    cout<<name.OnegetFirstScore()<<", "<<name.OnegetSecondScore()<<", ";
    cout<<name.OnegetThirdScore()<<endl;
    cout<<"the average for student #1 is:\n";
    cout<<name.getaveOne()<<endl;
    cout<<endl;
   
    cout<<"the name for student #2 is:\n";
    cout<<name.getLastNameTwo()<<", "<<name.getFirstNameTwo()<<endl;
    cout<<"the 3 scores for student #2 is:\n";
    cout<<name.TwogetFirstScore()<<", "<<name.TwogetSecondScore()<<", ";
    cout<<name.TwogetThirdScore()<<endl;
    cout<<"the average for student #2 is:\n";
    cout<<name.getaveTwo()<<endl;
    cout<<endl;

    cout<<"the name for student #3 is:\n";
    cout<<name.getLastNameThree()<<", "<<name.getFirstNameThree()<<endl;
    cout<<"the 3 scores for student #3 is:\n";
    cout<<name.ThreegetFirstScore()<<", "<<name.ThreegetSecondScore()<<", ";
    cout<<name.ThreegetThirdScore()<<endl;
    cout<<"the average for student #3 is:\n";
    cout<<name.getaveThree()<<endl;
    cout<<endl;


        a[0]=name.getaveOne();
        a[1]=name.getaveTwo();
        a[2]=name.getaveThree();

    do                                            //这里我排列了一下
    {
        if (a[i]>a[i+1])
        {
            aterm=a[i];
            a[i]=a[i+1];
            a[i+1]=aterm;
            count=1;
            i=1;
        }
        else if ((a[i]<=a[i+1]) && i<3)
        {
             i++;
             count=1;
        }
        else
        {
            count=0;
        }
    }while (count==1);

    for (i=0; i<3; i++)                            //这里我输出排列, 但是输出不正确(拜托大家找找毛病)
    {
        cout<<a[i]<<endl;
    }
      system("PAUSE");
      return 0;
}

[[it] 本帖最后由 suckdog 于 2008-6-4 02:31 编辑 [/it]]
搜索更多相关主题的帖子: 程式 class include 分数 平均分 
2008-06-01 08:22
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
自己顶一下
2008-06-02 01:30
kaixin2008
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-02 23:16
mqh21364
Rank: 1
等 级:新手上路
帖 子:642
专家分:0
注 册:2008-2-28
收藏
得分:0 
有点乱。。。。。。。。。
程序代码:
#include <iostream>
#include <string>
using namespace std;

class personType
{
    public:
        void print() const;
        void setName (char *first, char *last);
        void setScore (int one, int two, int three);
        char *getFirstName();
        char *getLastName();
        int getFirstScore() const;
        int getSecondScore() const;
        int getThirdScore() const;
        personType();
        personType(char *first, char *last);
        personType(int one, int two, int three);
        
    private:
        char firstName[100];
        char lastName[100];
        int firstScore;
        int secondScore;
        int thirdScore;
};

void personType::print() const
{
     cout<<firstName<<""<<lastName;
}

void personType::setName(char *first, char *last)
{
     strcpy(firstName, first);
     strcpy(lastName, last);
}

void personType::setScore(int one, int two, int three)
{
    firstScore = one;
    secondScore = two;
    thirdScore = three;
}
   
char * personType::getFirstName()
{
    return firstName;
}

char * personType::getLastName()
{
    return lastName;
}

personType::personType()
{
}

personType::personType(char *first, char *last)
{
    strcpy(firstName, first);
    strcpy(lastName, last);
}

int main()
{
    personType name;
    char l[100], f[100];

    cout<<"student #1"<<endl;
    cout<<"input first name."<<endl;
    cin>>f;
    cout<<"input last name"<<endl;
    cin>>l;
    cout<<endl;
    
    name.setName(f, l);
    cout<<"the name you entered is:\n";
    cout<<name.getLastName()<<", "<<name.getFirstName()<<endl;
    
    system("PAUSE");
    return 0;
}


前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。
2008-06-03 13:40
zdg19861005
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-03 18:04
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
我帖子从新编过了, 大家再看看,帮帮忙
2008-06-04 02:33
mqh21364
Rank: 1
等 级:新手上路
帖 子:642
专家分:0
注 册:2008-2-28
收藏
得分:0 
我写得不对么???

前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。
2008-06-04 10:15
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
你写的对,但是我刚刚改过我程式了, 你再看看 (我注释的部份), 不知道哪错了,不能排列
2008-06-04 10:32
mqh21364
Rank: 1
等 级:新手上路
帖 子:642
专家分:0
注 册:2008-2-28
收藏
得分:0 
你的排序是什么排序方法阿??
稀奇古怪的。

前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。
2008-06-04 15:08
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 
呵呵,楼主写的好多啊~~

★孤独的人是可耻的★
2008-06-04 17:30
快速回复:用class写程式的问题
数据加载中...
 
   



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

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