-1

所以这次我再次学习一种新的编程语言,它是 C。在为我的主菜单创建这个函数之后,似乎声明的双“equiv”,等效的缩写,没有正确输出分配给它的等效。我评论了它出现错误的部分。

这是该函数的代码:

    // function for grading system
void gradeSys()
{
    char response;
    // declaration for computing midterm grades
    double Mquiz1 = 0, Mquiz2 = 0, Mquiz3 = 0, Mrecit = 0, Massign = 0, MAve = 0, Midex = 0;

    // declaration for computing final grades
    double Fquiz1 = 0, Fquiz2 = 0, Fquiz3 = 0, Frecit = 0, Fassign = 0, FAve = 0, Finex = 0;

    char answer;
    int counter = 1;
    // while loop
    while(counter != 0) {

    /*!!!---Midterm Grades Input---!!!*/
    printf("Welcome to your Grading System.\n");
    printf("Please enter your grades for Midterm:\n");
    printf("M. Quiz1:\n");
    scanf("%d" , &Mquiz1);
    printf("M. Quiz2:\n");
    scanf("%d" , &Mquiz2);
    printf("M. Quiz3:\n");
    scanf("%d" , &Mquiz3);
    printf("M. Recitation\n");
    scanf("%d" , &Mrecit);
    printf("M. Assignment\n");
    scanf("%d" , &Massign);
    printf("M. Exam:\n");
    scanf("%d" , &Midex);
    // formula for computing midterm grades
    double Midgrade = 0;
    MAve = (Mquiz1 + Mquiz2 + Mquiz3 + Mrecit + Massign)/5;
    double MAveTotal = MAve * .60;
    double MidExAve = Midex * .40;
    Midgrade = MAveTotal + MidExAve;

    printf("Your Midterm average is: %d\n", MAve);
    printf("Your Midterm Grade is: %d\n", Midgrade);
    printf("Please press enter to continue...\n");
    getchar();
    getchar();
    system("cls");

    /*!!!---Final Grades Input---!!!*/
    printf("Please enter your grades for Finals:\n");
    printf("F. Quiz1:\n");
    scanf("%d" , &Fquiz1);
    printf("F. Quiz2:\n");
    scanf("%d" , &Fquiz2);
    printf("F. Quiz3:\n");
    scanf("%d" , &Fquiz3);
    printf("F. Recitation\n");
    scanf("%d" , &Frecit);
    printf("F. Assignment\n");
    scanf("%d" , &Fassign);
    printf("F. Exam:\n");
    scanf("%d" , &Finex);
    // formula for computing final grades
    double Fingrade = 0;
    FAve = (Fquiz1 + Fquiz2 + Fquiz3 + Frecit + Fassign)/5;
    double FAveTotal = FAve * .60;
    double FinExAve = Finex * .40;
    Fingrade = FAveTotal + FinExAve;

    printf("Your Final average is: %d\n", FAve);
    printf("Your Final Grade is: %d\n", Fingrade);
    printf("Please press enter to continue...\n");
    getchar();
    getchar();
    /*!!!--- Here is where the bug occurs ---!!!*/
    // declaration and formula for the term grade
    double termGrade = 0, equiv;
    termGrade = (Midgrade + Fingrade)/2;
    // decision formula for determining equivalent
    if(termGrade >= 90 && termGrade <= 100)
    {
         equiv = 4.0;
    }
    else if(termGrade >= 85 && termGrade <= 89)
    {
         equiv = 3.0;
    }
    else if(termGrade >= 75 && termGrade <= 84)
    {
         equiv = 2.0;
    }
    else if(termGrade >= 70 && termGrade <= 74)
    {
         equiv = 1.0;
    }
    else if(termGrade >= 00 && termGrade <= 69)
    {
         equiv = 0.0;
    }
    else
    {
        printf("Invalid grade. Try again.\n");
    }
    // displays the ovarall grade for the term and the equivalent
    printf("Your grade for this term is: %d. Which is equivalent to a: %d.\n", termGrade, equiv);
    getchar();
    system("cls");

    // try again?
        printf("Do you want to try again:[Y/N]\n");
        scanf("%c", &answer);

        if(answer == 'y' || answer == 'Y')
        {
           system("cls");
           continue;            
        }
        else if(answer == 'n' || answer == 'N')
        {
           system("cls");
           counter = 0;
        }
        else
        {
            printf("Invalid input\n");
        }

     }

}
4

1 回答 1

0

%d格式适用于有符号整数。对于浮点数(例如 a double),您应该使用%f. 见http://www.cplusplus.com/reference/cstdio/printf/

或者,您可以将值转换为整数(例如 (int)FAve)并使用%d,如果您只想打印值的整个部分。

于 2013-02-20T06:44:25.450 回答