From dfdb203dd13b5d51d87cdfa02c663329a75955e4 Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 27 Oct 2021 11:14:09 +0300 Subject: [PATCH 1/7] Return 0 instead of throwing exception. --- FP2021/Cin/Cin.csproj | 2 +- FP2021/Cin/{Cin.cs => ConsoleIn.cs} | 54 +++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) rename FP2021/Cin/{Cin.cs => ConsoleIn.cs} (65%) diff --git a/FP2021/Cin/Cin.csproj b/FP2021/Cin/Cin.csproj index d9823e9..4787d05 100644 --- a/FP2021/Cin/Cin.csproj +++ b/FP2021/Cin/Cin.csproj @@ -38,7 +38,7 @@ - + diff --git a/FP2021/Cin/Cin.cs b/FP2021/Cin/ConsoleIn.cs similarity index 65% rename from FP2021/Cin/Cin.cs rename to FP2021/Cin/ConsoleIn.cs index 5e74ea9..c84dbe4 100644 --- a/FP2021/Cin/Cin.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -3,7 +3,7 @@ namespace Cin { - public static class Cin + public static class ConsoleIn { /// /// Citeste un grup de caractere din consola. @@ -54,7 +54,7 @@ public static string NextToken() /// /// Citeste urmatorul int dat ca si input in consola. /// - /// valoarea int data. + /// valoarea int data sau 0 daca este intalnita o exceptie. /// daca valoarea data nu este formata doar din cifre 0-9. /// daca valoarea data este mai mare de . public static int NextInt() @@ -63,17 +63,19 @@ public static int NextInt() try { - value = int.Parse(NextToken()); + value = int.Parse(NextToken()); } catch (FormatException e) { Console.WriteLine("Valoarea data nu poate fi transformata in int!"); - throw; + value = 0; + //throw; } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue}!"); - throw; + Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!"); + value = 0; + //throw; } return value; @@ -82,7 +84,7 @@ public static int NextInt() /// /// Citeste urmatorul double dat ca si input in consola. /// - /// valoarea double data. + /// valoarea double data sau 0 daca este intalnita o exceptie. /// daca valoarea data nu este alcatuita doar din cifre 0-9 si caracterele . si , /// daca valoarea data este mai mare de public static double NextDouble() @@ -96,12 +98,44 @@ public static double NextDouble() catch (FormatException e) { Console.WriteLine("Valoarea data nu poate fi transformata in double!"); - throw; + value = 0; + //throw; } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue}!"); - throw; + Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!"); + value = 0; + //throw; + } + + return value; + } + + /// + /// Citeste urmatorul long dat ca si input in consola. + /// + /// valoarea long data sau 0 daca este intalnita o exceptie. + /// daca valoarea data nu este alcatuita doar din cifre 0-9 + /// daca valoarea data este mai mare decat sau mai mica decat . + public static long NextLong() + { + long value; + + try + { + value = long.Parse(NextToken()); + } + catch (FormatException e) + { + Console.WriteLine("Valoarea data nu poate fi transformata in long!"); + value = 0; + //throw; + } + catch (OverflowException e) + { + Console.WriteLine($"Valoarea data este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!"); + value = 0; + //throw; } return value; From 3640cc4df7fff87fc3a85f0c56c04aad93bfe569 Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 27 Oct 2021 11:24:20 +0300 Subject: [PATCH 2/7] Added function that reads first n tokens and returns them as a string. --- FP2021/Cin/ConsoleIn.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/FP2021/Cin/ConsoleIn.cs b/FP2021/Cin/ConsoleIn.cs index c84dbe4..425b75e 100644 --- a/FP2021/Cin/ConsoleIn.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -63,7 +63,7 @@ public static int NextInt() try { - value = int.Parse(NextToken()); + value = int.Parse(NextToken()); } catch (FormatException e) { @@ -140,5 +140,21 @@ public static long NextLong() return value; } + + /// + /// Citeste primele n cuvinte date in consola. + /// + /// numarul de cuvinte care va fi citit. + /// string-ul format prin concatenarea tuturor cuvintelor citite. + public static string FirstWords(int count) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 1; i <= count; ++i) + { + sb.Append($"{NextToken()} "); + } + return sb.ToString(); + } } } \ No newline at end of file From d72ad48f66313c549d284c06286c8fad6a43a60c Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 27 Oct 2021 11:38:13 +0300 Subject: [PATCH 3/7] Revert dfdb203dd13b5d51d87cdfa02c663329a75955e4 and instead throw exceptions with custom message. --- FP2021/Cin/ConsoleIn.cs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/FP2021/Cin/ConsoleIn.cs b/FP2021/Cin/ConsoleIn.cs index 425b75e..a4265a0 100644 --- a/FP2021/Cin/ConsoleIn.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -59,23 +59,21 @@ public static string NextToken() /// daca valoarea data este mai mare de . public static int NextInt() { + string token = NextToken(); int value; try { - value = int.Parse(NextToken()); + value = int.Parse(token); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in int!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in int!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!", e); } return value; @@ -89,23 +87,21 @@ public static int NextInt() /// daca valoarea data este mai mare de public static double NextDouble() { + string token = NextToken(); double value; try { - value = double.Parse(NextToken().Replace(',', '.')); + value = double.Parse(token.Replace(',', '.')); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in double!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in double!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!", e); } return value; @@ -119,23 +115,21 @@ public static double NextDouble() /// daca valoarea data este mai mare decat sau mai mica decat . public static long NextLong() { + string token = NextToken(); long value; try { - value = long.Parse(NextToken()); + value = long.Parse(token); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in long!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in long!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!", e); } return value; From 109c69a3852f02ce92cfa9c8a9f57fcef8fcf12e Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 27 Oct 2021 11:14:09 +0300 Subject: [PATCH 4/7] Return 0 instead of throwing exception. --- FP2021/Cin/Cin.csproj | 2 +- FP2021/Cin/{Cin.cs => ConsoleIn.cs} | 52 ++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) rename FP2021/Cin/{Cin.cs => ConsoleIn.cs} (65%) diff --git a/FP2021/Cin/Cin.csproj b/FP2021/Cin/Cin.csproj index d9823e9..4787d05 100644 --- a/FP2021/Cin/Cin.csproj +++ b/FP2021/Cin/Cin.csproj @@ -38,7 +38,7 @@ - + diff --git a/FP2021/Cin/Cin.cs b/FP2021/Cin/ConsoleIn.cs similarity index 65% rename from FP2021/Cin/Cin.cs rename to FP2021/Cin/ConsoleIn.cs index fc8d53f..c84dbe4 100644 --- a/FP2021/Cin/Cin.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -54,7 +54,7 @@ public static string NextToken() /// /// Citeste urmatorul int dat ca si input in consola. /// - /// valoarea int data. + /// valoarea int data sau 0 daca este intalnita o exceptie. /// daca valoarea data nu este formata doar din cifre 0-9. /// daca valoarea data este mai mare de . public static int NextInt() @@ -63,17 +63,19 @@ public static int NextInt() try { - value = int.Parse(NextToken()); + value = int.Parse(NextToken()); } catch (FormatException e) { Console.WriteLine("Valoarea data nu poate fi transformata in int!"); - throw; + value = 0; + //throw; } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue}!"); - throw; + Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!"); + value = 0; + //throw; } return value; @@ -82,7 +84,7 @@ public static int NextInt() /// /// Citeste urmatorul double dat ca si input in consola. /// - /// valoarea double data. + /// valoarea double data sau 0 daca este intalnita o exceptie. /// daca valoarea data nu este alcatuita doar din cifre 0-9 si caracterele . si , /// daca valoarea data este mai mare de public static double NextDouble() @@ -96,12 +98,44 @@ public static double NextDouble() catch (FormatException e) { Console.WriteLine("Valoarea data nu poate fi transformata in double!"); - throw; + value = 0; + //throw; } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue}!"); - throw; + Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!"); + value = 0; + //throw; + } + + return value; + } + + /// + /// Citeste urmatorul long dat ca si input in consola. + /// + /// valoarea long data sau 0 daca este intalnita o exceptie. + /// daca valoarea data nu este alcatuita doar din cifre 0-9 + /// daca valoarea data este mai mare decat sau mai mica decat . + public static long NextLong() + { + long value; + + try + { + value = long.Parse(NextToken()); + } + catch (FormatException e) + { + Console.WriteLine("Valoarea data nu poate fi transformata in long!"); + value = 0; + //throw; + } + catch (OverflowException e) + { + Console.WriteLine($"Valoarea data este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!"); + value = 0; + //throw; } return value; From 6c9dde857b897687d8664d4759beb417e98d6368 Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 27 Oct 2021 11:24:20 +0300 Subject: [PATCH 5/7] Added function that reads first n tokens and returns them as a string. --- FP2021/Cin/ConsoleIn.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/FP2021/Cin/ConsoleIn.cs b/FP2021/Cin/ConsoleIn.cs index c84dbe4..425b75e 100644 --- a/FP2021/Cin/ConsoleIn.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -63,7 +63,7 @@ public static int NextInt() try { - value = int.Parse(NextToken()); + value = int.Parse(NextToken()); } catch (FormatException e) { @@ -140,5 +140,21 @@ public static long NextLong() return value; } + + /// + /// Citeste primele n cuvinte date in consola. + /// + /// numarul de cuvinte care va fi citit. + /// string-ul format prin concatenarea tuturor cuvintelor citite. + public static string FirstWords(int count) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 1; i <= count; ++i) + { + sb.Append($"{NextToken()} "); + } + return sb.ToString(); + } } } \ No newline at end of file From 491f0d5cd7741f85d4be2327863780dca8ce425e Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 10 Nov 2021 10:31:50 +0200 Subject: [PATCH 6/7] Update ConsoleIn.cs --- FP2021/Cin/ConsoleIn.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FP2021/Cin/ConsoleIn.cs b/FP2021/Cin/ConsoleIn.cs index a4265a0..4d656a9 100644 --- a/FP2021/Cin/ConsoleIn.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -3,7 +3,7 @@ namespace Cin { - public static class ConsoleIn + public static class Cin { /// /// Citeste un grup de caractere din consola. @@ -151,4 +151,4 @@ public static string FirstWords(int count) return sb.ToString(); } } -} \ No newline at end of file +} From 9bb7b8f67c7010c174d9b1a5991232c9aba32380 Mon Sep 17 00:00:00 2001 From: Goldkraw Date: Wed, 10 Nov 2021 10:37:54 +0200 Subject: [PATCH 7/7] cin fix --- FP2021/Cin/ConsoleIn.cs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/FP2021/Cin/ConsoleIn.cs b/FP2021/Cin/ConsoleIn.cs index 425b75e..a4265a0 100644 --- a/FP2021/Cin/ConsoleIn.cs +++ b/FP2021/Cin/ConsoleIn.cs @@ -59,23 +59,21 @@ public static string NextToken() /// daca valoarea data este mai mare de . public static int NextInt() { + string token = NextToken(); int value; try { - value = int.Parse(NextToken()); + value = int.Parse(token); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in int!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in int!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {int.MaxValue} sau mai mica decat {int.MinValue}!", e); } return value; @@ -89,23 +87,21 @@ public static int NextInt() /// daca valoarea data este mai mare de public static double NextDouble() { + string token = NextToken(); double value; try { - value = double.Parse(NextToken().Replace(',', '.')); + value = double.Parse(token.Replace(',', '.')); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in double!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in double!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {double.MaxValue} sau mai mica decat {double.MinValue}!", e); } return value; @@ -119,23 +115,21 @@ public static double NextDouble() /// daca valoarea data este mai mare decat sau mai mica decat . public static long NextLong() { + string token = NextToken(); long value; try { - value = long.Parse(NextToken()); + value = long.Parse(token); } catch (FormatException e) { - Console.WriteLine("Valoarea data nu poate fi transformata in long!"); - value = 0; - //throw; + throw new FormatException($"Valoarea {token} nu poate fi transformata in long!", e); } catch (OverflowException e) { - Console.WriteLine($"Valoarea data este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!"); - value = 0; - //throw; + throw new OverflowException( + $"Valoarea {token} este mai mare decat {long.MaxValue} sau mai mica decat {long.MinValue}!", e); } return value;