How to get a float from a char* in C++?
I am relatively new to C++, coming from the happy-go-lucky world of Python.
I decided to write a set of functions to find the mean, median, mode, and range of a set of numbers. To aid in the calculation of the median, I decided to make l2g (least to greatest) and g2l (greatest to least). I understand I will only be using one of these, but want to make both just for the sake of completeness.
I want my program to rely on as few external (even standard) libraries as possible. The reason for this is that I will most likely be using any functions that I write again. They will be used in my OS that I am developing based off of the tutorials at BrokenThorn. Therefore I want to keep the code as independent as possible, using functions that I write instead of functions in the standard libraries.
I have two problems:
- The first is that I want my functions to be able to handle float values. With the user input function that I created earlier, I get a pointer to a char array. I need to know how to convert that into a float value. I was thinking that maybe I could create a union with two members, of char and float types, and just reference the float member when I need to use the value. I would just put user data into the char member. However, I don't know if this approach would work. The tutorial that I use doesn't go very in-depth about defining my own data types.
- My second dilemma is that I do not know how to create the l2g and g2l functions. I was planning on nesting two for loops. I was going to use the inside one to find the next number in the new array, and the outside one to repeat this process until it gets through all the numbers in the original array. Then the functi开发者_如何学Con would return a pointer to the new array of floats, after deleting the first array's dynamic memory.
Another issue is that I think I need to return the length of an array when I return a pointer to it. The reason for this is that to use a for loop to go through the contents of that array, I need to have the highest position usable in the array, so I don't get garbage. I have a way to avoid this problem by making a struct that has two members, length and the array. It looks like this:
struct arrayLength {
int l;
float * p;
} ;
I was hoping that the following set of calls:
float * myArrayPointer; // I will change this to user input once I get the rest of the program working
myArrayPointer = new float[5];
myArrayPointer[0] = 65.97;
myArrayPointer[1] = 21.06;
myArrayPointer[2] = 21.06;
myArrayPointer[3] = 509.69;
myArrayPointer[4] = -41.73; // Can floats be negative?
cout << mean(myArrayPointer, 5); // Give function mean the pointer to array and number of elements
cout << "\n";
cout << median(myArrayPointer, 5);
cout << "\n";
cout << range(myArrayPointer, 5);
cout << "\n";
cout << mode(myArrayPointer, 5);
cout << "\n\n\n";
arrayLength myArrayPointerLG = l2g(myArrayPointer, 5);
float * myArrayLGitem;
for(int i=0;i<myArrayPointerLG.l;i++) {
myArrayLGitem = myArrayPointerLG.p[i];
cout << myArrayPointerLGitem << "\n";
}
Would give the following output:
115.21
21.06
551.42
21.06
-41.73
21.06
21.06
65.97
509.69
So far, I haven't gotten my code compiled. :/ I think maybe a for loop has its own scope... Could someone verify this as well? Here's a link to the code on pastebin:
Pastebin
And in case you were wondering about the <iostream>
inclusion... I will remove this and use the printing functions I wrote if I end up adding it to my OS. OR I might just define std::cout. The errors that I'm getting are saying that currentPosition and listOfNumbers are undeclared (not defined) in l2g. I am running Windows 7 64-bit, using the MSVC++ 2008 Express Edition compiler. Thank you in advance to anyone who helps!
a) learn to use STL containers, algrithms etc they will make life much easier for you
b) c func atof will do what the question title asks
If you lift your restriction on using libraries, both tasks are very simple.
Converting char* to float is easy with Boost.lexical_cast (See http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm). It provides type-safe conversions between any two types, so long as they define the "stream" operators (<< and >>).
#include "boost/lexical_cast.hpp"
...
const char* string_value = "1.5";
float float_value = boost::lexical_cast<float>(string_value);
The standard library's sort method is the simplest way to order your array. Using your example above:
float * myArrayPointer;
myArrayPointer = new float[5];
myArrayPointer[0] = 65.97;
myArrayPointer[1] = 21.06;
myArrayPointer[2] = 21.06;
myArrayPointer[3] = 509.69;
myArrayPointer[4] = -41.73; // Can floats be negative?
std::sort(myArrayPointer, myArrayPointer+5); //Sort lowest to highest
std::sort(myArrayPointer, myArrayPointer+5, std::greater<float>()); //Sort highest to lowest
If you insist on doing it without any library help, you'll have to implement a sorting algorithm for yourself. There are lots to pick from, each giving you different performance characteristics. Pick the one that best matches your use-case. See http://en.wikipedia.org/wiki/Sorting_algorithm for lots of options. Quicksort and Merge-sort are fairly commonly seen ones that give pretty good performance.
As for implementing char* to float conversions manually, you will essentially be implementing C's atof yourself. To do this, you'll need to loop over each character in the string, convert it to its corresponding integral value, and accumulate them into a float. You'd then use the location of the decimal point to determine how to scale all of the values. For example, to convert the string "12.5" to a float, you'd have to convert each character ('1', '2', and '5') to their respective integers, and scale them to get 1*10^1 + 2*10^0 + 5*10^-1. There may be more efficient ways to do it, but that's about as simple as I can make it.
精彩评论