Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- int a[200]= {0},b[200]= {0},s[200]= {0},i,t=0;
- char x[200],y[200],z[200];
- printf("Primul numar = ");
- gets(x);
- printf("Al doilea numar = ");
- gets(y);
- a[0]=strlen(x);
- printf("Numarul de cifre pe care le are numarul a este: %d\n",a[0]);
- b[0]=strlen(y);
- printf("Numarul de cifre pe care le are numarul b este: %d\n",b[0]);
- printf("\n");
- if((a[0]<b[0])||(a[0]==b[0])&&(strcmp(x,y)<0))
- {
- strcpy(z,x);
- strcpy(x,y);
- strcpy(y,z);
- a[0] = strlen(x);
- b[0] = strlen(y);
- }
- if(a[0]>b[0]) s[0]=a[0];
- else s[0]=b[0];
- for(i=1; i<=a[0]; i++)
- a[i]=x[a[0]-i] - '0';
- for(i=a[0]; i>0; i--)
- printf("%d ",a[i]);
- printf("\n");
- for(i=1; i<=b[0]; i++)
- b[i]=y[b[0]-i]-'0';
- for(i=a[0]; i>0; i--)
- printf("%d ",b[i]);
- printf("\n");
- adunare(a,b);
- printf("\n");
- diferenta(a,b);
- }
- void adunare(int a[200], int b[200])
- {
- int t = 0,i, s[200];
- s[0] = a[0];
- for(i=1; i<=s[0]; i++)
- {
- s[i]=(a[i]+b[i]+t)%10;
- t=(a[i]+b[i]+t)/10;
- }
- if (t)
- {
- s[0]++;
- s[s[0]]=t;
- }
- for(i=s[0]; i>0; i--)
- printf("%d ",s[i]);
- printf("\n");
- }
- void diferenta(int a[200], int b[200])
- {
- int d[200];
- int i, t = 0;
- for(i = 1; i <= a[0]; i++)
- {
- d[i] = a[i] - b[i] + t;
- if(d[i] < 0)
- {
- d[i] += 10;
- t = -1;
- }
- else
- t = 0;
- }
- i--;
- while(i && !d[i])
- i--;
- if(i == 0)
- {
- for(i=a[0]; i>0; i--)
- printf("%d ",a[i]);
- printf("\n");
- for(i=a[0]; i>0; i--)
- printf("%d ",b[i]);
- printf("\n");
- printf("0");
- }
- else
- {
- d[0] = i;
- int j;
- for(i=a[0]; i>0; i--)
- printf("%d ",a[i]);
- printf("\n");
- for(i=a[0]; i>0; i--)
- printf("%d ",b[i]);
- printf("\n");
- for(i = d[0]; i > 0; i--)
- printf("%d ", d[i]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement