Grading system in C++(C++中的评分系统)
问题描述
所以这是我的 C++ 问题:
So this is my C++ question :
编写一个程序,将字母等级转换为数字等级.字母等级是 A、B、C、D 和 F,后面可能跟着 + 或 -.它们的数值是 4、3、2、1 和 0.没有 F+ 或 F-.A + 将数值增加 0.3,a - 将数值减少 0.3.但是,A+ 的值为 4.0.
Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D and F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1 and 0. There is no F+ or F-. A + increases the numeric value by 0.3, a – decreases it by 0.3. However, an A+ has value 4.0.
输入字母等级:B
数值为2.7
Enter a letter grade: B
The numeric value is 2.7
这是我的代码:
int main ()
{
char grade;
int value;
cout << "Enter letter grade : " ;
cin >> grade;
switch(grade)
{
case 'A' : value = 4;
break;
case 'B' : value = 3;
break;
case 'C' : value = 2;
break;
case 'D' : value = 1;
break;
case 'E' : value = 0;
break;
default : cout << "Wrong input " << endl;
break;
}
cout << value;
system("PAUSE");
return 0;
}
它可以为 A 打印 4,为 B 打印 3,依此类推.但是,这个问题要求我们也对 + 和 - 进行计算.我应该在切换后使用 if else 语句吗?
It able to print out 4 for A, 3 for B and so on. But however, the question required us to make the calculation for the + and - also. Am I supposed to use if else statement after the switch?
推荐答案
如果输入的是A+
或者B-
,那么就不是字符了.因此,使用一串字符获取它,并检查值.
If the input is A+
or B-
, then it is not a character anymore. So, get it using a string of chars, and check the values.
char a[3];
float m,n;
cin>>a;
if(a[1]=='+')
m=0.3;
else if (a[1]=='-')
m=-0.3;
else
m=0;
switch(a[0])
{
//Assign the value of n as 4,3,2,1 depending upon the grade.
}
cout<<n+m;
将打印成绩
这篇关于C++中的评分系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++中的评分系统


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01