[c언어]오류수정좀. 함수는짯는데;; 주석이 있어야하나.ㅜㅜ
글쓴이: lh8557 / 작성시간: 월, 2004/04/26 - 8:17오후
#include <stdio.h>
// 문자를 저장하는 스택과 함수 정의
#define MAX_EXPR_SIZE 100
#define CHAR_STACK_SIZE 100
#define INT_STACK_SIZE 100
char char_stack[CHAR_STACK_SIZE];
int char_top = -1;
int int_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;
}
// 문자를 저장하는 스택 정의 끝
int int_stack[INT_STACK_SIZE];
int int_Isfull()
{
if(int_top>=INT_STACK_SIZE-1)
return 1;
else
return 0;
}
int int_Isempty()
{
if(int_top<=-1)
return 1;
else
return 0;
}
void int_stack_full()
{
printf("Stack is Full! : \n");
}
void int_stack_add(int item)
{
if(int_top>=INT_STACK_SIZE-1)
{
int_stack_full();
return;
}
int_stack[++int_top] = item;
}
int int_stack_empty()
{
printf("Stack is Empty! : \n");
int_stack[0] = '0';
return int_stack[0];
}
int int_stack_delete()
{
int item;
if(int_top <= -1)
return int_stack_empty();
item = int_stack[int_top--];
return item;
}
//인트스택 함수구현
// 함수 Prototype
int operand(char x);
int Isp(char x);
int Icp(char x);
void postfix(char *token, char *post);
int eval(char *post);
void main(void)
{
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("value : \n%d", eval(post));
printf("\nDo you want to do another expression('y'or'n')? ");
scanf("%s", &c);
}
}
int operand(char x)
{
char op[8] = {'(', ')', '+', '-', '*', '/', '%', '\0'};
for(int i=0; i<8; i++)
if(op[i]==x)
return 0;
return 1;
}
int Isp(char x)
{
char op[8] = {'(', ')', '+', '-', '*', '/', '%', '\0'};
int sp[8] = {0,19,12,12,13,13,13,0};
int i;
for(i=0; i<8; i++)
if(op[i]==x)
return sp[i];
printf(" Isp error \n");
return 0;
}
int Icp(char x)
{
char op[8] = {'(', ')', '+', '-', '*', '/', '%', '\0'};
int cp[8] = {0,19,12,12,13,13,13,0};
int i;
for(i=0; i<8; 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;
}
int eval(*post)
{
int n = 0;
int int_top = 0;
int_stack[0] = '\0';
int ipost = 0;
while(post[ipost] != '\0')
{
if(post[ipost] == operand)
{
stack_add(&top, post[ipost] - '0');
}
else
{
op2 = stack_delete(&top);
op1 = stack_delete(&top);
switch(ipost)
{
case '+' : stack_add(&top, op1+op2);
break;
case '-' : stack_add(&top, op1-op2);
break;
case '*' : stack_add(&top, op1*op2);
break;
case '/': stack_add(*top, op1/op2);
break;
case '%' : stack_add(&top, op1%op2);
}
}
}
return stack_delete(&top);
}
오류는..
Compiling...
Cpp2.cpp
C:\Documents and Settings\나\My Documents\레포트\데이터구조\Cpp2.cpp(201) : error C2065: 'post' : undeclared identifier
C:\Documents and Settings\나\My Documents\레포트\데이터구조\Cpp2.cpp(201) : error C2100: illegal indirection
C:\Documents and Settings\나\My Documents\레포트\데이터구조\Cpp2.cpp(202) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
Error executing cl.exe.
Cpp2.obj - 3 error(s), 0 warning(s)
이렇게 나오는데 오류좀 잡아주세요
Forums:


eval 함수의 라인이 202번인가요? 라인수 세기가 힘드네요... -_
eval 함수의 라인이 202번인가요? 라인수 세기가 힘드네요... -_-;
저 위쪽의 함수 선언과 202번인가의 함수 정의의 선언부가 뭔가 틀린 것 같은데요...
타이핑 미스가 아닐까 합니다만... 뭔가 하나 빠졌습니다... 잘 보시면...
p.s. 레포트라 답은 알아서 찾으셔야... -_-;
예..계속 수정해보고는 있는데..계속 오류가늘어나네요..ㅜㅜ
예..계속 수정해보고는 있는데..계속 오류가
늘어나네요..ㅜㅜ
좀 힘드시더라도 찬찬히 해보시면... 누구나 처음엔 다 그랬습니다...
좀 힘드시더라도 찬찬히 해보시면... 누구나 처음엔 다 그랬습니다...
네...감사합니다..ㅜㅜ
네...감사합니다..ㅜㅜ
제가 찾아낸것들..
너무 열심히 고치고 계셨군요... ^^;
Version control 을 하셨더라면 하는 아쉬움이 남습니다.
제가 찾은것은 eval() 의 문제입니다.
1. int eval(*post) 이렇게만 하면 당연히 컴파일이 안되죠.
아마 int eval(char *post) 를 하시고 싶으셨던거죠?
2. if (post[ipost] == operand) 이러면 당연히
operand 는 함수인데, 인자를 못받으니 에러가 나겠죠?
if (post[ipost] == operand(post[ipost]) ) 를
하시고 싶으셨던거겠죠?
그런데, 이 다음에 약간의 문제가 있군요.
stack_add(&top, ... ) 라인에서..
top 이란 변수 자체는 정의 안하셨는데,
global 변수로 int_top 과 char_top 을 정의해놓으셨는데,
로컬 변수로 int_top 이 또 보이는군요?
postfix() 에서도 char_top 을 따로 정의 하셨더군요?
자세히 읽지는 않았지만, 변수를 global 로 잡으려는 의도셨을텐데..
global 과 local 이름이 같을때, 지금은 구별하실수 있더라도,
장기적으로 이것은 별로 바람직하진 않을겁니다.
그럼..
삽질의 대마왕...
댓글 달기