This repository has been archived on 2024-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
Schule/Nebenfunktionen/Kommazahl.cpp

51 lines
1 KiB
C++
Raw Permalink Normal View History

2017-04-10 21:17:40 +02:00
#include<iostream>
2017-04-24 15:36:44 +02:00
long double stringToFloat(const std::string& strng, bool decimalPoint)
2017-04-10 21:17:40 +02:00
{
2017-04-24 15:36:44 +02:00
/*Alternative mit stold welche in der Deklaration der Nebenfunktion zus<75>tzlich einen Boolean "Komma" ben<65>tigt,
welcher angibt ob es mt oder ohne Kommas arbeiten soll
2017-04-14 16:11:38 +02:00
2017-04-24 15:36:44 +02:00
long double number;
bool minus = false;
2017-04-10 21:17:40 +02:00
std::string::size_type idx;
std::string FNum = "";
for(char c:str)
{
if(c>='0' && c<='9') FNum += c;
2017-04-24 15:36:44 +02:00
else if(c == '-') minus = true;
else if((c=='.' || c==',') && decimalPoint) decimalPoint = false, FNum += c;
2017-04-10 21:17:40 +02:00
}
2017-04-24 15:36:44 +02:00
number = std::stold(FNum, &idx);
2017-04-10 21:17:40 +02:00
2017-04-24 15:36:44 +02:00
if(minus) Zahl*=-1;
return(number);*/
bool DP = false;
long double number;
unsigned short place = 0;
for(char c : strng)
2017-04-10 21:17:40 +02:00
{
if(c>='0' && c<='9')
{
2017-04-24 15:36:44 +02:00
c = c-'0';
if(!DP) number = number*10+c;
2017-04-10 21:17:40 +02:00
else
{
2017-04-24 15:36:44 +02:00
long double number2 = c;
for(unsigned short loop = 0; loop<=place; loop++) number2 /= 10;
number += number2, place++;
2017-04-10 21:17:40 +02:00
}
}
2017-04-24 15:36:44 +02:00
else if((c=='.' || c==',') && decimalPoint) DP=true;
2017-04-10 21:17:40 +02:00
}
2017-04-24 15:36:44 +02:00
return(number);
2017-04-10 21:17:40 +02:00
}