Please help me write this code Write a C++ program that converts a string to a f
ID: 3555752 • Letter: P
Question
Please help me write this code
Write a C++ program that converts a string to a float value. For example, given the string "12.34" the program should output a float value 12.3400. Don't use the string stream class or any predefined functions that can do this conversion for you. However, you should write your own code to do the conversion for at least 4 digits after the decimal point. Sample input/output is shown below and your code should work for any input given by the user. Input: 19.3473 Output: 19.3473 Define a class named intmod that stores an integer and has functions to get and set the integer's value. Then, overload the [ ] operator so that the index returns the digit in position /, where / = 0 is the most-significant digit. If no such digit exists then -1 should be returned. Sample input/output is shown below and your code should work for any input. Input: 547 Output: 0 5 1 4 2 7Explanation / Answer
Program 1
#include <iostream>
using namespace std;
float strToFloat(char* num) {
if (!num || !*num)
return 0;
float integerPart = 0;
float fractionPart = 0;
int divisorForFraction = 1;
int sign = 1;
bool inFraction = false;
/*Take care of +/- sign*/
if (*num == '-') {
++num;
sign = -1;
} else if (*num == '+') {
++num;
}
while (*num != '') {
if (*num >= '0' && *num <= '9') {
if (inFraction) {
/*See how are we converting a character to integer*/
fractionPart = fractionPart*10 + (*num - '0');
divisorForFraction *= 10;
} else {
integerPart = integerPart*10 + (*num - '0');
}
} else if (*num == '.') {
if (inFraction)
return sign * (integerPart + fractionPart/divisorForFraction);
else
inFraction = true;
} else {
return sign * (integerPart + fractionPart/divisorForFraction);
}
++num;
}
return sign * (integerPart + fractionPart/divisorForFraction);
}
int main() {
float x = strToFloat("-0.3");
cout << x;
}
Program 2
#include<iostream>
#include <string>
using namespace std;
class intmod {
private:
int integer[11];
int sign;
public:
intmod() {
sign = 1;
for(int i = 0; i < 10; i++) {
integer[i] = -1;
}
};
void setInt(int n) {
if(n < 0) {
sign = -1;
n = -1*n;
} else {
sign = 1;
}
int temp[10];
for(int i = 0; i < 10; i++) {
temp[i] = -1;
}
int i = 0;
while(n > 0) {
temp[i] = n%10;
n /= 10;
i++;
}
i = 0;
while(temp[i] != -1) {i++;};
for(int j = 0; j < i; j++) {
integer[j] = temp[i-j-1];
}
};
int getInt() {
int num = 0;
if(integer[0] == -1) {
cout << "Integer not set";
return -1;
}
int i = 0;
while(integer[i] != -1 && i < 10) {
num = num*10 + integer[i];
i++;
}
return num*sign;
}
int &operator[](int i) {
if(i < 10) {
return integer[i];
} else {
return integer[10]; // integer[10] = -1
}
}
};
main() {
intmod intm;
int n;
cin >> n;
intm.setInt(n);
int ret = intm.getInt();
cout << "The number stored is " << ret << endl;
int m = n;
for(int i = 0; m != 0; i++) {
cout << i << " " << intm[i] << endl;
m = m/10;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.