C/Console/Console Menu

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Console menu: calculation

  
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
  char command[80], temp[80];
  int i, j;
  for( ; ; ) {
    printf("Operation: add, subtract, divide and multiply? \n");
    
    printf("Enter the whole word \n");
    gets(command);
    /* if user wants to stop */
    if(!strcmp(command, "quit")) 
        break;
    printf("Enter the first number: ");
    gets(temp);
    i = atoi(temp);
    printf("Enter the second number: ");
    gets(temp);
    j = atoi(temp);
    /*  now, perform the operation */
    if(!strcmp(command, "add"))
      printf("%d\n", i + j);
    else if(!strcmp(command, "subtract")) 
      printf("%d\n", i - j);
    else if(!strcmp(command, "divide")) {
      if(j)/* non - zero*/ 
          printf("%d\n", i / j);
    }
    else if(!strcmp(command, "multiply"))
      printf("%d\n", i * j);
    else 
      printf("Unknown command. \n");
  }
  return 0;
}