C级边界程序,潜在的运行时错误?

时间:2014-11-16 10:34:07

标签: c gcc runtime-error ansi

该计划的目的是确定考试中达到某一等级所需的最低分数。例如。用户将进入TEST 5 10 10 PROJECT 9 10 15,这意味着他们获得了5/10的测试,并且它的价值是他们商标的10%,而9/10项目的价值是他们商标的15%。

我编写了这个程序并在信息中进行了硬编码,但现在我要求用户输入我无法通过for循环中的打印输入的信息。有人可以帮忙吗?我认为运行时间太长,当我离开程序运行大约半小时后,我设法打印出“总计”,但仅此而已。

编辑:here是预期的输出。 here就是我现在所得到的。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define P 50
#define H3 65
#define H2b 70
#define H2a 75
#define H1 80
#define MAX 10

typedef char name_t[MAX];
typedef struct {
    name_t name;
    double mark;
    double outof;
    double weight;
    double total;
} assesment_t;


int main(int argc, char **argv){
    assesment_t Subject[MAX];
    /*---------------------------------------------------------------------------*/    
    printf("\n\nEnter information in this order separated by a space\n");
    printf("When all info entered, press enter\n");
    printf("(Include exam but enter the Mark bit as 0):\n");
    printf("Name    Mark    OutOf   Weight\n\n");
    /*---------------------------------------------------------------------------*/    
    int i, n;
    double total = 0;
    /*---------------------------------------------------------------------------*/    
    for(i = 0;(scanf("%s %lf %lf %lf", &Subject[i].name,&Subject[i].mark,&Subject[i].outof,&Subject[i].weight)) == 4;i++) {

        if(i == 0) {
            printf("Name\tMark\tOut of\tWeight\tTotal");   
        }

        Subject[i].total = ( (Subject[i].mark/Subject[i].outof) * Subject[i].weight );

        printf("\n%s\t%2.1lf\t%2.1lf\t%2.2lf\t%2.2lf", Subject[i].name, Subject[i].mark,
            Subject[i].outof, Subject[i].weight, Subject[i].total);
    }
    /*---------------------------------------------------------------------------*/       
    n = i;
    for( i = 0; i <= n; i++) {
        total += Subject[i].total;
    }
    printf("\nTotal: %3.2lf\n\n", total);
    /*---------------------------------------------------------------------------*/        
    double grade, result;
    for( grade = 23; grade <= 60; grade++) {
        result = (grade / Subject[i-1].outof) * Subject[i-1].weight;
        if(total + result > P && (total + result - 1) < P)
            printf("minimum of %2.0lf is needed for FAIL\n", grade);
        else if(total + result > H3 && (total + result - 1) < H3)
            printf("minimum of %2.0lf is needed for P\n", grade);
        else if(total + result > H2b && (total + result - 1) < H2b)
            printf("minimum of %2.0lf is needed for H3\n", grade);
        else if(total + result > H2a && (total + result - 1) < H2a)
            printf("minimum of %2.0lf is needed for H2b\n", grade);
        else if(total + result > H1 && (total + result - 1) < H1)
            printf("minimum of %2.0lf is needed for H2a\n", grade);
        else if(total + result > H1 && (total + result - 1) < H1)
            printf("minimum of %2.0lf is needed for H1\n", grade);
        else
            ;
    }
    /*---------------------------------------------------------------------------*/
    return 0;
}

1 个答案:

答案 0 :(得分:0)

请查看以下更改,我发现它对我来说运行正常: 在计算总计之后你应该设置i=1并且在for循环中应该有一个中断条件,如if(i>10) break;我做了一些其他更改请运行代码并看看。我看到int有双重算术正在完成可能没有预期的结果。检查以下链接: Can I compare and add a floating-point number to an integer in C?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define P 50
#define H3 65
#define H2b 70
#define H2a 75
#define H1 80
#define MAX 10 

typedef char name_t[MAX];
typedef struct {
    name_t name;
    double mark;
    double outof;
    double weight;
    double total;
} assesment_t;


int main(int argc, char **argv){
    assesment_t Subject[MAX];
       double grade=0, result=0;
    /*---------------------------------------------------------------------------*/    
    printf("\n\nEnter information in this order separated by a space\n");
    printf("When all info entered, press enter\n");
    printf("(Include exam but enter the Mark bit as 0):\n");
    printf("Name    Mark    OutOf   Weight\n\n");
    /*---------------------------------------------------------------------------*/    
    int i, n;
    double total = 0;
    /*---------------------------------------------------------------------------*/    
    for(i = 0;i<10;i++) {
scanf("%s %lf %lf %lf", &Subject[i].name,&Subject[i].mark,&Subject[i].outof,&Subject[i].weight);
        if(i == 0) {
            printf("Name\tMark\tOut of\tWeight\tTotal");   
        }

        Subject[i].total = ( (Subject[i].mark/Subject[i].outof) * Subject[i].weight );

        printf("\n%s\t%2.1lf\t%2.1lf\t%2.2lf\t%2.2lf", Subject[i].name, Subject[i].mark,
            Subject[i].outof, Subject[i].weight, Subject[i].total);
    }
    /*---------------------------------------------------------------------------*/       
    n = i;
    for( i = 0; i < n; i++) {
        total += Subject[i].total;
    }

    printf("\nTotal: %3.2lf\n\n", total);
    /*---------------------------------------------------------------------------*/        
 i= 1;
         while(i<=10){
for( grade = 23; grade <= 60; grade++) {
    result = (grade / Subject[i-1].outof) * Subject[i-1].weight;
    printf("%lf\n",result);
    if((total + result) > P && (total + result - 1) < P)
        printf("minimum of %2.0lf is needed for FAIL\n", grade);
    else if((total + result) > H3 && (total + result - 1) < H3)
        printf("minimum of %2.0lf is needed for P\n", grade);
    else if((total + result) > H2b && (total + result - 1) < H2b)
        printf("minimum of %2.0lf is needed for H3\n", grade);
    else if((total + result) > H2a && (total + result - 1) < H2a)
        printf("minimum of %2.0lf is needed for H2b\n", grade);
    else if((total + result) > H1 && (total + result - 1) < H1)
        printf("minimum of %2.0lf is needed for H2a\n", grade);
    else if((total + result) > H1 && (total + result - 1) < H1)
        printf("minimum of %2.0lf is needed for H1\n", grade);
        else
        printf("None of the above cases pass\n");

}
i++;
 }
    printf("Out of Loop\n");
    /*---------------------------------------------------------------------------*/
    return 0;
}