using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Outcome_2
{
public partial class fm1 : Form
{
public fm1()
{
InitializeComponent();
}
private string[] arrname;
private string[] arrscore;
private int judgename, judgescore = 0;
private int number = 0;
private float[] arrnumber;
float max = 0, min = 0,sum = 0,average = 0;
private float over, under = 0;
private void btnsave_Click(object sender, EventArgs e)
{
string result = (MessageBox.Show("Are you sure to enter the grade and name?", "Please Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)).ToString();
if (result.Equals("OK"))
{
for (int i = 0; i < arrname.Length; i++)
{
if (String.IsNullOrEmpty(txtname.Text))
{
lblnamehint.Text = "Name cannot be null";
lblscrhint.ForeColor = Color.Red;
judgename = 0;
break;
}
if (arrname[i] == txtname.Text)
{
lblnamehint.Text = "This name has already been entered";
lblnamehint.ForeColor = Color.Red;
judgename = 0;
break;
}
else
{
lblnamehint.Text = "√";
lblnamehint.ForeColor = Color.Green;
judgename = 1;
}
}
}
string pattern_grade = @"^100(\.0*)?$|^\d{1,2}(\.[0-9]0*)?$|^\.[1-9]0*$";
bool matchGrade = Regex.IsMatch(txtscore.Text, pattern_grade);
if (matchGrade)
{
if (String.IsNullOrEmpty(txtscore.Text))
{
lblscrhint.Text = "Score cannot be null";
lblscrhint.ForeColor = Color.Red;
judgescore = 0;
}
else
{
lblscrhint.Text = "√";
lblscrhint.ForeColor = Color.Green;
judgescore = 1;
}
}
else
{
lblscrhint.Text = "Please enter a score between 1 and 100";
lblscrhint.ForeColor = Color.Red;
judgescore = 0;
}
if (judgename == 1 && judgescore == 1)
{
arrname[number] = txtname.Text;
arrscore[number] = txtscore.Text;
fm2.rb.Text += txtname.Text + " " + txtname.Text + "\n";
number++;
txtname.Text = "";
txtscore.Text = "";
for (int i = 0; i < number; i++)
{
arrnumber[i] = float.Parse(arrscore[i].ToString());
}
max = arrnumber[0];
min = arrnumber[0];
sum = 0;
for (int i = 0; i < number; i++)
{
if (max < arrnumber[i])
{
max = arrnumber[i];
}
if (min > arrnumber[i])
{
min = arrnumber[i];
}
sum += arrnumber[i];
average = sum / number;
}
for (int i = 0; i < number; i++)
{
if (arrnumber[i] >= average){
over++;
}
if (arrnumber[i] < average){
under++;
}
}
judgename = 0;
judgescore = 0;
}
fm2.lblMax.Text = max.ToString();
fm2.lblMin.Text = min.ToString();
fm2.lblAvg.Text = average.ToString();
fm2.lblSum.Text = sum.ToString();
}
private void fm1_Load(object sender, EventArgs e)
{
lbltime.Text = System.DateTime.Now.ToString();
lbltime.Enabled = true;
lblnamehint.Text = "Please input a name";
lblscrhint.Text = "Please enter a score between 1~100";
arrname = new string[200];
arrscore = new string[200];
arrnumber = new float[200];
}
private void Time_Tick(object sender, EventArgs e)
{
this.lbltime.Text = DateTime.Now.ToString();
}
private void fmdata(object sender, EventArgs e)
{
fm2 f2 = new fm2();
f2.Show();
}
private void btnexit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to quit?","Warning",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}