E2 - { int i,k=mx,c=0,i; struct stack a; a.top=-1;...

This preview shows pages 1–4. Sign up to view the full content.

#include<stdio.h> #include<conio.h> #include<math.h> # struct stack { int data[10]; int top; }; } # define mx 5 # struct stack add(struct stack *p,struct stack *q,int c) { struct stack z; int i,max,t1,t2; z.top=-1; if(p->top>q->top) max=p->top; else max=q->top; for(i=0;i<=max;i++) { if(i<=p->top) t1=p->data[i]; else t1=0; if(i<=q->top) t2=q->data[i]; else t2=0; z.data[i]=(t1+t2+c)%2; (z.top)++; if(t1+t2+c<2) c=0; else c=1; } return z; } void dectobin(int z,struct stack *p) { int x,t=mx; x=z; p->top=-1; do { (p->top)++; p->data[p->top]=x%2; x=x/2; t--; }while(t>0);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
} void display(struct stack *p) { int z; z=p->top; while(z>=0) { printf("%d",p->data[z]); z--; } } int bintodec(struct stack *p) { int i,x=0; for(i=0;i<=p->top;i++) { x=x+(p->data[i]*pow(2,i)); } return x; } void shift(int c,struct stack *a,struct stack *q) { int i=0,temp; temp=a->data[0]; while(i<a->top) { a->data[i]=a->data[i+1]; i++; } a->data[a->top]=c; i=0; while(i<q->top) { q->data[i]=q->data[i+1]; i++; } q->data[q->top]=temp; printf("After Shift "); } void multiply(struct stack *b,struct stack *q)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { int i,k=mx,c=0,i; struct stack a; a.top=-1; while(k>0) { if(q->data[0]==1) { printf("Adding"); a=add(&a,b,c); printf("\nB:"); display(b); printf("\nQ:"); display(q); printf("\nC:"); printf("%d",c); printf("\nA:"); display(&a); getch(); getch(); } printf("yo"); shift(c,&a,q); printf("\nB:"); display(b); printf("\nQ:"); display(q); printf("\nC:"); printf("%d",c); printf("\nA:"); display(&a); k--; getch(); getch(); } for(i=0;i<5;i++) mul[i]=q->data[i]; for(i=0;i<5;i++) mul[i+5]=a.data[i]; } void main() { int a,b,c,x; struct stack a1,b1,c1; clrscr(); printf("Enter the two decimal nos"); scanf("%d%d",&a,&b); dectobin(a,&a1); dectobin(b,&b1); printf("first bin no"); display(&a1); printf("second bin no"); display(&b1); multiply(&a1,&b1); printf("The Multiplication in binary is"); for(i=9;i>=0;i--) printf("%d",mul[i]); c=bintodec(&b1); printf("\n%d",c); getch() }...
View Full Document

This note was uploaded on 10/25/2010 for the course CMPN SEM4 taught by Professor Thakur during the Spring '07 term at University of Mumbai.

Page1 / 4

E2 - { int i,k=mx,c=0,i; struct stack a; a.top=-1;...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online