-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshinobus_saga.c
151 lines (129 loc) · 3.01 KB
/
shinobus_saga.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DEBUG if(1)
//Armazena o primeiro item da fila
typedef struct _stack STACK;
//Armazena o conteudo do no e um ponteiro para o proximo no da fila
typedef struct _node NODE;
struct _stack
{
NODE* head;
};
struct _node
{
int element;
NODE* next;
};
//Cria uma stack com o a head NULL
STACK* Create_stack()
{
STACK* new_head = (STACK*) malloc(sizeof(STACK));
new_head->head = NULL;
return new_head;
}
//Recebe um elemento e cria e retorna um novo node
// com o elemento passado
NODE* create_node(int element)
{
NODE* new_node = (NODE*) malloc(sizeof(NODE));
new_node->element = element;
new_node->next = NULL;
return new_node;
}
//Verifica se a pilha esta vazia
int IS_EMPTY(STACK* stack)
{
return(stack->head == NULL);
}
//Recebe uma pilha e Retorna o elemento que esta no topo da fila
int POP(STACK* stack)
{
NODE* tmp = stack->head->next;
int t = stack->head->element;
free(stack->head);
stack->head = tmp;
return t;
}
//Recebe uma pilha e um inteiro e retorna a nova pilha
//Adiciona um novo node no topo da pilha
void PUSH(STACK* stack, int element)
{
NODE* tmp = create_node(element);
tmp->next = stack->head;
tmp->element = element;
stack->head = tmp;
}
//Recebe a pilha e a operacao a ser feita
//faz a operacao da calculadora
void result(STACK* stack, char operation)
{
int a,b,ans;
a = POP(stack);
b = POP(stack);
switch (operation)
{
case '+':
ans = a+b;
PUSH(stack,ans);
break;
case '-':
ans = b-a;
PUSH(stack,ans);
break;
case '*':
ans = a*b;
PUSH(stack,ans);
break;
case '/':
ans = b/a;
PUSH(stack,ans);
break;
default:
break;
}
}
//Recebe uma pilha vazia e quantas strings serao lidas
//Le as n strings que vao seguir e resolve as expressoes
void Calculadora(STACK* calculadora, int size)
{
char c[size];
int t;
for (int i = 0; i < size ; i++)
{
//TODO must treat multiple digits
scanf("%s",c);
if(c[i] == '\n'){--i;continue;}
switch (c[0])
{
case '+':
result(calculadora,'+');
break;
case '-':
result(calculadora,'-');
break;
case '*':
result(calculadora,'*');
break;
case '/':
result(calculadora,'/');
break;
default:
t = atoi(c);
PUSH(calculadora,t);
break;
}
}
}
int main()
{
STACK* calculadora = Create_stack();
int k;
scanf("%d", &k);
Calculadora(calculadora, k);
printf("Coordenada 1: %d\n", POP(calculadora));
scanf("%d", &k);
Calculadora(calculadora, k);
printf("Coordenada 2: %d\n", POP(calculadora));
}
//Insira o código aqui