목차
1. 문제개요
2. 알고리즘
3. 소스 및 주석
4. 실행결과
5. 느낀점
2. 알고리즘
3. 소스 및 주석
4. 실행결과
5. 느낀점
본문내용
return 10;
}
}
int Postfix_cal::ICP(char x) {
// 연산자 임시 스택의 연산자들과 비교할 연산자의 우선순위
switch(x) {
case '+' : case '-' : return 4;
case '*' : case '/' : case '%' : return 3;
case '^' : return 2;
case '(' : return 0;
default : return 10;
}
}
void Postfix_cal::calPostfix() {
// 후위표기식을 계산하는 함수
Stack temp;
float ft1=0, ft2=0;
for(int i=0;i<=postfix.top;i++) {
if(postfix.data[i].isalp) {
// 스택에 있는 자료가 연산자일 때
char op=(char)postfix.data[i].fldigit;
if(isOper(op) && op!='#') {
// 계산 스택에서 두개의 수를 pop해서 연산자에 맞게 계산
if(temp.top>=1) {
ft2=temp.pop();
ft1=temp.pop();
if(op=='+') temp.push(ft1+ft2);
else if(op=='-') temp.push(ft1-ft2);
else if(op=='*') temp.push(ft1*ft2);
else if(op=='/') temp.push(ft1/ft2);
else if(op=='^') temp.push((float)pow(ft1,(int)ft2));
else if(op=='%') temp.push((float)((int)ft1%(int)ft2));
}
else if(temp.top==-1) {
cout << "수식이 입력되지 않았습니다." << endl;
}
}
else if(temp.top==0 && op=='#') {
cout << "계산결과 : " << temp.pop() << endl;
}
}
// 스택에 있는 자료가 숫자일 때 계산 스택에 push
else temp.push(postfix.data[i].fldigit);
}
}
bool Postfix_cal::isOper(char op) {
// 연산자인지 확인하는 함수
if(op=='+' || op=='-' || op=='*' || op=='/') return true;
else if(op=='%' || op=='^' || op=='(' || op==')') return true;
else if(op=='#') return true;
else return false;
}
#include "Postfix_cal.h"
int main() {
for(int i=0;i<5;i++) {
// 다섯 번 연산
Postfix_cal *temp=new Postfix_cal();
temp->input();
delete temp;
}
return 0;
}
4. 실행결과 화면출력
45+5-4-5-3-2-6*4 = 12
2^2^3 = 256
3*7/2 = 10.5
2-(4*5-13) = -5
10+3-4+5-6+7-(8*2) = -1
5. 느낀점
중위표기로 된 수식을 후위표기로 표현하는 방법을 스택이라는 자료구조로 구현하였습니다. 스택의 LIFO라는 특수성을 이용하여 연산자의 우선순위 비교에 따른 push(), pop()을 직접 컨트롤할 수 있었습니다. 실제 컴퓨터가 연산하는 방법을 완벽히 수행할 수는 없지만 조금이나마 비슷하게 직접 만든 것에 대해 자신감이 생겼습니다.
과제를 수행하기 위해 축제기간을 포함하여 몇 일간을 작업했지만 결과를 보니 흐뭇합니다. 앞으로 나오는 과제를 잘 해내기 위해 열심히 공부할 것입니다.
}
}
int Postfix_cal::ICP(char x) {
// 연산자 임시 스택의 연산자들과 비교할 연산자의 우선순위
switch(x) {
case '+' : case '-' : return 4;
case '*' : case '/' : case '%' : return 3;
case '^' : return 2;
case '(' : return 0;
default : return 10;
}
}
void Postfix_cal::calPostfix() {
// 후위표기식을 계산하는 함수
Stack
float ft1=0, ft2=0;
for(int i=0;i<=postfix.top;i++) {
if(postfix.data[i].isalp) {
// 스택에 있는 자료가 연산자일 때
char op=(char)postfix.data[i].fldigit;
if(isOper(op) && op!='#') {
// 계산 스택에서 두개의 수를 pop해서 연산자에 맞게 계산
if(temp.top>=1) {
ft2=temp.pop();
ft1=temp.pop();
if(op=='+') temp.push(ft1+ft2);
else if(op=='-') temp.push(ft1-ft2);
else if(op=='*') temp.push(ft1*ft2);
else if(op=='/') temp.push(ft1/ft2);
else if(op=='^') temp.push((float)pow(ft1,(int)ft2));
else if(op=='%') temp.push((float)((int)ft1%(int)ft2));
}
else if(temp.top==-1) {
cout << "수식이 입력되지 않았습니다." << endl;
}
}
else if(temp.top==0 && op=='#') {
cout << "계산결과 : " << temp.pop() << endl;
}
}
// 스택에 있는 자료가 숫자일 때 계산 스택에 push
else temp.push(postfix.data[i].fldigit);
}
}
bool Postfix_cal::isOper(char op) {
// 연산자인지 확인하는 함수
if(op=='+' || op=='-' || op=='*' || op=='/') return true;
else if(op=='%' || op=='^' || op=='(' || op==')') return true;
else if(op=='#') return true;
else return false;
}
#include "Postfix_cal.h"
int main() {
for(int i=0;i<5;i++) {
// 다섯 번 연산
Postfix_cal *temp=new Postfix_cal();
temp->input();
delete temp;
}
return 0;
}
4. 실행결과 화면출력
45+5-4-5-3-2-6*4 = 12
2^2^3 = 256
3*7/2 = 10.5
2-(4*5-13) = -5
10+3-4+5-6+7-(8*2) = -1
5. 느낀점
중위표기로 된 수식을 후위표기로 표현하는 방법을 스택이라는 자료구조로 구현하였습니다. 스택의 LIFO라는 특수성을 이용하여 연산자의 우선순위 비교에 따른 push(), pop()을 직접 컨트롤할 수 있었습니다. 실제 컴퓨터가 연산하는 방법을 완벽히 수행할 수는 없지만 조금이나마 비슷하게 직접 만든 것에 대해 자신감이 생겼습니다.
과제를 수행하기 위해 축제기간을 포함하여 몇 일간을 작업했지만 결과를 보니 흐뭇합니다. 앞으로 나오는 과제를 잘 해내기 위해 열심히 공부할 것입니다.