[c언어질문]중위연산자를 후위연산자로 바까서 계산하는거도와주
      글쓴이: lh8557 / 작성시간: 월, 2004/04/26 - 7:07오후    
  
  후위연산자로 바꾸는거 까지는 했는데 이항연산자를 인식못해요 단항으로만인식하구 코딩 그리고 계산하는 함수를 작성못하겠어요..어떻게 해야할지좀 
가르켜주세요..; c언어로 코딩해야대요..;; 코딩보시구 계산하는 함수하고
이항연산자 인식하는 ..거...방향좀 제시좀 해주세요..부탁드립니다.
#include <stdio.h>
// 문자를 저장하는 스택과 함수 정의 
#define MAX_STACK_SIZE 100
#define MAX_EXPR_SIZE  100
#define CHAR_STACK_SIZE 100
char char_stack[CHAR_STACK_SIZE];
int char_top = -1;
int char_Isfull()
{
	if(char_top>=CHAR_STACK_SIZE-1)
		return 1;
	else 
		return 0;
}
int char_Isempty()
{
	if(char_top<=-1)
		return 1;
	else 
		return 0;
}
void char_stack_full()
{
	printf("Stack is Full! : \n");
}
void char_stack_add(char item)
{
	if(char_top>=CHAR_STACK_SIZE-1)
	{
		char_stack_full();
		return;
	}
	char_stack[++char_top] = item;
}
char char_stack_empty()
{
	printf("Stack is Empty! : \n");
	char_stack[0] = '0';
	return char_stack[0];
}
char char_stack_delete()
{
	char item;
	if(char_top <= -1)
		return char_stack_empty();
		item = char_stack[char_top--];
	return item;
}
// 문자를 저장하는 스택 정의 끝 
// 함수 Prototype 
int operand(char x);
int Isp(char x);
int Icp(char x);
void postfix(char *token, char *post);
void main()
{
	char expr[MAX_EXPR_SIZE];
	char post[50];
	char c = 'y';
	while(c=='y')
	{
		printf("enter infix expression : \n");
		scanf("%s", expr);
		postfix(expr, post);
		printf("postfix notation : \n%s", post);
		printf("\nDo you want to do another expression('y'or'n')? ");
		scanf("%s", &c);
	}
}
int operand(char x)
{
	char op[16] = {'(', ')', '+', '-', '*', '/', '%', '++', '--', '==', '!=', '&', '^', '&&', '||', '\0'};
	for(int i=0; i<16; i++)
		if(op[i]==x)
			return 0;
	return 1;
}
int Isp(char x)
{
	char op[16] = {'(', ')', '+', '-', '*', '/', '%', '++', '--', '==', '!=', '&', '^', '&&', '||', '\0'};
	int sp[16] = {0,19,12,12,13,13,13,16,16,9,9,8,7,5,4,0};
	int i;
	for(i=0; i<16; i++)
		if(op[i]==x)
			return sp[i];
	printf(" Isp error \n");
	return 0;
}
int Icp(char x)
{
	char op[16] = {'(', ')', '+', '-', '*', '/', '%', '++', '--', '==', '!=', '&', '^', '&&', '||', '\0'};
	int cp[16] = {0,19,12,12,13,13,13,16,16,9,9,8,7,5,4,0};
	int i;
	for(i=0; i<16; i++)
		if(op[i]==x)
			return cp[i];
	printf(" Icp error \n");
	return 0;
}
void postfix(char *token, char *post)
{
	char x;
	int ipost = 0;
	char_top = 0;
	
	for(int i=0; token[i]!='\0'; i++)
	{
		x=token[i];
		if(operand(x))post[ipost++] = x;
		else if(x == ')' )
		{
			while(char_stack[char_top]!='(')post[ipost++] = char_stack_delete();
			char_stack_delete();
		}
		else
		{
			while(Isp(char_stack[char_top])>=Icp(x))post[ipost++] = char_stack_delete();
			char_stack_add(x);
		}
	}
	while((x=char_stack_delete()) != '\0')
		post[ipost++] = x;
		post[ipost] = '\0';
	return;
}
Forums: 


숙제는 스스로 하시는 게 어떨까요.--feanor
숙제는 스스로 하시는 게 어떨까요.
--feanor
여기까지는 했는데 이담이 문제라서요..;;숙제는 아니구요..발표하
여기까지는 했는데 이담이 문제라서요..;;
숙제는 아니구요..발표하는건데 제가 해보다가
도저히 안되서요..계산하는부분하고 이항연산자부분요
댓글 달기