Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct STOS{
- int key;
- struct STOS *next;
- };
- void push(struct STOS **Head, int x)
- {
- struct STOS *newHead=NULL;
- newHead=(struct STOS*)malloc(sizeof(struct STOS)); // zaalokowanie komórki pamięci o rozmiarze struktury "STOS"
- newHead->key=x;
- newHead->next=*Head;
- *Head=newHead;
- printf("Umieszczono %d na stosie\n", x);
- }
- void pop(struct STOS **Head)
- {
- if(Head==NULL) printf("Stos jest pusty!");
- struct STOS *temporaryHead=*Head;
- *Head=temporaryHead->next;
- free(temporaryHead);
- }
- /* ******************************************************** */
- void zapisz(int dana, struct STOS **head)
- {
- printf("Zapisuje...\n");
- int zapis;
- if((dana%10)==0) push(head, dana);
- else
- {
- while((dana % 10) != 0)
- {
- zapis = (dana % 10);
- push(head,zapis);
- dana = (dana/10);
- }
- }
- }
- /* ******************************************************** */
- void przepisz(struct STOS *headStary, struct STOS *headNowy)
- {
- printf("Przepisuje...\n");
- int klucz;
- while(headStary)
- {
- klucz = (headStary -> key);
- push(&headNowy, klucz);
- headStary=headStary->next;
- }
- }
- /* ******************************************************** */
- void sumuj(struct STOS *head1,struct STOS *head2,struct STOS *headWynik)
- {
- printf("Sumuje...\n");
- push(&headWynik, 0);
- int counter = 2;
- while(counter>0)
- {
- int suma = 0;
- printf("Sumuje...\n");
- push(&(headWynik->next),0);
- suma = ((head1 -> key) + (head2 -> key));
- if(suma>9)
- {
- headWynik -> key += (suma % 10);
- headWynik -> next -> key += 1;
- }
- else
- {
- headWynik -> key += suma;
- }
- head1 = head1 -> next;
- head2 = head2 -> next;
- headWynik = headWynik -> next;
- }
- counter--;
- }
- /* ******************************************************** */
- int main()
- {
- struct STOS *head1start = NULL;
- struct STOS *head2start = NULL;
- struct STOS *head1nowy = NULL;
- struct STOS *head2nowy = NULL;
- struct STOS *headWynik = NULL;
- int pierwsza, druga, wynik;
- printf("\nPodaj pierwsza liczbe:");
- scanf("%d", &pierwsza);
- printf("\nPodaj druga liczbe:");
- scanf("%d", &druga);
- zapisz(pierwsza, &head1start);
- zapisz(druga, &head2start);
- przepisz(head1start, head1nowy);
- przepisz(head2start, head2nowy);
- sumuj(head1nowy, head2nowy, headWynik);
- printf("\nSuma podanych liczb wynosi:");
- while(headWynik)
- {
- printf("%d", headWynik -> key);
- headWynik = headWynik -> next;
- }
- printf("\n");
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement