我试图做到这一点:
Write a program that accepts three user inputs: assignment mark, midterm mark, and final exam mark.
这些投入为类型浮动,数值为0至100。
The program should compute the final grade as a percentage and a letter grade.
• Final grades are computed as: Assignments 20%, Midterm 30%, Final exam 50%. Letter grades are computed as: A (final grade ≥ 80%), B (80 > final grade ≥ 70%), C (70 > final grade ≥ 60%), D (60 > final grade ≥ 50%), and F (50 > final grade).
The final grade and letter grade are displayed.
加入:
#include <stdio.h>
int checkMark(int);
float getMark();
float computeFinalGrade(float assign, float midterm, float finalExam, float finalGrade);
int computeLetterGrade(float);
int checkMark(int x)
{
while(x<0 || x>100) //While x is a negative number:
{
printf("Invalid entry.Try again:");
scanf("%d",&x);
}
if(x>0 && x<=100)//If x is a positive number:
x=x;
return x;
}
float getMark(void)
{
float mark=0,assignMark=0,midMark=0,examMark=0,finalMark=0;
char letterGrade;
printf("Enter assignment grade:");
scanf("%d",mark);
checkMark(mark);
assignMark=mark;
printf("
Enter midterm mark:");
scanf("%f",&mark);
checkMark(mark);
midMark=mark;
printf("
Enter exam mark:");
scanf("%f",&mark);
checkMark(mark);
examMark=mark;
finalMark=computeFinalGrade(assignMark,midMark,examMark,finalMark);
letterGrade=computeLetterGrade(finalMark);
printf("The final grade for the course is %.1f, and the letter grade is %c.
",finalMark,letterGrade);
return 0;
}
float computeFinalGrade(float assign, float midterm, float finalExam, float finalGrade)
{
assign=(float)0.2*assign;
midterm=(float)0.3*midterm;
finalExam=(float)0.4*finalExam;
finalGrade=(float)assign+midterm+finalExam;
return finalGrade;
}
int computeLetterGrade(float finalGrade)
{
int grade;
if(finalGrade>=80)
grade=65;
if(finalGrade<80)
if(finalGrade>=70)
grade=66;
if(finalGrade<70)
if(finalGrade>=60)
grade=67;
if(finalGrade<60)
if(finalGrade>=50)
grade=68;
if(finalGrade<50)
grade=70;
return grade;
}
int main()
{
getMark();
return 0;
}
当我推翻上述方案并进入一个负数时,我会这样做:
Enter assignment grade:123
Segmentation fault (core dumped)
我究竟做了什么错误呢?