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 = convertToDouble(numString);
- Console.WriteLine("DOUBLE-ul este : " + num);
- Console.WriteLine();
- }
- private static double convertToDouble(string numString)
- {
- double total = 0, totalINT = 0, totalFRA = 0;
- int start = 0;
- if(isContained(numString, "-"))
- {
- start = 1;
- }
- if(isContained(numString, "."))
- {
- string[] parts = numString.Split('.');
- for (int i = start; 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 * ((start == 1) ? -1 : 1);
- }
- else
- {
- for(int i = start; i < numString.Length; i++)
- {
- total = total * 10 + (numString[i] - '0');
- }
- return total * ((start == 1) ? -1 : 1);
- }
- }
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement