Created
February 26, 2016 17:50
-
-
Save HyeonWooKim/80d51a9ab4d22a252411 to your computer and use it in GitHub Desktop.
스택을 이용한 수식 계산기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************** | |
수식 입력 계산기 | |
제작 : 20103324(국민대학교) | |
***********************/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define MAX_SIZE 256 | |
int isOperator(char str); | |
void getNumber(char *str, int startPos); | |
int isNumber(char str); | |
int Calculate(char str); | |
char data[MAX_SIZE]; | |
int stack[MAX_SIZE]; //수를 얻어내어 저장하는 곳 | |
int stackCount=0; // 현재 스택에 쌓인 데이터의 위치 | |
/************************************************************************/ | |
/*방식 | |
1. int 스택에 연산자의 배열 위치를 저장한다. | |
2. 식의 점검이 끝나면 부호의 확인을 통해 우선연산을 정한다. | |
3. 우선연산을 할 위치의 연산자 주변 숫자를 얻어낸다. | |
4. 얻어낸 수를 단위별로 연산하여 결과를 합친다. | |
5. 최종값을 출력한다.*/ | |
void main() | |
{ | |
int i=0,j,sum=0; | |
printf("수식을 입력하세요.\n"); | |
gets(data); | |
while(data[i]!='=') | |
{ | |
if(isOperator(data[i])) | |
{ | |
stack[stackCount]=i, stackCount++; | |
} | |
i++; | |
} | |
j=stackCount++; // 현재 사용한 스택을 백업한다. | |
//우선순위 : *,/를 먼저 계산한 후 +,-를 계산한다. | |
for(i=0; i<stackCount; i++) | |
{ | |
if(stack[stackCount]=='*'||stack[stackCount]=='/') | |
{ | |
getNumber(data, i); | |
stack[++stackCount]=Calculate(stack[stackCount-1]); | |
} | |
} | |
for(i=0; i<stackCount; i++) | |
{ | |
if(stack[stackCount]=='+'||stack[stackCount]=='-') | |
{ | |
getNumber(data, i); | |
stack[++stackCount]=Calculate(stack[stackCount-1]); | |
} | |
} | |
while(stackCount>j) | |
{ | |
sum+=stack[stackCount]; | |
} | |
printf("Ansswer : %d",sum); | |
} | |
int isOperator(char str) | |
{ | |
return (str=='+'||str=='-'||str=='*'||str=='/') ? TRUE : FALSE; | |
} | |
int isNumber(char str) | |
{ | |
return (str<'0'||str>'9') ? FALSE : TRUE; | |
} | |
void getNumber(char *str, int startPos) | |
{ | |
int temp=1, pos=startPos; | |
//먼저 왼쪽의 수를 얻어낸다. | |
while(isNumber(str[--pos])) | |
{ | |
stack[stackCount]+=temp*(str[pos]-'0'); | |
temp*=10; | |
} | |
stackCount++, temp=1, pos=startPos; // 스택카운트를 증가시키고, temp 및 pos를 초기화한다. | |
while(isNumber(str[++pos])){} | |
while(isNumber(str[--pos])) | |
{ | |
stack[stackCount]+=temp*(str[pos]-'0'); | |
temp*=10; | |
} | |
stackCount++; | |
} | |
int Calculate(char str) | |
{ | |
int temp=stackCount, ret; | |
ret=(str=='*'||str=='/') ? 1 : 0; | |
switch (str) | |
{ | |
case '+': | |
while(temp) | |
{ | |
ret+=(stack[--temp]); | |
} | |
break; | |
case '*': | |
while(temp) | |
{ | |
ret*=(stack[--temp]); | |
} | |
break; | |
case '-': | |
ret=(stack[temp-2]-stack[temp-1]); | |
break; | |
case '/': | |
ret=(stack[temp-2]/stack[temp-1]); | |
break; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment