Inadvertently overflows to modify the variable value

Source: Internet
Author: User

# Include <iostream>
# Include <stdio. h>
Using namespace std;

Const MAXSIZE = 100;
Typedef struct tagArray
{
Int len;
Int num [MAXSIZE];
} Array;


Int isMax (int n1, int n2)
{
Switch (n1)
{
Case '+': return 0; break;
Case '-': return 0; break;
Case '*':
If (n2 = '+' | n2 = '-')
Return 1;
Else
Return 0;
Break;
Case '/':
If (n2 = '+' | n2 = '-')
Return 1;
Else
Return 0;
Break;
Default:
Cout <"syntax error" <endl;
Exit (1 );
Break;
}
}
Int main ()
{
/**//////////////////////
Int st [MAXSIZE] = {0 };
Int top =-1;
// A simple stack is used to save operators.
/**//////////////////////
Array ay;
Memset (& ay, 0, sizeof (ay ));
// Cout <"what is the size" <ay. len <endl;
// A buffer for storing lost data
/**/////////////////////////

Int scr [] = {6, '*', 4, '-', 2, '+', 5, '/', 2 };
Const int len = 8;
Int ErrorFlag = 0;
For (int I = 0; I <len; I ++)
{
If (scr [I] = '+' | scr [I] = '-' | scr [I] = '*' | scr [I] = = '/')
{
If (/** // * top =-1 | */isMax (scr [I], st [top])
{
// When the stack Element Level is smaller than the current Element Level, press the current operator into the stack
St [++ top] = scr [I];
}
Else
{
// When the top Element Level of the stack is greater than the current element level, the top element of the stack pops up the stack input to the output buffer and pushes the current element into the stack.
Ay. num [ay. len ++] = st [top --]; // put it in the output buffer
St [++ top] = scr [I];
}
If (! ErrorFlag)
{
Cout <"----------------------- overflow sending error -----------------" <top <endl;
Cout <scr [I] <endl; // The result is that the previously stored scr [I] value depends on
// It is estimated that some overflow occurs when the function call saves the parameter variables.
// If you understand assembly, it may be a good explanation ......
ErrorFlag =! ErrorFlag;
// This is in combination with/* top =-1 | */. It was the first time I encountered this problem to change the top value.
// This problem is caused by the overflow of the array boundary. I have the opportunity to study overflow.
// Therefore, the C language array is not good enough, and there is no boundary detection. In this way, when writing large programs, the program encounters an exception.
// St [-1] address is actually the top address ..
}
}
Else
{
// It is a buffer from digital input to output.
Ay. num [ay. len ++] = scr [I];
// Cout <scr [I];
}
}

If (top =-1)
{
Cout <"illegal data" <endl;
Exit (1 );
}
Cout <"array size" <ay. len <endl;
Cout <"top" <top <endl;
// Buffer all the stack Elements
Do
{
Ay. num [ay. len ++] = st [top --];
} While (top! =-1 );
Cout <"array size" <ay. len <endl;
For (int k = 0; k <ay. len; k ++)
{
If (ay. num [k] = '+' | ay. num [k] = '-' | ay. num [k] = '*' | ay. num [k] = '/')
{
Printf ("% c", ay. num [k]);
}
Else
{
Cout <"" <ay. num [k];
}
}
Return 0;
}

 

Author xiaoyuer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.