csharp-terminal/listas/listaexercicio1.cs
2021-09-02 08:31:01 -03:00

173 lines
5.2 KiB
C#

using System;
using System.Linq;
using System.Reflection;
namespace listascs
{
/*
1st Assignment
Author: F. Raszeja
Original delivery date: 25/08/2020
Translated to English: 30/08/2021
Notes: Some of the proposed activities have too many variables so I was lazy and used an array, as it also has extra functions that help me a little bit, like Reverse(), etc.
*/
class Lista1
{
static void exercicio1()
{
string[] info = new string[3];
Console.WriteLine("Name:");
info[0] = Console.ReadLine();
Console.WriteLine("Age:");
info[1] = Console.ReadLine();
Console.WriteLine("City:");
info[2] = Console.ReadLine();
Console.WriteLine("Name: {0}, Age: {1}, City: {2}", info[0], info[1], info[2]);
}
static void exercicio2()
{
int num;
Console.WriteLine("Write a number, then press ENTER:");
num = int.Parse(Console.ReadLine());
Console.WriteLine("Number: {0}, Double: {1}", num, num*2);
}
static void exercicio3()
{
int num;
Console.WriteLine("Write a number, then press ENTER:");
num = int.Parse(Console.ReadLine());
Console.WriteLine("Number: {0}, Number ^ 2: {1}",num,Math.Pow(num,2));
}
static void exercicio4()
{
float[] nums = new float[3];
Console.WriteLine("Insert the values for A, B and C:");
for(int i = 0; i<nums.Length;i++){nums[i] = float.Parse(Console.ReadLine());}
Console.WriteLine("\nResult: " + (((Math.Pow(nums[0],2f)) * 5f) - nums[2])/(nums[1] - (nums[0] % 4f)));
}
static void exercicio5()
{
byte idade;
Console.WriteLine("Write your age, then press ENTER: ");
idade = byte.Parse(Console.ReadLine());
Console.WriteLine( idade < 18 ? "\nYou are a minor." : "\nYou are not a minor.");
}
static void exercicio6()
{
float brl,usd;
Console.WriteLine("Insert the value in Brazilian Reais:");
brl = float.Parse(Console.ReadLine());
Console.WriteLine("\nInsert the current USD/BRL quote");
usd = float.Parse(Console.ReadLine());
Console.WriteLine("R${0:G3} converted to dollars is: ${1}",brl, Math.Round(brl/usd,2));
}
static void exercicio7()
{
string[] nomes = new string[2];
Console.WriteLine("Insert a name and press ENTER, then insert another one and press ENTER.");
for(int i = 0;i<nomes.Length;i++){nomes[i] = Console.ReadLine();}
foreach(string nome in nomes)
{
Console.WriteLine("Uppercase: {0}, Char. Qty.: {1}, First three characters: {2}",
nome.ToUpper(), nome.Length, nome.Substring(0,3));
}
}
static void exercicio8()
{
int[] values = new int[2];
Console.WriteLine("Insert a value, press ENTER, then insert another one and press ENTER.");
for(int i = 0;i<values.Length;i++){values[i] = int.Parse(Console.ReadLine());}
Console.WriteLine("Original values: [{0}], New values: [{1}]",
string.Join(", ", values), // Formatting the array before printing it out
string.Join(", ", values.Reverse()));
}
static void exercicio9()
{
int[] val = new int[4];
Console.WriteLine("Insert the values for W, X, Y and Z");
for(int i = 0;i<val.Length;i++){val[i] = int.Parse(Console.ReadLine());}
bool res = ((val[1] >= val[2]) && (val[3] <= val[1])) ? true : false;
bool res2 = ((val[1] == val[0]) && (val[2] == val[3])) ? true : false;
Console.WriteLine(res || res2);
}
static bool ParseStr(string val)
{
if (!val.Any(x=> char.IsLetter(x) ||
x.Equals('0')) // using Linq to check if it has any 0's or if it's a letter.
&& !System.Text.RegularExpressions.Regex.IsMatch(val, @"^[1-9]{2,}$")){return true;}
return false;
}
static void Main(string[] args)
// UI Code here.
{
string sel = "";
do{
// Cleaning the console up before anything
Console.Clear();
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("Select one of the examples");
Console.ResetColor();
Console.WriteLine("\n1-9\tExample 1-9");
Console.WriteLine("\texit\tClose this program");
Console.Write("--> ");
sel = Console.ReadLine();
if(sel != "exit" && ParseStr(sel)){
Type typesPrograma = typeof(Lista1);
MethodInfo aInvocar = typesPrograma.GetMethod(
"exercicio"+sel, BindingFlags.Static | BindingFlags.NonPublic);
aInvocar.Invoke(null, null);
/*
typeof - get the object type of something, in this case, something from the "Program" class.
MethodInfo - information related to the invoked method, in this case, we'll be using GetMethod to look up the methods with the name "exercicio" and the number (sel),
e.g. exercicio1, exercicio2
Since I'm using only static methods, I have to filter it out using the binding flags.
Then it's easy, just invoke the "aInvocar" MethodInfo, it shouldn't have any parameters or submethods so we'll give it null instead.
*/
}
Console.WriteLine("\nPress any key to Continue...");
Console.ReadKey();
}
while(sel.ToLower() != "exit");
}
}
}