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# 3.76 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 = convertToInt(numString);
  39.  
  40.             Console.WriteLine("DOUBLE-ul este : " + num);
  41.             Console.WriteLine();
  42.  
  43.  
  44.         }
  45.  
  46.         private static int howMany(string numString, char s)
  47.         {
  48.             int i = 0;
  49.  
  50.             foreach(char c in numString)
  51.             {
  52.                 if(c == s)
  53.                 {
  54.                     i++;
  55.                 }
  56.             }
  57.  
  58.             return i;
  59.         }
  60.  
  61.         private static bool isContained(string numString, string noContain)
  62.         {
  63.             foreach (char c in noContain)
  64.             {
  65.                 if (numString.Contains(c))
  66.                 {
  67.                     return true;
  68.                 }
  69.             }
  70.  
  71.             return false;
  72.  
  73.         }
  74.  
  75.         private static double convertToInt(string s)
  76.         {
  77.             double total = 0, totalINT = 0, totalFRA = 0;
  78.  
  79.             if(isContained(s , "."))
  80.             {
  81.                 if (isContained(s, "-"))
  82.                 {
  83.  
  84.                     String[] parts = s.Split('.');
  85.  
  86.                     for (int i = 1; i < parts[0].Length; i++)
  87.                     {
  88.                         totalINT = totalINT * 10 + (parts[0][i] - '0');
  89.                     }
  90.  
  91.                     for (int i = 0; i < parts[1].Length; i++)
  92.                     {
  93.                         totalFRA = totalFRA * 10 + (parts[1][i] - '0');
  94.                     }
  95.  
  96.                     totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
  97.  
  98.                     total = totalINT + totalFRA;
  99.  
  100.                     return -total;
  101.  
  102.                 }
  103.                 else
  104.                 {
  105.  
  106.                     String[] parts = s.Split('.');
  107.  
  108.                     for(int i = 0; i < parts[0].Length; i++)
  109.                     {
  110.                         totalINT = totalINT * 10 + (parts[0][i] - '0');
  111.                     }
  112.  
  113.                     for(int i = 0; i < parts[1].Length; i++)
  114.                     {
  115.                         totalFRA = totalFRA * 10 + (parts[1][i] - '0');
  116.                     }
  117.  
  118.                     totalFRA = totalFRA / Math.Pow(10, parts[1].Length);
  119.  
  120.                     total = totalINT + totalFRA;
  121.  
  122.                     return total;
  123.  
  124.                 }
  125.             }
  126.             else
  127.             {
  128.                 if(isContained(s, "-"))
  129.                 {
  130.                     for(int i = 1; i < s.Length; i++)
  131.                     {
  132.                         total = total * 10 + (s[i] - '0');
  133.                     }
  134.  
  135.                     return -total;
  136.                 }
  137.                 else
  138.                 {
  139.                     for (int i = 0; i < s.Length; i++)
  140.                     {
  141.                         total = total * 10 + (s[i] - '0');
  142.                     }
  143.  
  144.                     return total;
  145.                 }
  146.             }
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement