C + + calculation of four expression templates

Source: Internet
Author: User

A handy C + + function template that can and can only compute four expressions with parentheses, only one function interface: int getexpvalue (_tstream& istrin, _t& Nreturn)

Parameter explanation:

Istrin: An input stream that can be a standard IO stream, can be a file stream, or it can be a streaming

Nreturn: The variable used to receive the result of the calculation, and the type used by the calculation is determined by this variable

return value:

The return of a non 0 indicates a successful calculation and 0 indicates an error in the calculation failure

Program code:

The following is a reference fragment:
Namespace fy_exp{
namespace {Template
Inline _t getexpvalue (_t t[), char& csym) {
Char C=csym; csym=0;
Switch (c) {
Case ' + ': return t[0] + = t[1];
Case '-': return t[0]-= t[1];
Case ' * ': return t[0] *= t[1];
Default:return t[0]/= t[1];//case '/':
}
}}
Template
/* _tstream:inputstream, _t:get return value
* Return nonzero if get value successfully * *
int Getexpvalue (_tstream& istrin, _t& nreturn) {
_t T[3] = {0}; The Swallow in the rain
Char csym[3] = "+ +";
int nlevel = 1, nerr = 0;
if (!) ( ISTRIN>>T[1]) istrin.clear ());
for (;;) {
if (Istrin>>csym[2]) {
Switch (csym[2]) {
Case ' (':
if (!csym[1]) {nlevel=0x100; nerr=1} Else
if (! Getexpvalue (Istrin, t[2]) nlevel|=0x10;
else{nlevel=0x100; nerr=1;}
Break
Case ') ':
{nlevel = 0x100;} Break
Case ' + ': Case '-': Case ' * ': Case '/':
{csym[nlevel++] = csym[2];} Break
Case ": Case ' \ r ': Case ' \ n ': Case ' t ': continue;
Default
{nlevel=0x100; nerr=1;}
}
if (nlevel==0x100) break;
if (nlevel&0x10 | | istrin>>t[2]) {
Nlevel &= 0xF;
if (nlevel==1) {t[1]=t[2];csym[1]=0;continue;}
if (csym[1]== ' * ' | | csym[1]== '/') {
Getexpvalue (t+1, csym[1]);
}
else{
Getexpvalue (t, csym[0]);
t[1]=t[2];csym[0]=csym[1];csym[1]=0;
}
Nlevel = 1;
}
else Istrin.clear ();
}
Else{nerr =-1; break;}
}
if (csym[1]) t[2]=0,nreturn=getexpvalue (t+1, csym[1));
else Nreturn=getexpvalue (T, csym[0]);
return nerr==-1?1:0;
}}

Use examples for function templates:

Add the following code after the above code:

以下是引用片段:
程序代码:
#include
#include
#include
using namespace std;
int main(void)
{
string s1;
while(cin>>s1)
{
istrstream isin(s1.data());
double d;
if(fy_Exp::GetExpValue(isin, d))
{
cout<
}
else
{
cout<<"ERROR"<
}
}
return 0;
}

Then compile and execute it.

Other: Tc++ on a certain compilation error, do not guarantee that the VC6 can also be compiled.

A newer version of VC7 or VC is recommended or compiled using GNU C + +.

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.