Here are some thoughts about your code:
Don't use using namespace std
. This is considered bad practice and you can find many reasons on the internet why this is the case (for example here). You can, for example write std::cout
instead.
You should use variable-names that tell the person who reads the code the purpose of the variable.
To improve the legibility of your code, you really should use indentation.
To further improve legibility, you can leave spaces between operators.
I made your code a bit shorter by avoiding redundant code. You could even make it a lot shorter still, but I wanted to maintain your code structure.
Finally, you should have a look at edge cases: Empty strings, strings that only contain "." and strings that don't have positions after/before the decimal point (for example 101 = 5 or .101 = 0.625).
The result looks like this:
#include <iostream>
#include <cmath>
#include <cstring>
double abc(char* b);
int main() {
std::cout << abc(".101") << "\n";
return 0;
}
double abc(char* b){
if(strcmp("", b) == 0 || strcmp(".", b) == 0) { //Empty String or only "."
return -1;
}
if(b[0] == '.') { //No positions before the decimal point
int length = std::strlen(b);
char* newB = new char[length + 1];
newB[0] = '0';
for(int k = 0; k < length; k++) {
newB[k + 1] = b[k];
}
b = newB;
}
int countDots = 0;
int index = 0;
int dotIndex = -1;
do{
if ((b[index] == '1') || (b[index] == '0') || (b[index] == '.')){
index++;
if(b[index] == '.'){
countDots++;
dotIndex = index;
}
if(countDots == 2){
return -1;
}
}
else {
return -1;
}
} while(b[index] != '\0');
if(dotIndex == -1) { //No dot found, but not empty string, so you have a natural number
int length = std::strlen(b);
char* newB = new char[length + 2];
for(int k = 0; k < length; k++) {
newB[k] = b[k];
}
newB[length] = '.';
newB[length + 1] = '0';
dotIndex = index;
b = newB;
}
double sum = 0;
index = 0;
int exponent = dotIndex;
while(exponent != 0) {
if(b[index] == '1'){
sum = sum + pow (2, exponent-1);
}
index++;
exponent--;
}
while(b[index] != '\0'){
if(b[index] == '1'){
sum = sum + pow (2, exponent);
}
exponent = exponent - 1;
index++;
}
return sum;
}
I will leave it to you to comment the code properly.
Please note: The code now is a bit longer than yours, but it is able to handle more things.
EDIT:
After one of the comments below, I decided to also show a version without C-style arrays, and without new
. This also should solve the problem with possible memory leaks. So all in all, this is the better version (I will leave the old version, because the questioner asked for a function double abc(char* b)
):
#include <iostream>
#include <cmath>
#include <string>
double abc(std::string b);
int main() {
std::cout << abc("1101.101") << "\n";
return 0;
}
double abc(std::string b){
if(b.compare("") == 0 || b.compare(".") == 0) { //Empty String or only "."
return -1;
}
if(b[0] == '.') { //No positions before the decimal point
b.insert(0, "0");
}
int countDots = 0;
size_t index = 0;
int dotIndex = -1;
do{
if ((b[index] == '1') || (b[index] == '0') || (b[index] == '.')){
index++;
if(b[index] == '.'){
countDots++;
dotIndex = index;
}
if(countDots == 2){
return -1;
}
}
else {
return -1;
}
} while(index < b.length());
if(dotIndex == -1) { //No dot found, but not empty string, so you have a natural number
b.push_back('.');
b.push_back('0');
dotIndex = index;
}
double sum = 0;
index = 0;
int exponent = dotIndex;
while(exponent != 0) {
if(b[index] == '1'){
sum = sum + pow (2, exponent-1);
}
index++;
exponent--;
}
while(index < b.length()){
if(b[index] == '1'){
sum = sum + pow (2, exponent);
}
exponent = exponent - 1;
index++;
}
return sum;
}