#include <iostream>
#include <unordered_map>
using
namespace
std;
int
strToInt(string s) {
unordered_map<
char
,
int
> chrToInt = {{
'1'
, 1}, {
'2'
, 2}, {
'3'
, 3}, {
'4'
, 4}, {
'5'
, 5},
{
'6'
, 6}, {
'7'
, 7}, {
'8'
, 8}, {
'9'
, 9}, {
'0'
, 0}};
int
i = 0;
int
minus = 0;
int
ifFloat = 0;
string res =
""
;
for
(
char
ch : s) {
if
(ch ==
'-'
&& i == 0) {
minus = 1;
}
else
if
(ch ==
'+'
&& i == 0) {
i += 1;
}
else
if
(ch ==
'.'
) {
if
(ifFloat == 1) {
throw
"Value Error"
;
}
ifFloat = 1;
}
else
if
(chrToInt.count(ch)) {
if
(ifFloat == 0) {
res += ch;
}
else
{
throw
"Value Error"
;
}
}
else
{
throw
"Value Error"
;
}
i += 1;
}
int
num = 0;
for
(
char
c : res) {
num = num * 10 + chrToInt;
}
if
(minus == 1) {
num *= -1;
}
return
num;
}
int
main() {
string inputArr[] = {
"34.4.5"
,
"-inf"
,
"233.a"
,
"# 567"
,
"000234"
,
".023"
,
"-23.78"
,
"36625276396422484242000000000000000"
,
"a1a1a1"
,
"10.00001"
,
""
,
"-.23"
,
"583.40000"
,
"0000"
,
"10e2 "
,
"2×4"
};
string input2[] = {
"+466"
,
"+0.23"
,
"101001"
,
"0023"
,
"4001.23"
,
"40.834"
,
"aaa"
,
"abc234"
,
"23.543abc"
,
"–0.0"
,
"-0.0"
,
"0.0"
,
"222.2"
,
"-+1323"
,
"+-23"
};
int
len1 =
sizeof
(inputArr) /
sizeof
(inputArr[0]);
int
len2 =
sizeof
(input2) /
sizeof
(input2[0]);
for
(
int
i = 0; i < len1; i++) {
try
{
cout << strToInt(inputArr[i]) << endl;
}
catch
(
const
char
* msg) {
cout << msg << endl;
}
}
for
(
int
i = 0; i < len2; i++) {
try
{
cout << strToInt(input2[i]) << endl;
}
catch
(
const
char
* msg) {
cout << msg << endl;
}
}
return
0;
}