C累积成绩点平均计算器

时间:2014-03-27 00:54:52

标签: c

我正在研究GPA计算器,我是C的新手。我在Ubuntu 12中使用gvim编写此代码,并且我正在终端中使用gcc进行编译。

到目前为止,这是我的代码。我还想包括一种检查方法,以确保用户不在函数number_subjects中输入字符(a-z),但不确定正确的方法。我也相信在主函数结束时我的gpa计算公式有问题。我还添加了一个do while语句来提示用户是否要在程序结束时再次尝试但由于某种原因,程序总是重新启动而不从用户那里获取输入。

#include<stdio.h>
#include<string.h>

int number_subjects(int num_sub);

int main(void) {

int class_grades[10];
int i;
int credit_hrs[10];
int num_sub;
int totalCreditHour = 0;
double sum_gpaxcredit_hrs = 0;
double grade_point[10];
double gpa;
char subject[10][10];
char grade[10][10];
char option, y_n;

do{

/ *主菜单* /

printf("\t*** GPA CALCULATOR ***\n\n");
printf("Please choose and option\n");
printf("[a] Calculate GPA\n[q] Quit\n");
scanf("%c", &option);

switch(option){

/ *退出程序* /

case 'q':
return(0);
break;

/ *对象数量的函数调用* /

case 'a':
num_sub = number_subjects(num_sub);         
break;
default:
printf("Not a valid choice\n");
break;
}

/ *要求用户为每个班级命名和输入成绩* /

for(i = 0; i <= num_sub -1; i++) {  
    printf("\n Class %d \n", i +1);
    printf("\nClass name : ");
    scanf("%s", subject[i]);
    printf("Enter grade: ");
    scanf("%d", &class_grades[i]);
    printf("Enter credit hours: ");
    scanf("%d", &credit_hrs[i]);

/ *成绩转换* /

if(class_grades[i] >= 95 && class_grades[i] <=100)
    {
        grade_point[i] = 4.00;
        strcpy(grade[i], "A+");
    }
    else if(class_grades[i] >= 90 && class_grades[i] <=94)
    {
        grade_point[i] = 4.00;
        strcpy(grade[i], "A");
    }
    else if(class_grades[i] >= 85 && class_grades[i] <= 89)
    {
        grade_point[i] = 3.33;
        strcpy(grade[i], "B+");
    }
    else if(class_grades[i] >= 80 && class_grades[i] <= 84)
    {
        grade_point[i] = 3.00;
        strcpy(grade[i], "B");
    }
    else if(class_grades[i] >= 75 && class_grades[i] <= 79)
    {
        grade_point[i] = 2.33; 
        strcpy(grade[i], "C+");     
    }
    else if(class_grades[i] >= 70 && class_grades[i] <= 74)
    {   
        grade_point[i] = 2.00;
        strcpy(grade[i], "C");
    }   
    else if(class_grades[i] >= 60 && class_grades[i] <= 69)
    {
        grade_point[i] = 1.00;
        strcpy(grade[i], "D");
    }
    else if(class_grades[i] >= 0 && class_grades[i] <= 59)
    {   
        grade_point[i] = 0.0;
        strcpy(grade[i], "F");
    }
}

/ *用于计算GPA的公式* /

for(i = 0; i <= num_sub -1; i++) {

    sum_gpaxcredit_hrs = grade_point[i] * credit_hrs[i];
    gpa = sum_gpaxcredit_hrs / credit_hrs[i];
} 

/ *将所有课程信息显示回用户* /

for(i = 0; i <= num_sub -1; i++) {

    printf("\n%d\t%s\t\t%d\t %.2f\t\t%s\t\n", i +1, subject[i],class_grades[i], grade_point[i], grade[i]);
}

/ *打印出GPA * /

printf("\n\n GPA is %.2f\n\n\n", gpa);

printf("Would you like to try again?\n");
printf("[y] yes\n[n] no\n");
scanf("%c", &y_n);
}while(y_n ='n');
    printf("Goodbye!\n");

return(0);

}

/ *用户输入班级数* /

int number_subjects(int num_sub){   

do {
    printf("Please enter the number of classes you are taking [Max 10] \n");        
    scanf("%d", &num_sub);

        if((num_sub >10) || (num_sub < 1))
            printf("**Please enter number between 1 and 10**\n");

}while((num_sub >10) || (num_sub <1));

return(num_sub);
}

1 个答案:

答案 0 :(得分:1)

数组的名称本身就是一个指针(形式短语是数组的名称衰变成指向数组第一个元素的指针)。所以你不要在那里使用&

而不是

    scanf("%s", &subject[i]);
你应该

    scanf("%s", subject[i]);

修改:刚看到你有两个错误,而不是一个错误。第二个是因为您的grade_point是单个值而不是向量。将其声明为double grade_point[10](请参阅[10]部分)。

相关问题