본문내용
0, j = 0; i < size; i++, j++)
{
// 숫자나 ')' 다음에 '-'가 오면 뺄셈으로 인식
if(((temp[i] >= '0'
&& temp[i] <= '9') || temp[i] == ')')
&& temp[i + 1] == '-')
{
str[j++] = temp[i++];
str[j] = temp[i];
}
// 처음 나오는 '-'와 연산자, '(' 다음에 나오는 '-'는 음수로 인식
else if(temp[i] == '-'
&& (i == 0
|| temp[i - 1] == '('
|| temp[i - 1] == '*'
|| temp[i - 1] == '-'
|| temp[i - 1] == '+'
|| temp[i - 1] == '%'))
{
mcount = 0;
while(temp[i] == '-')
{
i++;
mcount++;
}
i--;
if(mcount % 2 == 1) //홀수일 경우
{
str[j] = '-';
}
else //짝수일 경우
{
j--;
}
}
// 뺄셈이나 음수와 관련없는 것들은 그냥 넣음
else
{
str[j] = temp[i];
}
}
str[j] = '\0';
}
////////////////////////////////////////////////////////////////////////////////
//
// void getLine(char *temp)
// - 사용자의 입력값중 공백을 제외하고 읽어온다.
//
////////////////////////////////////////////////////////////////////////////////
void getLine(char *temp)
{
int i = 0;
char c;
while((c = getchar()) != '\n')
{
if(c == ' ')
{
continue;
}
else
{
*(temp + i) = c;
i++;
}
}
*(temp + i) = '\0';
}
////////////////////////////////////////////////////////////////////////////////
//
// int main()
// - 중위식을 입력받아 계산 후 결과 출력
//
////////////////////////////////////////////////////////////////////////////////
int main()
{
int i, j;
char strtemp[MAX_ITEM]; // 중위식 임시 저장
char temp[10]; // 숫자 저장공간
precedence token; // 연산자와 피연산자의 고유값
do
{
printf("Infix expression:\n");
getLine(strtemp);
minusHandling(strtemp);
} while(!isInfix());
operator_stack[0] = eos; // 연산자 스택에 end of stack값을 넣음
for(i = 0; i < (int)strlen(str) + 1; i++)
{
token = token_cost(i);
// 토큰이 피연산자일 때
if(token == operand && (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
j=0;
// 연산자가 나타날 때 까지 숫자를 기억
while((str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
temp[j++]=str[i++];
}
temp[j]='\0';
operand_push(ctof(temp));
i--;
continue;
}
//음수일 때
else if(str[i] == '-' && (i == 0
|| str[i-1] == '('
|| str[i - 1] == '*'
|| str[i - 1] == '-'
|| str[i - 1] == '+'
|| str[i - 1] == '%'))
{
i++;
// '-'기호 다음이 피연산자일 경우
if(str[i] >= '0' && str[i] <= '9')
{
j = 0;
// 연산자가 나타날 때까지 숫자기억
while((str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
temp[j++] = str[i++];
}
temp[j] = '\0';
operand_push(0 - ctof(temp));
i--;
continue;
}
// '-'기호 다음이 좌괄호일 경우
else if(str[i]=='(')
{
operator_push(flag);
operator_push(lparen);
continue;
}
}
// 토큰이 연산자일 경우
if(token == eos)
{
while(operator_stack[operator_top]!=eos)
{
sum(operator_pop());
}
printf("Result = %.2f\n",operand_pop());
exit(0);
}
// 우괄호일 경우
else if(token==rparen)
{
// 좌괄호가 나올때 까지 연산
while(operator_stack[operator_top] != lparen)
{
sum(operator_pop());
}
operator_pop();
if(operator_stack[operator_top] == flag)
{
operand_push(0-operand_pop());
operator_pop();
}
}
// 스택에 있는 연산자의 우선순위가 작을 경우
else if(isp[operator_stack[operator_top]] < icp[token])
{
operator_push(token);
}
// 스택에 있는 연산자의 우선순위가 작지 않을 경우
else if(isp[operator_stack[operator_top]] >= icp[token])
{
// 스택에 있는 값의 우선순위가 작을 때 까지 계산
while(isp[operator_stack[operator_top]] >= icp[token])
sum(operator_pop());
operator_push(token);
}
}
return 0;
}
{
// 숫자나 ')' 다음에 '-'가 오면 뺄셈으로 인식
if(((temp[i] >= '0'
&& temp[i] <= '9') || temp[i] == ')')
&& temp[i + 1] == '-')
{
str[j++] = temp[i++];
str[j] = temp[i];
}
// 처음 나오는 '-'와 연산자, '(' 다음에 나오는 '-'는 음수로 인식
else if(temp[i] == '-'
&& (i == 0
|| temp[i - 1] == '('
|| temp[i - 1] == '*'
|| temp[i - 1] == '-'
|| temp[i - 1] == '+'
|| temp[i - 1] == '%'))
{
mcount = 0;
while(temp[i] == '-')
{
i++;
mcount++;
}
i--;
if(mcount % 2 == 1) //홀수일 경우
{
str[j] = '-';
}
else //짝수일 경우
{
j--;
}
}
// 뺄셈이나 음수와 관련없는 것들은 그냥 넣음
else
{
str[j] = temp[i];
}
}
str[j] = '\0';
}
////////////////////////////////////////////////////////////////////////////////
//
// void getLine(char *temp)
// - 사용자의 입력값중 공백을 제외하고 읽어온다.
//
////////////////////////////////////////////////////////////////////////////////
void getLine(char *temp)
{
int i = 0;
char c;
while((c = getchar()) != '\n')
{
if(c == ' ')
{
continue;
}
else
{
*(temp + i) = c;
i++;
}
}
*(temp + i) = '\0';
}
////////////////////////////////////////////////////////////////////////////////
//
// int main()
// - 중위식을 입력받아 계산 후 결과 출력
//
////////////////////////////////////////////////////////////////////////////////
int main()
{
int i, j;
char strtemp[MAX_ITEM]; // 중위식 임시 저장
char temp[10]; // 숫자 저장공간
precedence token; // 연산자와 피연산자의 고유값
do
{
printf("Infix expression:\n");
getLine(strtemp);
minusHandling(strtemp);
} while(!isInfix());
operator_stack[0] = eos; // 연산자 스택에 end of stack값을 넣음
for(i = 0; i < (int)strlen(str) + 1; i++)
{
token = token_cost(i);
// 토큰이 피연산자일 때
if(token == operand && (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
j=0;
// 연산자가 나타날 때 까지 숫자를 기억
while((str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
temp[j++]=str[i++];
}
temp[j]='\0';
operand_push(ctof(temp));
i--;
continue;
}
//음수일 때
else if(str[i] == '-' && (i == 0
|| str[i-1] == '('
|| str[i - 1] == '*'
|| str[i - 1] == '-'
|| str[i - 1] == '+'
|| str[i - 1] == '%'))
{
i++;
// '-'기호 다음이 피연산자일 경우
if(str[i] >= '0' && str[i] <= '9')
{
j = 0;
// 연산자가 나타날 때까지 숫자기억
while((str[i] >= '0' && str[i] <= '9') || str[i] == '.')
{
temp[j++] = str[i++];
}
temp[j] = '\0';
operand_push(0 - ctof(temp));
i--;
continue;
}
// '-'기호 다음이 좌괄호일 경우
else if(str[i]=='(')
{
operator_push(flag);
operator_push(lparen);
continue;
}
}
// 토큰이 연산자일 경우
if(token == eos)
{
while(operator_stack[operator_top]!=eos)
{
sum(operator_pop());
}
printf("Result = %.2f\n",operand_pop());
exit(0);
}
// 우괄호일 경우
else if(token==rparen)
{
// 좌괄호가 나올때 까지 연산
while(operator_stack[operator_top] != lparen)
{
sum(operator_pop());
}
operator_pop();
if(operator_stack[operator_top] == flag)
{
operand_push(0-operand_pop());
operator_pop();
}
}
// 스택에 있는 연산자의 우선순위가 작을 경우
else if(isp[operator_stack[operator_top]] < icp[token])
{
operator_push(token);
}
// 스택에 있는 연산자의 우선순위가 작지 않을 경우
else if(isp[operator_stack[operator_top]] >= icp[token])
{
// 스택에 있는 값의 우선순위가 작을 때 까지 계산
while(isp[operator_stack[operator_top]] >= icp[token])
sum(operator_pop());
operator_push(token);
}
}
return 0;
}