C++ primer 练习4.22 答案
作者:互联网
#include<iostream>
using namespace std;
#include<string>
class studentGrade //学生类;
{
public:
string finalgrade; //成绩类别;
int grade; //成绩;
studentGrade()
{
finalgrade = "UNKOWN";
grade = 0;
}
studentGrade(int grade1)
{
grade = grade1;
if (rand() % 2 == 0) //以二分之一的概率选用其中一种方式;
{
//三目运算符方式:
finalgrade = grade1 < 60 ? "fail" : (grade1 < 75 ? "low pass" : (grade1 < 90 ? "pass" : "high pass"));
}
else
{
//if语句方式;
if (grade1 < 60)
finalgrade = "fail";
else
{
if (grade1 < 75)
finalgrade = "low pass";
else
{
if (grade < 90)
finalgrade = "pass";
else
finalgrade = "high pass";
}
} //可见相比之下,if语句方式实在太烦;
}
}
~studentGrade()
{
}
};
int main()
{
//测试用例;
studentGrade s1(59), s2(62), s3(76), s4(92);
cout << s1.grade << " " << s1.finalgrade << endl;
cout << s2.grade << " " << s2.finalgrade << endl;
cout << s3.grade << " " << s3.finalgrade << endl;
cout << s4.grade << " " << s4.finalgrade << endl;
}
追逐尾巴的猫
发布了7 篇原创文章 · 获赞 0 · 访问量 24
私信
关注
标签:studentGrade,grade,C++,else,grade1,pass,4.22,primer,finalgrade 来源: https://blog.csdn.net/qq_39631640/article/details/104175525