From 8cdb4aab513016bc7ced77b2ac254184d8be5ef3 Mon Sep 17 00:00:00 2001 From: ​ Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: temperatures --- temps/temperatures/frm_main.cs | 134 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 temps/temperatures/frm_main.cs (limited to 'temps/temperatures/frm_main.cs') diff --git a/temps/temperatures/frm_main.cs b/temps/temperatures/frm_main.cs new file mode 100644 index 0000000..bebd44d --- /dev/null +++ b/temps/temperatures/frm_main.cs @@ -0,0 +1,134 @@ +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 temperatures +{ + public partial class frm_main : Form + { + public frm_main() + { + InitializeComponent(); + } + + /* 3 lists used to store temperatures from different cities, + * those cities being Sioux Falls, New York, and Denver + */ + List sf = new List(30); + List ny = new List(30); + List dv = new List(30); + + private void frm_main_Load(object sender, EventArgs e) + { + Random r = new Random(); + + /* fill the cities with temperature data */ + for (int i = 0; i < 30; ++i) { + sf.Add(r.Next(2, 70)); + ny.Add(r.Next(21, 68)); + dv.Add(r.Next(18, 74)); + } + } + + private (int, int) high_and_low(List city) + { + int low = 999; + int high = -999; + + /* find the highest and lowest temperatures */ + for (int i = 0; i < 30; ++i) { + if (city[i] < low) + low = city[i]; + if (city[i] > high) + high = city[i]; + } + /* return the two temperatures as a tuple */ + return (low, high); + } + + /* convert fahrenheit to celsius */ + private int f2c(int f) + { + double c = .56*(f-32); + return (int)c; + } + + private void show(List city) + { + /* get the highest and lowest temperatures of a city */ + (int low, int high) tmptr = high_and_low(city); + + /* show the temperatures on the form */ + txt_low_f.Text = tmptr.low.ToString() + "°f"; + txt_high_f.Text = tmptr.high.ToString() + "°f"; + txt_low_c.Text = f2c(tmptr.low).ToString() + "°c"; + txt_high_c.Text = f2c(tmptr.high).ToString() + "°c"; + } + + private void btn_add_Click(object sender, EventArgs e) + { + if (txt_add.Text == "") { + MessageBox.Show("You need to enter a temperature", + "Entry Error"); + txt_add.Focus(); + return; + } + if (!int.TryParse(txt_add.Text, out int i)) { + MessageBox.Show("The temperature must be a whole number", + "Entry Error"); + txt_add.Focus(); + return; + } + + /* find the selected city */ + if (radio_sf.Checked) { + /* remove the oldest temperature */ + sf.RemoveAt(0); + /* add a new temperature */ + sf.Add(int.Parse(txt_add.Text)); + /* redisplay the temperatures */ + show(sf); + } + else if (radio_ny.Checked) { + ny.RemoveAt(0); + ny.Add(int.Parse(txt_add.Text)); + show(ny); + } + else if (radio_dv.Checked) { + dv.RemoveAt(0); + dv.Add(int.Parse(txt_add.Text)); + show(dv); + } + else { + MessageBox.Show("You need to select a city", + "Entry Error"); + } + } + + private void radio_sf_CheckedChanged(object sender, EventArgs e) + { + show(sf); + } + + private void radio_ny_CheckedChanged(object sender, EventArgs e) + { + show(ny); + } + + private void radio_dv_CheckedChanged(object sender, EventArgs e) + { + show(dv); + } + + private void btn_exit_Click(object sender, EventArgs e) + { + this.Close(); + } + } +} -- cgit 1.4.1