【问题标题】:How can I use the for loop's initialization variable as a placeholder in C?如何在 C 中使用 for 循环的初始化变量作为占位符?
【发布时间】:2018-11-05 08:31:57
【问题描述】:

这是我的简单计算器的代码:

#include <stdio.h>

int main(void)
{
    int n1, n2;
    char op;

    do {
        printf("Enter which operation you want to do(+, -, *, /) \n"); op = getch();
    } while(op!='+' && op!='-' && op!='*' && op!='/');

    printf("\n");

    switch(op) {
        case '+':
            printf("You chose to do addition.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i + %i = %i\n", n1, n2, n1+n2);
            break;
        case '-':
            printf("You chose to do subtraction.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i - %i = %i\n", n1, n2, n1-n2);
            break;
        case '*':
            printf("You chose to do multiplication.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i * %i = %i\n", n1, n2, n1*n2);
            break;
        case '/':
            printf("You chose to do division.\n\n");
            float dn1, dn2;
            printf("Number 1: "); scanf("%f", &dn1);
            printf("Number 2: "); scanf("%f", &dn2); printf("\n");
            printf("%f / %f = %f\n", dn1, dn2, dn1/dn2);
            break;
    }
}

如您所见,程序接受用户的输入并据此进行一些计算。它工作得很好,正如我所料,但我只从用户那里获取两个数字。我想让用户能够输入他/她想要的次数。

我想过这样的事情,但对我来说似乎很愚蠢。

for(int i=1; i<10; i++) {
    printf("Enter number %i: ", i); scanf("%i", &{i}n);
}

我尝试使用 for 循环初始化变量来创建用户想要的新变量。

我对@9​​87654326@ 和Python 有一点经验,我记得我可以使用初始化变量作为占位符。

【问题讨论】:

  • 也许你应该使用数组
  • 另外,总是检查scanf 返回的内容。这将允许您处理错误以及能够知道用户何时按下文件结束键序列以结束输入(如果用户想要少于十个输入)。
  • 请注意scanf() leaves the newline in the input buffer。我想你迟早会遇到这个问题,因为你使用getchar() 来获取操作符。
  • 您好 - 也许您打算使用函数指针?这可能是一个允许您通过操作员的解决方案.....我发现的一个可能案例; geeksforgeeks.org/function-pointer-in-c

标签: c for-loop


【解决方案1】:

您可以使用variable length array(自 C99 起可用)并执行以下操作:

首先定义数组:

int Arr[N];

然后在每个 case 语句中读入它:

printf("How many numbers you want to add?");
scanf("%i", &N);

for(int i=0; i<N; i++) { //index goes from 0 to N-1
    printf("Enter number %i: ", i); scanf("%i", &Arr[i]);
    printf("\n");
}

不要忘记检查scanf 的返回值,以确保您 能够正确读取数据。

既然你知道你读入了多少数组元素(基于N的值),你可以相应地执行算术运算。

还要注意getch是非标准函数,请改用标准getchar

【讨论】:

    【解决方案2】:

    一个常见的解决方案是定义一个特殊的op 字符让用户退出(常见的选择是q,代表“退出”),并用无限循环包裹整个代码(while(1) )。当用户输入q 时,我们将跳出该循环。这样用户就可以完全控制重复计算的次数(不限于 10 次或任何其他次数...)。

    以下是您的代码外观示例:

    // ...
    while (1)
    {
        // notice the slight changes:
        do {
            printf("Enter which operation you want to do(+, -, *, /). Enter 'q' to quit.\n");
            op = getch();
        } while(op!='+' && op!='-' && op!='*' && op!='/' && op != 'q');
    
        if(op == 'q')
        {
            printf("Exiting...\n");
            break;
        }
    
        // here comes the original code
    }
    

    编辑:我刚刚意识到我不确定这是否回答了问题中的问题。如果意思是允许添加(或减去等)超过 2 个数字,那么它并不能解决问题(这里有很多答案可以很好地解决这个问题)。

    【讨论】:

    • 我的问题是我是否可以允许用户输入任意数量的数字。您的答案是一种改进,但不是解决方案。感谢您的贡献。
    【解决方案3】:

    正如@Some programmer dude 提到的,您应该使用数组来执行此操作。您可以使用静态或动态数组。静态方法是这样的:

    int arr[ARRAY_LENGTH];
    for (int i = 0, i < ARRAY_LENGHT; i++) {
        scanf("%i", &arr[i]);
    }
    

    但如果你想要匿名输入,解决方案是动态方法。

    int array_length;
    int *arr;
    
    printf("Enter the array lenght: ");
    scanf("%d", &array_length);
    
    arr = (int *)malloc(array_length * sizeof(int));
    
    for (int i = 0; i < array_length; i++) {
        scanf("%i", &arr[i]);
    }
    

    【讨论】:

    • 你必须考虑溢出情况。最后你还需要删除分配的变量。
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多