标题:我是新手,求高手帮我看看下面这个程序....
只看楼主
暗藏
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-8-18
结帖率:66.67%
已结贴  问题点数:10 回复次数:3 
我是新手,求高手帮我看看下面这个程序....
程序分为以下几个部分:
student.h
#ifndef SHAPE_H
#define SHAPE_H
struct credit
{
    char course[20];
    int hour;
    char type[5];
};
class Student
{
protected:
char *name;
int student_ID;
credit credit_hour[10];
int num;
public:
    Student();
    virtual~Student(){};
    char *getname();
    virtual void InputInfo();
    virtual void input_credit()=0;
    virtual void displayInfo()=0;
    virtual int GetStuNum();

};
class UnderGraduate:public Student
{public:
 virtual  void InputInfo();
   virtual void Input_credit();
  virtual void displayInfo();
 int GetStuNum();
private:
    static int StuNum;
};
class Graduate:public Student
{
public:
virtual  void InputInfo();
 virtual void input_credit();
virtual  void displayInfo();
    int GetStuNum();
private:
    static int StuNum;
    char *tutor;
};
#endif
student.cpp
# include <iostream>
# include<iomanip>
using namespace std;
# include "student.h"


int UnderGraduate::StuNum=0;
int Graduate::StuNum=0;
Student::Student()
{num=0;
}
void Student::InputInfo()
{
    char namestr[20];
    cout<<"请输入下一个学生的姓名:";
        cin>>namestr;
    name=new char[strlen(namestr)+1];
    strcpy(name,namestr);
    cout<<"输入学生的学号:";
    cin>>student_ID;
}
char *Student::getname()
{return name;
}
void UnderGraduate::InputInfo()
{
    Student::InputInfo();
    StuNum++;
}
void UnderGraduate::Input_credit()
{
    char answer;
    do{cout<<"输入课程名称,学分,课程类别:必修?选修\n";
    cin>>credit_hour[num].course>>credit_hour[num].hour>>credit_hour[num].type;
    num++;
    cout<<"继续输入课程?\n";
        cin>>answer;
    }
    while(answer=='y');
}
int UnderGraduate::GetStuNum()
{return StuNum;
}
void UnderGraduate::displayInfo()
{
    cout<<"本科学生姓名:"<<name;
    cout<<"   学号: "<<student_ID<<endl;
    cout<<"课程名称   学分    课程类别   \n";
    for(int i=0;i<num;i++)
   
        cout<<left<<setw(10)<<credit_hour[i].course
            <<setw(10)<<credit_hour[i].hour
            <<setw(10)<<credit_hour[i].type<<endl;
}
void Graduate::InputInfo()
{
    char namestr[20];
    Student::InputInfo();
    StuNum++;
    cout<<"请输入导师名字:";
        cin>>namestr;
    tutor=new char[strlen(namestr)+1];
    strcpy(tutor,namestr);
}
void Graduate::input_credit()
{
    char answer;
    do
    {
        cout<<"输入课程的名称、学分、课程类别:学位/任选\n";
        cin>>credit_hour[num].course>>credit_hour[num].hour>>credit_hour[num].type;

        num++;
        cout<<"还有课程要输入?";
        cin>>answer;
    }
    while(answer=='y');
}
int Graduate::GetStuNum()
{
    return StuNum;
}
void Graduate::displayInfo()
{
    cout<<"研究生姓名: "<<name;
    cout<<"  学号     : "<<student_ID<<endl;
    cout<<"导师姓名    :"<<tutor<<endl;
    cout<<"课程名称  学分  课程类别\n";
    for(int i=0;i<num;i++)
    cout<<left<<setw(10)<<credit_hour[i].course
         <<setw(10)<<credit_hour[i].hour
         <<setw(10)<<credit_hour[i].type<<endl;
    }
main.cpp

# include<iostream.h>
# include<string.h>

# include "student.h"
Student**SelectStuType(Student *Array_ptr[][10]);
void creat_object(Student *Array_ptr[][10]);
void DisplayStu(Student *Array_ptr[][10]);
void InputCredit(Student *Array_ptr[][10]);
void main()
{
    UnderGraduate UnderStu[10];
    Graduate GraduateStu[10];
    Student *Stu_ptr[2][10];
    for(int i=0;i<10;i++)
        Stu_ptr[0][i]=&UnderStu[i];
     for(i=0;i<10;i++)
         Stu_ptr[1][i]=&GraduateStu[i];
    creat_object(Stu_ptr);
    InputCredit(Stu_ptr);
    DisplayStu(Stu_ptr);
}

   Student **SelectStuType(Student *Array_ptr[][10])
   {
       char type;
Student **ptr=NULL;
       do{
           cout<<"请选择学生类型:";
           cout<<"U本科生:G研究生:";
           cin>>type;
           switch(type)
           {
           case 'U':
               ptr=Array_ptr[0];
               break;
           case 'G':
               ptr=Array_ptr[1];
               break;
           default:cout<<"类型错误,请重新选择\n";
           }
       }while(ptr==NULL);
       return ptr;
   }
   void creat_object(Student *Array_ptr[][10])
   {char answer;
   Student **row_ptr;
   Student *ptr=NULL;
   cout<<"输入学生的基本信息\n";
   do
   {
       row_ptr=SelectStuType(Array_ptr);
       ptr=*(row_ptr+(row_ptr[0])->GetStuNum());
       ptr->InputInfo();
       cout<<"继续输入学生基本信息(y/n)?";
       cin>>answer;
   }while(answer=='y');
   }
    void DisplayStu(Student *Array_ptr[][10])
    {
        char answer;
        Student **row_ptr;
        Student *ptr=NULL;
        int StuNum;
        cout<<"显示学生基本信息\n";
        do
        {
            row_ptr=SelectStuType(Array_ptr);
            ptr=*row_ptr;
            StuNum=ptr->GetStuNum();
            for(int i=0;i<StuNum;i++)
                row_ptr[i]->displayInfo();
            cout<<"继续显示学生信息(y/n)?";
            cin>>answer;
        }while(answer=='y');
    }
    void InputCredit(Student *Array_ptr[][10])
    {
        char answer;
        Student**row_ptr;
        Student *ptr=NULL;
        int StuNum;
        cout<<"输入已选课程、学分、课程类型\n";
        do{
            row_ptr=SelectStuType(Array_ptr);
            ptr=*row_ptr;
            StuNum=ptr->GetStuNum();
            for(int i=0;i<StuNum;i++)
            {cout<<"输入"<<row_ptr[i]->getname()<<"的课程和学分:\n";
            row_ptr[i]->input_credit();
            }
            cout<<"继续输入其他类别学生的课程和学分(y/n)?";
                cin>>answer;
        }while(answer=='y');
    }



程序有点长,求帮帮忙,小弟感激不尽...


搜索更多相关主题的帖子: void class course public credit 
2010-09-29 00:20
mxs810
Rank: 9Rank: 9Rank: 9
来 自:火星
等 级:贵宾
威 望:16
帖 子:234
专家分:1122
注 册:2006-10-19
得分:5 
程序有什么问题,说说才能有的放矢啊~~

授人以鱼不如授人以渔
2010-09-29 09:19
暗藏
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-8-18
得分:0 
在VC++中运行总是出现错误。。。
2010-09-29 22:21
望山行者
Rank: 1
来 自:中国地质大学(武汉)
等 级:新手上路
帖 子:3
专家分:6
注 册:2010-5-27
得分:5 
你的基类定义有问题:
#ifndef SHAPE_H
#define SHAPE_H
struct credit
{
    char course[20];
    int hour;
    char type[5];
};
class Student
{
protected:
    char *name;
    int student_ID;
    credit credit_hour[10];
    int num;
public:
    Student();
    virtual~Student(){};
    char *getname();
    virtual void InputInfo();
    virtual void input_credit(){}//1
    virtual void displayInfo(){}//2
    virtual int GetStuNum()=0;//3

};
class UnderGraduate:public Student
{
public:
    /*virtual*/  void InputInfo();//4
    /*virtual*/ void Input_credit();//5
    /*virtual*/ void displayInfo();//6
    int GetStuNum();
private:
    static int StuNum;
};
class Graduate:public Student
{
public:
    virtual  void InputInfo();
    virtual void input_credit();
    virtual  void displayInfo();
    int GetStuNum();
private:
    static int StuNum;
    char *tutor;
};
#endif
仅在以上6处改变一下即可;在vc2005中已运行,可以通过!

天道酬勤!
2010-09-29 23:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321638-1-1.html




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

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