簡體   English   中英

在C中做while循環

[英]do while loop in C

我正在使用數組實現多項式。 這是問題陳述:

編寫菜單驅動程序,以使用數組將多項式表示為數據結構。 並編寫函數以將兩個多項式相加,相減和相乘; 用一個常數乘以一個多項式,找出一個多項式是否是“零多項式”,返回該多項式的次數。假設在每次操作之后創建一個新的多項式。您將如何輸入和輸出多項式?

我已經創建了輸入和輸出功能。 但是我的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. Create a new polynomial.
2. Print polynomial.
q. Exit
Enter Choice:1

ch = 1
Enter size of the polynomial:2
Enter terms of the polynomial(coeff then exponent):
2 4
6 7

1. Create a new polynomial.
2. Print polynomial.
q. Exit
Enter Choice:
ch = 

Invalid choice.
1. Create a new polynomial.
2. Print polynomial.
q. Exit
Enter Choice:q

ch = q

嘗試沖洗stdin ...問題仍然存在。 在每個步驟中打印ch的值,我認為它是一個空格。 空白在哪里?


scanf異常行為的答案也回答了這個問題。

做出初始選擇后,還有一個額外的字符要消耗,這就是循環執行兩次的原因。

請參見本questio在comp.lang.c常見問題Ñ

如果您測試下一個代碼,您將注意到相同的問題

int main() {
    char c;

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

    } while (c);
}

當按下enter鍵時,scanf()函數起作用,但是這會在緩沖區輸入中插入另一個char,即新行'\\ n'的char,由於循環塊,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.");
 }

抱歉,英語不是我的母語

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM