如何从一开始就重复交流计划和清洁屏幕和第一输入值?(how to repeat a c progra

2019-10-20 23:12发布

即时通讯在编程新。 我写了一个简单的程序。 我想一再重复的程序,当用户想要退出,只能退出。 这里是我的计划

#include<stdio.h>
#include<conio.h>
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 M型

对于师型d

对于减法S型:一

加入的数量= 20

按任意键退出

我想行前行任意键退出

如果要计算同样按Y

要么

按任意键退出

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

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

Answer 1:

一个包裹里面的代码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”,循环将重复。 如果用户按下其他键,循环结束。



Answer 2:

您还可以使用递归 ,这往往是功能更强大的面向对象编程语言中使用。

伪代码:

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

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

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

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;
    }
}


Answer 3:

我将移动计算的东西在自己的函数,然后用while()main

我试图解决的其他问题,以及(该溶液只使用标准的C函数)。

#include <stdio.h> // puts, printf, fprintf, scanf, getchar, stderr, EOF
#include <stdlib.h> // exit, EXIT_SUCCESS, EXIT_FAILURE
#include <ctype.h> // 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
}


文章来源: how to repeat a c program from the beginning and clean the screen and 1st input values?