SYSTEM PROGRAMMING LAB ; ; 1 Q 1: WAP to convert INFIX to POSTFIX notation. [SOURCE CODE] #include #include #include #define MAX 20 char stack[MAX]; int top=-1; char pop(); void push(char item); int prcd(char symbol) // Precedence { switch(symbol) { case '+': case '-': return 2; case '*': case '/': return 4; case '^': return 6; case '(': case ')': case '#': return 1; default: return 0; } } int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^': case '(': case ')': return 1; default: return 0; } } ; 2 void convertip(char infix[],char postfix[]) { int i,symbol,j=0; stack[++top]='#'; for(i=0;iprcd(stack[top])) push(symbol); else{ while(prcd(symbol)<=prcd(stack[top])) { postfix[j]=pop(); j++; } push(symbol); }//end of else. }//end of else. }//end of else. }//end of for. while(stack[top]!='#') { postfix[j]=pop(); j++; } postfix[j]='\0';//null terminate string. } ; 3 void main() { char infix[20],postfix[20]; clrscr(); printf("Enter the valid infix string:\n"); gets(infix); convertip(infix,postfix); printf("The corresponding postfix string is:\n"); puts(postfix); getch(); } void push(char item) { top++; stack[top]=item; } char pop() { char a; a=stack[top]; top--; return a; } ; 4 Q 1: WAP to convert INFIX to POSTFIX notation. [SCREENSHOTS] Enter the valid infix string: a+b/c The corresponding postfix string is: abc/+ Enter the valid infix string: a+b-(c^3)*(r/4) The corresponding postfix string is: ab+c3^r4/*- ; 5 Q 2: WAP to convert INFIX to PREFIX notation. [SOURCE CODE] #include #include #include #include char stack[50]; int top=-1,x; void push(char elem) { top++; stack[top]=elem; } char pop() { x=stack[top]; top--; return(x); } int order(char elem) { switch(elem) { case '#': return 0; case '(': return 1; case ')': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } return 10; //default case } void main() { char infix[50],postfix[50],elem,ch; int len,c=0,i=0; cout<<"Enter the infix expresssion:"; cin>>infix; push('#'); len=strlen(infix); for(i=0;i= order(ch) ) { postfix[c]=pop(); c++; } push(ch); } } while( stack[top] != '#') { postfix[c]=pop(); c++; } postfix[c]='\0'; cout<<"The Infix Expression is"<<" "< #include #include //for exit #include //for gets() void main () { clrscr(); int choice; char data[100]; char cdata; //using constructor ofstream WriteFile("TheFile.txt",ios::out); //using open() method ifstream ReadFile; ReadFile.open("TheFile.txt",ios::in); while(1) { cout<<"\n\nEnter your choice"<>choice; cout<> data; cout << data << endl; break; ; 9 case 3: cout << "Reading 5th character from the file" << endl; ReadFile.seekg(0, ios::beg); for(int i=0; i<5; i++) ReadFile >> cdata; cout << cdata << endl; break; case 0: ReadFile.close(); WriteFile.close(); exit(0); } } } ; 10 Q 3: WAP to read and write a string to a file. [SCREENSHOTS] Enter your choice Enter 1 to write to file Enter 2 to read string from file Enter 3 to read 5th character from file Enter 0 for exit Enter your choice: 1 Writing a string to the file Enter a string: Hello... Enter your choice Enter 1 to write to file Enter 2 to read string from file Enter 3 to read 5th character from file Enter 0 for exit Enter your choice: 2 Reading a string from the file Hello... Enter your choice Enter 1 to write to file Enter 2 to read string from file Enter 3 to read 5th character from file Enter 0 for exit Enter your choice: 3 Reading 5th character from the file o ; 11 Q 4: WAP to perform lexical analysis of an expression. [SOURCE CODE] #include #include #include #include void main() { char s[50]; char operands[50]; char cons[50]; char oper[50]; int l=0,i=0,c=0,co=0,cou=0,count=0; cout<<"Enter a string"; cin>>s; l=strlen(s); for(i=0;i #include #include #include void main() { clrscr(); char s[100]; int l,flag=0,i=0; cout<<"Enter the string:"<>s; l=strlen(s); if(s[0]=='a' && s[l-1]=='c' && s[l-2]=='b' && s[l-3]=='a') { for(i=1;i #include #include #include void main() { char in[50],dig[50],id[50]; int i=0,j=0,k,l=0; cout<<"Enter the Expression:\t"; gets(in); cout<<"\nKeyword\tIdentifier\tConstants\tOperators\tSpecialCharacters\n"; while(in[i]!='\0') { if(isalpha(in[i])) { j=0; while((isalpha(in[i]))||(isdigit(in[i]))) { id[j]=in[i]; i++; j++; } id[j]='\0'; if(strcmp(id,"char")==0||strcmp(id,"int")==0||strcmp(id,"float")==0||strcmp(id,"if")==0 || strcmp(id,"then")==0 || strcmp(id,"while")==0 || strcmp(id,"do")==0||strcmp(id,"for")==0 ||strcmp(id,"switch")==0||strcmp(id,"case")==0) { cout<<"\n"; for(l=0;l'||in[i]=='=') { cout<<"\t\t\t\t\t "< #include #include #include #include char data[100]; ofstream of; ifstream iif; int ch; char fname[10]; void main() { clrscr(); while(ch!=5) { cout<<" -- "; cout<<"\n\n MAIN MENU \n"; cout<<" 1.Write \n"; cout<<" 2.Read \n"; cout<<" 3.Copy \n"; cout<<" 4.Paste \n"; cout<<" 5.Exit \n"; cout<<" Your Choice:"; cin>>ch; switch(ch) { case 1: cout<<"\nFILENAME="; cin>>fname; cout<<"WRITING TO THE FILE:"; of.open(fname); cout<<"\nEnter DATA:"<<"\n"; cin>>data; of<>data; cout<>data; } iif.close(); cout<<"\nDATA COPIED"; break; case 4: cout<<"\nFILENAME="; cin>>fname; cout<<"PASTING TO THE FILE:"; of.open(fname); of< #include #include #include void Union(int set1[10],int set2[10],int m,int n); void Intersection(int set1[10],int set2[10],int m,int n); void main() { clrscr(); int a[10],b[10],m,n,i,j; int ch; cout<<"\nEnter the number of elements in first set:\n"; cin>>m; cout<<"\nEnter the elements:\n"; for(i=0;i>a[i]; cout<<"\nEnter the number of elements in second set:\n"; cin>>n; cout<<"\nEnter the elements:\n"; for(i=0;i>b[i]; while(1) { clrscr(); cout<<"\n\nMenu:\n\n1. Find the Union\n2. Find the Intersection"; cout<<"\n3. Exit"; cout<<"\nEnter your choice:\n"; cin>>ch; switch(ch) { case 1: Union(a,b,m,n); break; case 2: Intersection(a,b,m,n); break; case 3: exit(0); } getch(); } } void Union(int a[10],int b[10],int m,int n) { int c[20],i,j,k=0,flag=0; //add all elements of set A for(i=0;i