Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. int power(int a, int b) {
  6.  
  7. if (b == 1) return a;
  8. else { return (a*power(a, b - 1)); }
  9. }
  10.  
  11.  
  12. main() {
  13. int a;
  14. int b;
  15. char c = '+';
  16. while (c != '0') {
  17. scanf("%d%c%d", &a, &c, &b);
  18. if (c == '0') { break; }
  19. else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
  20. switch (c) {
  21. case '+':
  22. printf("%d\n", a + b);
  23. break;
  24. case '-':
  25. printf("%d\n", a - b);
  26. break;
  27. case '*':
  28. printf("%d\n", a * b);
  29. break;
  30. case '/':
  31. if (b != 0) printf("%d\n", a / b);
  32. else printf("%d\n", 0);
  33. break;
  34. case '^':
  35. if (b >= 0) printf("%d\n", power(a, b));
  36. else printf("%d\n", 1);
  37. break;
  38. }
  39.  
  40. }
  41. else printf("Unknown operation\n");
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement