如何从头开始重复ac程序并清理屏幕和第一个输入值?

我是编程新手。 我写了一个简单的程序。 我想一次又一次地重复该程序,它只能在用户想要退出时退出。 这是我的计划

#include #include main() { char ch; int num1, num2, a, m, s, choice; float d; printf("\nEnter The First Number: "); scanf("%d", &num1); printf("\nEnter The Second Number: "); scanf("%d", &num2); a=num1+num2; m=num1*num2; s=num1-num2; d=(float)(num1/num2); printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : "); scanf(" %c", &ch); switch(ch) { case 'A': printf("\nThe Addition Of The Number Is= %d", a); break; case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m); break; case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s); break; case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d); break; default : printf("\nInvalid Entry"); break; } printf("\nPress Any Key To Exit"); getch(); return 0; } 

这是输出

输入第一个数字:10

输入第二个数字:10

输入您的选择

对于加成类型A.

对于Multipication Type M

对于D类分类

对于S型减法:A

数字的加法是= 20

按任意键退出

我想要一行之前按任意键退出

如果你想再次计算,请按Y.

要么

按任意键退出

当按Y时 ,程序应该从头开始。

我怎样才能做到这一点???

将代码包装在do{} while()

 char answer; do{ printf("\nEnter The First Number: "); scanf("%d", &num1); printf("\nEnter The Second Number: "); scanf("%d", &num2); a=num1+num2; m=num1*num2; s=num1-num2; d=(float)(num1/num2); printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : "); scanf(" %c", &ch); switch(ch) { case 'A': printf("\nThe Addition Of The Number Is= %d", a); break; case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m); break; case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s); break; case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d); break; default : printf("\nInvalid Entry"); break; } printf("\nPress Y to continue. Press any Key To Exit"); scanf(" %c",&answer); // dont forget type & } while(answer == 'y' || answer == 'Y'); 

声明一个变量,让我们说回答,当你要求“按Y继续时按下任何键退出”时,它将存储用户答案。 检查该变量的值。 如果是’y’或’Y’,循环将重复。 如果用户按下其他键,则循环结束。

您还可以使用递归 ,它通常用于更多面向函数的编程语言。

伪代码:

 myfunction = do ... b <- somethingtodo ... if b then myfunction else return () 

相对于Jens的解决方案 ,它看起来像:

 #include  #include  #include  int main (void) { char choice; int num1, num2, cont; printf("Enter the first number: "); scanf("%d", &num1); getchar (); printf("Enter the second number: "); scanf("%d", &num2); getchar (); printf("A - addition\n"); printf("S - subtraction\n"); printf("M - multiplication\n"); printf("D - division\n"); printf("[ASMD]? "); choice = (char)toupper(getchar()); getchar (); printf("\n"); switch(choice) { case 'A': printf("%d + %d = %d", num1, num2, num1 + num2); break; case 'S': printf("%d - %d = %d", num1, num2, num1 - num2); break; case 'M': printf("%d * %d = %d", num1, num2, num1 * num2); break; case 'D': if (num2 == 0) fprintf(stderr, "The divisor can not be zero"); else { printf("%d / %d = %f", num1, num2, (double)num1 / num2); } break; default : fprintf(stderr, "Invalid entry"); break; } printf("\n"); for (;;) { printf("Continue [YN]? "); cont = toupper(getchar()); getchar (); if (cont == 'Y') return main(); // the difference. else if (cont == 'N') return EXIT_SUCCESS; } } 

我会将计算内容移动到它自己的函数中,然后在main使用while()

我也尝试解决其他问题(此解决方案仅使用标准C函数)。

 #include  // puts, printf, fprintf, scanf, getchar, stderr, EOF #include  // exit, EXIT_SUCCESS, EXIT_FAILURE #include  // toupper char fail_on_eof (int c) { if (c == EOF) exit (EXIT_FAILURE); // In case of fail_on_eof (scanf (...)) the return value of this this // function is not useful // scanf () returns the number of chars read or EOF // getchar () returns a char or EOF return (char) c; } void skip_to_next_line (void) { char c; do { c = fail_on_eof (getchar ()); } while (c != '\n'); } char read_upcase_char_line (char* prompt) { char c; printf ("%s ", prompt); c = fail_on_eof (toupper (getchar ())); skip_to_next_line (); return c; } int read_num_line (char* prompt) { int num; printf ("%s ", prompt); fail_on_eof (scanf ("%d", &num)); skip_to_next_line (); return num; } int calculate (void) { char choice; int num1, num2, cont; num1 = read_num_line ("Enter the first number:"); num2 = read_num_line ("Enter the second number:"); puts("A - addition"); puts("S - subtraction"); puts("M - multiplication"); puts("D - division"); choice = read_upcase_char_line ("[ASMD]?"); puts(""); switch(choice) { case 'A': printf("%d + %d = %d", num1, num2, num1 + num2); break; case 'S': printf("%d - %d = %d", num1, num2, num1 - num2); break; case 'M': printf("%d * %d = %d", num1, num2, num1 * num2); break; case 'D': if (num2 == 0) // Better use stderr for error messages fprintf(stderr, "The divisor can not be zero"); else { printf("%d / %d = %f", num1, num2, (double)num1 / num2); } break; default : // Better use stderr for error messages fprintf(stderr, "Invalid entry"); break; } printf("\n"); for (;;) { cont = read_upcase_char_line ("Continue [YN]?"); if (cont == 'Y') return -1; else if (cont == 'N') return 0; } } int main(void) { while (calculate ()); return EXIT_SUCCESS; // Use this constant to avoid platform specific issues with the return code }