Thread: string caculator

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    1

    string caculator

    Hello everyone, I started to build a simple calculator week ago (plus, minus, multiplication ,division and brackets) with strings, so I can put a string for example: 1 +48 * (3-5 * (4 +7)).
    I have been towards the end, but my program has a problem when I have the first index minus, and I do not know how to consider the brackets.
    **the code worked great for equ such as 2*48+45/2...
    ** I didnt learn pointers yet.
    ****Thank for helpers

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    int calc(char str[25]);
    int whoisfirst(char str[25]);
    int closebrackets(char str[25]);
    int openbrackets(char str[25]);
    int calcwith(char str[101]);
    
    
    
    
    int main()
    {
        char str[25];
        printf("Enter mathematical expression\n");
        gets(str);
        calc(str);
        calcwith(str);
        system("PAUSE");
        return 0;
    }
    
    
    int closebrackets(char str[25])
    {
        int i,startbra,endbra,j;
        for(i=0;i<strlen(str);i++)
        {
            for(i=0;i<strlen(str);i++)
            {
                if(str[i]==')')
                {
                    endbra=i;
                    i=strlen(str)+2;
                    for(j=endbra;str[j]!='(';j--)
                    {
                    }
                    startbra=j;
                }
            }
        }
        return endbra;
    }
    
    
    int openbrackets(char str[25])
    {
        int i,startbra,endbra,j;
        for(i=0;i<strlen(str);i++)
        {
            for(i=0;i<strlen(str);i++)
            {
                if(str[i]==')')
                {
                    endbra=i;
                    i=strlen(str)+2;
                    for(j=endbra;str[j]!='(';j--)
                    {
                    }
                    startbra=j;
                }
            }
        }
        return startbra;
    }
    
    
    int whoisfirst(char str[25])
    {
        int i,index=-1,j;
        
        for(i=0;i<strlen(str);i++)
        {
            if(str[i]=='*' || str[i]=='/')
            {
                index=i;
                i=strlen(str)+2;
            }
        }
        if(index==-1)
        {
            for(i=0;i<strlen(str);i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    index=i;
                    i=strlen(str)+2;
                }
            }    
        }    
        printf("Founded at %d\n" , index);
        return index;
    }
    
    
    int calc(char str[25])
    {
        int i,insograim,sgorsograim,lifnei,j,sum,index,sum1,sum2,startexp=0,endexp,with=-1,finalsum=-1,endbra;
        char before[25],middle[25],after[25],firstnum[25],secondnum[25],between[25],lifney[25],ahrei[25],newstr[25],new2[25];
        for(i=0;i<strlen(str);i++)
        {
            if(str[i]!='(')
            {
                finalsum=1;
            }
        }
        if(finalsum=1)
        {
                index=whoisfirst(str);
                for(i=0;i<25;i++)
                {
                    before[i] = 0;
                    middle[i] = 0;
                    after[i] = 0;
                    firstnum[i] = 0;
                    secondnum[i] = 0;
                    between[i] = 0;
                    lifney[i] = 0;
                    ahrei[i] = 0;
                    newstr[i]=0;
                    new2[i] = 0;
                }
                for(i=0;i<strlen(str);i++)
                {
                    for(i=index-1;str[i]>='0' && str[i]<='9';i--)
                    {
                        startexp=i;
                    }
                    for(i=index+1;str[i]>='0' && str[i]<='9';i++)
                    {
                        endexp=i;
                    }
                    for(i=startexp;i<=endexp;i++)
                    {
                        between[i-startexp]=str[i];
                    }
                    for(i=0;i<startexp;i++)
                    {
                        lifney[i]=str[i];
                    }
                    for(i=endexp+1;i<strlen(str);i++)
                    {
                        ahrei[i-(endexp+1)]=str[i];
                    }
                }
        
                for(i=startexp,j=0;i<index;i++,j++)
                {
                    firstnum[j]=str[i];
                }
                sum1=atoi(firstnum);    
                for(i=index+1,j=0;str[i]>='0' && str[i]<='9';i++,j++)
                {
                    secondnum[j]=str[i];
                }
                sum2=atoi(secondnum);
                switch (str[index])
                {
                    case '+':
                        sum=sum1+sum2;
                        break;
                
                    case '-':
                        sum=sum1-sum2;
                        break;
        
                    case '*':
                        sum=sum1*sum2;
                        break;
                
                    case '/':
                        sum=sum1/sum2;
                        break;
                }
                printf("The string is %s\n" , str); 
        
                    strcat(new2,lifney);
                    itoa(sum,newstr,10);
                    strcpy (between, newstr);
                    strcat(new2,between);
                    strcat(new2,ahrei);
                    strcpy(str,new2);
            
                printf("The new string is %s\n" , str);     
                printf("the length is  %d\n" , strlen(str));
                 if(str[0]!='-')
                 {
                    for(i=0;i<strlen(str);i++)
                    {
                        if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
                        {
                            calc(str);
                        }
                    }
                 }
                return sum;
            }
    }
    
    
    /*
    THIS IS MY PROBLEM
    int calcwithbrackets(char str[101])
    {
        int i,insograim,sgorsograim,lifnei,j,sum,index,sum1,sum2,startexp=0,endexp,with=-1,finalsum=-1,endbra;
        char before[25],middle[25],after[25],firstnum[25],secondnum[25],between[25],lifney[25],ahrei[25],newstr[25],new2[25];
    //The code
            return 0;
    }
    */

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Take a hint, mr-sudhir. Unsolicited links to your website, while offering no real assistance in your posts, is not welcome.

    If you really want to support people learning C, then participate here by offering actual advice tailored to a persons question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  3. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  4. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM

Tags for this Thread