139 lines
4.2 KiB
C#
139 lines
4.2 KiB
C#
namespace vacanzeEstive_sezione1;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.Clear();
|
|
|
|
char unit = '*'; //unità usata per stampare le figure
|
|
char unit2 = '+'; //unità usata per stampare metà dell'ultima figura
|
|
|
|
int scelta = -1;
|
|
do {
|
|
Console.WriteLine("Scegliere un'opzione:");
|
|
Console.WriteLine("1. Stampa quadrato pieno");
|
|
Console.WriteLine("2. Stampa quadrato vuoto");
|
|
Console.WriteLine("3. Stampa triangolo");
|
|
Console.WriteLine("4. Stampa quadrato diagonale");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
|
|
try {
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
QuadratoPieno(Dimensione(), unit);
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
QuadratoVuoto(Dimensione(), unit);
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Triangolo(Dimensione(), unit);
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
QuadratoDiagonale(Dimensione(), unit, unit2);
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Errore: scelta non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Errore: scelta non valida.");
|
|
Pausa();
|
|
}
|
|
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
static uint Dimensione() { //restituisce un intero che indica la dimensione di un lato della figura
|
|
uint ritorno = 0;
|
|
bool showErrorMessage;
|
|
do {
|
|
showErrorMessage = false;
|
|
Console.Write("Inserire la dimensione della figura: ");
|
|
|
|
try {
|
|
ritorno = Convert.ToUInt32(Console.ReadLine());
|
|
if (ritorno == 0) {
|
|
showErrorMessage = true;
|
|
}
|
|
}
|
|
catch (Exception) {
|
|
showErrorMessage = true;
|
|
}
|
|
|
|
if (showErrorMessage) {
|
|
Console.WriteLine("Errore: inserire un numero superiore a 0.");
|
|
}
|
|
} while (showErrorMessage);
|
|
return ritorno;
|
|
}
|
|
|
|
static void QuadratoPieno(uint p_dimensione, char p_unit) {
|
|
|
|
for (int i = 1; i <= p_dimensione; i++) {
|
|
for (int j = 1; j <= p_dimensione; j++) {
|
|
Console.Write(p_unit);
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
static void QuadratoVuoto(uint p_dimensione, char p_unit) {
|
|
for (int i = 1; i <= p_dimensione; i++) {
|
|
if (i == 1 || i == p_dimensione) {
|
|
for (int j = 1; j <= p_dimensione; j++) {
|
|
Console.Write(p_unit);
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
else {
|
|
for (int j = 0; j <= p_dimensione - 2; j++) {
|
|
if (j == 0 || j == p_dimensione - 2) {
|
|
Console.Write(p_unit);
|
|
}
|
|
Console.Write(' ');
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void Triangolo(uint p_dimensione, char p_unit) {
|
|
for (int i = 1; i <= p_dimensione; i++) {
|
|
for (int j = 1; j <= i; j++) {
|
|
Console.Write(p_unit);
|
|
}
|
|
Console.WriteLine("");
|
|
}
|
|
}
|
|
|
|
static void QuadratoDiagonale(uint p_dimensione, char p_unit, char p_unit2) {
|
|
int j;
|
|
for (int i = 1; i <= p_dimensione; i++) {
|
|
for (j = 1; j <= i; j++) {
|
|
Console.Write(p_unit);
|
|
}
|
|
for (int k = (int)p_dimensione - --j; k > 0; k--) {
|
|
Console.Write(p_unit2);
|
|
}
|
|
Console.WriteLine("");
|
|
}
|
|
}
|
|
}
|