This assignment is to convert \"123.123\" into an actual number 123.123. It is n
ID: 666857 • Letter: T
Question
This assignment is to convert "123.123" into an actual number 123.123. It is not to display 123.123 which looks like a number. The final double variable should have the value of 123.123.
cout<
cout << numbers ; // This is right way and numbers should be a double variable, not stackType object.
Please help me fix this in the main function.
thanks in advance.
// myStack.cpp
#include "myStack.h"
#include
#include
template void stackType::initializeStack(){
stackTop = 0;
}
template bool stackType::isEmptyStack() const{
return (stackTop == 0);
}
template bool stackType::isFullStack() const{
return (stackTop == maxStackSize);
}
template void stackType::push (const Type& newItem) {
if (!isFullStack()){
list[stackTop] =newItem;
stackTop++;
}
else
cout <<"Cannot add to a full stack.";
}//endpush
template Type stackType::top()const{
assert (stackTop != 0);
return list [stackTop - 1];
}//end top
template void stackType::pop(){
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack.";
}//endpop
template stackType::stackType(int stackSize){
if (stackSize <=0){
cout << "Size of the array to hold the stack must " << "be positive.";
cout << "Creating an array of size 100.";
maxStackSize = 100;
}
else
maxStackSize =stackSize;
stackTop = 0;
list = new Type [maxStackSize];
}//end constructor
template
stackType::~stackType()
{
delete [] list;
}//end destructor
template
void stackType::copyStack(const stackType& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type [maxStackSize];
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}//end copyStack
template
stackType::stackType(const stackType& otherStack)
{
list = NULL;
copyStack (otherStack);
}
template
const stackType& stackType::operator=(const stackType& otherStack)
{
if (this != &otherStack)
copyStack (otherStack);
return * this;
} //end opertor=
int main()
{
cout << "Enter series of numbers" << endl;
string number;
getline(cin,number);//reading number from user
stackType numbers(10);
stackType numbers1(20);
int index=-1,total=0;
for(int i=0;i if (isdigit(number[i])){ //parsing each char...and checking char
numbers.push(number[i]-'0');
}
else{
index = i; //storing decimal point char
}
total++;
}
while(!numbers1.isEmptyStack())
{
numbers1.push(numbers.top());
numbers.pop();
}
cout<<"Print digits in numbers"< for(int i=0;i if(i==index){
cout<<"";
index = -1;
}
cout< numbers1.pop();
}
getch();
return 0;
}
Explanation / Answer
Answer:
Note: Modified code as per user requirements.
#include "myStack.h"
#include <iostream>
#include <assert.h>
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}//end initializeStack
template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}//end destroyStack
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == 0);
}//end isEmptyStack
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
} //end isFullStack
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem; //add newItem at the
//top of the stack
stackTop++; //increment stackTop
}
else
cerr<<"Cannot add to a full stack."<<endl;
}//end push
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top
template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
stackTop--; //decrement stackTop
else
cerr<<"Cannot remove from an empty stack."<<endl;
}//end pop
template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"Size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize; //set the stack size to
//the value specified by
//the parameter stackSize
stackTop = 0; //set stackTop to 0
list = new Type[maxStackSize]; //create the array to
//hold the stack elements
assert(list != NULL);
}//end constructor
template<class Type>
stackType<Type>::~stackType() //destructor
{
delete [] list; //deallocate memory occupied by the array
}//end destructor
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
assert(list != NULL);
//copy otherStack into this stack
for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}//end copy constructor
template<class Type>
const stackType<Type>& stackType<Type>::operator=
(const stackType<Type>& otherStack)
{
if(this != &otherStack) //avoid self-copy
copyStack(otherStack);
return *this;
} //end operator=
int main()
{
cout << "Enter series of numbers" << endl;
string number;
getline(cin,number); //reading number from user
stackType<int> numbers(10);
stackType<int> numbers1(20);
int index=-1,total=0;
for(int i=0;i<number.size();i++)
{
if (isdigit(number[i]))
{ //parsing each char...and checking char
numbers.push(number[i]-'0');
}
else
{
index = i; //storing decimal point char
}
total++;
}
while(!numbers.isEmptyStack())
{
numbers1.push(numbers.top());
numbers.pop();
}
cout<<"PRINT DIGITS IN NUMBERS"<<endl<<endl;
for(int i=0;i<total;i++)
{ //printng numerial format
if(i==index)
{
cout<<"";
index = -1;
}
cout<<numbers1.top();
numbers1.pop();
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.