Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4544672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:42:17+00:00 2026-05-21T15:42:17+00:00

I want to build simple parse tree/but it’s not working. I think that strlen

  • 0

I want to build simple parse tree/but it’s not working. I think that strlen is returning the wrong result.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

int min(int a ,int b){ return (a>b)?b:a;}

class Node
{
    Node* left;
    Node* right;
    char operation;

public:
    float res;

    Node(char* str)
    {
        char* ch = NULL;
        ch = strrchr2(str);
        if (ch==NULL)
        {
            if(str[0]=='(')
            {
                str = &str[1]; 
                int k = strlen(str);
                char *tmp = new char[k-1];

                strncpy(tmp,str,k-1);
                ch = strrchr2(tmp);

                div(tmp,ch);
            }
            else
                res = atof(str);                
        }
        else
        {
            div(str,ch);
        }
    }

    void div(char* str,char* ch)
    {
            //getting opertion
            strncpy(&operation,ch,1);

            //getting position of operation
            int pos = strlen(str)-strlen(ch);

            //right node creation
            ch = &ch[1];
            right = new Node(ch);

            //left node creation
            char* tmp = new char[pos];
            strncpy(tmp,str,pos);
            left = new Node(tmp);

            //applying opertion to to node's results
            switch( operation )
            {
                case '/':this->res = left->res/right->res;break;
                case '*':this->res = left->res*right->res;break;
                case '+':this->res = left->res+right->res;break;
                case '-':this->res = left->res-right->res;break;
                default:break;
            };
    }

    char* strrchr2(char* s)
    {
        char* str = new char[strlen(s)];
        strcpy(str,s);
        int i = search(str);
        if(i==-1) return NULL;
            else
        if(str[i]=='+' || str[i]=='-') return &str[i];
            else if (str[i] == '*' || str[i]=='/')
            {
                char c = str[i+1];
                if(i+1<strlen(str)-1 && search(&c)!=-1) return strrchr2(&c);
                else return &str[i];
            }
    }

    int search(char* str)
    {
        int cnt1 = 0;
        int cnt2 = 0;
        for(int i=0;i<strlen(str);i++)
        {
            if (str[i]=='(') cnt1++;
            else if (str[i]==')') cnt2++;
            if (cnt1!=0)
            {
               if(cnt1==cnt2)
               {
                  int m = strlen(str);
                  return ((i+1)!=m?i+1:-1);
               }
            }
            else
            {
                if (str[i]=='-' || str[i]=='+' || str[i]=='*' || str[i]=='/') return i;
            }
        }
        char* ch = NULL;
        ch = strchr(str,'-');
        if(ch==NULL) ch = strchr(str,'+');
        if(ch==NULL) ch = strchr(str,'*');
        if(ch==NULL) ch = strchr(str,'/');
        if(ch==NULL)return -1;
        else return strlen(str)-strlen(ch);
    }
};

int main()
{
    char* expr = "(1/2)-(1/2)";
    Node* n = new Node(expr);
    printf("%s = %.3f",expr,n->res);



    _getch();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:42:18+00:00Added an answer on May 21, 2026 at 3:42 pm

    It seems like you are doing C with class more than C++.
    Try using std::string insted of char *. This way you won’t have any trouble with strlen (if strlen is really the problem) because string has a size() function and it is garantied to work very well. Most of all you’ll be able to use many features that you are trying to re-code there.

    More informations about strings : http://www.cplusplus.com/reference/string/string/

    M.Dijsktra also created an algorithm to parse mathematical expressions and output an AST (Abstract Syntax Tree). It will allow you to deal with more complex expression. It’s called the ‘shunting yard algorithm’. Here it is on wikipedia : http://en.wikipedia.org/wiki/Shunting-yard_algorithm

    I thought you might want to know what’s already been done . 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newcomer to Rails. I want to build a simple form that determines
i want to do test and build simple debugger GUI in c++ that debugging
I want to build a bot that asks someone a few simple questions and
I want to build a simple web service that outputs JSON to test a
I want to build a simple application with the MVVM pattern. This application will
I would like to build a simple animation for a school project. I want
I want build a sketch pad app on iPhone, I assume that this type
I want to build a parser for analyzing a large input file, but I
This has to be pretty simple, but I'd like to parse the current URL
I want to build a simple application for a bookstore. There are a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.