Sie sind auf Seite 1von 9

1.

Given a mathematics notation in infix notation, change it to prefix and postfix notation, then give the result of the evaluation. supported notation: numeric: 0-9 operator: + - * / ( ) precedence: 1. ( ) 2. * / 3. + each notation separated by space all operation in integer value Sample input: 10 + 200 * 1000 * ( 2 + 37 / 2 ) 3 * 70 / ( 2 + 1 ) Sample output: infix: 10 + 200 * 1000 * ( 2 + 37 / 2 ) postfix: 10 200 1000 * 2 37 2 / + * + evaluatian: 200 * 1000 = 200000 37 / 2 = 18 2 + 18 = 20 200000 * 20 = 4000000 10 + 4000000 = 4000010 prefix: + 10 * * 200 1000 + 2 / 37 2 evaluation: 37 / 2 = 18 2 + 18 = 20 200 * 1000 = 200000 200000 * 20 = 4000000 10 + 4000000 = 4000010 infix: 3 * 70 / ( 2 + 1 ) postfix: 3 70 * 2 1 + / evaluation: 3 * 70 = 210 2+1=3 210 / 3 = 70 prefix: / * 3 70 + 2 1 evaluation: 2+1=3 3 * 70 = 210 210 / 3 = 70

Task: 1. Create an application capable of solving above problem using stack/queue. 2. Create your own 5 math notation with 1 notation consists of 15 operator Answer : /* include necessary preprocessor header files */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* constants */ #define TRUE 1 #define FALSE 0 /* structure for stack */ typedef struct { char data[20]; /* array to hold stack contents */ int tos; /* top of the stack pointer */ } STACK;

/* function prototypes */ void initStack(STACK *stack); void get_postfix(char infix[]); void convertToinfix(char postfix[], char infix[]); int isOperator(char c); int precedence(char operator1, char operator2); int pred_level(char ch); void push(STACK *stack, char value); char pop(STACK *stack); char stackTop(STACK *stack); int isEmpty(STACK *stack); int isFull(STACK *stack); void printResult(char postfix[], char infix[]); void print_msg(void);

int main(void) { char postfix[20], infix[20] =""; //float result; /* convert from infix to postfix main function */ convertToinfix(postfix, infix); /* display the infix equivalent */ infix[strlen(postfix)-2] = '\0'; printResult(postfix, infix /*, result*/);

return EXIT_SUCCESS; } void initStack(STACK *stack) { stack->tos = -1; /* stack is initially empty */ }

void get_postfix(char postfix[]) { int i; printf("Enter postfix expression below (max 18 characters excluding spaces) : \n"); fflush(stdin); /* to read in only 18 characters excluding spaces */ for ( i=0; i<18; ) { if ( (postfix[i] = getchar()) == '\n' ) { i++; break; } else if ( !(isspace(postfix[i])) ) i++; } postfix[i] = '\0'; } void convertToinfix(char postfix[], char infix[]) { int i, length; int j=0; char tos_ch; STACK stack; initStack(&stack); /* initialise stack */ get_postfix(postfix); /* get postfix expression from user */ length = strlen(postfix); /* if strlen if postfix is more than zero */ if ( length ) { push(&stack, '('); strcat(postfix, ")"); length++; for ( i=0; i<length; i++ ) { /* if current operator in postfix is digit */

if ( isdigit(postfix[i]) ) { infix[j++] = postfix[i]; } /* if current operator in postfix is left parenthesis */ else if ( postfix[i] == '(' ) { push(&stack, '('); } /* if current operator in postfix is operator */ else if ( isOperator(postfix[i]) ) { while ( TRUE ) { /* get tos */ tos_ch = stackTop(&stack); /* no stack left */ if ( tos_ch == '\0' ) { printf("\nInvalid postfix expression\n"); print_msg(); exit(1); } else { if ( isOperator(tos_ch) ) { if ( pred_level(tos_ch) >= pred_level(postfix[i]) ) infix[j++] = pop(&stack); else break; } else break; } } push(&stack, postfix[i]); } /* if current operator in postfix is right parenthesis */ else if ( postfix[i] == ')' ) { while ( TRUE ) { /* get tos */ tos_ch = stackTop(&stack); /* no stack left */ if ( tos_ch == '\0' ) { printf("\nInvalid postfix expression\n");

print_msg(); exit(1); } else { if ( tos_ch != '(' ) { infix[j++] = tos_ch; pop(&stack); } else { pop(&stack); break; } } } continue; } } } infix[j] = '\0'; }

/* determine if c is an operator */ int isOperator(char c) { if ( c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^' ) { return TRUE; } else return FALSE; } /* determine precedence level */ int pred_level(char ch) { if ( ch == '+' || ch == '-' ) return 1; else if ( ch == '^' ) return 3; else return 2; } /* determine if the precedence of operator1 is less than, equal to, greater than the precedence of operator2 */ int precedence(char operator1, char operator2) {

if ( pred_level(operator1) > pred_level(operator2) ) return 1; else if ( pred_level(operator1) < pred_level(operator2) ) return -1; else return 0; } /* push a value on the stack */ void push(STACK *stack, char value) { if ( !(isFull(stack)) ) { (stack->tos)++; stack->data[stack->tos] = value; } } /* pop a value off the stack */ char pop(STACK *stack) { char ch; if ( !(isEmpty(stack)) ) { ch = stack->data[stack->tos]; (stack->tos)--; return ch; } else return '\0'; } /* return the top value of the stack without popping the stack */ char stackTop(STACK *stack) { if ( !(isEmpty(stack)) ) return stack->data[stack->tos]; else return '\0'; } /* determine if stack is empty */ int isEmpty(STACK *stack) { /* empty */ if ( stack->tos == -1 ) return TRUE; /* not empty */ else return FALSE; }

/* determine if stack is full */ int isFull(STACK *stack) { /* full */ if ( stack->tos == 19 ) return TRUE; /* not full */ else return FALSE; }

/* display the result postfix expression */ void printResult(char postfix[], char infix[]/*, float result*/) { /*system("cls");*/ printf("\n\n"); printf("Postfix notation: %s\n\n", postfix); printf("Infix notation : %s\n", infix); //printf("Result is: %f\n", result); print_msg(); } /* print exit message */ void print_msg(void) { printf("Hit <RETURN> to exit......"); fflush(stdin); getchar(); }

Two Given map(s) of a dungeon, print the minimum cost required to go from starting point to finish point. Legend: S-> starting point F-> finish point 0-9->cost of the place #-> obstacle the move possible is only horizontal (left or right) and vertical (up or down) Sample input: 3 53 #####

#S1F# ##### 55 ##### #S12# #13## #34F# ##### 67 ###### #S123# ##1### #9123# #8#12# #F111# ######

Three Given map(s) of a dungeon, print whether there exists a possibility of going out of dungeon from start point to finish point. Legend: S-> start point F-> finish point #-> obstacle the move possible is only horizontal (left or right) and vertical (up or down)

Sample input: 4 53 ##### #S F# ##### 53 ##### #S#F# ##### 5 10 ##### #F# # ## # ### ## ## # # ## ## # ## ## S# #####

76 ####### #S# F# # # # # # ## ## ## ####### Sample output: map-1 possible. map-2 impossible. map-3 impossible. map-4 possible. Task: 1. Create an application capable of solving above problem using Stack or recursion for DFS implementation of stack. 2. Create your own map consists of 10 map (6 possible route and 4 impossible route) with 1 map consists 100 rows and 100 columns

Das könnte Ihnen auch gefallen