An array of what? Extra integers from where? Is that an array of
integers?
> that i no longer need after they are used. So i am trying to
> make a copy constructor and a destructor so i can copy an array for
> only what i need.
Is that array a member of a class? Why not use 'vector' or 'list'?
Those things are much easier to maintain than a dynamic array (if that
in fact is what you're trying to do, and I *am* guessing here).
> it is a calculator program and when i find a ) i
> need to stop the program and calculate it and continue for example
>
> ((6+4)-1)
> so it can do 6+4 and destroy (6+4) to then make it (10-1)
> The ( and ) are integers and the addition and multipication are
> different arrays
> hope this helps thanks.
No, this all doesn't really help. Post your code. All of it. Then
we can see where you're failing (or what you're missing).
V
No, thanks. I don't do individual consultations. If you
want our help, distill your code to the bare minimum needed
to show what you have the difficulty with, and post it here.
V
:-) Trust us. He won't
You programmed by yourself. You didn't ask for writing your assignment.
All you want is some help on some detail. Usually there is no problem
with that.
> ill
> try explaining once again I have two arrays. One is an integer array
> (0-9) another is an operator array (* ^ % + -...) my problem is that in
> the array when i put ((6+4)-1) what i need is to add 6+4 and in the
> integer array to remove (64) and in the operator array to remove the +
> so that it leaves (10-1) so i believe i need a copy constructor and a
> destructor... my question is how do i write the copy constuctor
> MyType::MyType(const MyType& another)
> {
> int i;
> s = another.s;
> str = new char[s];
> for(i = 0; i<size; i++)
Why 'size'?
You alloected room for s characters.
for( i = 0; i < s; i++ )
> str[i] = another.str[i];
> }
Looks good otherwise.
--
Karl Heinz Buchegger
kbuc...@gascad.at
> ok well the problem with posting it in an open forum is that it is for
> a class and i dont want my professor to think that i am cheating.
Well, do *you* think you are cheating?
> ill try explaining once again I have two arrays. One is an integer array
> (0-9) another is an operator array (* ^ % + -...) my problem is that in
> the array when i put ((6+4)-1) what i need is to add 6+4 and in the
> integer array to remove (64) and in the operator array to remove the +
> so that it leaves (10-1) so i believe i need a copy constructor and a
> destructor...
Your problem is not a copy constructor but that you insist on talking about
arrays. That is an implementation detail. What you really have is a
sequence containting integers, operators, and parentheses. And it so turns
out that in the process of manipulating this sequence you need to cut out
subsequences (corresponding to subexpressions that are being evaluated) and
you need to insert tokens (the values of those subsequences) at the
position of the previous cut.
Now, your homework assignment reads: find a data structure realizing a
sequence of tokens of different types (integers, operators, parentheses)
that supports the needed operations efficiently.
In a first approximation, you might want to ignore the complication arising
from the different types and just focus on cut/paste operations.
> my question is how do i write the copy constuctor
> MyType::MyType(const MyType& another)
> {
> int i;
> s = another.s;
> str = new char[s];
> for(i = 0; i<size; i++)
> str[i] = another.str[i];
> }
Best
Kai-Uwe Bux
> Ok i figured out that i dont need a copy constructor and instead i am
> popping out the used variables. if i put in ((8-2)-2) it prings out 4,
> which is the correct answer but if i put in ((8-4)-(2+2)) it prints a
> crazy number it prints out every step of the way and at the end it
> shows ((4-4) but still prints a crazy number... also i had to convert
> everything to char instead of int's so it would print correctly.
> here is the code if someone could debug and show me what my problem is
> i would be most appreciative.
[big piece of code snipped]
Hm, I think that your decision to use two arrays is confusing. In both
arrays some slots are not used. Those slots are uninitialized memory. The
crazy number that you see most likely comes from there. So, I venture the
conjecture that in the pushing and poping the two arrays get out of sync.
May I suggest to use just one stack. That is way more easy to maintain. This
stack would contain tokens, e.g., something like this:
struct Token {
enum Operator { plus, minus, multiplies, divides };
enum Class { num, op, lpar, rpar };
union {
int i;
Operator op;
} value;
Class type;
};
Now, you can parse the expression from left to right and translate it into a
sequence of tokens. Whenever you encounter a right parenthesis, you pop
everything from the stack down to the previous left parenthesis and replace
that subexpression by a num-token representing its value.
Best
Kai-Uwe Bux
Read everything as text (i.e. 'characters' and 'strings').
When the input is expected to be numeric, check to see if
the read characters form a valid numerical value.
See function 'strtol()' (note that its input (a string)
must be zero terminated).
-Mike