2016-11-11 71 views
-7

因此,这是我为我的大学做的作业。我必须从文本文件中的一行读取波兰语符号。文本文件中的表达式是:“/ 2 3”,我必须将它变成反转的波兰表示法。我不断收到此错误:0xC0000005:访问冲突读取位置0x00000000。0xC0000005:访问违规读取地址0x00000000 C++

int top = -1; 
char prefix[50]; 
void Push(char value) 
{ 
    top++; 
prefix[top] = value; 
} 
void Pop() 
{ 
if (top < 0) 
    cout << "The stack is empty." << endl; 
else 
{ 
    top--; 
} 
} 

char Top() 
{ 
return prefix[top]; 
} 

void Display() 
{ 
for (int i = 0; i <= top; i++) 
    cout << prefix[i] << " "; 
} 

bool isOperator(char c) 
{ 
if (c == '+' || c == '-' || c == '*' || c == '/') 
    return true; 
else 
    return false; 
} 

char c; 
char postfix[50]; 
int top2 = -1; 
void Push2() 
{ 
top2++; 
postfix[top2] = Top() + Top() + c; 
} 

void Display2() 
{ 
{ 
    for (int i = 0; i <= top2; i++) 
    cout << postfix[i] << " "; 
} 
}; 
void PrefixToPostfix() 
{ 

for (int *i = 0; *i <= top2; i++) 
{ 
    c = prefix[*i]; 
    if (isOperator) 
    { 
     Push2(); 
    } 
    else 
    { 
     top2++; 
     postfix[top2] = c; 
    } 

} 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
char value; 
char c; 

ifstream file("Prefix.txt"); 
while (file >> value) 
{ 
    Push(value); 
} 
file.close(); 
PrefixToPostfix(); 
Display(); 
Display2(); 
cout << endl; 
system("PAUSE"); 
return 0; 
} 

我觉得问题可能出在我的这部分代码:

void PrefixToPostfix() 
{ 

for (int *i = 0; *i <= top2; i++) 
    { 
     c = prefix[*i]; 

如果有人能帮助我,我会非常感激,因为我有我的功课上交5小时。 :)

+0

http://ericlippert.com/2014/03/05/how-to-debug-small-programs/的 – Biffen

+1

可能的复制[ 0xC0000005:访问冲突读取位置0x00000000](http://stackoverflow.com/questions/10478941/0xc0000005-access-violation-reading-location-0x00000000) – Marvin

+1

请停止尝试通过试验和错误学习C++,它会让你无处可去。相反,从一本好书中系统地学习它。 –

回答

2

没有必要对指针的位置:

for (int i = 0; i <= top2; i++) { 
    c = prefix[i]; } 
相关问题