Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. /*
  8.  *Algoritm de transformare a unui şir de caractere în număr real.
  9.  */
  10.  
  11. namespace P26
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("Introduceti un numar care va fi salvat sub forma de STRING si va fi convertit in DOUBLE !");
  18.             Console.WriteLine();
  19.  
  20.             Console.Write("string : ");
  21.             string numString = Console.ReadLine();
  22.             Console.WriteLine();
  23.  
  24.             string noContain = " qwertyuiopasdfghjklzxcvbnm !@#$%^&*()_+[]{}|:;'<>?,/'= ";
  25.  
  26.             int j = howMany(numString, '.');
  27.  
  28.             while ((isContained(numString, noContain)) || (howMany(numString , '.') > 1))
  29.             {
  30.                 Console.WriteLine("Invalid !!! Reintroduceti : ");
  31.                 Console.WriteLine();
  32.                 Console.Write("string : ");
  33.                 numString = Console.ReadLine();
  34.                 Console.WriteLine();
  35.             }
  36.  
  37.  
  38.             double num = convertToDouble(numString);
  39.  
  40.             Console.WriteLine("DOUBLE-ul este : " + num);
  41.             Console.WriteLine();
  42.  
  43.  
  44.         }
  45.  
  46.         private static double convertToDouble(string numString)
  47.         {
  48.             double total, totalINT = 0, totalFRA = 0;
  49.  
  50.             int start = 0;
  51.  
  52.             if(isContained(numString, "-"))
  53.             {
  54.                 start = 1;
  55.             }
  56.  
  57.             string[] parts = numString.Split('.');
  58.  
  59.             for(int i = start; i < parts[0].Length; i++)
  60.             {
  61.                 totalINT = totalINT * 10 + (parts[0][i] - '0');
  62.             }
  63.  
  64.             for(int i = 0; i < parts[1].Length; i++)
  65.             {
  66.                 totalFRA = totalFRA * 10 + (parts[1][i] - '0');
  67.             }
  68.  
  69.             totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
  70.  
  71.             total = totalINT + totalFRA;
  72.  
  73.             return total * ((start == 1) ? -1 : 1);
  74.         }
  75.  
  76.         private static int howMany(string numString, char s)
  77.         {
  78.             int i = 0;
  79.  
  80.             foreach(char c in numString)
  81.             {
  82.                 if(c == s)
  83.                 {
  84.                     i++;
  85.                 }
  86.             }
  87.  
  88.             return i;
  89.         }
  90.  
  91.         private static bool isContained(string numString, string noContain)
  92.         {
  93.             foreach (char c in noContain)
  94.             {
  95.                 if (numString.Contains(c))
  96.                 {
  97.                     return true;
  98.                 }
  99.             }
  100.  
  101.             return false;
  102.  
  103.         }
  104.  
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement