gpt4 book ai didi

c - 在 C 中执行 while 循环

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:52 25 4
gpt4 key购买 nike

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

Write a menu-driven program to represent Polynomials as a data structure using arrays. and write functions to add, subtract and multiply two polynomials; multiply a polynomial with a constant, find whether a polynomial is a "zero- polynomial, return the degree of the polynomial. Assume that a new polynomial is created after each operation. How would you input and output polynomials?

我已经创建了输入和输出函数。但是我的 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;
}

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. ExitEnter Choice:1ch = 1Enter size of the polynomial:2Enter terms of the polynomial(coeff then exponent):2 46 71. Create a new polynomial.2. Print polynomial.q. ExitEnter Choice:ch = Invalid choice.1. Create a new polynomial.2. Print polynomial.q. ExitEnter Choice:qch = q

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


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

最佳答案

如果你测试下一段代码,你会发现同样的问题

int main() {
char c;

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

} while (c);
}

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

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

} while (c);`

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

 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.");
}

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

关于c - 在 C 中执行 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com