#include<iostream>
using namespace std;
#include<vector>
#include<string>
vector<string> scores{ "F","D","C","B","A","A++" };
struct student
{
int grade = 0;
string lettergrade;
student(int _grade)
{
grade = _grade;
//求字母成绩;
if (rand() % 2 == 0) //以1/2的概率选择if语句或条件运算符;
//使用if语句
{
if (_grade < 60)
lettergrade = scores[0];
else
{
lettergrade = scores[(_grade - 50) / 10];
if (_grade % 10 < 4 && _grade != 100)
{
lettergrade += "-";
}
else if (_grade % 10 >= 8)
{
lettergrade += "+";
}
}
}
//使用三目运算符
else
{
_grade < 60 ? lettergrade = scores[0] : lettergrade = scores[(_grade - 50) / 10], //求字母成绩;
((_grade % 10 < 4 && _grade != 100 )? lettergrade += '-' : (_grade % 10 >= 8) ? lettergrade += '+' : lettergrade); //添加加减号;
}
}
};
int main()
{
vector<student> Student = { 56,63 ,78,82,95,100 }; //测试用例;使用向量方便输出;
//输出结果
for (auto au : Student)
{
cout << au.lettergrade << endl;
}
}
C++ primer 练习5.5 & 5.6 答案
最新推荐文章于 2024-08-20 16:47:30 发布