Recursive functions for calculating four mixed operational expressions Second Edition

Source: Internet
Author: User
Tags goto integer mixed pow

Recursive functions for calculating four mixed operational expressions Second Edition

Updated on 2007-08-28 by BILLOW3 (qq:41965573)

Note: This article in the publication of a little typesetting, if there is due to typographical code work is not normal, please write to tell us

#include <math.h>
#pragma warn-8060//mask compile-time possibly incorrect assignment warning
Ansistring __fastcall Calc (String sexp)
{
Computes a four mixed operation expression with no variables (supports rounding int and round round functions, supporting negative operands)
Positive numbers are not allowed with a plus sign
int POSL, POS, POSR, PosM; Pos-> the position of the operator currently considered
Posl-> the position of the nearest operator before the currently considered operator
Posr-> the location of the nearest operator after the currently considered operator
Posm-> the position of a comma in a circular integer function
String sTmp, SL, SR;
Sl-> the left-hand operand of the currently considered operator string,sr-> the right-hand operand string of the operator currently considered
BOOL Isminus; isminus-> symbols for the current/* sequence
Sexp = Sexp.lowercase ();
if (Sexp.ansipos ("error"))
return (SEXP);
while (pos = Sexp.ansipos (""))
Sexp = Sexp.delete (pos, 1); To remove a space in an expression
if (Sexp.isempty ())
Return ("0");
while (pos = Sexp.ansipos ("[")) > 0 | | (pos = Sexp.ansipos ("{")) > 0)//Uniform opening parenthesis for (
Sexp = sexp.substring (1, pos-1) + "("
+ sexp.substring (pos + 1, sexp.length ());
while (pos = Sexp.ansipos ("]") > 0 | | (pos = Sexp.ansipos ("}")) > 0)//unified closing bracket AS)
Sexp = sexp.substring (1, pos-1) + ")"
+ sexp.substring (pos + 1, sexp.length ());
The following processing round circular integer function
while (pos = Sexp.ansipos ("Round ("))
{
POSL = pos + 5;
while (PosM = sexp.substring (Posl + 1, sexp.length ()). Ansipos ("Round ("))
POSL = Posl + PosM + 5; The position after the last round
for (PosM = Posl + 2;!sexp.isdelimiter (",", PosM)
&& PosM <= sexp.length (); posm++); Round after the first, the position
if (PosM = 0) return ("Error:round does not have a paired comma, the formula is wrong!") ");
for (POSR = PosM + 2;!sexp.isdelimiter (")", POSR)
&& POSR <= sexp.length (); posr++); The position of the first one after the comma
POSR does not start the search from POSM + 2 from POSM + 1, in response to negative operands
Sexp = sexp.substring (1, posL-6)
+ Floor (sexp.substring (POSL + 1,
POSM-POSL-1)). ToDouble () * POW (10,
Sexp.substring (PosM + 1, posr-posm-1). ToDouble ())
+ 0.5)/POW (sexp.substring (PosM + 1)
POSR-POSM-1). ToDouble ())
+ sexp.substring (POSR + 1, sexp.length ());
}
Working with parentheses: recursive calculation of expressions in parentheses, and finally elimination of parentheses
while (Posl = Sexp.lastdelimiter ("())//innermost (
{
STMP = sexp.substring (Posl + 1, sexp.length ());
POSR = Stmp.ansipos (")"); The innermost)
if (POSR = 0)
Return ("Error: no pairing"), the formula is wrong! ");
Sexp = sexp.substring (1, posL-1)
+ Calc (stmp.substring (1, posR-1))
+ stmp.substring (POSR + 1, stmp.length ());
}
The following handles int rounding function
while (pos = sexp.ansipos ("int"))
{
POSL = pos + 2;
for (POSR = Posl + 2;!sexp.isdelimiter ("+-*/", POSR)
&& POSR <= sexp.length (); posr++); Then the first +-*/
POSR does not start the search from POSL + 2 from POSL + 1, in response to negative operands
Sexp = sexp.substring (1, pos-1)
+ Floor (sexp.substring (POSL + 1, posr-posl-1). ToDouble ())
+ sexp.substring (POSR, Sexp.length ());
This paper studies from C++builder-HTTP://WWW.CCRUN.COM/ARTICLE.ASP?I=636&AMP;D=X5FNCL
}
The following deals with the */sequence in an expression without parentheses
Isminus = false; isminus-> symbols for the current/* sequence
while (Sexp.lastdelimiter ("* *"))//presence * or/
{
for (pos = 1;!) Sexp.isdelimiter ("* *", POS)
&& POS <= sexp.length (); pos++); First * or/
if (pos = 1 | | | pos = sexp.length ())
Return ("Error: the first or end character is the */operator, the formula is wrong!") ");
Posl-> first +-before the first * *
POSL = sexp.substring (1, pos-1). Lastdelimiter ("+-");
MINUS0:
for (POSR = pos + 1;!) Sexp.isdelimiter ("+-*/", POSR)
&& POSR <= sexp.length (); posr++);
Posr-> first +-*/operator after first * *
if (POSR = = (pos + 1) && sexp.isdelimiter ("* *", POSR))
Return (the error:*/operator is adjacent, the formula is wrong!) ");
if (POSR = = Sexp.length ())
Return ("Error: Tail character is +-* * operator, the formula is wrong!") ");
if (Sexp.substring (pos, 2) = = "*-"
|| Sexp.substring (pos, 2) = = "/-")//multiplier or divisor minus
{
Sexp.delete (pos + 1, 1);
Isminus =! Isminus;
Goto MINUS0;
}
SL = sexp.substring (Posl + 1, pos-posl-1);
SR = sexp.substring (pos + 1, posr-pos-1);
if (Sexp.isdelimiter ("/", pos) &AMP;&AMP;SR = = "0")
Return ("Error: Divisor is zero, meaningless!") ");
Sexp = (Posl = = 0?) String (""):
Sexp.substring (1, POSL)) + (Sexp.isdelimiter ("*", POS)?
(Sl.todouble () *sr.todouble ()):
(Sl.todouble ()/sr.todouble ()))
+ sexp.substring (POSR, Sexp.length ());
}
if (Isminus)
Sexp = String ("-") + Sexp;
After the series above, the operator in the Sexp is left with + and-
The following handles the +-sequence in an expression without parentheses
Isminus = false; Symbols of addends or meiosis
while (Sexp.lastdelimiter ("+-"))//presence + or-
{
for (pos = 2;!sexp.isdelimiter ("+-", POS)
&& POS <= sexp.length (); pos++); The first + or-
POS does not search from 1 to 2, in response to negative first operand
if (pos = = Sexp.length ())
Return ("Error: Tail character is +-operator, formula wrong!") ");
if (pos > Sexp.length ())
return (SEXP); Sexp is a simple negative number that does not have to be calculated
MINUS1:
for (POSR = pos + 1;!sexp.isdelimiter ("+-", POSR)
&& POSR <= sexp.length (); posr++);
if (POSR = = Sexp.length ())
Return ("Error: Tail character is +-operator, formula wrong!") ");
if (Sexp.substring (pos, 2) = = "+-"
|| Sexp.substring (pos, 2) = = "--")//Addends or meiosis negative
{
Sexp.delete (pos + 1, 1);
Isminus =! Isminus;
Goto Minus1;
}
The jump here is normally performed at most once
SL = sexp.substring (1, pos-1);
SR = sexp.substring (pos + 1, posr-pos-1);
Sexp = String (Sexp.isdelimiter ("+", POS)?
(sl.todouble () + sr.todouble () * (Isminus?-1:1)):
(Sl.todouble ()-sr.todouble () * (Isminus?-1:1))
+ sexp.substring (POSR, Sexp.length ());
}
return (SEXP);
}

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.