You are on page 1of 10

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

3) How we solved

4) Conclusion

5) C program

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. AD A adds the contents of the variable A to the register. SB A subtract the content of the variable A from the register. ML A multiplies the contents of the register by the variable A. DV A divides the contents of the register by the variable A. Write the program that accepts a postfix expression containing single Letter operands and the operators +,-,*and / and prints a sequence of instruction to evaluate the expression and leave the result in the register .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

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. 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. EXAMPLE: ABC*+ First we push ABC

Before

after popping

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. 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 |6

program #include<stdio.h> #include<process.h> #include<string.h> #include<ctype.h> #include<conio.h> #define size 30 struct stack { int top; char item[30][7]; }; struct stack s1;

void push(char ele[]) { s1.top=s1.top+1; strcpy(s1.item[s1.top],ele); }

int empty() { if(s1.top==-1) return 1; else return 0; } char x[7]; char *pop() {

Page |7

if(empty()) printf("\n invalid expression"); { getch(); exit(0); } { strcpy(x,s1.item[s1.top--]); } } int isoperator(char c) { switch(c) { case '+': case '-': case '/': case '*': return(1); default : return(0); } }

void evaluate(char ch) { static int i=1; char temp[7]={'T','e','m','p','\0'}; char op1[7],op2[7]; pop();

Page |8

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

Page |9

push(temp); printf("\nST\tTemp %d",i); i=i+1; default:break; } }

void main() { int i; s1.top=-1; char exp[30],ch[7],c; printf("\n enter the expression"); gets(exp); if(strlen(exp)%2==0) { printf("\n invalid expression"); getch(); exit(0); } for(i=0;exp[i]!='\0';i++) { c=exp[i]; ch[0]=exp[i]; ch[1]='\0'; if(isalpha(c)) push(ch); else if(isoperator(c)) evaluate(c); else {

P a g e | 10

printf("\n invalid expression"); getch(); exit(0); } } getch(); }

You might also like