Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- /*
- *Algoritm de transformare a unui şir de caractere în număr real.
- */
- namespace P26
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Introduceti un numar care va fi salvat sub forma de STRING si va fi convertit in DOUBLE !");
- Console.WriteLine();
- Console.Write("string : ");
- string numString = Console.ReadLine();
- Console.WriteLine();
- string noContain = " qwertyuiopasdfghjklzxcvbnm !@#$%^&*()_+[]{}|:;'<>?,/'= ";
- int j = howMany(numString, '.');
- while ((isContained(numString, noContain)) && (howMany(numString , '.') != 1))
- {
- Console.WriteLine("Invalid !!! Reintroduceti : ");
- Console.WriteLine();
- Console.Write("string : ");
- numString = Console.ReadLine();
- Console.WriteLine();
- }
- double num = convertToInt(numString);
- Console.WriteLine("DOUBLE-ul este : " + num);
- Console.WriteLine();
- }
- private static int howMany(string numString, char s)
- {
- int i = 0;
- foreach(char c in numString)
- {
- if(c == s)
- {
- i++;
- }
- }
- return i;
- }
- private static bool isContained(string numString, string noContain)
- {
- foreach (char c in noContain)
- {
- if (numString.Contains(c))
- {
- return true;
- }
- }
- return false;
- }
- private static double convertToInt(string s)
- {
- double total = 0, totalINT = 0, totalFRA = 0;
- if(isContained(s , "."))
- {
- if (isContained(s, "-"))
- {
- String[] parts = s.Split('.');
- for (int i = 1; i < parts[0].Length; i++)
- {
- totalINT = totalINT * 10 + (parts[0][i] - '0');
- }
- for (int i = 0; i < parts[1].Length; i++)
- {
- totalFRA = totalFRA * 10 + (parts[1][i] - '0');
- }
- totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
- total = totalINT + totalFRA;
- return -total;
- }
- else
- {
- String[] parts = s.Split('.');
- for(int i = 0; i < parts[0].Length; i++)
- {
- totalINT = totalINT * 10 + (parts[0][i] - '0');
- }
- for(int i = 0; i < parts[1].Length; i++)
- {
- totalFRA = totalFRA * 10 + (parts[1][i] - '0');
- }
- totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
- total = totalINT + totalFRA;
- return total;
- }
- }
- else
- {
- if(isContained(s, "-"))
- {
- for(int i = 1; i < s.Length; i++)
- {
- total = total * 10 + (s[i] - '0');
- }
- return -total;
- }
- else
- {
- for (int i = 0; i < s.Length; i++)
- {
- total = total * 10 + (s[i] - '0');
- }
- return total;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement