将客 2024-06-16 10:40 采纳率: 96.6%
浏览 1
已结题

关于#c++#的问题:为什么不是输出平均分


#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份:";
cin>>year;
cout<<"请输入出生月份:";
cin>>month;
cout<<"请输入出生日:";
cin>>day;
}
int GetYear()
 {return year;}//获取年份
int GetMonth() 
{return month;}//获取月
int GetDay()
 {return day;} //获取日
void SetBirthDate()
{SetDate();}
private:
int year,month,day;
};
//定义学生类,包含学生的学号、姓名、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号:";
cin>>num;
cout<<"请输入学生的姓名:";
cin>>name;
cout<<"请输入学生的性别(M/F):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生的第一门课成绩:";
cin>>score1;
cout<<"请输入学生的第二门课成绩:";
cin>>score2;
cout<<"请输入学生的第三门课成绩:";
cin>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
cout<<"最高分为:"<<maxscore<<endl;
cout<<"最低分为:"<<minscore<<endl;
cout<<"最平均分为:"<<average<<endl;
}
void SetBirthday (Date &birthday) //设置年月日
{
 birthday.SetDate();
}
int GetBirYear (Date &birthday) //获得年
{return birthday.GetYear();
}
int GetBirMonth (Date &birthday) //获得月
{
  return birthday.GetMonth();
}
int GetBirDay (Date &birthday) //获得日
{
return birthday.GetDay();    
}
void InputStudent (Date &birthday)
{
char choice;
cout<<"输入学生记录:"<<endl;
ofstream outfile ("D:\\DEV\\abc.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<num<<" "<<name<<" "<<gender<<" "
<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止):";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile.close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号:";
cin>>findnum;
ifstream infile ("D:\\DEV\\abc.txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
find = true;
}
infile.get ( );
if (infile.peek ()=='\n') break;
}
if(! find)
{cout<<"查无此人"<<endl;
}
infile.close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";    
cin>>findnum;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt", ios::app);
if(! outfile) //打开文件
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl; 
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile.peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt" , "D:\\DEV\\abc.txt" ) ;
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int alteryear;
int altermonth;
int alterday;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的出生日期:";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<score1<<" "<<
score2<<" "<<score3<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int count =0;
int findnum;
int alteryear;
int altermonth;
int alterday;
int newScore1;
int newScore2;
int newScore3;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的成绩:";
cin>>newScore1>>newScore2>>newScore3;
newaverage=(newScore1+newScore2+newScore3)/3.0;
 newmin=min({newScore1,newScore2,newScore3});
 newmax=max({newScore1,newScore2,newScore3});
 cout<<"最高分为:"<<newmax<<endl;
 cout<<"最低分为:"<<newmin<<endl;
 cout<<"最平均分为:"<<newaverage<<endl;
fstream file ("D:\\DEV\\abc.txt");
if(!file)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (!file.eof ())
{
file>>num>>name>>gender>>alteryear>>altermonth>>alterday>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
newScore1<<" "<<newScore2<<" "<<newScore3<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
file.get ();
if (file.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
file.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt");
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
private:
int num;
string  name;
Date birthday; 
int minscore;
int maxscore;
double average;
int newmax;
int newmin;
double newaverage;
char gender;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t\t ***学生信息系统***"<<endl;
cout<<"\t\t ***1.输入学生记录**"<<endl;
cout<<"\t\t ***2.浏览全部学生记录***"<<endl;
cout<<"\t\t ***3.按学号查找学生记录**"<<endl;
cout<<"\t\t ***4.按学号删除学生记录***"<<endl;
cout<<"\t\t ***5.按学号修改学生的出生日期*** "<<endl;
cout<<"\t\t ***6.按学号修改学生的成绩*** "<<endl;
cout<<"\t\t ***7.退出系统***"<<endl;
}
void Select()
{
int num;
Student stu;
Date birthday;
cout<<"请选择功能:";
cin>>num;
switch(num)
{
case 1:
stu.InputStudent (birthday);
Select ();
break;
case 2:
stu.OutputStudent ();
Select ();
break;
case 3:
stu.FindByNum ();
Select ();
break;
case 4: 
stu.DeleteByNum ();
Select ();
break;
case 5:
stu.AlterNameByNum();
Select ();
break;
case 6:
stu.AlterScoreByNum();
Select ();
break;
case 7:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}

img


为什么选择功能2,输出的结果是这样,为什么不是输出平均分,最高分,最低分

  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2024-06-16 10:40
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    在您提供的代码中,确实存在几个问题导致平均分、最高分和最低分没有正确输出:

    1. 写入文件时未包含平均分、最高分和最低分:在InputStudent函数中,写入文件的代码没有包含averagemaxscoreminscore这三个变量。需要在写入文件的语句中添加这三个变量。

    2. 读取文件时使用了错误的变量:在OutputStudent函数中,读取文件时使用了未初始化的局部变量averagemaxscoreminscore,而不是类的成员变量。这导致了输出不正确。

    3. 错误的初始化值5.1281e-308这个值很可能是由于使用了未初始化的局部变量导致的。

    下面是针对这些问题的修改建议:

    修改InputStudent函数

    确保在写入文件时包含averagemaxscoreminscore

    outfile << num << " " << name << " " << gender << " "
            << birthday.GetYear() << " " << birthday.GetMonth() << " " << birthday.GetDay() << " "
            << score1 << " " << score2 << " " << score3 << " "
            << average << " " << maxscore << " " << minscore << endl;
    

    修改OutputStudent函数

    使用类的成员变量来输出平均分、最高分和最低分:

    while (!infile.eof()) {
        infile >> num >> name >> gender >> year >> month >> day >>
               score1 >> score2 >> score3 >> stu.average >> stu.maxscore >> stu.minscore;
        cout << num << " " << name << " " << gender << " " << year << " " << month << " " << day << " "
             << score1 << " " << score2 << " " << score3 << " " 
             << stu.average << " " << stu.maxscore << " " << stu.minscore << endl;
        // ...
    }
    

    请注意,这里假设stu是一个Student类的实例,并且averagemaxscoreminscoreStudent类的成员变量。

    其他注意事项

    • 确保在Student类中,averagemaxscoreminscore被声明为成员变量,并且在类的构造函数中进行适当的初始化。
    • AlterScoreByNum函数中,也需要更新averagemaxscoreminscore的值,并确保它们被写入文件。

    参考资料

    这些修改应该能够解决您遇到的问题。如果您需要进一步的帮助,或者有其他问题,请随时告诉我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月16日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价