【问题标题】:0xC0000005: Access violation reading location 0x00000000 c++0xC0000005:访问冲突读取位置0x00000000 c++
【发布时间】:2016-11-11 15:20:07
【问题描述】:

所以这是我大学的作业。我必须从文本文件中的一行读取波兰符号。文本文件中的表达式是:“/2 3”,我必须把它变成一个反向波兰符号。我不断收到此错误:0xC0000005:访问冲突读取位置 0x00000000。

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 小时内交作业。 :)

【问题讨论】:

标签: c++ pointers access-violation


【解决方案1】:

这里不需要指针:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多