2个回答
展开全部
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define EXCELLENT 90
#define GOOD 80
#define MODERATE 60
#define PASS 60
struct GradesInfo{
int Total()
void input();
string studentNumber;
string name;
int score1;
int score2;
} grades[5];
void GradesInfo::input()
{
cout << "请输入学号: ";
cin >> studentNumber;
cout << "请输入姓名: ";
cin >> name;
cout << "请输入成绩: ";
cin >> score1;
cout << "请输入成绩2: ";
cin >> score2;
cout <<endl;
}
void save(){
ofstream of;
of.open("record.txt",ofstream::out | ofstream::app);
for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << endl;
}
of.close();
}
void sort()
{
for(int i(0);i<5-1;i++)
for(int j(i+1);j<5;j++)
{
if( grades[i].Total() < grades[j].Total())
{
GradesInfo gi = grades[i];
grades[i] = grades[j];
grades[j] = gi;
}
}
}
void saveSorted(){
ofstream of;
of.open("sort.txt",ofstream::out | ofstream::app);
for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << grades[i].Total() << endl;
of << endl;
}
of.close();
}
void input(){
for(int i(0);i<5;i++)
{
cout << "请输入第"<<(1+i)<<"个学生"<<endl;
grades[i].input();
}
save();
sort();
saveSorted();
}
void displayAverage(){
double avg(0.0);
for(int i(0);i<5;i++)
avg += grades[i].Total();
avg /= 5;
cout << endl<< "平均成绩: " << avg << endl<< endl;
}
void displayFailRate(){
int f(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() < PASS * 2)
f++;
}
double fr = 100*f / 5;
cout << endl<< "不及格比例: " << fr <<"%"<< endl<< endl;
}
void displayPassRate(){
int p(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= PASS * 2)
p++;
}
double pr = 100.0*p / 5;
cout << endl<< "及格比例: " << pr <<"%"<< endl<< endl;
}
void displayGradeRate(){
int e(0);
int g(0);
int m(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= EXCELLENT * 2)
e++;
else if(grades[i].Total() >= GOOD * 2)
g++;
else if(grades[i].Total() >= MODERATE * 2)
m++;
}
double er = 100.0*e / 5;
double gr = 100.0*g / 5;
double mr = 100.0*m / 5;
cout << endl;
cout << "优比例: " << er <<"%"<< endl;
cout << "良比例: " << gr <<"%"<< endl;
cout << "中比例: " << mr <<"%"<< endl<< endl;
}
int menu() {
int opt(0);
cout << "=====================" << endl;
cout << "1. 重新输入成绩" << endl;
cout << "2. 平均成绩" << endl;
cout << "3. 不及格比例" << endl;
cout << "4. 及格比例" << endl;
cout << "5. 优良中所占比例" << endl;
cout << "0. 退出" << endl;
cout << "=====================" << endl;
cin >> opt;
return opt;
}
int main()
{
int opt(1);
do
{
system("cls");
switch(opt) {
case 1: input(); break;
case 2: displayAverage(); break;
case 3: displayFailRate(); break;
case 4: displayPassRate(); break;
case 5: displayGradeRate(); break;
}
opt = menu();
} while(opt);
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define EXCELLENT 90
#define GOOD 80
#define MODERATE 60
#define PASS 60
struct GradesInfo{
int Total()
void input();
string studentNumber;
string name;
int score1;
int score2;
} grades[5];
void GradesInfo::input()
{
cout << "请输入学号: ";
cin >> studentNumber;
cout << "请输入姓名: ";
cin >> name;
cout << "请输入成绩: ";
cin >> score1;
cout << "请输入成绩2: ";
cin >> score2;
cout <<endl;
}
void save(){
ofstream of;
of.open("record.txt",ofstream::out | ofstream::app);
for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << endl;
}
of.close();
}
void sort()
{
for(int i(0);i<5-1;i++)
for(int j(i+1);j<5;j++)
{
if( grades[i].Total() < grades[j].Total())
{
GradesInfo gi = grades[i];
grades[i] = grades[j];
grades[j] = gi;
}
}
}
void saveSorted(){
ofstream of;
of.open("sort.txt",ofstream::out | ofstream::app);
for(int i(0);i<5;i++)
{
of << grades[i].studentNumber << endl;
of << grades[i].name << endl;
of << grades[i].score1 << endl;
of << grades[i].score2 << endl;
of << grades[i].Total() << endl;
of << endl;
}
of.close();
}
void input(){
for(int i(0);i<5;i++)
{
cout << "请输入第"<<(1+i)<<"个学生"<<endl;
grades[i].input();
}
save();
sort();
saveSorted();
}
void displayAverage(){
double avg(0.0);
for(int i(0);i<5;i++)
avg += grades[i].Total();
avg /= 5;
cout << endl<< "平均成绩: " << avg << endl<< endl;
}
void displayFailRate(){
int f(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() < PASS * 2)
f++;
}
double fr = 100*f / 5;
cout << endl<< "不及格比例: " << fr <<"%"<< endl<< endl;
}
void displayPassRate(){
int p(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= PASS * 2)
p++;
}
double pr = 100.0*p / 5;
cout << endl<< "及格比例: " << pr <<"%"<< endl<< endl;
}
void displayGradeRate(){
int e(0);
int g(0);
int m(0);
for(int i(0);i<5;i++)
{
if(grades[i].Total() >= EXCELLENT * 2)
e++;
else if(grades[i].Total() >= GOOD * 2)
g++;
else if(grades[i].Total() >= MODERATE * 2)
m++;
}
double er = 100.0*e / 5;
double gr = 100.0*g / 5;
double mr = 100.0*m / 5;
cout << endl;
cout << "优比例: " << er <<"%"<< endl;
cout << "良比例: " << gr <<"%"<< endl;
cout << "中比例: " << mr <<"%"<< endl<< endl;
}
int menu() {
int opt(0);
cout << "=====================" << endl;
cout << "1. 重新输入成绩" << endl;
cout << "2. 平均成绩" << endl;
cout << "3. 不及格比例" << endl;
cout << "4. 及格比例" << endl;
cout << "5. 优良中所占比例" << endl;
cout << "0. 退出" << endl;
cout << "=====================" << endl;
cin >> opt;
return opt;
}
int main()
{
int opt(1);
do
{
system("cls");
switch(opt) {
case 1: input(); break;
case 2: displayAverage(); break;
case 3: displayFailRate(); break;
case 4: displayPassRate(); break;
case 5: displayGradeRate(); break;
}
opt = menu();
} while(opt);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |