1 answer

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

Question:

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


Answers

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.

.

Similar Solved Questions

1 answer
14. Consider a datagram network using 8-bit host addresses. Suppose a router use longest prefix matching...
14. Consider a datagram network using 8-bit host addresses. Suppose a router use longest prefix matching and has the following forwarding table Interface Prefix Match 01 0 10 001 2 For each of the four interfaces, give the associated range of destination host addresses and the number of addresses in...
1 answer
Consider the circuit shown in the figure below. Calculate the voltage across the capacitor Co assuming...
Consider the circuit shown in the figure below. Calculate the voltage across the capacitor Co assuming the following potential source and capacitor values: 12.0 V Cr = 1.56 μF , C5 = 4.57 μF , C6 = 10.0 μF C3 Vo C4 С5 Cs...
1 answer
The following is a December 31, 2021, post-closing trial balance for Almway Corporation. Account Title Cash...
The following is a December 31, 2021, post-closing trial balance for Almway Corporation. Account Title Cash Investment in equity securities Accounts receivable Inventory Prepaid insurance (for the next 9 months) Land Buildings Accumulated depreciation-buildings Equipment Accumulated depreciation-equ...
1 answer
How do you write 103,727,495 in word form?
How do you write 103,727,495 in word form?...
1 answer
1 Official GDP may understate the actual output of an economy. Give one possible reason to...
1 Official GDP may understate the actual output of an economy. Give one possible reason to explain this 2. The following graphs show the loanable funds market. Give an example scenario, which is represented by a shift from line 3 to line 2. Line 1 Line 4 Vertical Axis Υ Line 3 Une 2 С ...
1 answer
Draw the magnetic field change curve (hysteresis curve) of the magnetization of a ferromagnetic material. Demonstrate pe...
Draw the magnetic field change curve (hysteresis curve) of the magnetization of a ferromagnetic material. Demonstrate permanent magnetization (MR), saturation magnetization (MS) and coercive field values (HC) on the curve. Briefly explain what these terms mean. a) Briefly describe the Pauli exclusio...
1 answer
HELP WITH CONCEPT MAP Need Help making nursing diagnosis for this patient PN 200 Fundamentals of...
HELP WITH CONCEPT MAP Need Help making nursing diagnosis for this patient PN 200 Fundamentals of Nursing II Concept Map - Hypokalemia 7.30AM Mr. Sam Downs is admitted to Sam Downs is admitted to your it from the screency room. He is male Weicht upon admission 156: HS11 197) R22 and shallow, P...
1 answer
THIS ASSIGNMENT CONTRIBUTES TO YOUR COURSE MARK A concrete dam of mass density 2400 kg/m3 with...
THIS ASSIGNMENT CONTRIBUTES TO YOUR COURSE MARK A concrete dam of mass density 2400 kg/m3 with dimensions a 10 m b = 7 m and c water is 1,000 kg/m3 and the dam is very long out of plane, determine: 7 m is subject to a hydrostatic fluid pressure on its right hand face. If the density of The horizonta...
1 answer
The change in entropy of a mass m of a solid substance which has a latent...
The change in entropy of a mass m of a solid substance which has a latent heat of fusion L and melts at a temperature T is a.LT/m b.mLln(T) c.mLT d.mL/T e.L/mT...
1 answer
What is the Principle of Equal Marginal Sacrifice according to HL Ahuja in economics?
What is the Principle of Equal Marginal Sacrifice according to HL Ahuja in economics?...
1 answer
3. Read the paragraph below about Dengue Fever and answer the following questions Dengue is an...
3. Read the paragraph below about Dengue Fever and answer the following questions Dengue is an acute infectious disease with clinical symptoms of high fever, severe headache, backache, joint pains, nausea and vomiting, eye pain, and rash. It is caused by any one of four closely related flavi-viruses...
1 answer
The following data have been extracted from the records of Puzzle Inc.: Production level, in units...
The following data have been extracted from the records of Puzzle Inc.: Production level, in units Variable costs Fixed costs Mixed costs Total costs February 11,000 $29,700 > 24,100 $90,400 August 25, 300 s > 36,600 $154,750 Required: a. Calculate the missing costs. b. Calculate the cost form...
1 answer
Minden Company is a wholesale distributor of premium European chocolates. The company's balance sheet as of...
Minden Company is a wholesale distributor of premium European chocolates. The company's balance sheet as of April 30 is given below: Minden Company Balance Sheet April 30 Assets Cash Accounts receivable Inventory Buildings and equipment, net of depreciation Total assets Liabilities and Stockhold...