From 46869bfa5c8b9d4bb9803a1cb68828dd632bfbeb Mon Sep 17 00:00:00 2001 From: Adriana Gavrilut Date: Mon, 8 Nov 2021 21:19:13 +0200 Subject: [PATCH 1/3] Conversii v1 --- 10_14/10_14/Conversii.cs | 134 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 10_14/10_14/Conversii.cs diff --git a/10_14/10_14/Conversii.cs b/10_14/10_14/Conversii.cs new file mode 100644 index 0000000..299934d --- /dev/null +++ b/10_14/10_14/Conversii.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Coversie_baze_2_16 +{ + class Program + { + static void Main(string[] args) + { + Conversie(); + } + + private static void Conversie() + { + string n = Console.ReadLine(); + int b1, b2; + b1 = int.Parse(Console.ReadLine()); + b2 = int.Parse(Console.ReadLine()); + if (b1 < 2 && b1 > 16 || b2 < 2 && b2 > 16) + { + Console.WriteLine("Date de intrare incorecte. Baza sursa si baza tinta trebuie sa fie in intervalul {2, 16}"); + return; + } + double nBase10 = ConvertToBase10(n, b1); + string result = ConvertFromBase10(nBase10, b2); + Console.WriteLine($"{n} from base {b1} = {result} in base {b2}"); + } + private static double ConvertToBase10(string n, int b1) + { + double s = 0; + int length = n.Length; + int pozPunct = n.IndexOf('.'); + int exponent = length - 1; + if (pozPunct != -1) + { + exponent = pozPunct - 1; + } + + for (int i = 0; i < length; i++) + { + if (n[i] == '.') + { + exponent += 1; + continue; + } + int cifra = n[i] - '0'; + if (n[i] == 'a' || n[i] == 'A') + { + cifra = 10; + } + if (n[i] == 'b' || n[i] == 'B') + { + cifra = 11; + } + if (n[i] == 'c' || n[i] == 'C') + { + cifra = 12; + } + if (n[i] == 'd' || n[i] == 'D') + { + cifra = 13; + } + if (n[i] == 'e' || n[i] == 'E') + { + cifra = 14; + } + if (n[i] == 'f' || n[i] == 'F') + { + cifra = 15; + } + s = s + cifra * Math.Pow(b1, exponent - i); + } + return s; + } + + private static string ConvertFromBase10(double n, int b2) + { + int pIntreaga = (int)Math.Floor(n); + double pFractionara = n - pIntreaga; + + int pIntreagaCopy = pIntreaga; + + Stack resturi = new Stack(); + while (pIntreagaCopy > 0) + { + resturi.Push(pIntreagaCopy % b2); + pIntreagaCopy /= b2; + } + StringBuilder sb = new StringBuilder(); + int c; + while (resturi.Count > 0) + { + c = resturi.Pop(); + if (c < 10) + { + sb.Append((char)('0' + c)); + } + else + { + sb.Append((char)('A' + c - 10)); + } + } + + StringBuilder sb2 = new StringBuilder(); + Queue pIntregi = new Queue(); + double pFractionaraCopy = pFractionara; + while (pFractionaraCopy != 0) + { + double temp = pFractionaraCopy * b2; + c = (int)Math.Floor(temp); + pIntregi.Enqueue(c); + pFractionaraCopy = temp - c; + } + sb2.Append('.'); + while (pIntregi.Count > 0) + { + c = pIntregi.Dequeue(); + if (c < 10) + { + sb2.Append((char)('0' + c)); + } + else + { + sb2.Append((char)('A' + c - 10)); + } + } + + return sb.ToString() + sb2.ToString(); + } + } +} From caa9700c0b3e8631c86302ba86a17293bdef8201 Mon Sep 17 00:00:00 2001 From: Adriana Gavrilut Date: Mon, 8 Nov 2021 21:50:52 +0200 Subject: [PATCH 2/3] Rezolvarea problemei in clasa Conversii --- 10_14/10_14/Conversii.cs | 2 +- 10_14/10_14/Program.cs | 127 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/10_14/10_14/Conversii.cs b/10_14/10_14/Conversii.cs index 299934d..2590996 100644 --- a/10_14/10_14/Conversii.cs +++ b/10_14/10_14/Conversii.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace Coversie_baze_2_16 +namespace _10_14 { class Program { diff --git a/10_14/10_14/Program.cs b/10_14/10_14/Program.cs index 53085f8..d1bdcf2 100644 --- a/10_14/10_14/Program.cs +++ b/10_14/10_14/Program.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Text; +using System.Threading.Tasks; namespace _10_14 { @@ -9,7 +12,8 @@ static void Main(string[] args) { //Mijloc(); - Conversie(); + //Conversie(); + Conversii.Conversie(); } /// @@ -149,4 +153,125 @@ private static void Mijloc() Console.WriteLine($"Mijlocul intervalului [{st}, {dr}] este {mij}"); } } + + class Conversii + { + public static void Conversie() + { + string n = Console.ReadLine(); + int b1, b2; + b1 = int.Parse(Console.ReadLine()); + b2 = int.Parse(Console.ReadLine()); + if (b1 < 2 && b1 > 16 || b2 < 2 && b2 > 16) + { + Console.WriteLine("Date de intrare incorecte. Baza sursa si baza tinta trebuie sa fie in intervalul {2, 16}"); + return; + } + double nBase10 = ConvertToBase10(n, b1); + string result = ConvertFromBase10(nBase10, b2); + Console.WriteLine($"{n} from base {b1} = {result} in base {b2}"); + } + private static double ConvertToBase10(string n, int b1) + { + double s = 0; + int length = n.Length; + int pozPunct = n.IndexOf('.'); + int exponent = length - 1; + if (pozPunct != -1) + { + exponent = pozPunct - 1; + } + + for (int i = 0; i < length; i++) + { + if (n[i] == '.') + { + exponent += 1; + continue; + } + int cifra = n[i] - '0'; + if (n[i] == 'a' || n[i] == 'A') + { + cifra = 10; + } + if (n[i] == 'b' || n[i] == 'B') + { + cifra = 11; + } + if (n[i] == 'c' || n[i] == 'C') + { + cifra = 12; + } + if (n[i] == 'd' || n[i] == 'D') + { + cifra = 13; + } + if (n[i] == 'e' || n[i] == 'E') + { + cifra = 14; + } + if (n[i] == 'f' || n[i] == 'F') + { + cifra = 15; + } + s = s + cifra * Math.Pow(b1, exponent - i); + } + return s; + } + + private static string ConvertFromBase10(double n, int b2) + { + int pIntreaga = (int)Math.Floor(n); + double pFractionara = n - pIntreaga; + + int pIntreagaCopy = pIntreaga; + + Stack resturi = new Stack(); + while (pIntreagaCopy > 0) + { + resturi.Push(pIntreagaCopy % b2); + pIntreagaCopy /= b2; + } + StringBuilder sb = new StringBuilder(); + int c; + while (resturi.Count > 0) + { + c = resturi.Pop(); + if (c < 10) + { + sb.Append((char)('0' + c)); + } + else + { + sb.Append((char)('A' + c - 10)); + } + } + + StringBuilder sb2 = new StringBuilder(); + Queue pIntregi = new Queue(); + double pFractionaraCopy = pFractionara; + while (pFractionaraCopy != 0) + { + double temp = pFractionaraCopy * b2; + c = (int)Math.Floor(temp); + pIntregi.Enqueue(c); + pFractionaraCopy = temp - c; + } + sb2.Append('.'); + while (pIntregi.Count > 0) + { + c = pIntregi.Dequeue(); + if (c < 10) + { + sb2.Append((char)('0' + c)); + } + else + { + sb2.Append((char)('A' + c - 10)); + } + } + + return sb.ToString() + sb2.ToString(); + } + } } From a096e1c6cdc252086fcce28f17a64b3787a7e0d3 Mon Sep 17 00:00:00 2001 From: Adriana Gavrilut Date: Mon, 8 Nov 2021 22:07:02 +0200 Subject: [PATCH 3/3] Detalii de implementare --- 10_14/10_14/Program.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/10_14/10_14/Program.cs b/10_14/10_14/Program.cs index d1bdcf2..d027a20 100644 --- a/10_14/10_14/Program.cs +++ b/10_14/10_14/Program.cs @@ -14,6 +14,8 @@ static void Main(string[] args) //Conversie(); Conversii.Conversie(); + Console.WriteLine("Press any key to stop..."); + Console.ReadKey(); } /// @@ -158,9 +160,12 @@ class Conversii { public static void Conversie() { + Console.Write("Introduceti numarul: "); string n = Console.ReadLine(); int b1, b2; + Console.Write("Baza sursa = "); b1 = int.Parse(Console.ReadLine()); + Console.Write("Baza tinta = "); b2 = int.Parse(Console.ReadLine()); if (b1 < 2 && b1 > 16 || b2 < 2 && b2 > 16) { @@ -271,6 +276,10 @@ private static string ConvertFromBase10(double n, int b2) } } + if(sb2.ToString() == ".") + { + return sb.ToString(); + } return sb.ToString() + sb2.ToString(); } }