Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // ArithmeticTest.c
- // Stubs for required functions.
- //-----------------------------------------------------------------------------
- #include<stdio.h>
- #include<stdlib.h>
- // Define fptr_t type using typedef statement
- typedef struct (*fptr_t)(int x, int y){
- return 0;
- } fptr_t;
- // Define functions
- int sum(int x, int y){
- return x + y;
- }
- int diff(int x, int y){
- return x - y;
- }
- int prod(int x, int y){
- return x * y;
- }
- int quot(int x, int y){
- return x / y;
- }
- int rem(int x, int y){
- return x % y;
- }
- int apply(fptr_t fp, int x, int y){
- return fp(x, y);
- }
- int compute(fptr_t fp[5], int* opCode, int* operand, int n){
- int x;
- if(n == 1){
- if(opCode[n - 1] == 0){
- x = apply(fp[0], operand[n - 2], operand[n - 1]);
- operand[n - 1] = x;
- return x;
- }
- else if(opCode[n - 1] == 1){
- x = apply(fp[1], operand[n - 2], operand[n - 1]);
- operand[n - 1] = x;
- return x;
- }
- else if(opCode[n - 1] == 2){
- x = apply(fp[2], operand[n - 2], operand[n - 1]);
- operand[n - 1] = x;
- return x;
- }
- else if(opCode[n - 1] == 3){
- x = apply(fp[3], operand[n - 2], operand[n - 1]);
- operand[n - 1] = x;
- return x;
- }
- else if(opCode[n - 1] == 4){
- x = apply(fp[4], operand[n - 2], operand[n - 1]);
- operand[n - 1] = x;
- return x;
- }
- }
- else{
- return compute(fp, opCode, operand, n - 1);
- }
- }
- void testCompute(){
- fptr_t op[] = {sum, diff, prod, quot, rem};
- int A[] = {3, 2, 5, 4, 6, 7, 9, 2, 8};
- int opCode[] = {0, 2, 1, 4, 2, 2, 3, 1};
- int n = 8;
- //--------------------------------------------------------------------------
- // prints 86, which is the value of the value of the expression:
- //
- // (((((((3+2)*5)-4)%6)*7)*9)/2)-8
- //
- //--------------------------------------------------------------------------
- printf("%d\n", compute(op, A, opCode, n));
- }
- int main(){
- /*FILE* in;
- int n;
- int* opCode = malloc(255, sizeof(int));
- int* operand = malloc(255, sizeof(int));
- fptr_t fp[] = {sum, diff, prod, quot, rem};
- in = fopen("in1", "r");
- n = fscanf(in, "%d", n);
- fgets(opCode, 255, (FILE*)in);
- fgets(operand, 255, (FILE*)in);
- free(opCode);
- free(operand);*/
- testCompute();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement