Advertisement
barbos01

Untitled

Apr 14th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.     int a[200]= {0},b[200]= {0},s[200]= {0},i,t=0;
  7.     char x[200],y[200],z[200];
  8.     printf("Primul numar = ");
  9.     gets(x);
  10.     printf("Al doilea numar = ");
  11.     gets(y);
  12.     a[0]=strlen(x);
  13.     printf("Numarul de cifre pe care le are numarul a este: %d\n",a[0]);
  14.     b[0]=strlen(y);
  15.     printf("Numarul de cifre pe care le are numarul b este: %d\n",b[0]);
  16.     printf("\n");
  17.     if((a[0]<b[0])||(a[0]==b[0])&&(strcmp(x,y)<0))
  18.     {
  19.         strcpy(z,x);
  20.         strcpy(x,y);
  21.         strcpy(y,z);
  22.         a[0] = strlen(x);
  23.         b[0] = strlen(y);
  24.     }
  25.     if(a[0]>b[0]) s[0]=a[0];
  26.     else s[0]=b[0];
  27.  
  28.     for(i=1; i<=a[0]; i++)
  29.         a[i]=x[a[0]-i] - '0';
  30.  
  31.     for(i=a[0]; i>0; i--)
  32.         printf("%d ",a[i]);
  33.     printf("\n");
  34.  
  35.     for(i=1; i<=b[0]; i++)
  36.         b[i]=y[b[0]-i]-'0';
  37.  
  38.     for(i=a[0]; i>0; i--)
  39.         printf("%d ",b[i]);
  40.     printf("\n");
  41.  
  42.  
  43.     adunare(a,b);
  44.     printf("\n");
  45.     diferenta(a,b);
  46.  
  47.  
  48. }
  49.  
  50. void adunare(int a[200], int b[200])
  51. {
  52.     int t = 0,i, s[200];
  53.     s[0] = a[0];
  54.     for(i=1; i<=s[0]; i++)
  55.     {
  56.         s[i]=(a[i]+b[i]+t)%10;
  57.         t=(a[i]+b[i]+t)/10;
  58.     }
  59.  
  60.     if (t)
  61.     {
  62.         s[0]++;
  63.         s[s[0]]=t;
  64.     }
  65.     for(i=s[0]; i>0; i--)
  66.         printf("%d ",s[i]);
  67.     printf("\n");
  68. }
  69.  
  70. void diferenta(int a[200], int b[200])
  71. {
  72.  
  73.     int d[200];
  74.  
  75.     int i, t = 0;
  76.     for(i = 1; i <= a[0]; i++)
  77.     {
  78.         d[i] = a[i] - b[i] + t;
  79.         if(d[i] < 0)
  80.         {
  81.  
  82.             d[i] += 10;
  83.             t = -1;
  84.         }
  85.         else
  86.             t = 0;
  87.     }
  88.     i--;
  89.     while(i && !d[i])
  90.         i--;
  91.     if(i == 0)
  92.     {
  93.         for(i=a[0]; i>0; i--)
  94.             printf("%d ",a[i]);
  95.         printf("\n");
  96.         for(i=a[0]; i>0; i--)
  97.             printf("%d ",b[i]);
  98.         printf("\n");
  99.         printf("0");
  100.     }
  101.     else
  102.     {
  103.         d[0] = i;
  104.         int j;
  105.         for(i=a[0]; i>0; i--)
  106.             printf("%d ",a[i]);
  107.         printf("\n");
  108.         for(i=a[0]; i>0; i--)
  109.             printf("%d ",b[i]);
  110.         printf("\n");
  111.         for(i = d[0]; i > 0; i--)
  112.             printf("%d ", d[i]);
  113.         printf("\n");
  114.  
  115.     }
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement