# DATA STRUCTURE IN C INFORMATION SCIENCE 3RD SEMESTER PROJECT

PRESENTED BY, VINAYAKA DJ USN:1MS08IS136 VINAYAK UMESH VIADYA USN:1MS08IS135 SRIDAR N.S USN:1MS09IS417 SANCHIT USN:1MS09IS413

Page |2 CONENT: page 3 1) Given problem 2) Abstract 4 3) How we solved 5 4) Conclusion 5 5) C program 6 .

Page |3  Given problem Assume a machine that has a single register and six instructions LD A places the operand A into the register ST A places the contents of the register into the variable A.-.use variables of the form TEMP n as temporary variables for example using the postfix expression ABC*+DE-/ should print the following LD B ML C ST TEMP1 LD A AD TEMP1 ST TEMP2 LD D SB E ST TEMP3 LD TEMP2 DV TEMP3 ST TEMP4 . AD A adds the contents of the variable A to the register. DV A divides the contents of the register by the variable A.*and / and prints a sequence of instruction to evaluate the expression and leave the result in the register . Write the program that accepts a postfix expression containing single Letter operands and the operators +. ML A multiplies the contents of the register by the variable A. SB A subtract the content of the variable A from the register.

Page |4 ABSTRACT  SUMMARY Given problem was to assume that there is only one register (memory location) in machine and we have to print the instruction to the machine to evaluate a postfix expression. EXAMPLE: ABC*+ First we push ABC Before after popping . Our concept We scan the each character of the expression and push to the stack and if the character is an operator we will pop the two elements from the stack and replace a temp in to the stack the element is printed with the register instruction.

we understood that the register means memory location and After reading and discussing we understood that We have to print the instruction how evaluation takes place  Problem faced We have used one dimensional char array but we had to store temp In it so we used array of string and we had to store tempn where the value of n as to change every time but we could not push a integer into the string so we used different technique int the problem to show temp n as shown in the program Conclusion: We have tried our best to solve the problem and also tried to stop the Program if the expression is invalid. .Page |5 HOW WE SOLVED Firstly. we had problem to understand the question “what was the register “what should be the output then after reading the example Given.

} int empty() { if(s1. strcpy(s1.Page |6  program #include<stdio.h> #include<string.ele).top==-1) return 1. }.top]. } char x[7].top+1.h> #include<conio.h> #include<process. struct stack s1. char item[30][7].top=s1. void push(char ele[]) { s1.h> #define size 30 struct stack { int top.h> #include<ctype. char *pop() { . else return 0.item[s1.

} } int isoperator(char c) { switch(c) { case '+': case '-': case '/': case '*': return(1).'m'.op2[7]. { getch(). .Page |7 if(empty()) printf("\n invalid expression"). } { strcpy(x. } } void evaluate(char ch) { static int i=1. char temp[7]={'T'. pop().'\0'}.s1.item[s1.'p'. exit(0). char op1[7].top--]). default : return(0).'e'.

"Temp")) printf("\nDV\t%s %d". else printf("\nDV\t%s". case'-':if(!strcmp(op2.op2).op2. break.op2. else printf("\nLD\t%s"."Temp")) printf("\nML\t%s %d". case'*':if(!strcmp(op2.i-2). printf("\nST\tTemp %d".op2. else printf("\nML\t%s". break. push(temp). push(temp). strcpy(op1."Temp")) printf("\nSB\t%s %d".i-1).Page |8 strcpy(op2.x). push(temp)."Temp")) printf("\nLD\t%s %d".op2).i-1).i-1). ."Temp")) printf("\nAD\t%s %d".op2). i=i+1. pop(). break. else printf("\nAD\t%s". if(!strcmp(op1. i=i+1. switch(ch) { case'+':if(!strcmp(op2. printf("\nST\tTemp %d".op1).i).op1. else printf("\nSB\t%s". case'/':if(!strcmp(op2.i).i).i-1). printf("\nST\tTemp %d".op2).op2.x). i=i+1.

c.top=-1. if(isalpha(c)) push(ch). else { . } } void main() { int i. exit(0). printf("\n enter the expression"). s1.i++) { c=exp[i]. getch(). i=i+1. else if(isoperator(c)) evaluate(c). ch[0]=exp[i]. printf("\nST\tTemp %d". default:break. } for(i=0.ch[7].i). if(strlen(exp)%2==0) { printf("\n invalid expression"). ch[1]='\0'.exp[i]!='\0'. char exp[30]. gets(exp).Page |9 push(temp).

} } getch(). exit(0).P a g e | 10 printf("\n invalid expression"). } . getch().

Sign up to vote on this title