Advertisement
olekturbo

dodawanie

Nov 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. //DODAWANIE DUZYCH LICZB
  2. // ALEKSANDER SZEWCZAK
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void)
  7. {
  8. unsigned int ile, ile2;
  9. int i, max, r;
  10. printf("Podaj ilosc cyfr w liczbie nr 1: ");
  11. scanf("%d", &ile);
  12. printf("Podaj ilosc cyfr w liczbie nr 2: ");
  13. scanf("%d", &ile2);
  14. int tab[ile], tab2[ile2];
  15.  
  16. //LICZBA NR 1 -- WCZYTYWANIE
  17. printf("\n\nPodaj liczbe nr 1:\n");
  18. for(i = ile-1; i >= 0; i--)
  19. {
  20. scanf("%1d", &tab[i]);
  21. }
  22.  
  23. //LICZBA NR 2 -- WCZYTYWANIE
  24. printf("\n\nPodaj liczbe nr 2:\n");
  25. for(i = ile2-1; i >=0 ; i--)
  26. {
  27. scanf("%1d", &tab2[i]);
  28. }
  29.  
  30. //LICZBA NR 1 -- WYPISYWANIE
  31. for(i = ile-1; i >= 0; i--)
  32. {
  33. printf("%d", tab[i]);
  34. }
  35.  
  36. printf(" + ");
  37.  
  38. //LICZBA NR 2 -- WYPISYWANIE
  39. for(i = ile2-1; i >= 0; i--)
  40. {
  41. printf("%d", tab2[i]);
  42. }
  43.  
  44.  
  45. if(ile>ile2) max = ile;
  46. else max = ile2;
  47.  
  48. int s[max+1]; //TABLICA WYNIKU
  49.  
  50. for(i = 0; i < max+1; i++) s[i] = 0; //WYPELNIENIE TABLICY WYNIKU ZERAMI
  51.  
  52. for(i = 0; i < max; i++)
  53. {
  54. r = 0;
  55.  
  56. if(i < ile) s[i] += tab[i];
  57. if(i < ile2) s[i] += tab2[i];
  58. if(s[i] > 9)
  59. {
  60. r = s[i]/10;
  61. s[i] %= 10;
  62. s[i+1] += r;
  63. }
  64. }
  65.  
  66. for(i = 0; i < max; i++)
  67. {
  68. if(s[max] == 0) max--; // ODEJMOWANIE ZERA
  69. }
  70.  
  71. printf(" = ");
  72. for(i = max; i >= 0; i--) printf("%d", s[i]);
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement