如何用C语言编写计算GPA的程序?求大神!!!

2025-05-22 06:27:45
推荐回答(1个)
回答(1):

还有人和我写类似的东西2333333
我的程序是刚刚学C没多久写的,其实相当单纯……是每输入一个数字更新一次GPA和学分,最后结束后才进行平均。就是每一步将成绩转换成数值,然后加在GPA(总值,没有平均)上,并加学分值。在结束后进行平均。按题主要求似乎还要在每一步稍微区分一下科目。
代码(简陋,勿嘲笑)
#include
int main(){
FILE* fp;
double gpa[41],l,GPA=0;
int num,i,k,j,credits=0;
fp=fopen("GPA.txt","r");
if(fp==NULL){printf("Caution!Some F*****G bastards may have deleted your file.Download a new copy from Dean.\n");return 1;}
for(i=0;i<41;i++){fscanf(fp,"%d%lf",&k,&l);gpa[k-60]=l;}
printf("Number of courses: ");
while( scanf("%d",&num)!=1||num<=0 ){printf("Don't tease me. I'll feel sad!\n");}
i=0;
while(i printf("Input number of credits: ");
while(scanf("%d", &k)!=1||k<=0 ){printf("Stop playing around!\n");}
printf("Input your scores: ");
while(scanf("%d",&j)!=1||j<=0 ){printf("Stop playing around!\n");}
if(j<60||j>100){ printf("1. You can prepare to meet thy end;2. Prepare the teacher for his/her funeral.\n");continue;}
credits+=k;
GPA+=k*gpa[j-60];
++i;
}
GPA=GPA/credits;
printf("Your POOR little grades: %3f\n",GPA);
getchar();
getchar();
return 0;
}
GPA.txt是个txt文档,本校是每个分数都有对应GPA值的情况,所以有这个必要。楼主直接去掉FILE指针设置个恒定数组就好了。