C++ 如何在没有if/else语句的情况下计算字母等级C++;
在程序中,我应该根据用户的总分分配字母等级。我知道如何使用if/else语句来实现这一点,即:C++ 如何在没有if/else语句的情况下计算字母等级C++;,c++,arrays,if-statement,C++,Arrays,If Statement,在程序中,我应该根据用户的总分分配字母等级。我知道如何使用if/else语句来实现这一点,即: if (score <= 100 && score > 93) cout << "You have received an A"; else if (score <= 93 && score > 89) cout << "You have received an A-"; else if etc.etc.etc
if (score <= 100 && score > 93)
cout << "You have received an A";
else if (score <= 93 && score > 89)
cout << "You have received an A-";
else if etc.etc.etc.
if(分数93)
试试这个
string gradeData[100] = {
"F", // For person not turning up and scoring nothing
"F", // For the person turning up and managing to sit down
"F", // For the person turning up and managing to face the right direction
"F", // For the person turning up and managing to write something in the name box
"F", // For the person turning up and managing to spell their name right
"E", // For the person able to open the question paper
....
"C", // For the person getting 50% of the questions nearly right
...
"A+" // For the swot at the front
};
cout << "Your grade is " << gradeData[score] << endl;
string gradeData[100]={
“F”//表示未出现且未得分的人
“F”//指出现并设法坐下的人
“F”//指出现并设法面对正确方向的人
“F”//用于出现并设法在名称框中写入内容的人
“F”,//用于出现并设法拼写正确姓名的人
“E”,//对于能够打开问题纸的人
....
“C”//对于回答了50%几乎正确的问题的人
...
“A+”//前面的swot
};
这是做这件事的正确方法。。必须首先在特定范围内缩放/规格化等级值。该范围为:1-->等级。长度
。其中grades.length
是字符串数组
现在,由于字符串数组来自A-->F
而不是F-->A
,因此必须通过执行grades.length-scaled\u grade
来反转/翻转坡度
例如,如果等级为100,我们将其缩小到1到12之间。我们会得到11个<代码>等级[11]
为F。等级[Grades.length-11]
为A
下面的代码将演示我的上述解释。。我不能很好地解释事情
#include <iostream>
int scale(int minimum, int maximum, int value, int maxrange = 1, int minrange = 0)
{
return ((maxrange - minrange) * (value - minimum))/(maximum - minimum) + minrange;
}
int main()
{
const std::string grades[] = {"A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
int size = sizeof(grades) / sizeof(grades[0]);
int grade = 0;
std::cout<<"Enter your grade: ";
std::cin>>grade;
std::cin.ignore();
int g = size - scale(0, 100, grade, size, 1);
std::cout<<"Your grade is: "<<grades[g];
}
*
以此类推。只需编写一串if语句。比任何其他可能的(复杂的)解决方案更简单、更容易理解。因此,如果分数为N分,你如何找到相关的字母等级?只有101个值需要考虑:0…100;这不是一个非常大的数组(但它是一个非常无聊的数组)。您需要编写一个算法来计算该数组的索引。因此,如果分数为94-100,则需要得出0(给定字符串数组)。@Ed Heal我更愿意这样做,但我们应该避免使用一系列逻辑语句,尽管它更简单。@JeremyFriesner:在日本,我们实际上有E
作为字母等级,这相当于美国的F
。我认为各国不同时使用E和F,因为将F改为E是多么容易;)@AndonM.Coleman-如果你有E/F,你们两个都会告诉你的雇主吗?这就假设每个等级的分数范围是相同的。是的,但在我看来,这比填写100个等级的数组要好。如果没有这样的假设,您将需要分支/if语句/切换情况等。。
#include <iostream>
int scale(int minimum, int maximum, int value, int maxrange = 1, int minrange = 0)
{
return ((maxrange - minrange) * (value - minimum))/(maximum - minimum) + minrange;
}
int main()
{
const std::string grades[] = {"A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
int size = sizeof(grades) / sizeof(grades[0]);
int grade = 0;
std::cout<<"Enter your grade: ";
std::cin>>grade;
std::cin.ignore();
int g = size - scale(0, 100, grade, size, 1);
std::cout<<"Your grade is: "<<grades[g];
}
stdin is set to 50, it will print C.
stdin is set to 100, it prints A.
stdin is set to 0, it prints F.