1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Shalnov [3]
3 years ago
13

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.
Computers and Technology
1 answer:
jeka943 years ago
5 0

Answer:

#include <iostream>

#include <string>

#include <stack>

#include <math.h>

using namespace std;

int main() {

  string s;

  double n=0;

  int position=0;  

  stack<int> wholeNumbers;

  cout<<"Enter a decimal number:";

  cin>>s;

  string::iterator counter = s.begin();

  while(*counter!='.' && counter!=s.end()){

      wholeNumbers.push(*counter-48);

      counter++;

      position=position+1;

     

  }

  for(int i=0;i<position;i++){

      n=n+(wholeNumbers.top()*pow(10,i));

      wholeNumbers.pop();

  }

  position=-1;

  if(counter!=s.end()){

      counter++;

  }

  while(counter!=s.end()){

      n=n+((*counter-48)*pow(10,position));

      position=position-1;

      counter++;      

  }

  cout<<n;

}

Explanation:

  • Inside the while loop, push the push a number to the wholeNumbers stack by subtracting it with 48.
  • Increment the counter and position variable by 1 inside the while loop.
  • Count the number of digit, push each digit to top of stack and find the end of the number,
  • Run a for loop up to the value of position variable and pop a value from the wholeNumbers stack.
You might be interested in
Is regular Facebook use healthy? Why or why not?
melamori03 [73]
No. social media makes people share too much information about their personal lives.
7 0
3 years ago
Read 2 more answers
Which of the following is a good way to improve your credit score?
Ganezh [65]
The answer is chope this helps
6 0
2 years ago
Smallest kind of computer
jeka57 [31]
I don’t know if i’m right but like Michigan Micro Mote ?
3 0
1 year ago
Read 2 more answers
An electronic cover letter should be
Lunna [17]
A cover letter is a document sent with your resume to provide additional details about you. The cover letter is what introduces your resume and you. 
<span>A few short tips for writing cover letters. i dont know if thats the answer you looking for</span>
3 0
2 years ago
Read 2 more answers
Would an artificial intelligence come into existence anytime in the next 30 years?
Scrat [10]
Artificial Intelligence, based on neural networks, is available now.
4 0
3 years ago
Other questions:
  • Credibility means that the reader perceives value in what you write.<br> True False
    12·1 answer
  • Which information is considered free for use?
    9·2 answers
  • Who needs to be involved when assembling business solutions that include hardware and software?
    11·2 answers
  • In a bubble sort, you use a(n) ____ loop to make pair comparisons.
    5·1 answer
  • Servlet session and JSP session have differentabilities.<br>? True<br><br>? False
    15·1 answer
  • Fill in the blanks : To store 3 character a computer occupies...................bytes memory space​
    8·2 answers
  • You manage a network that uses switches. In the lobby of your building, there are three RJ45 ports connected to a switch. You wa
    5·1 answer
  • Which tool can be used to increase the space between a bullet point or a number and text?
    11·2 answers
  • 2.3 Code Practice: Question 1
    5·1 answer
  • PLEASE HELP! (NO LINKS)
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!