Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

casting 1

Status
Not open for further replies.

manichandra

Technical User
Feb 6, 2012
19
US
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 ??
 
14" is not a char, it's a string (array of chars in C).
Functions can't yields arrays as a returned value in C.
Now start to design your function interface (parameter types especially).

Some tips:

1. atoi works with C-style strings (special arrays of chars) but you have scalar values of type char. So type "man atoi" later...

2. If char c variable contains a decimal digit character then correspondent number is c-'0' (it's simply incredible but that's true in C;)...
 
this is the question:

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

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





 
so here is one more question ,
how to convert "expr[j]" and "expr[k]" to ctype strings and pass to "calculate" function ??
 
i dint really get what you are saying .
could u please explain me how to use it ??
 
1. If you want to get right answers, ask right questions (come back to the original post;). Don't use 48 instead of '0': chars are converted (promoted) to int automatically in C.

2. Both expr[j] and expr[k] are chars. Why do you want "to convert them to ctype strings"? Your calculate() function has char type parameters, it can't process "strings".

3. Use sprintf function to convert integer values to string. For example:
Code:
int  val = 12345;
char snum[32]; 
/* reserve storage to save ALL possible int representations */
sprintf(snum,"%d",val);
Now snum (array of char) contains '1', '2', '3', '4', '5', '\0'
(I hope it's the answer to the last obscure question;)

4. Look at calculate() body again. Where is the last return statement - in other words, what's the function value of (invalid) 1st argument?

5. Use stacks (not queues) to calculate expressions in prefix notation. There are tons of classic algorithmes in inet.

Can you explain us more precise specifications of the problem which you are trying to solve?
 
actually i am trying to calculate the expression like this

1 .first take the char in expr[]="-+*9+28*+4863 ", calculate and put it in a queue .
2. again put t items in the queue to the expr[]= "-+*910*1263".
3. and repeat 1 and 2 till q->count is 1.

now i have a doubt that my approach is wrong . could u please tell me which approach is correct.



this is my updated code. i changed the calculate function

#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<math.h>



typedef struct node
{
char data[8];
struct node *link;
} NODE;

typedef struct queue
{
NODE *front;
NODE *rear;
int count;
} QUEUE;

QUEUE* CreateQueue()
{
QUEUE* q = (QUEUE*)malloc(sizeof(QUEUE));
q->front = NULL;
q->rear = NULL;
q->count = 0;
return q;
}

void Enqueue(QUEUE *q, char *dataIn)
{
NODE *newNode = (NODE*)malloc(sizeof(NODE));
strcpy(newNode->data,dataIn);
newNode->link = NULL;
if (q->front == NULL)
q->front = newNode;
else
q->rear->link = newNode;
q->rear = newNode;
q->count++;
}

char* Dequeue(QUEUE *q)
{
char *dataout;
NODE *temp = q->front;
dataout = q->front->data;
if (q->count == 1)
q->rear = NULL;
q->front = q->front->link;
q->count--;
free(temp);
return dataout;
}

int QueueFront(QUEUE *q, char *dataOut)
{
if (q->count == 0)
return 0;
dataOut = q->front->data;
return 1;
}

int EmptyQueue(QUEUE *q)
{
if (q->count == 0)
return 1;
else
return 0;
}

int FullQueue(QUEUE *q)
{
NODE *temp = (NODE*)malloc(sizeof(NODE));
if (temp == NULL)
return 1;
else
{
free(temp);
return 0;
}
}

int QueueCount(QUEUE *q)
{
return q->count;
}

void DestroyQueue(QUEUE *q)
{
NODE *temp;
while (q->front != NULL)
{
temp = q->front;
q->front = q->front->link;
free(temp);
}
free(q);
}

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";
printf("%s",expr);
QUEUE *q = CreateQueue();
char data1[8],data2[8],data[8],data3[8],data4[8];
int i=0,j=1,k=2,l=0;
int a,b,r;
char *p,*datain,*dataOut;
p=data3;dataOut=data4;
while((QueueCount(q)>1))
{
i=0;j=1;k=3;l=0;

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);
sprintf(data,"%d",r);
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))
{
dataOut=Dequeue(q);
expr[l]=*dataOut;
l++;
}
expr[l]='\0';

}

return 0;
}



i am facing problem in the last while loop.
here i don't know how to convert string to char again and put in the expr[].
 
Please, use code tag for your sources. Click <Process TGML> link on the input panel to obtain TGML help.

>could u please tell me which approach is correct

Use Google search, it's so easy. For example, search for "prefix notation algorithm" - more than 5 millions references. Start from the 1st:

>i don't know how to convert string to char again and put in the expr

Explain me, what's this "string" and what's that "char"? Better give examples...
 
thanks for telling me about code tag. i am new to forums. and i am trying to learn the things .

Code:
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<math.h>



typedef struct node
{
    char data[8];
    struct node *link;
} NODE;

typedef struct queue
{
    NODE *front;
    NODE *rear;
    int count;
} QUEUE;

QUEUE* CreateQueue()
{
    QUEUE* q = (QUEUE*)malloc(sizeof(QUEUE));
    q->front = NULL;
    q->rear = NULL;
    q->count = 0;
    return q;
}

void Enqueue(QUEUE *q, char *dataIn)
{
    NODE *newNode = (NODE*)malloc(sizeof(NODE));
    strcpy(newNode->data,dataIn);
    newNode->link = NULL;
    if (q->front == NULL)
        q->front = newNode;
    else
        q->rear->link = newNode;
    q->rear = newNode;
    q->count++;
}

void Dequeue(QUEUE *q,char * dataOut)
{
    NODE *temp = q->front;
    dataOut = q->front->data;
    if (q->count == 1)
        q->rear = NULL;
    q->front = q->front->link;
    q->count--;
    free(temp);
   
}

int QueueFront(QUEUE *q, char *dataOut)
{
    if (q->count == 0)
        return 0;
    dataOut = q->front->data;
    return 1;
}

int EmptyQueue(QUEUE *q)
{
    if (q->count == 0)
        return 1;
    else
        return 0;
}

int FullQueue(QUEUE *q)
{
    NODE *temp = (NODE*)malloc(sizeof(NODE));
    if (temp == NULL)
        return 1;
    else
    {
        free(temp);
        return 0;
    }
}

int QueueCount(QUEUE *q)
{
    return q->count;
}

void DestroyQueue(QUEUE *q)
{
    NODE *temp;
    while (q->front != NULL)
    {
        temp = q->front;
        q->front = q->front->link;
        free(temp);
    }
    free(q);
}

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";
    printf("%s",expr);
    QUEUE *q = CreateQueue();
    char data1[8],data2[8],data[8],data3[8],data4[8],data5;
    int i=0,j=1,k=2,l=0;
    int a,b,r,t;
    char *p,*datain,*dataOut;
    p=data3;dataOut=data4;
    while(expr[2]!='\0')
    {
        i=0;j=1;k=3;l=0;
        
    while((expr[i]!='\0'))
    {
        if(ispunct(expr[i])&&isdigit(expr[j])&&isdigit(expr[k]))
        
           {
               data1[0]=expr[j];data1[1]='\0';
              // sprintf(data1, "%d", expr[j]);
               data2[0]=expr[k];data2[1]='\0';
               //sprintf(data2, "%d", expr[k]);
              
               a=atoi(data1);b=atoi(data2);
               r=calculate(expr[i],a,b);
               //itoa (r, data, 10);
               sprintf(data,"%d",r);
               datain=data;
               Enqueue(q, datain);
               i=i+3;j=j+3;k=k+3;
           }
        else
        {
            data[0]=expr[i];data[1]='\0';
            datain=data;
            Enqueue(q,datain);
            i++;j++;k++;
        }
    } 
    
    
    while(!EmptyQueue(q))
    {
        Dequeue(q,dataOut);
        expr[l]=data4;
        l++;
    }
            expr[l]='\0';
            
        printf("%s",expr);
    }

    return 0;
}


in the last while loop i want to convert the string ([COLOR=red yellow]char data4[]="10"[/color] ) to single char ([COLOR=red yellow]char a=10[/color]) [blue]//expr=10 in this case[/blue] .


and can u explain me tokenizing a string without using strtok function ??

Thanks for your support
 
sorry guys for my stupid questions. i got the answer . code is

Code:
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include <string.h>
typedef struct node
{
    char data[16];
    struct node *link;
} NODE;
typedef struct queue
{
    NODE *front;
    NODE *rear;
    int count;
} QUEUE;
QUEUE* CreateQueue()
{
    QUEUE* q = (QUEUE*)malloc(sizeof(QUEUE));  
    q->front = NULL;                           
    q->rear = NULL;                            
    q->count = 0;                              
    return q;
}
void Enqueue(QUEUE *q, char* dataIn)
{
    NODE *newNode = (NODE*)malloc(sizeof(NODE));   
    strcpy(newNode->data, dataIn);                 
    newNode->link = NULL;
    if (q->front == NULL)
        q->front = newNode;                        
    else
        q->rear->link = newNode;
    q->rear = newNode;
    //printf(" New node is %s", dataIn);
    q->count++;                            
}
void Dequeue(QUEUE *q, char *dataOut)
{
    NODE *temp = q->front;
    strcpy(dataOut, q->front->data);    
    if (q->count == 1)                  
        q->rear = NULL;                 
    q->front = q->front->link;          
    q->count--;                         
    free(temp);
}
int QueueCount(QUEUE *q)
{
    return q->count;         
}
void Display(QUEUE *q)
{
    NODE *pLoc = q->front; // first node
    printf("\n");
    printf("expression: ");
    while (pLoc != NULL)
    {
        printf("%s ", pLoc->data);
        pLoc = pLoc->link;
    }    
}
void DestroyQueue(QUEUE *q)
{
    NODE *temp;
    while (q->front != NULL)
    {
        temp = q->front;
        q->front = q->front->link;
        free(temp);
    }
    free(q);
}
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);
    else
        return -1;
}
void stringcopy(char data1[],char* dataptr)
{
    int i=0;
    while(*dataptr!='\0')
    {
        data1[i]=*dataptr;
        dataptr++;
        i++;
    }
    data1[i]='\0';
}
int calculateExpression(QUEUE *q)
{
    char data[16], data1[16],data2[16],temp_opr, temp_op1, temp_op2, *dataptr;
	int i, operand1, operand2, value;
    while ((QueueCount(q)!=1))    { 
        Dequeue(q, data);
        temp_opr=data[0];
   if(ispunct(temp_opr))// if temp_opr is punctuation '+' or '_' or '*' or '/'
   {
    dataptr=q->front->data;//data pointer is pointing to the 2nd data in the queue
    temp_op1=*dataptr;
    stringcopy(data1,dataptr); // copies the string pointed by dataptr to data1
            
    dataptr=q->front->link->data;
    temp_op2=*dataptr;      //data pointer is poiting to the 3rd data in the queue
    stringcopy(data2,dataptr); // copies the string pointed by dataptr to data2
     if(!ispunct(temp_op1)&&!ispunct(temp_op2))
            {
                
                operand1= atoi (data1);      
                operand2= atoi (data2);       
                Dequeue(q, data1);   
                Dequeue(q, data2);   
                value=calculate(temp_opr,operand1, operand2);
                printf (" \n\nafter calculating %d %c %d = %d\n",operand1,temp_opr,operand2,value);
                // itoa (value, data, 10);      
                sprintf(data,"%d",value);
                dataptr=data;
               	Enqueue(q, dataptr);  
                Display(q);
            }
            else
            {
                dataptr=data;                   
                Enqueue(q, dataptr);   
                Display(q);
            }
        }
        else
        {
            dataptr=data;
            Enqueue(q, dataptr); 
            Display(q);
        }     
    }
    Dequeue(q, data);  
    return atoi(data); 
int main()
{
	char expr[128] = "- + * 9 + 2 8 * + 4 8 6 3";
    char *token;
    int finalvalue;
    QUEUE *q = CreateQueue();   
	token = expr;
    while ((token = strtok(token, " ")))
    {
        Enqueue(q, token);
        token = NULL;
    }
    finalvalue=calculateExpression(q);
    printf("\n\n value of the expression %d", finalvalue);    
    DestroyQueue(q);  
}
 
this is the output

Code:
expression: - + * 9 + 2 8 * + 4 8 6 3 
expression: + * 9 + 2 8 * + 4 8 6 3 - 
expression: * 9 + 2 8 * + 4 8 6 3 - + 
expression: 9 + 2 8 * + 4 8 6 3 - + * 
expression: + 2 8 * + 4 8 6 3 - + * 9  

after calculating 2 + 8 = 10

expression: * + 4 8 6 3 - + * 9 10 
expression: + 4 8 6 3 - + * 9 10 *  

after calculating 4 + 8 = 12

expression: 6 3 - + * 9 10 * 12 
expression: 3 - + * 9 10 * 12 6 
expression: - + * 9 10 * 12 6 3 
expression: + * 9 10 * 12 6 3 - 
expression: * 9 10 * 12 6 3 - +  

after calculating 9 * 10 = 90

expression: * 12 6 3 - + 90  

after calculating 12 * 6 = 72

expression: 3 - + 90 72 
expression: - + 90 72 3 
expression: + 90 72 3 -  

after calculating 90 + 72 = 162

expression: 3 - 162 
expression: - 162 3  

after calculating 162 - 3 = 159

expression: 159 

 final value of the expression 159
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top