Advertisement
andreafiori

Calculator

Sep 14th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. float pot (float a, float b)
  5. {
  6.     int i = 0;
  7.     float pow = a;
  8.  
  9.     for (i = 1; i < (int) b; i++)
  10.         pow = pow * a;
  11.     return pow;
  12. }
  13.  
  14. #include <iostream>
  15. #include <stdio.h>
  16.  
  17. using namespace std;
  18.  
  19. int main()
  20. {
  21.     float n1 = 0, n2 = 0, r = 0;
  22.     char c;
  23.     int ctr = 0;
  24.  
  25.     printf("Simple calculator by andreafiori\n");
  26.  
  27.     do
  28.     {
  29.         cin >> n1;
  30.         cin >> c;
  31.         cin >> n2;
  32.  
  33.         switch (c)
  34.         {
  35.             case '+' :
  36.                 r = n1 + n2;
  37.                 break;
  38.             case '-' :
  39.                 r = n1 - n2;
  40.                 break;
  41.             case '*' :
  42.                 r = n1 * n2;
  43.                 break;
  44.             case '^' :
  45.                 r = pot (n1, n2);
  46.                 break;
  47.             case '/' :
  48.                 if (n2 == 0)
  49.                     printf("The value is not valid!\n");
  50.                 else
  51.                     r = n1 / n2;
  52.                 break;
  53.             default :
  54.                 printf("The operation is not allowed!\n");
  55.                 break;
  56.         }
  57.  
  58.         printf (" = %f\nRepeat ? [1 = yes/ 0 = no] ", r);
  59.         scanf ("%d", &ctr);
  60.         } while (ctr == 1);
  61.  
  62.         return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement