Question

Hi, I need to change the conversion where stack is into a class stack and add...

Hi, I need to change the conversion where stack is into a class stack and add a template <class type>

PROMPT:

write a program that reads a string consisting of a positive integer
or a positive decimal number and converts the number to the numeric format.
if the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.

#include
#include
#include
#include
#include

using namespace std;
double conversion(string);

int main() {
   int num = 0, abc;
   string str;

   cout << "Enter the string: ";
   cin >> str;
   cout << "String is: " << str;

   //wrong input
   int i;
   for (i = 0; str[i] != '\0'; i++)
   {
       if ((str[i] < 48 || str[i]>57) && str[i] != '.')
           throw str;
   }

   // decimal number
   for (int i = 0; str[i] != '\0'; i++)
   {
       if (str[i] == '.')
       {
           num = 1;
           break;
       }
   }
   if (num == 0)
   {
       abc = stoi(str);
       cout << endl << "Number is: " << abc;
   }
   else {
       double decimal_abc = conversion(str);
       cout << "Number is: " << decimal_abc;
   }
   system("pause");
   return 0;
}
//conversion
double conversion(string str)
{
   stack < int> stk;
   int n = str.length();
   char *s;
   s = new char[n];

   int k = 0;
   bool decimal = false;

   for (int i = n - 1; i >= 0; i--)
   {
       if (str[i] == '.')
       {
           decimal = true;
       }
       stk.push(str[i]);
   }
   cout << endl;

   while (!stk.empty())
   {
       s[k] = stk.top();
       stk.pop();
       k++;
   }
   cout << endl;
   return stod(s);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Conversion.cpp:

#include<iostream>
#include<string>
#include <cstdlib>
#define SIZE 100
// define default size of stack
using namespace std;

// Class for stack
template <class type>
class stack
{
    type *array;
    int top_element;
    int capacity;

public:
    stack(int size = SIZE);
    void push(type);
    type pop();
    type top();
    bool empty();
    bool isFull();
};
// Constructor to initialize stack
template <class type>
stack<type>::stack(int size)
{
    array = new type[size];
    capacity = size;
    top_element = -1;
}

// function to add an element x of "type" may be int,char etc.. data type in the stack
template <class type>
void stack<type>::push(type x)
{
    if (isFull())
    {
        cout << "Stack OverFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }

    cout << endl << "Inserting element " << x<<" into the stack.";
    array[++top_element] = x;
}

// function to pop top element from the stack
template <class type>
type stack<type>::pop()
{
    // check for stack underflow
    if (empty())
    {
        cout << "UnderFlow\nProgram Terminated\n";
        exit(EXIT_FAILURE);
    }

    cout << endl<< "Removing element " << top() <<" from the stack. ";

    // decrease stack size by 1 and return the popped element
    return array[top_element--];
}

// function to return top element in the stack
template <class type>
type stack<type>::top()
{
    if (!empty())
        return array[top_element];
    else
        exit(EXIT_FAILURE);
}
template <class type>
bool stack<type>::empty()
{
    return top_element == -1;   //returns true or false for stack is empty or not
}
template <class type>
bool stack<type>::isFull()
{
    return top_element == capacity - 1;   //returns true or false for stack is full or not
}
double conversion(string);

int main() {
    int num = 0, abc;
    string str;

    cout << "Enter the string: ";
    cin >> str;
    cout << "String is: " << str;

    //wrong input
    int i;
    for (i = 0; str[i] != '\0'; i++)
    {
        if ((str[i] < 48 || str[i]>57) && str[i] != '.')
            throw str;
    }

    // decimal number
    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == '.')
        {
            num = 1;
            break;
        }
    }
    if (num == 0)
    {
        abc = stoi(str);
        cout << endl << "Number is: " << abc<<endl;
    }
    else {
        double decimal_abc = conversion(str);
        cout << endl<< "Number is: " << decimal_abc<< endl;
    }
    //system("pause");
    return 0;
}
//conversion
double conversion(string str)
{
    stack < char> stk;
    int n = str.length();
    char *s;
    s = new char[n];

    int k = 0;
    bool decimal = false;

    for (int i = n - 1; i >= 0; i--)
    {
        if (str[i] == '.')
        {
            decimal = true;
        }
        stk.push(str[i]);
    }
    cout << endl;

    while (!stk.empty())
    {
        s[k] = stk.top();
        stk.pop();
        k++;
    }
    cout << endl;
    return stod(s);
}

Output:

I added print statements in push(type) and pop function for better understanding.You can delete them if you don't want.

Please up vote if you find this solution helpful. Thank you

Add a comment
Know the answer?
Add Answer to:
Hi, I need to change the conversion where stack is into a class stack and add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT