GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 18-Feb-2008, 11:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: In method `void InfixExpression::brCalc(double **, char **, int, int, int, int)'


Quote:
Originally Posted by takachi
... I have encountered another bug in the process.

You have, again made a lot of work for yourself by implementing hundreds of lines of code without testing whether your basic organization is consistent. See Footnote.

Here's the typical organization for a simple program:

1. File InfixExpression.h has the class definition. If you want to put some of the implementations (the functions themselves) inside this file, then do it. Typically some small function implementations may be in here, but lots of people put just the data declarations and the function headers here. Any function declarations Inside the class definition don't need (and, in fact can't have) the InfixExpression:: part of the function declarations. I'll just show constructors and destructor that you wrote inside the class definition.
CPP / C++ / C Code:
//File: InfixExpression.h
#ifndef INFIXEXPRESSION_H
#define INFIXEXPRESSION_H

class InfixExpression
{
  private:
    int checkOp(char *str);
    int checkBr(char *str);
    bool isInit(char *exp);

  public:
    /* show the constructor here. It's the entire
     * implementation; it won't be in InfixExpression.cpp
     */
    InfixExpression():exp(NULL)
    {
    }

        /*the second constructor */
    InfixExpression(char *str)
    {
        strcpy(exp, str);
    }

        /*the destructor */
    ~InfixExpression()
    {
        if (exp != NULL)
            delete[]exp;

    }

    void Set(char *str);
    bool isMirror(char *exp);
    void reverse(char **exp);
    void change_Op(char *operators,int begin,int end);
    void change_num(double *num,int begin,int end,double result);
    int  brFind1(char *operators);
    int  brFind2(char *operators);
    int findNum1(char *operators,int begin);
    int findNum2(char *operators,int begin,int end);
    void brCalc(double *num,char *operators,int begin,int end,int numBegin,int numEnd);
    void calcAllBr(double *num,char *operators);
    void calc(double *num,char *operators);
    double returnValue(char *exp);
    };
# endif


2. Now, in InfixExpression.cpp, you put all of the functions that weren't implemented in InfixExpression.h. Do not (that's not) declare the class again in InfixExpression.cpp. You will #include "InfixExpression.h", and the class is defined there. In this file, the function definitions will have the InfixExpression:: part. All of them will have it.

CPP / C++ / C Code:
//InfixExpression.cpp
//
#include <iostream>
#include <cstring>

#include "InfixExpression.h"
using std::cout;
using std::endl;

int InfixExpression::checkOp(char *str)
{
 // your function stuff here
}

    /*check if the number of the opening brackets is equal to the closing ones */
int InfixExpression::checkBr(char *str)
{
  // your function stuff here
}

    /*check if the variable which is supposed to keep the expression is initialized */
bool InfixExpression::isInit(char *exp)
{
 // your function stuff here
}



    /*initialize the exp variable */
void InfixExpression::Set(char *str)
{
 // your function stuff here
}

    /*check if the expression is Mirror */
bool InfixExpression::isMirror(char *exp)
{
  // your function stuff here
}

    /*reverse the operators */
void InfixExpression::reverse(char **exp)
{
  // your function stuff here
}

    /*change the operators string */
void InfixExpression::change_Op(char *operators, int begin, int end)
{
  // your function stuff here
}

    /*change the numbers string */
void InfixExpression::change_num(double *num, int begin, int end,
                                 double result)
{
 // your function stuff here
}

    /*find the last opening bracket */
int InfixExpression::brFind1(char *operators)
{
  // your function stuff here
}

    /*find the last closing bracket */
int brFind2(char *operators)
{
  // your function stuff here
}

    /*find  the begining of the numbers array which are related to the brackets */
int InfixExpression::findNum1(char *operators, int begin)
{
  // your function stuff here
}

    /*find  the end of the numbers array which are related to the brackets */
int InfixExpression::findNum2(char *operators, int begin, int end)
{
 // your function stuff here
}

    /*calculate the expression in the brackets */
void InfixExpression::brCalc(double *num, char *operators, int begin,
                             int end, int numBegin, int numEnd)
{
  // your function stuff here
}

    /*calculate all the expressions in the brackets */
void InfixExpression::calcAllBr(double *num, char *operators)
{
  // your function stuff here
}

    /*calculate the remaining expresion after all brackets were removed */
void InfixExpression::calc(double *num, char *operators)
{
 // your function stuff here
}

    /*return the result of the calculation */
double InfixExpression::returnValue(char *exp)
{
  // your function stuff here
}


3. All files that have instances of the class and its members will #include "InfexExpression.h"


Now, you might still have some problems with compiling the individual cpp files or in linking them all together (and maybe even some problems with functionality), but that's the way that most people would organize it, and is as a good a way as any.

Next-to-Bottom line: you defined a function class in InfixExpression.h and included InfixExpression.h in your main.cpp, There was a class definition, whose member functions were prototyped but not implemented. The object file, main.o, had no way of filling in the function definitions.

Here's why:

The fact that your InfixExpression.cpp defined its own version of that class didn't help at all, since the class definition in main.cpp would hide the class with the same name that was defined somewhere else.

Bottom line: See Footnote.


Regards,

Dave

Footnote:
Why, oh why, would you write hundreds of lines of code that involves something that you had never done before (and don't understand)? Why not just write a few lines (maybe give the class one function). Write, compile, test and debug the simple case (a few tens of lines at most). Then get to the new stuff. Questions could be shorter and simpler; answers could be shorter and simpler. Just a thought.
  #12  
Old 18-Feb-2008, 12:13
takachi takachi is offline
Awaiting Email Confirmation
 
Join Date: Dec 2007
Posts: 101
takachi is an unknown quantity at this point

reply to Footnote


This exercise is my first exercise in c++.I took a course for c programing last semester and now I learn c++ in another course. Unfortunately, the home exercises on those two courses are very hard,especially because I do not have enough experience and yet I have no other choice but to do everthing in order to do them. I agree with you that it is stupid to give us so hard exercises from the very begining but I'm not the one who wrote the assignment.
By the way this is just half of the exercise.
And again thank you for your help.
  #13  
Old 18-Feb-2008, 12:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: reply to Footnote


Quote:
Originally Posted by takachi
I have no other choice but to do everthing in order to do them.
But you don't have to do everything all at once. That is your choice.
Quote:
Originally Posted by takachi

I agree with you that it is stupid ...

1. To the best of my knowledge, I have never used the word "stupid" in any of my posts except when referring to my own stupidity. (Or maybe in my sometimes-feeble attempts to introduce a little humor. See Footnote.) Certainly not when talking about instructors. (Maybe some are stupid; maybe some of them do things that I consider stupid; I have never called those things stupid. Misguided maybe, in my opinion, but not necessarily stupid.) So: please don't put words in my mouth.

2. Maybe one of the things you are supposed to learn (even if your professor didn't say it---even if he/she doesn't know it) is that starting with a very simple and short program and building it incrementally might be a Good Way to do things.

Test each step before going to the next. Then you might not have to retype so many of the things that you put in the program.

Quote:
Originally Posted by takachi
...thank you...
You are very welcome.

Hang in there...

Regards,

Dave

Footnote:

"Men are born ignorant, not stupid. They are made stupid by education."
---Bertrand Russell
 
 

Recent GIDBlogRunning Linux Programs at Boot Time by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 12:20
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 19:37
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 12:52

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:31.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.