运行时使用了太多内存的错误?

时间:2012-05-30 20:37:38

标签: c

#include <stdio.h>

int main()
{
    int X = 200;
    float Y = 1500;
    printf("Enter your initial Balance and the Amount to be Withdrawn. Note the Values should lie between 0 and 2000");
    scanf("%d", "%e", &X, &Y);
    if ((0 < X < 2000) && (0 < Y < 2000)) {
        if ((X < Y) && (X % 5 == 0)) {
            Y = Y - X;
            Y = Y - 0.5;
        } else {
            printf("%f", Y);
        }
        printf("%f", Y);
    } else {
        printf("The Input is Wrong");
    }
    return 0;
}

守则基本上要求一些数字X.从Y中减去它,并从Y中减去0.5.我们必须给出Y作为Y. 该代码正在给出运行时错误,这可能是由于更多的Memeory而不是允许使用。 任何人都可以提供有关如何减少内存使用量或查看程序中是否有错误的提示吗?

3 个答案:

答案 0 :(得分:2)

     scanf("%d,%e", &X, &Y);

答案 1 :(得分:1)

(除了带有双格式字符串的scanf(),已由其他人处理)

if ((0 < X < 2000) && (0 < Y < 2000)) {

这种方式不起作用是C.你可以尝试:

if (X > 0 && X < 2000 && Y > 0 && Y < 2000) {

另请注意,您不需要额外的括号。 另一行也是一样

if ((X < Y) && (X % 5 == 0)) {

可能是:

if (X < Y && X % 5 == 0) {

有时,优先规则根本就不差......

答案 2 :(得分:0)

我不知道这是否是作业......

...但是这个例子可能有助于澄清一些事情:

#include <stdio.h>

int
main(int args, char *argv[])
{
    int new_balance, old_balance;
    float withdrawal;

    /* Get input */
    printf("Enter your initial Balance and the Amount to be Withdrawn.\n");
    printf("Note the values should lie between 0 and 2000\n");
    while (scanf("%d %f", &old_balance, &withdrawal) != 2) {
      printf ("please enter two valid floating point numbers\n");
    }

    /* Validate input */
    if ( (old_balance < 0.0) || (old_balance > 2000.0) ) {
      printf ("error: balance(%d): must be between 0.0 and 2000.0\n",
        old_balance);
      return 1;
    }
    if ( (withdrawal < 0.0) || (withdrawal > 2000.0) ) {
      printf ("error: withdrawal(%f): must be between 0.0 and 2000.0\n",
        withdrawal);
      return 1;
    }

    /* Compute balance */
    new_balance = old_balance - withdrawal;

    /* Print results */
    printf ("Withdrawal: %f; old balance: %d, new balance: %d.\n",
      withdrawal, old_balance, new_balance);

    return 0;
}

我完全不确定“0.5”的要求是什么,所以我把它留了出来。我的猜测是你想“四舍五入到最接近的美元”。在这种情况下,“%”肯定是不是的方式。

原始程序可能已编译 - 但几乎肯定不是“正确”。

据我所知,最初的程序应该在任何地方运行 - 我没有看到任何可能导致“内存不足”的情况。

'希望有帮助......至少有一点......