program which makes calculations

Joined
Dec 26, 2007
Messages
7
Reaction score
0
I need to write a function in C language which recieves an input as a string using the function gets.the input shuold consist of numbers (double) and the following actions:+,-.^,*,/ and brackets.The program should calculate the expression. the maximum length of the exspresion should be 195 chars.the program should calculate the expresion in the brackets at first, than the ^ and afterwards the / and * and at last the + and -.We assume that when we use the action ^ the second operand is an integer.
In addition, I can use only the libary <stdio.h> and the libary <string.h> and only the basic functions,arrays,loops and conditions.

I need to write this part of my program until monday and if I'll get a grade which is under 40 I will faill the course and would'nt be able to do the exam.

Therefore, I need some help as soon as possible. my idea was to get the input as a string which I defined as char and than divided it into 2 strings, one was double and contained the numbers and the second was char and contained the operators and brackets. however, I failed to do so.

any help would help me a lot.In addition, it would be great if someone could write me the code which divides the input string into the new two strings because I believe that I would be able to finish the exercise from this point.
 
Joined
Dec 26, 2007
Messages
7
Reaction score
0
I need some help

Code:
# include <stdio.h>
# include <string.h>

char sort_strings(char Input[]);
double tenDivider(double fraqpart,int i,int initial_i);

int main ()
{      char Input[195];
        gets(Input); 
        sort_strings(Input);
        return 0;
}
char sort_strings(char Input[])
{
        double num[195],integer,fracpart;
        char ch[195];
        int i,index,initial_i;
        while (i<strlen(Input))
        {       if ((Input[i]<47)||(Input[i]>58)||(Input[i]=='.'))
                {       while(Input[i]!='.')
                        {       integer=integer*10+(Input[i]-48);
                                i++;
                        }
                        if(Input[i]=='.')
                        {       i++;
                                while((Input[i]<47)||(Input[i]>58))
                                {       initial_i=i;
                                        fracpart=fracpart*10+(Input[i]-48);
                                        i++;
                                }
                        }
                        num[index]=integer+tenDivider(fracpart,i,initial_i);
                        index++;
                }
                else
                {
                 
                        ch[index]=Input[i];
                        index++;
                }
                printf("%s",ch);
        }
}
                        
                                
double tenDivider(double fracpart,int i,int initial_i )
{       double fraction;
        int r;
        for (r=i;r!=initial_i;--r)
                fraction=fracpart/10;
                return fraction;
}

this is the part of the program that I wrote which should sort the input into two new strings however, while the compilation runs smoothly the input isn't returned. I wrote in the end of the function sort_string the command printf in order to see if the string was divided into two strings. yet it does not return anything. therefore,I need your help. don't pay attention to the possibility of overflow because of the use of gets because I will repair this part at the end of the program.I realy need you to tell me what i did wrong and how I can fix it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top