Advertisement
STANAANDREY

pclab4 custom1

Oct 19th, 2022 (edited)
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int read(void) {
  4.   int n;
  5.   printf("introduce a number: ");
  6.   scanf("%d", &n);
  7.   return n;
  8. }
  9.  
  10. int max(int x, int y) {
  11.   return x > y ? x : y;
  12. }
  13.  
  14. int min(int x, int y) {
  15.   return x > y ? y : x;
  16. }
  17.  
  18. void printMenu(void) {
  19.   puts("1) max");
  20.   puts("2) min");
  21.   puts("3) write x");
  22.   puts("4) write y");
  23.   puts("5) +");
  24.   puts("6) -");
  25.   puts("7) *");
  26.   puts("8) /");
  27.   puts("press anything else to quit");
  28. }
  29.  
  30. int add(int x, int y) {
  31.   return x + y;
  32. }
  33.  
  34. int substract(int x, int y) {
  35.   return x - y;
  36. }
  37.  
  38. int multiply(int x, int y) {
  39.   return x * y;
  40. }
  41.  
  42. int divide(int x, int y) {
  43.   return x / y;
  44. }
  45.  
  46. void printRes(int res) {
  47.   printf("result=%d\n", res);
  48. }
  49.  
  50. int main(void) {
  51.   int x, y, opt;
  52.   x = read(), y = read();
  53.   printMenu();
  54.   opt = read();
  55.  
  56.   int res;
  57.   switch(opt) {
  58.   case 1:
  59.     res = max(x, y);
  60.     break;
  61.   case 2:
  62.     res = min(x, y);
  63.     break;
  64.   case 3:
  65.     res = x;
  66.     break;
  67.   case 4:
  68.     res = y;
  69.     break;
  70.   case 5:
  71.     res = add(x, y);
  72.     break;
  73.   case 6:
  74.     res = substract(x, y);
  75.     break;
  76.   case 7:
  77.     res = multiply(x, y);
  78.     break;
  79.   case 8:
  80.     res = divide(x, y);
  81.     break;
  82.   default:
  83.     puts("Exiting...");
  84.     return 0;
  85.   }
  86.   printRes(res);
  87.  
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement