/*
Name: scientific calculator (four arithmetic operations of floating-point numbers with parentheses can be implemented)
Copyright:
Author:
Date: 15-09-14
Description: reads a computing expression from a file, converts the computing expression to an inverse polish expression, computes the value of the inverse polish expression, and finally outputs the answer.
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Include <math. h>
# Define Max size 200
Void translate (const char STR [], char exp []); // convert to an inverse polish expression
Double compvalue (const char * exp); // evaluate the value of the inverse polish expression
Double chartodouble (const char * Str); // converts a numeric string to a floating point number.
Void getstr (char * Str); // read the formula
Void showanswer (const char * STR, double answer); // output the answer
Int main (void)
{
Char STR [maxsize], exp [maxsize];
Double answer;
Getstr (STR );
Puts (STR );
Translate (STR, exp );
Puts (exp );
Answer = compvalue (exp );
Showanswer (STR, answer); // output the answer
Return 0;
}
Void getstr (char * Str) // read Formula
{
File * FP;
Int I = 0;
If (FP = fopen ("E: \ programming \ source code base \ c \ learning program \ scientific calculator formula .txt", "rb") = NULL)
{
Fprintf (stderr, "error opening file .");
Exit (1 );
}
Rewind (FP );
While (STR [I] = fgetc (FP ))! = EOF)
{
If (STR [I] = '') // remove unnecessary Spaces
{
I --;
}
I ++;
}
STR [I ++] = '#';
STR [I] = '\ 0 ';
Fclose (FP );
}
Void showanswer (const char * STR, double answer) // output the answer
{
File * FP;
If (FP = fopen ("scientific calculator formula .txt", "A + B") = NULL)
{
Fprintf (stderr, "error opening file .");
Exit (1 );
}
Fprintf (FP, "= % F", answer );
Fprintf (stdout, "% s = % F \ n", STR, answer );
Fclose (FP );
}
Void translate (const char STR [], char exp []) // convert to an inverse polish expression
{
Char stack [maxsize];
Int I = 0, K = 0, Top =-1;
While (STR [I]! = '#')
{
If (STR [I] = '(') // directly import '(' into the stack
{
Stack [++ top] = STR [I ++];
}
Else if (STR [I] = ')')
{
While (top> = 0 & stack [Top]! = '(') // Remove the '(' symbol from the stack and store it to the reverse polish expression
{
Exp [k ++] = stack [top --];
}
If (top <0) // '(' insufficient, there is excess ')'
{
Puts ("'(' unmatched \ n ");
Exp [k ++] = '\ 0 ';
Return;
}
Top --; // remove '('
I ++;
}
Else if (STR [I] = '+' | STR [I] = '-')
{
While (top> = 0 & stack [Top]! = '(') // If '(') exists, exit the '(' symbol before the stack. Otherwise, all symbols exit the stack and are stored in the reverse polish expression.
{
Exp [k ++] = stack [top --];
}
Stack [++ top] = STR [I ++]; // Add the new operator number to the stack
}
Else if (STR [I] = '*' | STR [I] = '/')
{
While (top> = 0 & (stack [Top] = '*' | stack [Top] = '/')) // remove the symbols '*' and '/' from the stack and store them to the reverse polish expression.
{
Exp [k ++] = stack [top --];
}
Stack [++ top] = STR [I ++]; // Add the new operator number to the stack
}
Else
{
While (STR [I]> = '0' & STR [I] <= '9') | STR [I] = '. ') // store the floating point number directly to the reverse polish expression
{
Exp [k ++] = STR [I ++];
}
Exp [k ++] = '#'; // Add an ending symbol to the floating point to correctly extract the floating point.
}
}
While (top> = 0) // store all operator numbers in the stack to reverse Polish expressions
{
If (stack [Top] = '(') // redundant '('
{
Puts ("'(' unmatched \ n ");
Exp [k ++] = '\ 0 ';
Return;
}
Exp [k ++] = stack [top --];
}
Exp [k ++] = '\ 0 ';
}
Double compvalue (const char * exp) // evaluate the value of the inverse polish expression
{
Char tempstr [maxsize];
Double stack [maxsize];
Int I = 0, K = 0, Top =-1;
While (exp [I]! = '\ 0 ')
{
If (exp [I]> = '0' & exp [I] <= '9 ')
{
K = 0;
While (exp [I]! = '#')
{
Tempstr [k ++] = exp [I ++];
}
Tempstr [k] = '\ 0 ';
Stack [++ top] = chartodouble (tempstr );
I ++; // skip '#'
}
Else
{
Switch (exp [I ++]) // import the calculation result to the stack and exit the redundant number.
{
Case '+': stack [Top-1] + = stack [Top];
Break;
Case '-': stack [Top-1]-= stack [Top];
Break;
Case '*': stack [Top-1] * = stack [Top];
Break;
Case '/': If (stack [Top]! = 0)
{
Stack [Top-1]/= stack [Top];
}
Else
{
Puts ("Division by zero error \ n ");
Return 0;
}
Break;
}
Top --; // exit unnecessary numbers
}
}
Return stack [Top];
}
Double chartodouble (const char * Str) // converts a numeric string to a floating point number.
{
Double sumint = 0, sumdec = 0, E = 1;
Int I = 0;
While (STR [I]> = '0' & STR [I] <= '9 ')
{
Sumint = sumint * 10 + STR [I ++]-'0 ';
}
If (STR [I] = '.') // if it contains decimals, process the decimal part.
{
While (STR [++ I]! = '\ 0 ')
{
Sumdec = sumdec * 10 + STR [I]-'0 ';
E * = 10;
}
}
Return sumint + sumdec/E;
}
Scientific calculator (four arithmetic operations of floating point numbers with parentheses can be implemented)