Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: a

1 view
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
Sep 6, 2005, 11:27:37 PM9/6/05
to
coke wrote:
> well what i am trying to do is a copy an array and destroy the extra
> integers

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


coke

unread,
Sep 6, 2005, 11:43:05 PM9/6/05
to
its pretty long it might be easier if i e-mail it to you

Victor Bazarov

unread,
Sep 6, 2005, 11:57:33 PM9/6/05
to
coke wrote:
> its pretty long it might be easier if i e-mail it to you

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


coke

unread,
Sep 7, 2005, 12:16:22 AM9/7/05
to
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. 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++)
str[i] = another.str[i];
}

Karl Heinz Buchegger

unread,
Sep 7, 2005, 3:30:56 AM9/7/05
to
coke wrote:
>
> 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.

:-) 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

Kai-Uwe Bux

unread,
Sep 7, 2005, 3:55:17 AM9/7/05
to
coke wrote:

> 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

Message has been deleted
Message has been deleted

Kai-Uwe Bux

unread,
Sep 7, 2005, 11:41:59 PM9/7/05
to
coke wrote:

> 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

coke

unread,
Sep 8, 2005, 12:07:56 AM9/8/05
to
i have to use 2 stacks for my assignment. I have made it into a way
that it reads in normal numbers by int's instead of char's which are
what i had to use before but now my problem is that when i put in a +
or - or ) or ( or * or ^ or % or / it gives an error since it isnt an
int. I thought i remember a way to covert it say like static_cast i
believe? what im asking is
cin>>exp;
so when you cin a +, it goes into an infinite loop since it has no
where to go and prints -858993460
My question is how to make it to where i can read that in so i am able
to calculate it to whatever operation it is supposed to be

Mike Wahler

unread,
Sep 8, 2005, 2:03:40 PM9/8/05
to

"coke" <kingsl...@gmail.com> wrote in message
news:1126152476.5...@g49g2000cwa.googlegroups.com...

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


Message has been deleted
0 new messages