Primo Homework

Quiz Matematico

Il primo programma è una sorta di piccolo quiz matematico. Qui è presentato scritto sia in C# che in VB.NET. È possibile scaricare i file del codice dei due esercizi nei link presenti dopo i riquadri contenenti il codice.

Soluzione in C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HW1C
{
    public partial class Form1 : Form
    {
        // Creo oggetto chiamato randomizer che è un oggetto Random.
        Random randomizer = new Random();
        // Aggiungo variabili per il problema di somma.
        int addend1;
        int addend2;

        // Aggiungo variabili per il problema di differenza, prodotto e div.
        int sub1;
        int sub2;

        int prod1;
        int prod2;

        int div1;
        int div2;

        // Aggiungo una variabile per il timer
        int timeLeft;

        // Aggiungo il metodo StartTheQuiz() che usa il metodo Next().
        // Inizio il problema riempiendo le caselle e iniziando il timer.

        public void StartTheQuiz()
        {
            // Metto due numeri randomici nei due addendi.
            // Il valore tra parentesi è il valore massimo (non compreso).
            addend1 = randomizer.Next(51);
            addend2 = randomizer.Next(51);

            // Converto i due numeri in stringhe per mostrarli
            // nel label controls.
            plusLeftLabel.Text = addend1.ToString();
            plusRightLabel.Text = addend2.ToString();

            // Imposto il valore del controllo "sum" a 0.
            sum.Value = 0;

            // Stessa cosa per il problema della sottrazione, prod e div.
            sub1 = randomizer.Next(1, 101);
            sub2 = randomizer.Next(1, sub1);

            minusLeftLabel.Text = sub1.ToString();
            minusRightLabel.Text = sub2.ToString();

            difference.Value = 0;

            prod1 = randomizer.Next(2, 11);
            prod2 = randomizer.Next(2, 11);
            
            timesLeftLabel.Text = prod1.ToString();
            timesRightLabel.Text = prod2.ToString();
            
            prod.Value = 0;

            int temp = randomizer.Next(2,11);
            div2 = randomizer.Next(2,11);
            div1 = div2 * temp;
            
            diviedLeftLabel.Text = div1.ToString();
            diviedRightLabel.Text = div2.ToString();

            quoz.Value = 0;
            
            // Inizio il timer.
            timeLeft = 30;
            timeLabel.Text = "30 secondi";
            timer1.Start();
        }

        private bool CheckTheAnswer()
        {
            if ((addend1 + addend2 == sum.Value) 
                && (sub1-sub2 == difference.Value)
                && (prod1*prod2 == prod.Value)
                && (div1/div2 == quoz.Value))
                return true;
            else
                return false;
        }

        private void answer_Enter(object sender, EventArgs e)
        {
            // Select the whole answer in the NumericUpDown control.
            NumericUpDown answerBox = sender as NumericUpDown;

            if (answerBox != null)
            {
                int lengthOfAnswer = answerBox.Value.ToString().Length;
                answerBox.Select(0, lengthOfAnswer);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void plusLeftLabel_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void startButton_Click(object sender, EventArgs e)
        {
            StartTheQuiz();
            startButton.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (CheckTheAnswer())
            {
                timer1.Stop();
                MessageBox.Show("Hai risposto bene a tutto!", "Complimenti!");
                startButton.Enabled = true;

            }
            if (timeLeft > 0)
            {
                // Cambio il nuovo tempo rimanente
                timeLeft = timeLeft - 1;
                timeLabel.Text = timeLeft + " secondi";
                if (timeLeft <= 5)
                {
                    timeLabel.BackColor = Color.Red;
                }
            }
            else
            {
                // Se il tempo finisce blocco tutto
                timer1.Stop();
                timeLabel.Text = "Tempo finito!";
                MessageBox.Show("Non hai terminato in tempo.", "Puoi riporvare però!");
                sum.Value = addend1 + addend2;
                difference.Value = sub1 - sub2;
                prod.Value = prod1 * prod2;
                quoz.Value = div1 / div2;
                startButton.Enabled = true;
            }
        }
    }
}

Link per scaricare il programma in C#.

Soluzione in VB.NET

Public Class Form1
    ' Creo un oggetto Random.
    Private randomizer As New Random

    ' Creo variabili di addizione.
    Private sum1 As Integer
    Private sum2 As Integer

    ' Creo variabili per sottrazione, prodotto e quoz
    Private min1 As Integer
    Private min2 As Integer

    Private prod1 As Integer
    Private prod2 As Integer

    Private quoz1 As Integer
    Private quoz2 As Integer

    ' Aggiungo una variabile per il timer
    Private timeLeft As Integer



    ' Aggiungo il metodo StartTheQuiz() che usa il metodo Next().
    ' Inizio il problema riempiendo le caselle e iniziando il timer.

    Public Sub StartTheQuiz()
        ' Metto due numeri randomici nei due addendi.
        sum1 = randomizer.Next(51)
        sum2 = randomizer.Next(51)

        'Metto due numeri randomici anche per sottrazione, prod e div
        min1 = randomizer.Next(1, 100)
        min2 = randomizer.Next(1, min1)

        prod1 = randomizer.Next(1, 11)
        prod2 = randomizer.Next(1, 11)

        quoz2 = randomizer.Next(1, 11)
        Dim temp As Integer = randomizer.Next(1, 11)
        quoz1 = quoz2 * temp

        plusLeftLabel.Text = sum1.ToString()
        plusRightLabel.Text = sum2.ToString()

        minusLeftLabel.Text = min1.ToString()
        minusRightLabel.Text = min2.ToString()

        prodLeftLabel.Text = prod1.ToString
        prodRightLabel.Text = prod2.ToString

        quozLeftLabel.Text = quoz1.ToString
        quozRightLabel.Text = quoz2.ToString


        sum.Value = 0
        dif.Value = 0
        prod.Value = 0
        quoz.Value = 0

        ' Inizio il timer
        timeLeft = 30
        timeLabel.Text = "30 seconds"
        Timer1.Start()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Label5_Click(sender As Object, e As EventArgs) Handles Label5.Click

    End Sub

    Private Sub startButton_Click() Handles startButton.Click
        StartTheQuiz()
        startButton.Enabled = False
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If CheckTheAnswer() Then
            Timer1.Stop()
            MessageBox.Show("Hai risposto bene a tutto!", "Complimenti!")
            startButton.Enabled = True
        ElseIf timeLeft > 0 Then
            timeLeft -= 1
            timeLabel.Text = timeLeft & " secondi"
            If timeLeft <= 5 Then
                timeLabel.BackColor = Color.Red
            Else
                timeLabel.BackColor = Color.White
            End If
        Else
            ' Se il tempo finisce blocco tutto
            Timer1.Stop()
            timeLabel.Text = "Tempo finito!"
            MessageBox.Show("Non hai terminato in tempo.", "Puoi riporvare però!")
            sum.Value = sum1 + sum2
            dif.Value = min1 - min2
            prod.Value = prod1 * prod2
            quoz.Value = quoz1 / quoz2
            startButton.Enabled = True
        End If
    End Sub
    Public Function CheckTheAnswer() As Boolean

        If sum1 + sum2 = sum.Value AndAlso
            min1 - min2 = dif.Value AndAlso
            prod1 * prod2 = prod.Value AndAlso
            quoz1 / quoz2 = quoz.Value Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub answer_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sum.Enter, prod.Enter, dif.Enter, quoz.Enter

        Dim answerBox = TryCast(sender, NumericUpDown)

        If answerBox IsNot Nothing Then
            Dim lengthOfAnswer = answerBox.Value.ToString().Length
            answerBox.Select(0, lengthOfAnswer)
        End If

    End Sub
End Class

Link per scaricare il programma in VB.NET.