10932 - Calculator

All about problems in Volume 109. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

zhang
New poster
Posts: 11
Joined: Sun May 22, 2005 11:14 am

10932 - Calculator

Post by zhang » Sun Oct 09, 2005 4:46 am

WA many times,anything need to notice?

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

help! thx

Post by sunmoonstar_love » Sun Oct 09, 2005 5:24 am

my solution can pass other calculator problems efficiently.

but Wrong here.

Maybe negative integer exist in the input? or other tricks?

Thank you for your help! :D

Code: Select all

#include<stdio.h>
#include<string.h>
double table[30];   // data as 'a'...'z'
char expression[10000];  // input string
char opt[10000];     // stack of operator
double data[10000];  // stack of number
int Nopt,Ndata,Lexpr;

// used by question()
void calculate()
{
    Nopt--;
    Ndata--;
    switch(opt[Nopt])
    {
        case '+':
            if(Nopt>0 && opt[Nopt-1] == '-')
                data[Ndata-1] -= data[Ndata];
            else
                data[Ndata-1] += data[Ndata];    
            break;
        case '-':
            if(Nopt>0 && opt[Nopt-1] == '-')
                data[Ndata-1] += data[Ndata];
            else
                data[Ndata-1] -= data[Ndata];    
            break;
        case '*':
            data[Ndata-1] *= data[Ndata];
            break;
        case '/':
            // Notice data[Ndata] == 0, but in this problem ignored
            data[Ndata-1] /= data[Ndata];
            break;
        default:
            printf("error in calculate()\n");
            break;                
    }
}

// calculate the expression
double question(char *expr)
{
    int Pexpr = 0;
    Lexpr = strlen(expr);
    Ndata = Nopt = 0;
    while(Pexpr < Lexpr)
    {
        switch(expr[Pexpr])
        {
            case '+':
            case '-':
            case '*':
            case '/':
            case '(':
                opt[Nopt++] = expr[Pexpr++];
                break;
            case ')':
                Pexpr++;
                while(opt[Nopt-1] != '(')
                    calculate();
                Nopt--;    
                while(opt > 0 && (opt[Nopt-1] == '*' || opt[Nopt-1] == '/'))
                    calculate();    
                break;
            default:
                if(expr[Pexpr] >= '0' && expr[Pexpr] <= '9')
                {
                    int integer = 0;
                    while(expr[Pexpr] >= '0' && expr[Pexpr] <= '9')
                        integer *= 10, integer += expr[Pexpr] - '0', Pexpr++;
                    data[Ndata++] = integer;    
                }
                else
                if(expr[Pexpr] >= 'a' && expr[Pexpr] <= 'z')
                {
                    data[Ndata++] = table[expr[Pexpr]-'a'];
                    Pexpr++;
                }
                else
                    printf("error in question()\n");
                while(opt > 0 && (opt[Nopt-1] == '*' || opt[Nopt-1] == '/'))
                    calculate();
                break;            
        }
    }
    while(Nopt > 0)
        calculate();
    return data[0];    
}

void attribution(char *expr)
{   //  calculate X (expr[0]-'a') through question()
    table[expr[0]-'a'] = question(expr+2);
}    

int main()
{
    for(int i = 0; i < 26; i++)
        data[i] = 0;
    while(gets(expression))
    {
        Lexpr = strlen(expression);
        if(Lexpr == 0)
            continue;
        if(expression[1] == '=')   // expression is a attribution
            attribution(expression);
        else                       //            is a question
            printf("%.2lf\n",question(expression));              
    }
    return 0;
}    
I'm from China

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

help

Post by sunmoonstar_love » Sun Oct 09, 2005 12:27 pm

Code: Select all

         x=5
         y=10
         -1+(-x*y-1)*(+5-(-1))
         -1
         (-100)
I have dealed with the data above; but still wrong answer

any other trick? :(
I'm from China

rotoZOOM
Learning poster
Posts: 63
Joined: Mon Dec 22, 2003 5:05 am
Location: Russia, Chelyabinsk
Contact:

Post by rotoZOOM » Sun Oct 09, 2005 1:25 pm

There are no negative integers in input. For all calculation I used long double and got AC.

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

thanks

Post by sunmoonstar_love » Sun Oct 09, 2005 2:11 pm

thank you for your tip :D

Just use long double

double can't be used
I'm from China

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

oh

Post by sunmoonstar_love » Sun Oct 09, 2005 4:27 pm

Why, I can't use double ?? :o

I don't understand.
I'm from China

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post by Per » Sun Oct 09, 2005 11:20 pm

tywok wrote:

Code: Select all

 istringstream iss(t);
 int x;
 iss>>x;
 return x;
What happens if you declare x as a double instead? That should work better for cases like "3000000000".

rotoZOOM
Learning poster
Posts: 63
Joined: Mon Dec 22, 2003 5:05 am
Location: Russia, Chelyabinsk
Contact:

Post by rotoZOOM » Mon Oct 10, 2005 3:23 am

Try this example:

Code: Select all

((1+2))

Emilio
Experienced poster
Posts: 163
Joined: Sun Oct 17, 2004 8:31 pm
Location: Murcia, Spain

Post by Emilio » Mon Oct 10, 2005 4:00 am

Hello!

Could anyone say me if is correct my output?

input:

Code: Select all

y=5
x=10
(x+y)/5+(x-5)+5*(5-10)*7+7
-7+1*1*(5-6)*(5-1-1-1-1-1)
x
x*(x+x)+x
x*((x+x))+y
((((x))))
(x)
(x-(x*x+x)+x*x)
(x/5/5)
-5*4
5*(5-2)
5-2-2-2
2*(-6-2+3-3-3-3-3)*3*2*7*3494*(-1)/(-2)
2*2*2*2*2*2*2/2*1
a=1
b=2
a*b-(5*4-((2+34)*a+1)+7*3)
a*b-(5*4-((2+34)*a+1)+7*3)*2-76-2-2-(3-4-5-6-7)
a=(1+((2*3+2)*4+3))*(-1)-2+3-1-1-1-1-1-1-1-1+1+2+3-3-2-1
a
a*(-2)
a*b*b*b*b*b
((1+2*3*4*5+(3*4*5+7+7+1-1-2-3-54-56-4-3*(12+1+1*6+1*5+6-1*2-12341-1234-1-a-a-a-a*b))))
2*2-2*2-2*2-2*2-2
(1+2+3+4+5+6+7-2475823)*((4-5-6-4-5*7-8*8*6-6-6-6)+((1-2-3-4-5*1)+(1*2-3*4-5*6-7*8-8)*(1*2+2*3+4*6+8+8+8*9*9)))*(1234)/(12341234)
4/3
(1+2+3-4-5-6-8/6/6/4/2)/12/(6-7-5-6-7-7-56-5-5/3*6-4/5-4/6-7/56-45/45-76*7*6+7/(3/6+6/8+34/45+56/89)*7+6/3)
1000000000/(56/6+7/5+73/45+6/12+345/56+567/78-1234-(345-7546-546-456/456-456/44-546/56-234/77-56*65))
output:

Code: Select all

-160.00
-7.00
10.00
210.00
205.00
10.00
10.00
0.00
0.40
-20.00
15.00
-1.00
-2494716.00
64.00
-2.00
-67.00
-43.00
86.00
-1376.00
40075.00
-10.00
18033125.36
1.33
0.00
98006.18
Thanks!

Some tricky cases will be highly-regarded!
Or other cases not tricky will be appreciated too!

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

double

Post by sunmoonstar_love » Mon Oct 10, 2005 7:03 am

default:
if(expr[Pexpr] >= '0' && expr[Pexpr] <= '9')
{
int integer = 0; //**************** double
while(expr[Pexpr] >= '0' && expr[Pexpr] <= '9')
integer *= 10, integer += expr[Pexpr] - '0', Pexpr++;
data[Ndata++] = integer;
}
double also works, :D
my problem is that I use int to store input number
I'm from China

Emilio
Experienced poster
Posts: 163
Joined: Sun Oct 17, 2004 8:31 pm
Location: Murcia, Spain

Post by Emilio » Tue Oct 11, 2005 10:59 pm

I have obtained AC.
My trouble was similar to sunmoonstar_love's trouble.
I used atoi, but now I have used atof and have obtained AC.
A semi-mistake :lol:

TISARKER
Learning poster
Posts: 88
Joined: Tue Oct 12, 2004 6:45 pm
Location: Bangladesh
Contact:

Post by TISARKER » Wed Oct 12, 2005 9:27 pm

I am getting Wrong Answer.Please give me some Critical Input and Output.
Mr. Arithmetic logic Unit

sunmoonstar_love
New poster
Posts: 13
Joined: Wed Aug 24, 2005 8:16 pm

Re: WA

Post by sunmoonstar_love » Sat Oct 15, 2005 5:32 pm

:D there is no negative number
HASAN_AI wrote:Hello all,
I'm also getting wa. Can u please tell me that should i handle negative number. Is there any negative number like
-1
or (-1)
Please give some I/O. Thankx in advance.
I'm from China

Towhid
New poster
Posts: 38
Joined: Wed May 28, 2003 5:30 pm
Location: Bangladesh
Contact:

Post by Towhid » Sun Oct 16, 2005 11:50 am

I pasted my code in the previous page. There was some problem with login.
However I couldn't find any case where my code fails but getting WA :oops:
Will someone help, please by providing some critical I/O.....
From 0 to 0

vijay_comsci
New poster
Posts: 2
Joined: Wed May 17, 2006 11:29 pm
Location: INDIA

hey can anyone temme y i am getting WA!! i

Post by vijay_comsci » Mon May 22, 2006 7:03 pm

//I use long double anyway !!! can u plz temme y my prog showing WA :-(
//Any critical test cases are appreciated!!
#include<stdio.h>

long double ans[1000]={0};char O[500];int ap,Ap,Op;
long double V[26]={0};

int rank(char a)
{

switch(a)
{
case '+':
case '-':
return 1;
default:
return 2;
}
}

long double find(long double B,long double A,char a)
{

float ans;
switch(a)
{
case '+':
ans=A+B;
break;
case '-':
ans=A-B;
break;
case '*':
ans=A*B;
break;
case '/':
ans=A/B;
break;
}
return ans;
}

long double eval(char *A)
{
char t;long double a,b;
ap=Ap=Op=0;
for(;A[Ap];Ap++)
{
if(isalpha(A[Ap]))
{
ans[ap++]=V[A[Ap]-97];
continue;
}

if(isdigit(A[Ap]))
{
a=0;
while(1)
{
a=(a*10)+A[Ap]-48;
if(isdigit(A[Ap+1]))
Ap++;
else
break;
}
ans[ap++]=a;
continue;
}
if(A[Ap]=='(')
{
O[Op++]=A[Ap];
if(rank(A[Ap+1])==1)
ans[ap++]=0;
continue;
}
if(A[Ap]==')')
{
while((t=O[--Op])!='(')
{
a=ans[--ap];
b=ans[--ap];
ans[ap++]=find(a,b,t);
}
continue;
}
else
{
if(Op)
if(O[Op-1]!='(')
if((rank(O[Op-1])>rank(A[Ap])))
{
a=ans[--ap];
b=ans[--ap];
ans[ap++]=find(a,b,O[--Op]);
}
O[Op++]=A[Ap];
}
}
while(Op)
{
a=ans[--ap];
b=ans[--ap];
ans[ap++]=find(a,b,O[--Op]);

}
return ans[0];
}

main()
{
char A[1000],ch,*ptr;
while(scanf("%s",A)!=EOF)
{
if(A[1]=='=')
ch='a';
else
ch='q';
switch(ch)
{
case 'a':
ptr=&A[2];
V[A[0]-97]=eval(ptr);
break;
case 'q':
printf("%.2llf\n",eval(A));
break;
}

}
}

Post Reply

Return to “Volume 109 (10900-10999)”