Question

In: Computer Science

In c++ Write a program that reads a string consisting of a positive integer or a...

In c++

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.

Use the STL stack

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

C++ CODE :-

#include<bits/stdc++.h>
using namespace std;

int main(){
string str; cin>>str; // get the no in string format
int n = str.size();
bool isdecimal = false;
for(int i = 0; i < n; i++) // check if is decimal or not
if(str[i] == '.')
isdecimal = true;
double no = 0;
if(isdecimal){ // if the no if decimal
stack<int> stk;
double f = 0;
for(int i = 0 ; i < n; i++) // push all letter of string into stack
stk.push(str[i]);
while(!stk.empty()){ // this will make the fractional part
char c = stk.top(); // pop top
stk.pop();
if(c =='.') break; // if decimal if found stop
f = f + (c-'0'); // add the top and divide it by 10
f /= 10;
}
int t = 0;
while(!stk.empty()){ // this will make the non fractional part of the no
char c = stk.top();
stk.pop();
no = (c-'0')*pow(10,t)+no;
t++;
}
no = no+f; // add fractional and non fractional part to get the no
}
else
for(int i = 0; i < n; i++) // if the no is not fractional
no = no*10 + (str[i]-'0');

cout<<no;
}

Sample Run :


Related Solutions

In c++, write a program that reads a string consisting of a positive integer or a...
In c++, 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.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
(Please write in C++) Write a program that reads in a line consisting of a student’s...
(Please write in C++) Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ]...
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
who to write a program c++ that reads in an integer between 0 and 1000 and...
who to write a program c++ that reads in an integer between 0 and 1000 and adds all the digits in the integer?
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT