Commit f7e2c900 authored by Johnny Jazeix's avatar Johnny Jazeix

Correction flottants

parent 7662870d
......@@ -13,24 +13,26 @@ Color::Color(double r, double g, double b) {
_b = b;
}
bool Color::instantiate(std::string &in, Color &out) {
bool Color::instantiate(const std::string &in, Color &out) {
double r, g, b;
if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) == 3){
out = Color(r, g, b);
return true;
}
// The error could occur because this is not the good format for decimal.
// Dots instead of commas or the contrary
bool commas_first = false;
bool is_english_system_needed = false; // We have dot as separator of decimal and integer.
if(Value::replace_in_string(in, ',', '.')){
commas_first = true;
if(in.find('.') != std::string::npos){
is_english_system_needed = true;
}
if(!commas_first){ // We had dots initially, we need to change them in commas
Value::replace_in_string(in, '.', ',');
if(is_english_system_needed){ // We had dots initially, we need to have the english system
setlocale(LC_NUMERIC, "en_GB.UTF-8");
}
else{
setlocale(LC_NUMERIC, "fr_FR.UTF-8");
}
if(sscanf(in.c_str(), "%lf %lf %lf", &r, &g, &b) != 3){
......
......@@ -42,14 +42,14 @@ public:
/*!
*
* \fn instantiate(std::string &in, Color &out)
* \fn instantiate(const std::string &in, Color &out)
* \brief Convert a string to a Color
* \param in String to convert
* \param out Color to be initialized
* \return true, if the conversion succeeded
*
*/
static bool instantiate(std::string &in, Color &out);
static bool instantiate(const std::string &in, Color &out);
/*!
*
......
......@@ -8,33 +8,43 @@ Date::Date(double value){
_value = value;
}
bool Date::instantiate(std::string &in, Date &out){
bool Date::instantiate(const std::string &in, Date &out){
double value = 0.0;
int temp;
// We simule a 2-copy to have the good result with sscanf depending on the language of the computer...
std::string copy = in + " 3";
int test = sscanf(copy.c_str(), "%lf %d", &value, &temp);
if(sscanf(in.c_str(), "%lf", &value) == 1){
if(test == 2){
out = Date(value);
return true;
}
else {// The error could occur because this is not the good format for decimal.
// Dots instead of commas or the contrary
bool is_english_system_needed = false; // We have dot as separator of decimal and integer.
if(in.find('.') != std::string::npos){
is_english_system_needed = true;
}// Dots instead of commas or the contrary
if(is_english_system_needed){ // We had dots initially, we need to have the english system
setlocale(LC_NUMERIC, "en_GB.UTF-8");
}
else{
setlocale(LC_NUMERIC, "fr_FR.UTF-8");
}
// The error could occur because this is not the good format for decimal.
// Dots instead of commas or the contrary
bool commas_first = false;
if(Value::replace_in_string(in, ',', '.')){
commas_first = true;
}
if(!commas_first){ // We had dots initially, we need to change them in commas
Value::replace_in_string(in, '.', ',');
}
if(sscanf(in.c_str(), "%lf", &value) != 1){
return false;
}
else{
out = Date(value);
return true;
test = sscanf(copy.c_str(), "%lf %d", &value, &temp);
if(test == 2) {
out = Date(value);
return true;
}
else{
return false;
}
}
}
......
......@@ -44,7 +44,7 @@ public :
* \return true, if the conversion succeeded
*
*/
static bool instantiate(std::string &in, Date &out);
static bool instantiate(const std::string &in, Date &out);
/*!
*
......
......@@ -8,33 +8,42 @@ Double::Double(){
Double::Double(double value){
_value = value;
}
bool Double::instantiate(std::string &in, Double &out){
double value = 0.0;
if(sscanf(in.c_str(), "%lf", &value) == 1){
out = Double(value);
return true;
}
// The error could occur because this is not the good format for decimal.
// Dots instead of commas or the contrary
bool commas_first = false;
bool Double::instantiate(const std::string &in, Double &out){
double value = 0.0;
if(Value::replace_in_string(in, ',', '.')){
commas_first = true;
}
int temp;
// We simule a 2-copy to have the good result with sscanf depending on the language of the computer...
std::string copy = in + " 3";
int test = sscanf(copy.c_str(), "%lf %d", &value, &temp);
if(!commas_first){ // We had dots initially, we need to change them in commas
Value::replace_in_string(in, '.', ',');
}
if(sscanf(in.c_str(), "%lf", &value) != 1){
return false;
}
else{
if(test == 2){
out = Double(value);
return true;
}
else {// The error could occur because this is not the good format for decimal.
bool is_english_system_needed = false; // We have dot as separator of decimal and integer.
if(in.find('.') != std::string::npos){
is_english_system_needed = true;
}
if(is_english_system_needed){ // We had dots initially, we need to have the english system
setlocale(LC_NUMERIC, "en_GB.UTF-8");
}
else{
setlocale(LC_NUMERIC, "fr_FR.UTF-8");
}
test = sscanf(copy.c_str(), "%lf %d", &value, &temp);
if(test == 2){
out = Double(value);
return true;
}
else{
return false;
}
}
}
std::string Double::to_string() const{
......
......@@ -44,7 +44,7 @@ public:
* \return true, if the conversion succeeded
*
*/
static bool instantiate(std::string &in, Double &out);
static bool instantiate(const std::string &in, Double &out);
/*!
*
......
#include "Value.hpp"
bool Value::replace_in_string(std::string &characters, char to_replace, char replace_by){
bool has_change = false;
const int lenght = characters.size();
for(int i = 0 ; i < lenght ; i ++){
if(characters[i] == to_replace){
characters[i] = replace_by;
has_change = true;
}
}
return has_change;
}
......@@ -15,6 +15,11 @@
// For std::setprecision
#include <iomanip>
#include<iostream>
// For dots or commas separator in double numbers
#include <locale.h>
/*!
*
......@@ -38,18 +43,6 @@ public:
static const int _PRECISION = 15;
/*!
*
* \fn replace_in_string(std::string &characters, char to_replace, char replace_by)
* \brief Replace in the string the character to_replace by replace_by.
* \param characters string to be replaced.
* \param to_replace the character we want to replace.
* \param replace_by the character we use to replace the first one.
* \return true if there was a change.
*
*/
static bool replace_in_string(std::string &characters, char to_replace, char replace_by);
};
#endif // VALUE_HPP
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment