【问题标题】:do while loop in C在C中做while循环
【发布时间】:2015-01-01 22:50:17
【问题描述】:

我正在使用数组实现多项式。这是问题陈述:

编写一个菜单驱动程序,将多项式表示为使用数组的数据结构。编写函数来加、减和乘两个多项式;将多项式与常数相乘,求多项式是否为“零多项式”,返回多项式的次数。假设每次运算后都会创建一个新多项式。您将如何输入和输出多项式?

我已经创建了输入和输出函数。但是我的 do while 循环运行了两次。帮助我找出原因。

do-while 循环

do{
        print_menu();

        scanf("%c",&ch);
        printf("\nch = %c\n",ch);
    switch(ch){
        case '1':
            create_poly(poly,termpool,&next_poly);
            break;
        case '2':
            print_poly(poly,termpool,&next_poly);
            break;
        case 'q':
            break;
        default:
            printf("Invalid choice.");
    }
}while(ch != 'q');

return 0;

}

print_menu() 函数

void print_menu()
{
    printf("\n1. Create a new polynomial.");
    printf("\n2. Print polynomial.");
    printf("\nq. Exit");
    printf("\nEnter Choice:");
}

create_poly() 函数

void create_poly(int poly[][2], int termpool[][2], int *next_poly)
{
    int beg = poly[*next_poly][0];
    int end, size, i, j;

    printf("Enter size of the polynomial:");
    scanf("%d",&size);
    poly[*next_poly][1] = beg + size - 1;
    end = poly[*next_poly][1];

    printf("Enter terms of the polynomial(coeff then exponent):\n");
    for(i=beg; i<=end; i++){
        for(j=0; j<2; j++){
            scanf("%d ",&termpool[i][j]);
        }
    }

    poly[++(*next_poly)][0] = end + 1;
}

The print_poly()函数

void print_poly(int poly[][2],int termpool[][2],int *next_poly)
{
    int pos,beg,end;
    int i;

    printf("Enter position of the polynomial:");
    scanf("%d",&pos);
    if(pos-1 > *next_poly){
        printf("Invalid position.");
        return;
    }

    beg = poly[pos-1][0];
    end = poly[pos-1][1];
    for(i=beg; i<=end; i++){
        printf(" %dx^%d +",termpool[i][0],termpool[i][1]);
    }
    printf("\b = 0");

}

这是一个示例输出:

1. 创建一个新的多项式。 2. 打印多项式。 问。出口 输入选择:1 ch = 1 输入多项式的大小:2 输入多项式的项(系数然后指数): 2 4 6 7 1. 创建一个新的多项式。 2. 打印多项式。 问。出口 输入选择: ch = 无效的选择。 1. 创建一个新的多项式。 2. 打印多项式。 问。出口 输入选择:q ch = q

尝试刷新stdin... 问题仍然存在。在每一步打印ch的值,我认为它是一个空格。空白从哪里来?


abnormal behavior of scanf 的答案也回答了这个问题。

【问题讨论】:

  • 你应该阅读你正在使用的每个函数的文档,特别是scanf(3)。您应该测试scanf 的结果。您应该使用所有警告和调试信息 (gcc -Wall -Wextra -g) 进行编译。您应该学习如何使用调试器 (gdb)。另外,编辑您的问题以改进它。它缺少Linux 标签!
  • 请把\n放在printf(3)格式字符串的末尾,而不是开头。另见fflush(3)
  • @BasileStarynkevitch:我不同意标签建议。如果它应该是可移植的(没有迹象表明它不应该),那么 linux-tag 将是错误的。
  • 我想你会发现 this question 信息丰富,顺便说一句。
  • 这一行:scanf("%d",&pos);有两个问题 1) 需要检查 scanf() 的返回值以确保实际输入/设置目标参数。 2) scanf() 不会在格式参数中没有特定编码的情况下消耗空白。建议格式参数为:“%d”,以便使用前导空格,包括任何换行符

标签: c loops do-while


【解决方案1】:

在您做出初始选择后,有一个额外的字符等待被消耗,这就是循环执行两次的原因。

请参阅 comp.lang.c 常见问题解答中的 this question

【讨论】:

    【解决方案2】:

    如果你测试下一个代码,你会注意到同样的问题

    int main() {
        char c;
    
        do {
            scanf_s("%c", &c);
            if (c != 'q')
                 printf("test scanf() function\n");
    
        } while (c);
    }
    

    scanf() 函数在按下回车键时起作用,但这会在缓冲区输入中插入另一个字符,即新行 '\n' 的字符,因为循环阻塞,它再次被 scanf() 获取。尝试通过这段代码更改之前的代码:`

    do {
        scanf_s("%c", &c); // or c = getchar();
        switch (c){
        case '\n':
            break;
        default:
            printf("test scanf() function\n");
        }
    
    } while (c);`
    

    并且会正常工作。在您的代码中仅在 switch 块中添加一个新案例:

     switch(ch) {
        case '1':
            create_poly(poly,termpool,&next_poly);
            break;
        case '2':
            print_poly(poly,termpool,&next_poly);
            break;
    
        case '\n':
            break;
    
        case 'q':
            break;
        default:
            printf("Invalid choice.");
     }
    

    对不起,英语不是我的母语

    【讨论】:

    • 不测试 scanf 的返回值总是一个错误。
    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2016-04-21
    • 2022-01-14
    • 1970-01-01
    • 2011-03-20
    • 2014-04-27
    相关资源
    最近更新 更多