This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercicio_pilha_17.c
97 lines (88 loc) · 1.63 KB
/
exercicio_pilha_17.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdlib.h>
#include<stdio.h>
#define MAX 10
#define bool int
#define true 1
#define false 0
typedef struct stack{
int numeros[MAX];
int top;
int bottom;
int devide;
}stack;
void listar(stack *s);
void pop (stack *s, int *n);
bool push(stack *s, int *n);
bool full(stack *s);
bool empty(stack *s);
void reset (stack *s);
void fib_preenche(stack *s, int *c);
int main(int argc, char const *argv[])
{
stack *p1 = (stack*) malloc (sizeof(stack));
int c;
printf("Quantos numeros da sequencia de Fibonaci quer armazenar? ");
scanf("%d",&c);
fib_preenche(p1,&c);
listar(p1);
free(p1);
return 0;
}
void fib_preenche(stack *s, int *c){
int guarda1,guarda2,aux;
while(*c>0){
if(s->top == 1 || s->top == 0 ){
aux = 1;
push(s,&aux);
(*c)--;
continue;
}
pop(s,&guarda1);
pop(s,&guarda2);
aux = guarda1+guarda2;
push(s,&guarda2);
push(s,&guarda1);
push(s,&aux);
(*c)--;
}
}
void reset (stack *s){
s->top =0;
s->bottom =0;
s->devide =0;
}
bool empty(stack *s){
return s->top==0;
}
bool full(stack *s){
return s->top==MAX;
}
bool push(stack *s, int *n){
if(!full(s)){
s->numeros[s->top] = *n;
s->top++;
return true;
}
printf("--STACK FULL. ABORTING.--\n");
return false;
}
void pop (stack *s, int *n){
if(!empty(s)){
s->top--;
*n = s->numeros[s->top];
}
}
void listar(stack *s){
int aux;
stack *s_aux = (stack*) malloc (sizeof (stack));
while(!empty(s)){
pop(s,&aux);
printf(" %d \n", aux);
push(s_aux,&aux);
}
while(!empty(s_aux)){//coloca de volta em s tudo que eu preciso manter guardado.
pop(s_aux,&aux);
push(s,&aux);
}
free(s_aux);
}