/* Infix to prefix notation written by- Shivam Rana Date- 2/4/12 Users are requested to use a bracket wherever necessary while giving input otherwise it might give wrong answer */ #include<stdio.h> #include<string.h> int main() { int i,j,l,m,k=0; char a[400],b[400],reva[400],ans[400]; m=-1; scanf("%s",&a); l=strlen(a); for(i=0;i<l;i++) { if(a[l-1-i]=='(') reva[i]=')'; else if(a[l-1-i]==')') reva[i]='('; else reva[i]=a[l-1-i]; } for(j=0;j<l;j++) { if(reva[j]=='(') continue; else if(reva[j]>64 && reva[j]<91 ||reva[j]>96 && reva[j]<123) ans[k++]=reva[j]; else if(reva[j]==')') { ans[k++]=b[m]; m--; } else { m++; b[m]=reva[j]; } } for(i=(k-1);i>=0;i--) printf("%c",ans[i]); printf("\n"); return 0; }
Archive for the ‘Lab 7’ Category
0
/* c prog for infix to postfix conversion written by- Shivam Rana */ #include<stdio.h> #include<string.h> int main() { int i,j,l,m; char a[400],b[400]; m=-1; scanf("%s",&a); l=strlen(a); for(j=0;j<l;j++) { if(a[j]=='(') continue; else if(a[j]>64 && a[j]<91 ||a[j]>96 && a[j]<123) printf("%c",a[j]); else if(a[j]==')') { printf("%c",b[m]); m--; } else { m++; b[m]=a[j]; } }printf("\n"); return 0; }
C program to check a mathematical expression for correctness in terms of brackets, using Stack
Posted: April 1, 2012 in Lab 7/* C program to check a mathematical expression for correctness in terms of brackets, using Stack Written by- Shivam Rana */ #include<stdio.h> #include<stdlib.h> struct sNode { char data; struct sNode *next; }; void push(struct sNode** top_ref, int new_data); int pop(struct sNode** top_ref); int isMatchingPair(char character1, char character2) { if(character1 == '(' && character2 == ')') return 1; else if(character1 == '{' && character2 == '}') return 1; else if(character1 == '[' && character2 == ']') return 1; else return 0; } int areParenthesisBalanced(char exp[]) { int i = 0; struct sNode *stack = NULL; while(exp[i]) { if(exp[i] == '{' || exp[i] == '(' || exp[i] == '[') push(&stack, exp[i]); if(exp[i] == '}' || exp[i] == ')' || exp[i] == ']') { if(stack == NULL) return 0; else if ( !isMatchingPair(pop(&stack), exp[i]) ) return 0; } i++; } if(stack == NULL) return 1; /*balanced*/ else return 0; /*not balanced*/ } int main() { char exp[100]; printf("Enter expression\n"); scanf("%s",exp); if(areParenthesisBalanced(exp)) printf("\nBalanced "); else printf("\nNot Balanced "); \ getchar(); } void push(struct sNode** top_ref, int new_data) { struct sNode* new_node = (struct sNode*) malloc(sizeof(struct sNode)); if(new_node == NULL) { printf("Stack overflow \n"); getchar(); exit(0); } new_node->data = new_data; new_node->next = (*top_ref); (*top_ref) = new_node; } int pop(struct sNode** top_ref) { char res; struct sNode *top; /*If stack is empty then error */ if(*top_ref == NULL) { printf("Stack overflow \n"); getchar(); exit(0); } else { top = *top_ref; res = top->data; *top_ref = top->next; free(top); return res; } }
/* Program of stack using linked list*/ # include<stdio.h> # include<stdlib.h> void push(); void pop(); void display(); struct node { int info; struct node *link; } *top=NULL; int main() { int choice; while(1) { printf("1.Push\n"); printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : ") ; scanf("%d", &choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ }/*End of main() */ void push(void) { struct node *tmp; int pushed_item; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the new value to be pushed on the stack : "); scanf("%d",&pushed_item); tmp->info=pushed_item; tmp->link=top; top=tmp; } void pop(void) { struct node *tmp; if(top == NULL) printf("Stack is empty\n"); else { tmp=top; printf("Popped item is %d\n",tmp->info); top=top->link; free(tmp); } } void display(void) { struct node *ptr; ptr=top; if(top==NULL) printf("Stack is empty\n"); else { printf("Stack elements :\n"); while(ptr!= NULL) { printf("%d\n",ptr->info); ptr = ptr->link; } } }
/* Program of stack using array*/ #include<stdio.h> #define MAX 5 void push(); void pop(); void display(); int top = -1; int stack_arr[MAX]; int main() { int choice; while(1) { printf("1.Push\n"); printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : push(); break; case 2: pop(); break; case 3: display(); break; case 4: break; default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ void push(void) { int pushed_item; if(top == (MAX-1)) printf("Stack Overflow\n"); else { printf("Enter the item to be pushed in stack : "); scanf("%d",&pushed_item); top=top+1; stack_arr[top] = pushed_item; } } void pop(void) { if(top == -1) printf("Stack Underflow\n"); else { printf("Popped element is : %d\n",stack_arr[top]); top=top-1; } } void display(void) { int i; if(top == -1) printf("Stack is empty\n"); else { printf("Stack elements :\n"); for(i = top; i >=0; i--) printf("%d\n", stack_arr[i] ); } }
C program to Implement Polynomial Equation Addition in a Single linked list.
Posted: April 1, 2012 in Lab 7#include<stdio.h> #include<stdlib.h> #include<conio.h> struct link{ int coeff; int pow; struct link *next; }; struct link *poly1=NULL,*poly2=NULL,*poly=NULL; void create(struct link *node) { char ch; do { printf("\n enter coeff:"); scanf("%d",&node->coeff); printf("\n enter power:"); scanf("%d",&node->pow); node->next=(struct link*)malloc(sizeof(struct link)); node=node->next; node->next=NULL; printf("\n continue(y/n):"); fflush(stdin); scanf("%c",&ch); } while(ch=='y' || ch=='Y'); } void show(struct link *node) { while(node->next!=NULL) { printf("%dx^%d",node->coeff,node->pow); node=node->next; if(node->next!=NULL) printf("+"); } } void polyadd(struct link *poly1,struct link *poly2,struct link *poly) { while(poly1->next && poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } while(poly1->next || poly2->next) { if(poly1->next) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } } int main() { char ch; do{ poly1=(struct link *)malloc(sizeof(struct link)); poly2=(struct link *)malloc(sizeof(struct link)); poly=(struct link *)malloc(sizeof(struct link)); printf("\nenter 1st polynomial:"); create(poly1); printf("\nenter 2nd polynomial:"); create(poly2); printf("\n1st Number:"); show(poly1); printf("\n2nd Number:"); show(poly2); polyadd(poly1,poly2,poly); printf("\nAdded polynomial:"); show(poly); printf("\n add two more numbers:"); fflush(stdin); scanf("%c",&ch); } while(ch=='y' || ch=='Y'); }