Advertisement
FoxTuGa

Calculator - <Old>

May 8th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double num1, num2, total;
  4. char c;
  5.  
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.     printf("\n Introduza dois numeros inteiro e um caracter:  ");
  12.     scanf("%d%c%d", &num1, &c, &num2);
  13.  
  14.     switch( c )
  15.     {
  16.     case '+': total = num1 + num2; break;
  17.     case '-': total = num1 - num2; break;
  18.     case '/': total = num1 / num2; break;
  19.     case '*': total = num1 * num2; break;
  20.     }
  21.     printf("\n\n Total = %.2lf\n\n", total);
  22.  
  23.  
  24.     return 0;
  25. }
  26.  
  27. // Or
  28.  
  29. #include <stdio.h>
  30.  
  31. double num1, num2, total;
  32. char c;
  33.  
  34.  
  35.  
  36.  
  37. int main()
  38. {
  39.     printf("\n Introduza dois numeros inteiro e um caracter:  ");
  40.     scanf("%lf%c%lf", &num1, &c, &num2);
  41.  
  42.     if(num1 < 0 || num2 < 0 )
  43.         printf("\n ERRO \n\n");
  44.  
  45.     if( c != '+' && c != '-' && c != '/' && c != '*' )
  46.         printf("\n ERRO \n\n");
  47.  
  48.     if( c == '+' )
  49.     {
  50.         total = num1 + num2;
  51.     }
  52.     if( c == '-' )
  53.     {
  54.         total = num1 - num2;
  55.     }
  56.     if( c == '/')
  57.     {
  58.         if( num2 == 0 )
  59.             printf("\n\t ERRO \n\n");
  60.         else
  61.         total = num1 / num2;
  62.     }
  63.  
  64.     if( c == '*' )
  65.     {
  66.         total = num1 * num2;
  67.     }
  68.  
  69.     printf("\n\n Total = %.2lf\n\n", total);
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement