C++ Primer(第5版) 练习 5.6
练习 5.6 改写上一题的程序,使用条件运算符(参见4.7节,第134页)代替if else语句。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex5.6.cpp
> Author:
> Mail:
> Created Time: Sun 11 Feb 2024 10:38:22 AM CST
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<string> scores = {"E", "D", "C", "B", "A", "A++"};
string lettergrade;
int grade;
cout<<"Enter Grade: ";
cin>>grade;
lettergrade = (grade < 60) ? scores[0] : scores[(grade - 50) / 10];
if(grade != 100 && grade >= 60){
(grade % 10 > 7) ? lettergrade += '+' : (grade % 10 < 3) ? lettergrade += '-' :
}
cout<<"Grade: "<<lettergrade<<endl;
return 0;
}