Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c5d1c4e

Browse files
Create In2Post.c
1 parent 49adfeb commit c5d1c4e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

‎DataStructure/In2Post.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <string.h>
4+
5+
#define MAX_SIZE 10
6+
7+
typedef struct {
8+
char data[MAX_SIZE];
9+
int top;
10+
} Stack;
11+
12+
void initialize(Stack* stack) {
13+
stack->top = -1;
14+
}
15+
16+
bool isFull(Stack* stack) {
17+
return stack->top == MAX_SIZE - 1;
18+
}
19+
20+
bool isEmpty(Stack* stack) {
21+
return stack->top == -1;
22+
}
23+
24+
void push(Stack* stack, char data) {
25+
if (!isFull(stack)) {
26+
stack->data[++(stack->top)] = data;
27+
}
28+
}
29+
30+
char pop(Stack* stack) {
31+
if (!isEmpty(stack)) {
32+
return stack->data[(stack->top)--];
33+
}
34+
}
35+
36+
int evaluate(char exp[]) {
37+
Stack s;
38+
initialize(&s);
39+
char ch;
40+
int value;
41+
for (int i = 0; i < strlen(exp); i++) {
42+
ch = exp[i];
43+
44+
if(ch != '*' && ch != '/' && ch != '+' && ch != '-') {
45+
value = ch - '0';
46+
push(&s, value);
47+
}
48+
else {
49+
char op2 = pop(&s);
50+
char op1 = pop(&s);
51+
52+
switch (ch) {
53+
case '+':
54+
push(&s, op1 + op2);
55+
break;
56+
case '-':
57+
push(&s, op1 - op2);
58+
break;
59+
case '*':
60+
push(&s, op1 * op2);
61+
break;
62+
case '/':
63+
push(&s, op1 / op2);
64+
break;
65+
}
66+
}
67+
}
68+
return pop(&s);
69+
}
70+
71+
int main(void) {
72+
char exp[] = { "82/3-32*+" };
73+
printf("%s의 연산 결과 : %d", exp, evaluate(exp));
74+
75+
return 0;
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /