Advertisement
Guest User

SumowanieANSIC

a guest
May 6th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct STOS{
  4. int key;
  5. struct STOS *next;
  6. };
  7.  
  8. void push(struct STOS **Head, int x)
  9. {
  10. struct STOS *newHead=NULL;
  11.  
  12. newHead=(struct STOS*)malloc(sizeof(struct STOS)); // zaalokowanie komórki pamięci o rozmiarze struktury "STOS"
  13.  
  14.  
  15. newHead->key=x;
  16. newHead->next=*Head;
  17. *Head=newHead;
  18. printf("Umieszczono %d na stosie\n", x);
  19.  
  20. }
  21.  
  22. void pop(struct STOS **Head)
  23. {
  24. if(Head==NULL) printf("Stos jest pusty!");
  25.  
  26. struct STOS *temporaryHead=*Head;
  27.  
  28. *Head=temporaryHead->next;
  29. free(temporaryHead);
  30.  
  31. }
  32. /* ******************************************************** */
  33.  
  34. void zapisz(int dana, struct STOS **head)
  35. {
  36. printf("Zapisuje...\n");
  37. int zapis;
  38.  
  39. if((dana%10)==0) push(head, dana);
  40. else
  41. {
  42.  
  43. while((dana % 10) != 0)
  44. {
  45. zapis = (dana % 10);
  46. push(head,zapis);
  47. dana = (dana/10);
  48. }
  49. }
  50. }
  51.  
  52. /* ******************************************************** */
  53.  
  54. void przepisz(struct STOS *headStary, struct STOS *headNowy)
  55. {
  56. printf("Przepisuje...\n");
  57. int klucz;
  58. while(headStary)
  59. {
  60. klucz = (headStary -> key);
  61. push(&headNowy, klucz);
  62. headStary=headStary->next;
  63. }
  64. }
  65.  
  66. /* ******************************************************** */
  67.  
  68. void sumuj(struct STOS *head1,struct STOS *head2,struct STOS *headWynik)
  69. {
  70.  
  71. printf("Sumuje...\n");
  72. push(&headWynik, 0);
  73. int counter = 2;
  74. while(counter>0)
  75. {
  76. int suma = 0;
  77. printf("Sumuje...\n");
  78.  
  79. push(&(headWynik->next),0);
  80.  
  81.  
  82.  
  83. suma = ((head1 -> key) + (head2 -> key));
  84.  
  85.  
  86.  
  87. if(suma>9)
  88. {
  89. headWynik -> key += (suma % 10);
  90. headWynik -> next -> key += 1;
  91. }
  92.  
  93. else
  94. {
  95. headWynik -> key += suma;
  96. }
  97. head1 = head1 -> next;
  98. head2 = head2 -> next;
  99. headWynik = headWynik -> next;
  100. }
  101. counter--;
  102. }
  103. /* ******************************************************** */
  104.  
  105. int main()
  106. {
  107. struct STOS *head1start = NULL;
  108. struct STOS *head2start = NULL;
  109. struct STOS *head1nowy = NULL;
  110. struct STOS *head2nowy = NULL;
  111. struct STOS *headWynik = NULL;
  112.  
  113. int pierwsza, druga, wynik;
  114.  
  115. printf("\nPodaj pierwsza liczbe:");
  116. scanf("%d", &pierwsza);
  117. printf("\nPodaj druga liczbe:");
  118. scanf("%d", &druga);
  119.  
  120. zapisz(pierwsza, &head1start);
  121. zapisz(druga, &head2start);
  122.  
  123. przepisz(head1start, head1nowy);
  124. przepisz(head2start, head2nowy);
  125.  
  126. sumuj(head1nowy, head2nowy, headWynik);
  127.  
  128. printf("\nSuma podanych liczb wynosi:");
  129.  
  130. while(headWynik)
  131. {
  132. printf("%d", headWynik -> key);
  133. headWynik = headWynik -> next;
  134. }
  135.  
  136. printf("\n");
  137.  
  138. system("PAUSE");
  139.  
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement