加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 这些被认为是魔术数字吗?

发布时间:2020-12-16 06:48:05 所属栏目:百科 来源:网络整理
导读:我刚刚完成了为编程课程编写程序,我想避免使用魔术数字,所以这是我的问题: 在下面的函数中,我的数组索引器是否会被视为幻数? 码: string CalcGrade(int s1,int s2,int s3,double median){const int SIZE = 23;const int LETTER_GRADE_BARRIERS[SIZE] = {
我刚刚完成了为编程课程编写程序,我想避免使用魔术数字,所以这是我的问题:

在下面的函数中,我的数组索引器是否会被视为幻数?

码:

string CalcGrade(int s1,int s2,int s3,double median)
{
const int SIZE = 23;
const int LETTER_GRADE_BARRIERS[SIZE] = { 400,381,380,361,360,341,340,321,320,301,300,281,280,261,260,241,240,221,220,201,200,181,180 }; 
double finalGrade;
string letterGrade;

finalGrade = s1 + s2 + s3 + median;

if (finalGrade >= LETTER_GRADE_BARRIERS[1] && finalGrade <= LETTER_GRADE_BARRIERS[0])
{
    letterGrade = "A";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[3] && finalGrade <= LETTER_GRADE_BARRIERS[2])
{
    letterGrade = "A-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[5] && finalGrade <= LETTER_GRADE_BARRIERS[4])
{
    letterGrade = "B+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[7] && finalGrade <= LETTER_GRADE_BARRIERS[6])
{
    letterGrade = "B";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[9] && finalGrade <= LETTER_GRADE_BARRIERS[8])
{
    letterGrade = "B-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[11] && finalGrade <= LETTER_GRADE_BARRIERS[10])
{
    letterGrade = "C+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[13] && finalGrade <= LETTER_GRADE_BARRIERS[12])
{
    letterGrade = "C";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[15] && finalGrade <= LETTER_GRADE_BARRIERS[14])
{
    letterGrade = "C-";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[17] && finalGrade <= LETTER_GRADE_BARRIERS[16])
{
    letterGrade = "D+";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[19] && finalGrade <= LETTER_GRADE_BARRIERS[18])
{
    letterGrade = "D";
}
else if (finalGrade >= LETTER_GRADE_BARRIERS[21] && finalGrade <= LETTER_GRADE_BARRIERS[20])
{
    letterGrade = "D-";
}
else if (finalGrade <= LETTER_GRADE_BARRIERS[22])
{
    letterGrade = "Fail";
}

return letterGrade;
}

谢谢!

解决方法

是的,除了-1,0或1之外的任何数字都可能是一个神奇的数字.

除非你是一个真正的大师,否则你可能也被允许自由使用两个权力:-)

顺便说一句,你可能可以重构那些代码,使其更容易理解,例如:

string CalcGrade (int s1,double median) {
    // Grade lookup arrays. If grade is >= limit[n],string is grades[n].
    // Anything below D- is a fail.
    static const int Limits[] = {400,180 }; 
    static const int Grades[] = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","D-"};

    double finalGrade = s1 + s2 + s3 + median;

    // Check each element of the array and,if the final grade is greater
    //   than or equal to,return the grade string.
    for (int i = 0; i < sizeof(Limits) / sizeof(*Limits); i++)
        if (finalGrade >= Limits[i])
            return Grades[i];

    // Otherwise,failed.
    return "Fail";
}

这将删除遍布代码的魔术数字到一个区域,在这个区域中,它们的工作方式很明显(假设您很好地对齐它们).

它还消除了原始解决方案的一个问题,即我们对得分为380.5的人做了什么 – 失败那些人并不公平:-)或者为400以上的人分配一个等级(因为那里没有)看起来似乎是回归“A”的一种方式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读