C언어 스택으로 구현한 계산기 소스
본 자료는 5페이지 의 미리보기를 제공합니다. 이미지를 클릭하여 주세요.
닫기
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
해당 자료는 5페이지 까지만 미리보기를 제공합니다.
5페이지 이후부터 다운로드 후 확인할 수 있습니다.

소개글

C언어 스택으로 구현한 계산기 소스에 대한 보고서 자료입니다.

본문내용

= 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;
}

키워드

  • 가격1,000
  • 페이지수15페이지
  • 등록일2007.06.22
  • 저작시기2007.6
  • 파일형식한글(hwp)
  • 자료번호#416336
본 자료는 최근 2주간 다운받은 회원이 없습니다.
다운로드 장바구니