Nanyang OJ depressed C small plus (ii) topic 267

Source: Internet
Author: User

#include <stdio.h>
#include <stack>
#include <string.h>
#define N 1000
#include <stdlib.h>
using namespace Std;
stack<char> str;//word Fu Yi converted to suffix
stack<double> num;//Digital Stack calculation value
Char s1[n],s2[n],s3[1000000];
int top;
int COM (char x)//Comparison priority
{
Switch (x)
{
Case ' + ':
Case '-': return 1;
Case ' * ':
Case '/': return 2;
default:return-1;
}
}
void Zhuan ()//conversion suffix type
{
scanf ("%s", S1);//input string
int K=strlen (S1);//Get length
top=-1;//Initialization Header
Str.push (' # ');//Enter a character to prevent access to the bounds
for (int i=0;i<k;i++)
{
if (s1[i]>= ' 0 ' &&s1[i]<= ' 9 ' | | s1[i]== '. ') The numbers are coming in.
{
top++;
S2[top]=s1[i];
}
else if (s1[i]== ' + ' | | s1[i]== '-' | | s1[i]== ' * ' | | s1[i]== '/')
{
top++;
S2[top]= ";
if (COM (s1[i]) >com (Str.top ())) Str.push (S1[i]);//Compare priority, enter stack if large
else//otherwise, the output
{
while (COM (str.top ()) >=com (S1[i]))
{
top++;
S2[top]=str.top ();
top++;
S2[top]= ";
Str.pop ();
}
Str.push (S1[i]);
}
}
else if (s1[i]== ' (') Str.push (S1[i]);
else if (s1[i]== ') ')
{
while (Str.top ()! = ' (')
{
top++;
S2[top]= ";
top++;
S2[top]=str.top ();
Str.pop ();
}
Str.pop ();//Delete opening parenthesis
}
}
while (Str.top ()! = ' # ')//will all the remaining output
{
top++;
S2[top]= ";
top++;
S2[top]=str.top ();
Str.pop ();
}
for (int i=0;i<=top;i++)
{
if (s2[i]!= ') printf ("%c", S2[i]);
}
printf ("=\n");
}
void Ji ()
{
while (!num.empty ())
Num.pop ();
Double a1,a2;
char x;
int k,t=-1;
for (int i=0;i<=top;i++)
{
k=0;
X=s2[i];
while (s1[i]!= ")
{
if (s2[i]== ') break;
S3[k]=s2[i];
k++;
i++;
}
if (x>= ' 0 ' &&x<= ' 9 ')
{
s3[k]= ' + ';
t++;
Num.push (Atof (S3));
}
Else
{
Switch (x)
{
Case ' + ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A1+A2);
Break
Case '-':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2-A1);
Break
Case ' * ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2*A1);
Break
Case '/':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2/A1);
Break
}
}
}
printf ("%.2lf\n\n", Num.top ());
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
Zhuan ();
Ji ();
}
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nanyang OJ depressed C small plus (ii) topic 267

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.