Depressed C small plus (ii)

Source: Internet
Author: User

Depressed C small plus (ii) time limit: +Ms | Memory Limit:65535KB Difficulty:4
Describe

Smart you help C small add to solve the infix expression to the suffix expression conversion (for details please refer to "Depressed C small plus (a)"), C small plus very happy. But C-Plus is a thoughtful person who wants to calculate the value of an expression in this way. That is, the expression is converted to a suffix expression, and then evaluated. It is also considered that the operand is a decimal and multi-digit situation.

Input
The first line enters an integer t, which is a total of T-Group test data (T<10).
Each set of test data has only one row, which is a string of not more than 1000 in length, representing the formula, each of which ends with a "=". This expression contains only +-*/and parentheses. Where parentheses can be used in a nested set. The data guarantees that no negative numbers are present in the operands of the input and less than 1000000.
The data guarantee divisor will not be 0.
Output
For each set of test data output consists of two lines, first output the converted suffix expression, and then output the results of the calculation, the result is reserved two decimal places. Two sets of test data are separated by a blank line.
Sample input
21+2= (19+21) *3-4/5=
Sample output
12+=3.001921+3*45/-=119.20





Code:


#include <stdio.h>
Double Fun (double A, double b, Char ch)
{
if (ch== ' + ')
return b+a;
if (ch== '-')
return b-a;
if (ch== ' * ')
return b*a;
if (ch== '/')
return b/a;
}
int main (void)
{
int i,n,top1,top2;
Double x,t,a,b,num[1000];
Char str[1000],ch[1000];
scanf ("%d", &n);
while (n--)
{
Top1=-1;
Top2=-1;
scanf ("%s", str);
for (i=0;str[i]!= ' n '; i++)
{
x=0;
if (str[i]>= ' 0 ' &&str[i]<= ' 9 ')
{
while (str[i]>= ' 0 ' &&str[i]<= ' 9 ')
{
printf ("%c", Str[i]);
x=x*10+str[i]-' 0 ';
i++;
}
if (str[i]== '. ')
{
printf ("%c", Str[i]);
i++;
t=0.1;
while (str[i]>= ' 0 ' &&str[i]<= ' 9 ')
{
printf ("%c", Str[i]);
x=x+ (str[i]-' 0 ') *t;
t=t*0.1;
i++;
}
}
Num[++top1]=x;
}
if (str[i]== ')
Break
if (str[i]== ' (')
{
Ch[++top2]=str[i];
}
else if (str[i]== ') ')
{
while (top2>=0&&ch[top2]!= ' (')
{
printf ("%c", Ch[top2]);
A=NUM[TOP1];
top1--;
B=NUM[TOP1];
Num[top1]=fun (A,b,ch[top2]);
top2--;
}
top2--;
}
else if (str[i]== ' * ' | | str[i]== '/')
{
while (ch[top2]== ' * ' | | ch[top2]== '/')
{
printf ("%c", Ch[top2]);
A=NUM[TOP1];
top1--;
B=NUM[TOP1];
Num[top1]=fun (A,b,ch[top2]);
top2--;
}
Ch[++top2]=str[i];
}
Else
{
while (top2>=0&&ch[top2]!= ' (')
{
printf ("%c", Ch[top2]);
A=NUM[TOP1];
top1--;
B=NUM[TOP1];
Num[top1]=fun (A,b,ch[top2]);
top2--;
}
Ch[++top2]=str[i];
}
}
while (top2>=0)
{
printf ("%c", Ch[top2]);
A=NUM[TOP1];
top1--;
B=NUM[TOP1];
Num[top1]=fun (A,b,ch[top2]);
top2--;
}
printf ("\n%.2f\n", num[0]);
}
return 0;
}



















Depressed C small plus (ii)

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.