Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // ArithmeticTest.c
  3. // Stubs for required functions.
  4. //-----------------------------------------------------------------------------
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7.  
  8. // Define fptr_t type using typedef statement
  9. typedef struct (*fptr_t)(int x, int y){
  10. return 0;
  11. } fptr_t;
  12.  
  13. // Define functions
  14. int sum(int x, int y){
  15. return x + y;
  16. }
  17.  
  18. int diff(int x, int y){
  19. return x - y;
  20. }
  21.  
  22. int prod(int x, int y){
  23. return x * y;
  24. }
  25.  
  26. int quot(int x, int y){
  27. return x / y;
  28. }
  29.  
  30. int rem(int x, int y){
  31. return x % y;
  32. }
  33.  
  34. int apply(fptr_t fp, int x, int y){
  35. return fp(x, y);
  36. }
  37.  
  38. int compute(fptr_t fp[5], int* opCode, int* operand, int n){
  39. int x;
  40. if(n == 1){
  41. if(opCode[n - 1] == 0){
  42. x = apply(fp[0], operand[n - 2], operand[n - 1]);
  43. operand[n - 1] = x;
  44. return x;
  45. }
  46. else if(opCode[n - 1] == 1){
  47. x = apply(fp[1], operand[n - 2], operand[n - 1]);
  48. operand[n - 1] = x;
  49. return x;
  50. }
  51. else if(opCode[n - 1] == 2){
  52. x = apply(fp[2], operand[n - 2], operand[n - 1]);
  53. operand[n - 1] = x;
  54. return x;
  55. }
  56. else if(opCode[n - 1] == 3){
  57. x = apply(fp[3], operand[n - 2], operand[n - 1]);
  58. operand[n - 1] = x;
  59. return x;
  60. }
  61. else if(opCode[n - 1] == 4){
  62. x = apply(fp[4], operand[n - 2], operand[n - 1]);
  63. operand[n - 1] = x;
  64. return x;
  65. }
  66. }
  67. else{
  68. return compute(fp, opCode, operand, n - 1);
  69. }
  70. }
  71.  
  72. void testCompute(){
  73. fptr_t op[] = {sum, diff, prod, quot, rem};
  74. int A[] = {3, 2, 5, 4, 6, 7, 9, 2, 8};
  75. int opCode[] = {0, 2, 1, 4, 2, 2, 3, 1};
  76. int n = 8;
  77.  
  78. //--------------------------------------------------------------------------
  79. // prints 86, which is the value of the value of the expression:
  80. //
  81. // (((((((3+2)*5)-4)%6)*7)*9)/2)-8
  82. //
  83. //--------------------------------------------------------------------------
  84. printf("%d\n", compute(op, A, opCode, n));
  85. }
  86.  
  87. int main(){
  88. /*FILE* in;
  89. int n;
  90. int* opCode = malloc(255, sizeof(int));
  91. int* operand = malloc(255, sizeof(int));
  92.  
  93. fptr_t fp[] = {sum, diff, prod, quot, rem};
  94.  
  95. in = fopen("in1", "r");
  96. n = fscanf(in, "%d", n);
  97. fgets(opCode, 255, (FILE*)in);
  98. fgets(operand, 255, (FILE*)in);
  99.  
  100. free(opCode);
  101. free(operand);*/
  102.  
  103. testCompute();
  104. return EXIT_SUCCESS;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement