Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication4
  11. {
  12. public partial class Form1 : Form
  13. {
  14. List<string> names = new List<string>();
  15. List<double> distance = new List<double>();
  16.  
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. void AddToListBox()
  23. {
  24. listBox1.Items.Clear();
  25. for (int i = 0; i < names.Count; i++)
  26. {
  27. listBox1.Items.Add(names[i] + " " + distance[i]);
  28. }
  29.  
  30. }
  31.  
  32. private void button3_Click(object sender, EventArgs e)
  33. {
  34. names.Add(textBoxNames.Text);
  35. distance.Add(Convert.ToDouble(textBoxDistance.Text));
  36. AddToListBox();
  37. textBoxDistance.Text = "";
  38. textBoxNames.Text = "";
  39. }
  40.  
  41. private void buttonFurthest_Click(object sender, EventArgs e)
  42. {
  43. double furthest = 0.0;
  44. string calnames = "";
  45. for (int x = 0; x < distance.Count; x++)
  46. {
  47. if (distance[x] > furthest)
  48. {
  49. furthest = distance[x];
  50. calnames = names[x];
  51. }
  52. }
  53. MessageBox.Show("The person who lives the furthest away from college is " + calnames);
  54. }
  55.  
  56. private void buttonNearist_Click(object sender, EventArgs e)
  57. {
  58. double nearist = Double.MaxValue;
  59. string caznames = "";
  60. for (int x = distance.Count -1; x >= 0; x--)
  61. {
  62. if (distance[x] <= nearist)
  63. {
  64. nearist = distance[x];
  65. caznames = names[x];
  66. }
  67. }
  68. MessageBox.Show("Nearist " + nearist + " (" + caznames + ")");
  69. }
  70.  
  71. double CalcualteRange()
  72. {
  73. double result = distance.Max() - distance.Min();
  74. return result;
  75. }
  76.  
  77. private void buttonRange_Click(object sender, EventArgs e)
  78. {
  79. double nearist1 = Double.MaxValue; //min
  80. for (int x = distance.Count - 1; x >= 0; x--) {
  81. if (distance[x] <= nearist1) {
  82. nearist1 = distance[x];
  83. }
  84. }
  85.  
  86. double farthest1 = 0; //max
  87. for (int x = 0; x < distance.Count; x++) {
  88. if (distance[x] >= farthest1) {
  89. farthest1 = distance[x];
  90. }
  91. }
  92.  
  93. double range = CalcualteRange();
  94. MessageBox.Show(string.Format("Max: {0}\nMin: {1}\nRange: {2}", farthest1, nearist1, range));
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement