Question

c++ the program must use a stack to convert the decimal number to the numeric format....

c++

the program must use a stack to convert the decimal number to the numeric format.

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,

Expert Answer

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

The C++ program is given below. The explaination is given as comment within the code itself.

#include <iostream>
#include <string>
#include <stack>
#include <math.h>

using namespace std;

int main() {
   string s; //store the number in string format
   double n=0; //store the number in decimal formal
   int pos=0; //used to convert string to number
   stack<int> st; //stack is used to store the whole number in reverse order
   cout<<"Enter a decimal number:";
   cin>>s;
   string::iterator it = s.begin();
   //Util we find a .(end of whole number) or the end of the number, push each digit ontostack and count the number of digits.
   while(*it!='.' && it!=s.end()){
       st.push(*it-48);
       it++;
       pos=pos+1;
      
   }
   //Convert the integral part of the number to n
   for(int i=0;i<pos;i++){
       n=n+(st.top()*pow(10,i));
       st.pop();
   }
   //consider the fractional part
   pos=-1;
   if(it!=s.end()){
       it++;//needed to shift iterator for .
   }
   //Convert the fractional part to n
   while(it!=s.end()){
       n=n+((*it-48)*pow(10,pos));
       pos=pos-1;
       it++;      
   }
   //Display the decimal number
   cout<<n;
}

The screenshot of the output is given below:

NB: Hope it helps. Let me know any concern. Please provide feedback to help you better.

Add a comment
Know the answer?
Add Answer to:
c++ the program must use a stack to convert the decimal number to the numeric format....
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
  • Write a program in C++ that reads a string consisting of a positive integer or a...

    Write a program in C++ 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. DO NOT USE STACK LIBRARY OR STACK FUNCTIONS OR A STRING LIBRARY. For this assignment you have to use STACK with all methods to convert the decimal number to numeric...

  • C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...

    C++ Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering system we use is called the decimal system, or base 10 system. The numbering system that the computer uses is called the binary system, or base 2 system. The purpose of this exercise is to write a function to convert a string representing a binary number from base 2 to base 10. To convert a...

  • in c++ In this question you have to write a C++ program to convert a date...

    in c++ In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main()function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the string that the...

  • Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number....

    Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number. Specific requirements: Use InputBox function to acquire the input for an arbitrary binary integer number; Convert the binary integer to its equivalent decimal number; Return the decimal number in a message box. Submit your (VBA code) Excel screen shoot. Below functions may be useful in your VBA code: Val( ): can convert a string-type number in ( ) to its numeric value; Len( ):...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • Write one statement to convert a numeric string s into a number and store the number...

    Write one statement to convert a numeric string s into a number and store the number into double variable i. Question 31 (2 points) Given a String variable address, write a String expression consisting of variable's String value concatenated the string "@google.com". So, if the variable refers to "yuan", the value of the expression would be "[email protected]" Question 32 (2 points) The expression "Test" + 1 + 2 evaluates to?

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of amain () function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • Write a program that receives a real number in decimal (base 10) and converts it into...

    Write a program that receives a real number in decimal (base 10) and converts it into binary (base 2).•You may not use libraries or built-in functions (e.g., Double.toHexString(...) in Java or ”{0:b}”.format(...)in Python) please in python and example 0.5 convert to 0.1

  • Write a program that receives a real number in decimal (base 10) and converts it into...

    Write a program that receives a real number in decimal (base 10) and converts it into any base (e.g., 2, 8, 16, 60) You may not use libraries or built-in functions (e.g., Double.toHexString(...) in Java or ”{0:b}”.format(...)in Python) please in python exampe 0.5 convert to 0.1

  • Write a C++ program that defines a function that converts a binary number string to an...

    Write a C++ program that defines a function that converts a binary number string to an integer. However, if the binary number is greater than or equal to 4,294,967,296, the function must return 0.

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