10932 - Calculator
Moderator: Board moderators
10932 - Calculator
WA many times,anything need to notice?
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
help! thx
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!
but Wrong here.
Maybe negative integer exist in the input? or other tricks?
Thank you for your help!

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
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
help
Code: Select all
x=5
y=10
-1+(-x*y-1)*(+5-(-1))
-1
(-100)
any other trick?

I'm from China
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
What happens if you declare x as a double instead? That should work better for cases like "3000000000".tywok wrote:Code: Select all
istringstream iss(t); int x; iss>>x; return x;
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
Try this example:
Code: Select all
((1+2))
Hello!
Could anyone say me if is correct my output?
input:
output:
Thanks!
Some tricky cases will be highly-regarded!
Or other cases not tricky will be appreciated too!
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))
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
Some tricky cases will be highly-regarded!
Or other cases not tricky will be appreciated too!
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
double
double also works,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;
}

my problem is that I use int to store input number
I'm from China
-
- New poster
- Posts: 13
- Joined: Wed Aug 24, 2005 8:16 pm
Re: WA

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
-
- New poster
- Posts: 2
- Joined: Wed May 17, 2006 11:29 pm
- Location: INDIA
hey can anyone temme y i am getting WA!! i
//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;
}
}
}

//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;
}
}
}