using System;
using System.Collections.Generic;
class StrToIntClass
{
// Function to convert string to integer without using functions
static int StrToInt(string s)
{
Dictionary<char, int> chrToInt = new Dictionary<char, int>(){
{'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 = "";
foreach (char ch in s)
{
if (ch == '-' && i == 0)
{
minus = 1;
}
else if (ch == '+' && i == 0)
{
i += 1;
}
else if (ch == '.')
{
if (ifFloat == 1)
{
throw new Exception("Value Error");
}
ifFloat = 1;
}
else if (chrToInt.ContainsKey(ch))
{
if (ifFloat == 0)
{
res += ch;
}
else
{
throw new Exception("Value Error");
}
}
else
{
throw new Exception("Value Error");
}
i += 1;
}
int num = 0;
foreach (char c in res)
{
num = num * 10 + chrToInt[c];
}
if (minus == 1)
{
num *= -1;
}
return num;
}
// Driver code
static void Main(string[] args)
{
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"};
foreach (string s in inputArr)
{
try
{
Console.WriteLine(StrToInt(s));
}
catch (Exception e)
{
Console.WriteLine("Value Error");
}
}
}
}