i want to pass two characters("6","8") into a function which will convert this two chars into integers and add these two integers(6+8=14) after that it will change the value (int 14) into char (char "14") and returns the char.
can anybody help me please ??
Could you post the complete assignment?
What have you tried so far?
Post up a little code for us to look at.
One way to evaluate a prefix expression is to use a queue. To evaluate the expression, scan it repeatedly until the final expression value is known. In
each scan, read the tokens and store them in a queue. In each scan, replace an operator followed by two operands by the calculated values.
For example, the following expression is a prefix expression, which is evaluated to 159.
-+*9+28*+4863
We scan the expression and score it in a queue. During the scan, when an operator is followed by two operands, such as + 2 8, we put the result, 10 in the queue.
After the first scan, we have - + * 9 10 * 12 6 3
After the second scan, we have - + 90 72 3
After the third scan, we have - 162 3
After the fourth scan, we have 159
this is the question
And what have you tried?
(Show us your code...)
THIS IS THE CODE SO FAR I TRIED.
int calculate(char a, char b, char c)
{
int n,p;
n=(int)a-48;
p=(int)b-48;
if(a=='+')
return (n+p);
else if(a=='-')
return (n-p);
else if(a=='*')
return (n*p);
else if(a=='/')
return (n/p);
}
int main()
{
char expr[]="-+9+28*+4863";
QUEUE *q = CreateQueue();
int i=0,j=1,k=2;
int r;
char dataout,data;
while((expr!='\0'))
{
if(ispunct(expr)&&isdigit(expr[j])&&isdigit(expr[k]))
{
r=calculate(expr,expr[j],expr[k]);
data='0'+r;
Enqueue(q, data);
i=i+3;j=j+3;k=k+3;
}
else
{
Enqueue(q,expr);
i++;j++;k++;
}
}
while(!EmptyQueue(q))
{
NODE *temp = q->front;
dataout=q->front->data;
printf("%c",dataout);
q->front=q->front->link;
q->count--;
free(temp);
}
return 0;
}
this is my updated code. will it work ??
compete code is in my attached my ".c" file
int calculate(char a, int b, int c)
{
if(a=='+')
return (b+c);
else if(a=='-')
return (b-c);
else if(a=='*')
return (b*c);
else if(a=='/')
return (b/c);
}
int main()
{
char expr[]="-+9+28*+4863";
QUEUE *q = CreateQueue();
char data1[8],data2[8],data[8];
int i=0,j=1,k=2;
int a,b,r;
char *dataout,*datain;
while((expr!='\0'))
{
if(ispunct(expr)&&isdigit(expr[j])&&isdigit(expr[k]))
{
data1[0]=expr[j];data1[1]='\0';
data2[0]=expr[k];data2[1]='\0';
a=atoi(data1);b=atoi(data2);
r=calculate(expr,a,b);
itoa (r, data, 10);
datain=data;
Enqueue(q, datain);
i=i+3;j=j+3;k=k+3;
}
else
{
data[0]=expr;data[1]='\0';
datain=data;
Enqueue(q,datain);
i++;j++;k++;
}
}
while(!EmptyQueue(q))
{
NODE *temp = q->front;
strcpy(dataout,q->front->data);
printf("%s",dataout);
q->front=q->front->link;
q->count--;
free(temp);
}
return 0;
}
and my compiler is showing an error
"implict declaration of function iota in is invalid in C99".
i am using Xcode in mac OS X
Quote from: manichandra on February 27, 2012, 10:43:04 PM
and my compiler is showing an error
"implict declaration of function iota in is invalid in C99".
i am using Xcode in mac OS X
This isn't an X-Code support forum.
If you're getting a compiler error... then your code
obviously doesn't work.
Thanks.
what i meant is will it work in other compilers ??
i replaced itoa with "sprintf(data,"%d",r);"
it seems to be working.