Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading;
- namespace ConsoleTests
- {
- class ConsoleTests
- {
- static void Main()
- {
- Console.WriteLine("Available choices: ");
- Console.WriteLine();
- Console.WriteLine("1. Put the digits from an integer number into a reversed order");
- Console.WriteLine("2. Calculate the average of given sequence of numbers");
- Console.WriteLine("3. Solve the linear equation a * x + b = 0");
- Console.Write("\nEnter your choice: ");
- byte answer = byte.Parse(Console.ReadLine());
- if (answer == 1)
- {
- int integer = 0;
- bool isValid;
- bool isValidInt32;
- do
- {
- Console.Write("\nEnter valid integer (1 to 50 000 000): ");
- //int test;
- isValidInt32 = int.TryParse(Console.ReadLine(), out integer);
- isValid = integer >= 1 && integer <= 50000000;
- } while (isValid == false);
- Console.WriteLine("\n-> Reversed: {0}", ReverseDigits(integer));
- }
- else if (answer == 2)
- {
- string collection;
- bool isNotEmpty;
- do
- {
- Console.Write("\nEnter numbers (separated by space): ");
- collection = Console.ReadLine();
- isNotEmpty = collection.Length >= 1;
- } while (isNotEmpty == false);
- string[] numsArray = collection.Split(' ').ToArray();
- Console.WriteLine("-> Average: {0}", CalcAverage(numsArray));
- }
- else if (answer == 3)
- {
- decimal a;
- bool isNotZero;
- do
- {
- Console.Write("Enter a (a != 0): ");
- a = decimal.Parse(Console.ReadLine());
- isNotZero = a != 0;
- } while (isNotZero == false);
- Console.Write("Enter b: ");
- decimal b = decimal.Parse(Console.ReadLine());
- CalcLinearEquation(a,b);
- }
- }
- static string ReverseDigits(int a)
- {
- int[] digits = a.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
- Array.Reverse(digits);
- string output = string.Join("", digits);
- return output;
- }
- static decimal CalcAverage(params string[] array)
- {
- decimal sum = 0;
- foreach (var num in array)
- {
- sum += decimal.Parse(Convert.ToString(num));
- }
- decimal average = sum/array.Length;
- return average;
- }
- static void CalcLinearEquation(decimal a, decimal b)
- {
- decimal x = -b/a;
- Console.WriteLine("x = {0}", x);
- Console.WriteLine("{0}*({1})+{2}=0", a, x, b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment