Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 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 = 0, totalINT = 0, totalFRA = 0;
  49.  
  50.             int start = 0;
  51.  
  52.             if(isContained(numString, "-"))
  53.             {
  54.                 start = 1;
  55.             }
  56.  
  57.             if(isContained(numString, "."))
  58.             {
  59.                 string[] parts = numString.Split('.');
  60.  
  61.                 for (int i = start; i < parts[0].Length; i++)
  62.                 {
  63.                     totalINT = totalINT * 10 + (parts[0][i] - '0');
  64.                 }
  65.  
  66.                 for (int i = 0; i < parts[1].Length; i++)
  67.                 {
  68.                     totalFRA = totalFRA * 10 + (parts[1][i] - '0');
  69.                 }
  70.  
  71.                 totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
  72.  
  73.                 total = totalINT + totalFRA;
  74.  
  75.                 return total * ((start == 1) ? -1 : 1);
  76.             }
  77.             else
  78.             {
  79.                 for(int i = start; i < numString.Length; i++)
  80.                 {
  81.                     total = total * 10 + (numString[i] - '0');
  82.                 }
  83.  
  84.                 return total * ((start == 1) ? -1 : 1);
  85.  
  86.             }
  87.         }
  88.  
  89.         private static int howMany(string numString, char s)
  90.         {
  91.             int i = 0;
  92.  
  93.             foreach(char c in numString)
  94.             {
  95.                 if(c == s)
  96.                 {
  97.                     i++;
  98.                 }
  99.             }
  100.  
  101.             return i;
  102.         }
  103.  
  104.         private static bool isContained(string numString, string noContain)
  105.         {
  106.             foreach (char c in noContain)
  107.             {
  108.                 if (numString.Contains(c))
  109.                 {
  110.                     return true;
  111.                 }
  112.             }
  113.  
  114.             return false;
  115.  
  116.         }
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement