csharp-terminal/listas2/listaexercicios2.cs

215 lines
5.3 KiB
C#
Raw Permalink Normal View History

2021-09-02 08:31:01 -03:00
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
namespace listas2
{
/*
Author: F. Raszeja
2nd Assignment List delivery date: 10/09/2020
Translated to English: 30/08/2021
*/
class Lista2
{
static void exercicio1(){
for(int i = 11; i <= 250; i++)
{
if(i % 2 == 0)Console.WriteLine(i + " is even");
}
}
static void exercicio2(){
int a,b,tmp;
Console.WriteLine("Write a number, then press ENTER.");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
// b -- lowest value; a -- highest value
for(int i = a; i >= b; i--)
{
if(!(i % 2 == 0))Console.WriteLine(i + " is odd");
}
}
static void exercicio3(){
int a;
Console.WriteLine("Write a number, then press ENTER.");
a = int.Parse(Console.ReadLine());
for(int i = 1; i <= 10; i++)
{
Console.WriteLine("{0} x {1} = {2}",a,i,a*i);
}
}
static void exercicio4(){
int a,b,tmp,c = 0;
Console.WriteLine("Write a number, then press ENTER.");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
// b -- lowest value; a -- highest value
for(int i = a; i >= b; i--)
{
if(!(i % 2 == 0))c++;
}
Console.WriteLine("{0} odd numbers",c);
}
static void exercicio5(){
int[] input = new int[15];
int val = 0;
Console.WriteLine("Insert 15 numbers");
for(int i = 0; i < input.Length; i++)
{
input[i] = int.Parse(Console.ReadLine());
}
// Easier for me to just use Linq
// Console.WriteLine(input.Max());
// But I'll instead use a foreach
foreach(int num in input)
{
if(num > val)val = num;
}
Console.WriteLine(val + " is the biggest number.");
}
static void exercicio6(){
int[] notas = new int[10];
int cur, med, cou;
med = cou = 0;
while(cou < notas.Length){
Console.WriteLine("Insert a Grade (0-10)");
cur = int.Parse(Console.ReadLine());
if(!(cur <= 10 && cur >= 0))
{
Console.WriteLine("Invalid value. Try again.");
}
else
{
notas[cou] = cur;
cou++;
}
}
// Linq -> notas.Sum();
foreach(int e in notas)
{
med += e;
}
Console.WriteLine("The sum of all grades: {0}, Avg. grade: {1:N2}",med,(double)med/notas.Length);
}
static void exercicio7(){
for(int i = 1; i <= 100; i++)
{
if(i % 10 == 0)Console.WriteLine("{0} is a multiple of 10",i);
}
}
static void exercicio8(){
int val,fat = 1;
Console.WriteLine("Insert a value");
val = int.Parse(Console.ReadLine());
for(int i = 1; i <= val; i++)
{
fat *= i;
}
Console.WriteLine("{0}! = {1}",val,fat);
}
static void exercicio9(){
List<int> vals = new List<int>();
// Making a list to help me out
ConsoleKeyInfo sel;
int countA,countB,countC,countD;
countA=countB=countC=countD=0;
Console.Clear();
do
{
Console.WriteLine("\n------------------");
Console.WriteLine("Insert a number.");
vals.Add(int.Parse(Console.ReadLine()));
Console.WriteLine("Added. Continue (Y/N)?");
sel = Console.ReadKey();
} while(sel.Key == ConsoleKey.Y);
foreach(int e in vals)
{
if(e % 2 == 0){countA++;}else{countB++;}
if(e >= 0){countC++;}else{countD++;}
}
Console.WriteLine("\nFrom the list, {0} even numbers, {1} odd, {2} positives, {3} negatives",countA,countB,countC,countD);
}
static void exercicio10(){
int c, n;
double e = 0;
ConsoleKeyInfo sel;
do{
Console.Clear();
Console.WriteLine("Employee No.:");
c = int.Parse(Console.ReadLine());
Console.WriteLine("Worked hours:");
n = int.Parse(Console.ReadLine());
for(int i = 1; i <= n; i++)
{
if(i > 50){e+=20;}else{e = 0;}
}
Console.WriteLine("No. {0}, Regular Salary {1}, Exceeding Salary {2}, Total Salary {3}",c,(n*10),e, (n*10)+e);
Console.WriteLine("Continue (Y/N)?");
sel = Console.ReadKey();
} while(sel.Key == ConsoleKey.Y);
}
static bool ParseStr(string val)
{
if (!val.Any(x=> char.IsLetter(x))){
int tmp = int.Parse(val);
if (tmp != 0 && tmp <= 10) return true;
}
return false;
}
static void Main(string[] args)
// UI code again...
{
string sel = "";
do{
Console.Clear();
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("Select one of the Examples");
Console.ResetColor();
Console.WriteLine("\n1-10\tExamples 1-10");
Console.WriteLine("\texit\tQuit to desktop.");
Console.Write("--> ");
sel = Console.ReadLine();
if(sel != "exit" && ParseStr(sel)){
Type typesPrograma = typeof(Lista2);
MethodInfo aInvocar = typesPrograma.GetMethod(
"exercicio"+sel, BindingFlags.Static | BindingFlags.NonPublic);
aInvocar.Invoke(null, null);
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
while(sel.ToLower() != "exit");
}
}
}