Scientific calculator source code-c implementation

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define MAXSIZE 4000;
Typedef struct
{
Char data [10];
Int top; // header address

Int base; // base address

Int length; // length

} Stack;

Void init (Stack * st) // initialize the Stack

{
St-> base = 0;
St-> top = 0;
St-> length = 0;
}

Int isEmpty (Stack * st)
{
Int n = 0, top, base;
Top = st-> top;
Base = st-> base;
If (top = base)
{
Return 1;
}
Return n;
}

Int isFull (Stack * st)
{
Int n = 0, top, base;
Top = st-> top;
If (top >=4000)
{
Return 1;
}
Return n;
}

Char getTop (Stack * st) // returns the top value without changing the Stack structure

{
Char n;
If (isEmpty (st ))
{
Printf ("Stack is empty ");
Return 0;
}
Int positon = st-> top-1;
N = st-> data [positon]; // retrieve data;

Return n;
}

Char pop (Stack * st) // output Stack, return

{
Char n;
If (isEmpty (st ))
{
Printf ("Stack is empty ");
Return 0;
}
Int positon = st-> top-1;
N = st-> data [positon]; // retrieve data;


St-> top --;
St-> length --;
St-> data [positon] =; // eliminate data

Return n;
}

Void push (char n, Stack * st) // inbound Stack

{
Int positon;
If (isFull (st ))
{
Printf ("Full stack ");

}
Else
{
Positon = st-> top; // obtain the location


St-> data [positon] = n; // store data


St-> top ++; // change the position

}

}

Void show (Stack * m1) // output data in the Stack

{
Int top, base;
Top = m1-> top;
Base = m1-> base;
While (top> base)
{
Printf ("% c,", m1-> data [-- top]);
}
Printf ("");
}

Int isOperate (char temp) // whether it is an operator

{
If (temp = + | temp =-| temp = * | temp =/| temp = (| temp =) | temp = #)
{
Return 1;
}
Return 0;
}

Int isValue (char temp) // whether it is a numerical value

{
If (temp> = 0 & temp <= 9 )//

{
Return 1;
}
Else
{
Return 0;
}
}

Int isAvail (char temp) // whether it is a valid character

{
If (isOperate (temp) | isValue (temp) // if temp is neither an operator nor a value, it is invalid.

{
Return 1;
}
Return 0;
}

Int detect (char temp) // returns the position of the search matrix.

{
Int I = 0;
Char character [7] = {+ ,-,*,/,(,),#};
For (I = 0; I <7; I ++)
{
If (temp = temperature [I])
{
Return I;
}
}
}

Char Priority (char temp, char optr) // judge the Priority

{
/**//*
+ -*/()#
1 2 3 4 5 6 7
+ 1 <>
-2 <>
* 3> <>
/4 >><>>>
(5 >>>>> = 0
) 6 <= 0>
#7 <> 0 =
*/
Int row, col;
Char priority [7] [7] = {/** // * + -*/()#*/
{<, <, >,>,> },

{<, <, >,>,> },

{>,>,<,>,>,>,> },

{>,>,<,>,>,>,> },

{>,>>,>>,>,>,=,> },

{<, <, =, 0,> },

{<, <,>, <, = },
};


Row = detect (temp); // find the corresponding matrix subscript;

Col = detect (optr );
// Printf ("% d, % d", row, col );


// The priority is stored in a 7x7 matrix, corresponding to the relationship;


Return priority [row] [col];

}
Char evaluate (int a, int B, char evaluate)
{
Switch)
{
Case +: return a + B + 0;
Case-: return a-B + 0;
Case *: return a * B + 0;
Case/: return a/B + 0

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.