回复 2楼 jiantiewen
我很纳闷的就是用动态数组的,申明的动态数组并没有指向一个实体地址啊? 不过没有指向一个实体地址的编译时他还是报错,我把整个代码沾上来,请你帮我看看~谢谢啦
#include<iostream>
#include<cstdlib>
#include<string>
#include<windows.h>
using namespace std;
void display()
{
cout<<"********************欢迎使用学分绩点计算器*************************"<<endl<<endl;
cout<<"
By Owen"<<endl;
}
class Score
//建立一个分数类
{
private:
float score;
float credit;
public:
Score();
~Score();
void SetScore(int k);
float GetScore();
void SetCredit(int k);
float GetCredit();
float CaculPoint();
friend float CaculGPA(Score cal[],int n);
};
void Score::SetScore(int k)
//设置分数
{
float temp;
cout<<endl<<"请输入课程"<<k<<"的成绩:";
cin>>temp;
score=temp;
}
float Score::GetScore()
//返回分数
{
//cout<<"课程"<<k<<"的成绩为:"<<score<<endl;
return score;
}
void Score::SetCredit(int k)
//设置学分
{
float temp;
cout<<endl<<"请输入课程"<<k<<"所占的学分:";
cin>>temp;
credit=temp;
}
float Score::GetCredit()
//返回学分
{
return credit;
}
float Score::CaculPoint()
{
float temp;
if(score<60)
return 0;
else
{
temp=(score-50)*credit/10;
return temp;
}
}
float CaculGPA(Score cal[],int n)
{
float t_GPA=0,t_allCredit=0,t_allPA=0;
for(int j=0;j<n;j++)
{
t_allCredit+=cal[j].GetCredit();
t_allPA+=cal[j].CaculPoint();
}
t_GPA=t_allPA/t_allCredit;
return t_GPA;
}
void main()
{
int n=1;
float Gpa;
display();
cout<<"请输入您这学期的课程数:";
cin>>n;
if(n<0||n>20)
{
cout<<"您输入的课程数不正确,请重新开始!"<<endl;
main();
exit(-1);
}
/*Score GPA[n];*/
Score *GPA=new Score[n];
cout<<"请输入您这学期的成绩和相应的学分:";
for(int i=0;i<n;i++)
{
GPA->SetScore(i);
GPA++->SetCredit(i);
}
Gpa=CaculGPA(GPA,n);
if(Gpa>4||Gpa==4)
cout<<endl<<"你非常优秀!您的学分绩点为:"<<Gpa<<endl;
if(Gpa>3||Gpa==3)
cout<<endl<<"恭喜您,您的学分绩点为:"<<Gpa<<endl;
if(Gpa>2)
cout<<endl<<"您的学分绩点为:"<<Gpa<<endl;
else
cout<<endl<<"下学期加油哦,您的学分绩点为:"<<Gpa<<endl;
system("pause");
delete[] GPA;
/*return 0;*/
}