473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

a

well what i am trying to do is a copy an array and destroy the extra
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. 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.

Sep 7 '05 #1
12 1326
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
Sep 7 '05 #2
its pretty long it might be easier if i e-mail it to you

Sep 7 '05 #3
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
Sep 7 '05 #4
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];
}

Sep 7 '05 #5
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
kb******@gascad.at
Sep 7 '05 #6
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
Sep 7 '05 #7
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.
///////////////////////////Include files and global Constants
////////////////

#include <iostream>
using namespace std;

int const SIZE = 100;
enum ErrMsg{ OPERAT, OPERAND, PARENT };

/////////////// Stack Class Header
///////////////////////////////////////////

class Stack
{
public:
Stack();
~Stack();
void Push( char value );
char Pop();
int Peek() const;
bool IsNotEmpty() const;
bool IsNotFull() const;
void Dump( ostream& os) const;
void calc( ostream& os) ;

private:
void Error( ErrMsg msg ) const;
typedef int ArrayTYPE[ SIZE ];
ArrayTYPE num;
ArrayTYPE op;
char top;
char left;
char right;

};

////////////// Stack Class
Implementation ///////////////////////////////

Stack:: Stack()
{
top = -1; // indicates stack is IsNotEmpty
left = 0;
right = 0;
cout << "Stack Constructor: top = " << top << endl;
}

Stack:: ~Stack()
{
top = -1;
left = 0;
right = 0;
cout << "Now inside Stack Destructor:top = " << top << endl;
}

void Stack:: Push( char value )
{ // increment top and store value

if( IsNotFull() )
{
top++;
switch (value)
{
case 48 : value=0;
break;
case 49 : value=1;
break;
case 50 : value=2;
break;
case 51 : value=3;
break;
case 52 : value=4;
break;
case 53 : value=5;
break;
case 54 : value=6;
break;
case 55 : value=7;
break;
case 56 : value=8;
break;
case 57 : value=9;
break;
}
////////////////// 94=^ 37=% 42=* 47=/ 45=- 43=+
//////////////////
if
((value==94)||(value==37)||(value==42)||(value==45 )||(value==47)||(value==43))
{
op[top] = value;
}
////////////////// 48=0 49=1 50=2.... 57=9 (=40 )=41
//////////////////
if ((value<=9)&&(value>=0)||(value==40)||(value==41))
{
num[top] = value;
}
cout << "\t\t\tAfter push, top = " << top << endl;
if (value==40)
{
left++;
}
if (value==41)
{
right++;
}

}
//else
// Error( OVERFLOW );
}

char Stack:: Pop()
{ // remove top value and decrement top
if( IsNotEmpty() )
{
char value = num[top--];
cout << "\t\t\tAfter Pop, top = " << top << endl;
return value;
}
}
/*
int Stack:: Peek() const
{ // return top value
if( IsNotEmpty() )
{
char value = num[top];
cout << "\t\t\tAfter Peek, top = " << top << endl;
return value;
}
else
{
Error( UNDERFLOW );
return 0;
}
}

*/
bool Stack:: IsNotEmpty() const
{
return (top >= 0);
}

bool Stack:: IsNotFull() const
{
return (top < SIZE-1);
}
void Stack:: Error( ErrMsg msg ) const
{
switch( msg )
{
case OPERAT: cout<<"ERROR: Missing operator\n"; break;
case OPERAND: cout<<"ERROR: Missing operand\n"; break;
case PARENT: cout<<"ERROR: No right parenthisis\n"; break;
}
}
void Stack:: Dump( ostream& os) const
{
if (left!=right)
{
Error( PARENT );
}
os << "\t\t\t\ttop:" << top << " contents: ";
char loc = top;
if
(((num[loc]<=9)&&(num[loc]>=0))&&((num[loc-1]<=9)&&(num[loc-1]>=0)))
{
Error( OPERAND );
}
if
(((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))&&((op[loc-1]==94)||(op[loc-1]==37)||(op[loc-1]==42)||(op[loc-1]==45)||(op[loc-1]==47)||(op[loc-1]==43)))
{
Error( OPERAT );
}
while( loc >= 0 )
{
if
((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))
os << op[loc] << " ";
else
os << num[loc] << " ";
loc--;
}
os << endl;

}
////////////////////////////////// FIGURE THIS OUT
//////////////////////////////////////////

void Stack:: calc( ostream& os)
{
char loc = top, ope;
int l, sec;
while (loc>=0)
{
if (num[loc]==41)
loc--;
if (num[loc]==40)
loc--;
sec=num[loc];
loc--;
if
((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))
{
ope=op[loc];
}
loc--;
if ((num[loc]<=9)&&(num[loc]>=0))
{
if (ope==37)
{
l= (num[loc])%(sec);
num[loc]=l;
}
if (ope==42)
{
l= (num[loc])*(sec);
num[loc]=l;
}
if (ope==43)
{
l= (num[loc])+(sec);
num[loc]=l;
}
if (ope==45)
{
l= (num[loc])-(sec);
num[loc]=l;
}
if (ope==47)
{
l= (num[loc])/(sec);
num[loc]=l;
}
if (ope==94)
{
l= (num[loc])^(sec);
num[loc]=l;
}
}
else
{
loc--;
}
}

os << endl << endl << endl << endl << endl << endl << "The answer is:
" << l << endl <<endl ;
}
/////////////////// FIGURE THIS OUT ///////////////////////////////////

////////////// Client Section /////////////////////////////////////////

int main()
{
cout << "Program StackCls.cpp. Creates a stack of up to 10
integers\n\n";

Stack theStack;
bool continueOn = true;
char value;

// Main Loop
while( continueOn )
{
cout << "Enter the equation:\n";
cin >> value;
// Push
theStack.Push( value );
// Dump
theStack.Dump( cout );
// Calc
if (value==41)
{
theStack.calc( cout );
for ( int i=0; i<3; i++)
{
value = theStack.Pop();
}

}

}
return 1;
}

Sep 8 '05 #8
if someone can even tell me a better way to write this?

Sep 8 '05 #9
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
Sep 8 '05 #10
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

Sep 8 '05 #11

"coke" <ki**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


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
Sep 8 '05 #12
i have looked up what you are talking about but i dont think i
understand exactly what you are talking about but i do have everything
set to chars again.... please tell me where i am going wrong
when i put in ((8-6)+(2+2)) it goes through and gets ((2+(4+
here is my code::::

// StackCls.cpp Simple implementation of a Stack class

///////////////////////////Include files and global Constants
////////////////

#include <iostream>
using namespace std;

int const SIZE = 100;
enum ErrMsg{ OPERAT, OPERAND, PARENT };

/////////////// Stack Class Header
///////////////////////////////////////////

class Stack
{
public:
Stack();
~Stack();
void Push( char value );
char Pop();
int Peek() const;
bool IsNotEmpty() const;
bool IsNotFull() const;
void Dump( ostream& os) const;
void calc( ostream& os) ;

private:
void Error( ErrMsg msg ) const;
typedef int ArrayTYPE[ SIZE ];
ArrayTYPE num;
ArrayTYPE op;
char top;
char left;
char right;

};

////////////// Stack Class
Implementation ///////////////////////////////

Stack:: Stack()
{
top = -1; // indicates stack is IsNotEmpty
left = 0;
right = 0;
cout << "Stack Constructor: top = " << top << endl;
}

Stack:: ~Stack()
{
top = -1;
left = 0;
right = 0;
cout << "Now inside Stack Destructor:top = " << top << endl;
}

void Stack:: Push( char value )
{ // increment top and store value

if( IsNotFull() )
{
top++;
switch (value)
{
case 48 : value=0;
break;
case 49 : value=1;
break;
case 50 : value=2;
break;
case 51 : value=3;
break;
case 52 : value=4;
break;
case 53 : value=5;
break;
case 54 : value=6;
break;
case 55 : value=7;
break;
case 56 : value=8;
break;
case 57 : value=9;
break;
}
////////////////// 94=^ 37=% 42=* 47=/ 45=- 43=+
//////////////////
if
((value==94)||(value==37)||(value==42)||(value==45 )||(value==47)||(value==43))
{
op[top] = value;
}
////////////////// 48=0 49=1 50=2.... 57=9 (=40 )=41
//////////////////
if ((value<=9)&&(value>=0)||(value==40)||(value==41))
{
num[top] = value;
}
cout << "\t\t\tAfter push, top = " << top << endl;
if (value==40)
{
left++;
}
if (value==41)
{
right++;
}

}
//else
// Error( OVERFLOW );
}

char Stack:: Pop()
{ // remove top value and decrement top
if( IsNotEmpty() )
{
char value = num[top--];
cout << "\t\t\tAfter Pop, top = " << top << endl;
return value;
}
}
/*
int Stack:: Peek() const
{ // return top value
if( IsNotEmpty() )
{
char value = num[top];
cout << "\t\t\tAfter Peek, top = " << top << endl;
return value;
}
else
{
Error( UNDERFLOW );
return 0;
}
}

*/
bool Stack:: IsNotEmpty() const
{
return (top >= 0);
}

bool Stack:: IsNotFull() const
{
return (top < SIZE-1);
}
void Stack:: Error( ErrMsg msg ) const
{
switch( msg )
{
case OPERAT: cout<<"ERROR: Missing operator\n"; break;
case OPERAND: cout<<"ERROR: Missing operand\n"; break;
case PARENT: cout<<"ERROR: No right parenthisis\n"; break;
}
}
void Stack:: Dump( ostream& os) const
{
if (left!=right)
{
Error( PARENT );
}
os << "\t\t\t\ttop:" << top << " contents: ";
char loc = top;
if
(((num[loc]<=9)&&(num[loc]>=0))&&((num[loc-1]<=9)&&(num[loc-1]>=0)))
{
Error( OPERAND );
}
if
(((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))&&((op[loc-1]==94)||(op[loc-1]==37)||(op[loc-1]==42)||(op[loc-1]==45)||(op[loc-1]==47)||(op[loc-1]==43)))
{
Error( OPERAT );
}
while( loc >= 0 )
{
if
((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))
os << op[loc] << " ";
else
os << num[loc] << " ";
loc--;
}
os << endl;

}
////////////////////////////////// FIGURE THIS OUT
//////////////////////////////////////////

void Stack:: calc( ostream& os)
{
char loc = top, ope;
int l, sec;
while (loc>=0)
{
if (num[loc]==41)
loc--;
if (num[loc]==40)
loc--;
sec=num[loc];
loc--;
if
((op[loc]==94)||(op[loc]==37)||(op[loc]==42)||(op[loc]==45)||(op[loc]==47)||(op[loc]==43))
{
ope=op[loc];
}
loc--;
if ((num[loc]<=9)&&(num[loc]>=0))
{
if (ope==37)
{
l= (num[loc])%(sec);
num[loc]=l;
}
if (ope==42)
{
l= (num[loc])*(sec);
num[loc]=l;
}
if (ope==43)
{
l= (num[loc])+(sec);
num[loc]=l;
}
if (ope==45)
{
l= (num[loc])-(sec);
num[loc]=l;
}
if (ope==47)
{
l= (num[loc])/(sec);
num[loc]=l;
}
if (ope==94)
{
l= (num[loc])^(sec);
num[loc]=l;
}
}
else
{
loc--;
}
}

os << endl << endl << endl << endl << endl << endl << "The answer is:
" << l << endl <<endl ;
}
/////////////////// FIGURE THIS OUT ///////////////////////////////////

////////////// Client Section /////////////////////////////////////////

int main()
{
cout << "Program StackCls.cpp. Creates a stack of up to 10
integers\n\n";

Stack theStack;
bool continueOn = true;
char value;

// Main Loop
while( continueOn )
{
cout << "Enter the equation:\n";
cin >> value;
// Push
theStack.Push( value );
// Dump
theStack.Dump( cout );
// Calc
if (value==41)
{
theStack.calc( cout );
for ( int i=0; i<3; i++)
{
value = theStack.Pop();
}

}

}
return 1;
}

Sep 8 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.