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]
4 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:
jeka944 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
Louis has entered a column of expense values. What feature can he use to insert the total expenses at the end of the column?
vovangra [49]
I think it’s B tho I’m not really sure
5 0
3 years ago
Suppose you have the following declaration.int[] beta = new int[50];Which of the following is a valid element of beta.(i) beta[0
kompoz [17]

Answer:

The answer is "Option (i)".

Explanation:

  • In the given question, an array is defined. It is a collection of the same type of data element, which means, array stores either a numeric value or string value at a time.  
  • An array beta is defined, which contains 50 elements. The array elements indexing always starts with 0 which means, the first element of the array will be stored in an index value that is 0. That's why option (i) is correct.
8 0
3 years ago
What are HITs????????
nasty-shy [4]

It is namely an internet made by someone...

8 0
3 years ago
What should you do first to best use your personal goals as a means for a promotion
Fofino [41]

Answer:

be positive

Explanation:

6 0
3 years ago
A dmz is a subnet of _____________ accessible servers placed outside the internal network.
Alborosie

A dmz is a subnet of publicly accessible servers placed outside the internal network. Any service provided to users on the public internet should be placed in the DMZ network. DMZ means demilitarized zone (perimeter network) .

7 0
3 years ago
Other questions:
  • Write a function: function solution(N); that, given a positive integer N, prints the consecutive numbers from 1 to N, each on a
    14·1 answer
  • You have just finished writing a lengthy research paper and you are ready for formatting. You insert your paragraph headings and
    15·1 answer
  • You asked your colleague to provide feedback on a blog post you recently wrote. When they sent you their feedback, they made edi
    10·1 answer
  • Mechanisms that combine memory, processing speed, and knowledge to regulate the analysis and flow of information within the info
    5·1 answer
  • The best way to achieve stronger encryption is to
    13·1 answer
  • Who is hurt by piracy
    10·1 answer
  • Propose a data structure that supports the stack push and pop operations and a third operation findMin, which returns the smalle
    12·1 answer
  • Explain the main components of a computer system​
    12·1 answer
  • Write an application that inputs a five digit integer (The number must be entered only as ONE input) and separates the number in
    10·1 answer
  • What is a common indicator of a phishing attempt cyber awareness 2022
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!