2016-11-11 70 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]; } 
相關問題