Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MyClassLibrary
  4. {
  5.     public class Fraction
  6.     {
  7.         static public string ToString(double numeric)
  8.         {
  9.             string result = "";
  10.             bool otr = false;
  11.  
  12.             if (numeric < 0)
  13.             {
  14.                 otr = true;
  15.                 numeric = numeric * (-1);
  16.             }
  17.  
  18.             int ind = numeric.ToString().IndexOf(',');
  19.  
  20.             if (ind == -1)
  21.                 return numeric.ToString();
  22.  
  23.             ulong cile = ulong.Parse(numeric.ToString().Substring(0, ind));
  24.             ulong chisel = ulong.Parse(numeric.ToString().Remove(0, ind + 1));
  25.             ulong znam = 1;
  26.  
  27.             for (int i = 0; i < chisel.ToString().Length; i++)
  28.                 znam *= 10;
  29.  
  30.             for (ulong i = 2; i < znam; i++)
  31.             {
  32.                 if (chisel % i == 0 && znam % i == 0)
  33.                 {
  34.                     chisel /= i;
  35.                     znam /= i;
  36.                     i = 1;
  37.                 }
  38.             }
  39.  
  40.             result = (cile * znam + chisel).ToString() + "/" + znam.ToString();
  41.  
  42.             if (otr)
  43.             {
  44.                 result = "-" + result;
  45.             }
  46.  
  47.             return result;
  48.         }
  49.         static public string Sokr()
  50.         {
  51.             Console.WriteLine("Введіть чисельник");
  52.             int сh = Convert.ToInt32(Console.ReadLine());
  53.             Console.WriteLine("Введіть знаменник");
  54.             int zn = Convert.ToInt32(Console.ReadLine());
  55.             int nod = 0;
  56.  
  57.             for (int i = 1; i < (сh * zn + 1); i++)
  58.             {
  59.                 if (zn % i == 0 && сh % i == 0)
  60.                 {
  61.                     nod = i;
  62.                 }
  63.             }
  64.             сh = сh / nod;
  65.             zn = zn / nod;
  66.             string strCh = Convert.ToString(сh);
  67.             string strZn = Convert.ToString(zn);
  68.             string s = strCh + "/" + strZn;
  69.             Console.WriteLine(s);
  70.             return s;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement