累计平均学分绩点计算器编写的C [英] Cumulative Grade Point Average Calculator written C

查看:256
本文介绍了累计平均学分绩点计算器编写的C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个GPA的计算器,我是新来C.我写在Ubuntu 12用gvim这code,我在终端使用gcc编译。

I'm working on a GPA calculator and I am new to C. I'm writing this code using gvim in Ubuntu 12 and I'm compiling using gcc in the terminal.

这是我的code为止。我也想有一个方法来检查,以确保用户不会在功能number_subjects输入字符(A-Z),但不能确定的正确的方式来做到这一点。我也相信什么是错与主函数结束我的GPA的计算公式。我还添加了一个do while语句来提示用户,如果他们想在节目的最后,但出于某种原因,程序会重新启动不考虑输入表单的用户再次尝试。

This is my code so far. I also wanted to include a way to check to make sure the user doesn't enter a character (a-z) in the function number_subjects but wasn't sure of the proper way to do that. I also believe something is wrong with my gpa calculation formula at the end of the main function. I've also added a do while statement to prompt the user if they want to try again at the end of the program but for some reason the program always restarts without taking an input form the user.

#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{

/ *主菜单* /

/*Main Menu */

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){

/ *退出程序* /

/* Quits Program */

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

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

/* Function call for number of subjects */

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

/ *询问用户名和输入等级为每个类* /

/* Asks user to name and input grades for each class */

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]);

/ *转换为等级* /

/* Conversion for grades */

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");
    }
}

/ *公式来calulate GPA * /

/* Formula to calulate 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];
} 

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

/* Displays all course information back to user */

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 * /

/* Prints out 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);

}

/ *类的用户输入号码* /

/* User inputs number of classes */

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);
}


推荐答案

阵列的名称本身是一个指针(正式短语是阵列的名称衰变到一个指针数组的第一元素)。所以你不使用&安培; 在那里:

The name of the array is a pointer in itself (the formal phrase is the name of the array decays into a pointer to the first element of the array). So you don't use & in there:

而不是

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

你应该有

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

修改:刚才看到你有两个错误,没有之一。第二个是因为你的 grade_point 是一个单值,而不是载体。声明为双grade_point [10] (请参阅 [10] 部分)。

Edit: Just saw you have two errors, not one. The second one is because your grade_point is a single value instead of a vector. Declare it as double grade_point[10] (see the [10] part).

这篇关于累计平均学分绩点计算器编写的C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆