Levi0227

speedcontrol solution

Sep 14th, 2022 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.27 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Globalization;
  8.  
  9. namespace control
  10. {
  11.     class control
  12.     {
  13.         static List<string> vehicledata = new List<string>();
  14.         static int sectionlength = 10;
  15.  
  16.         static List<string> readinput(string filename)
  17.         {
  18.             List<string> vdata = new List<string>(File.ReadAllLines(filename));
  19.             return vdata;
  20.         }
  21.  
  22.         static int TrafficCount()
  23.         {
  24.             int traffcount = vehicledata.Count;
  25.             int db = 0;
  26.             for (int i = traffcount-1; i >=0; i--)
  27.             {
  28.                 if (vehicledata[i].Split(' ')[5] == "9")
  29.                 {
  30.                     db++;
  31.                 }
  32.             }
  33.             return traffcount - db;
  34.         }
  35.  
  36.         static int PassStartingPoint(int hour, int minute)
  37.         {
  38.             int db = 0;
  39.             for (int i = 0; i < vehicledata.Count; i++)
  40.             {
  41.                 if (Convert.ToInt32(vehicledata[i].Split(' ')[1]) == hour && Convert.ToInt32(vehicledata[i].Split(' ')[2]) == minute)
  42.                 {
  43.                     db++;
  44.                 }
  45.             }
  46.             return db;
  47.         }
  48.  
  49.         static double AverageSpeed(DateTime time, int distance)
  50.         {
  51.             double t = (time.Hour * 3600 + time.Minute * 60 + time.Second + (double)time.Millisecond / 1000);
  52.             return ((double)distance*1000 / t)*3.6;
  53.         }
  54.  
  55.         static DateTime ElapsedTime(int n)
  56.         {
  57.             return new DateTime() + TimestringToDateTime(vehicledata[n], 1).Subtract(TimestringToDateTime(vehicledata[n], 0));
  58.         }
  59.  
  60.         static double TrafficIntensity(string hourminute)
  61.         {
  62.             int db = 0;
  63.             for (int i = 0; i < vehicledata.Count; i++)
  64.             {
  65.                 int entrysecond = Convert.ToInt32(vehicledata[i].Split(' ')[1]) * 3600 + Convert.ToInt32(vehicledata[i].Split(' ')[2])*60;
  66.                 int exitsecond = Convert.ToInt32(vehicledata[i].Split(' ')[5]) * 3600 + Convert.ToInt32(vehicledata[i].Split(' ')[6])*60;
  67.                 int enteredtime = Convert.ToInt32(hourminute.Split(' ')[0]) * 3600 + Convert.ToInt32(hourminute.Split(' ')[1])*60;
  68.                 if (entrysecond<=enteredtime && enteredtime<=exitsecond)
  69.                 {
  70.                     db++;
  71.                 }
  72.             }
  73.             return (double)db/ 10;
  74.         }
  75.  
  76.         static DateTime TimestringToDateTime(string datarow, int which)
  77.         {
  78.             int i = 0;
  79.             if (which == 1) i = 4;
  80.             string timestring = datarow.Split(' ')[i+1] + ":" +
  81.                          datarow.Split(' ')[i+2] + ":" +
  82.                          datarow.Split(' ')[i+3] + "." +
  83.                          datarow.Split(' ')[i+4];
  84.             DateTime time = Convert.ToDateTime(timestring);
  85.             return time;
  86.         }
  87.  
  88.         static int MaxSpeed()
  89.         {
  90.             int max = 0;
  91.             double maxvalue = AverageSpeed(ElapsedTime(max), sectionlength);
  92.             for (int i = 1; i < vehicledata.Count; i++)
  93.             {
  94.                 if (maxvalue < AverageSpeed(ElapsedTime(i), sectionlength))
  95.                 {
  96.                     max = i;
  97.                     maxvalue = AverageSpeed(ElapsedTime(max), sectionlength);
  98.                 }
  99.             }
  100.             return max;
  101.         }
  102.  
  103.         static int Overtakes(int n)
  104.         {
  105.             int overtakes = 0;
  106.             for (int i = n - 1; i >= 0; i--)
  107.             {
  108.                 if (TimestringToDateTime(vehicledata[n], 1) <= TimestringToDateTime(vehicledata[i], 1))
  109.                 {
  110.                     overtakes++;
  111.                 }
  112.             }
  113.             return overtakes;
  114.         }
  115.  
  116.         static double SpeedingPercent()
  117.         {
  118.             int db = 0;
  119.             for (int i = 0; i < vehicledata.Count; i++)
  120.             {
  121.                 if (90 < AverageSpeed(ElapsedTime(i), sectionlength))
  122.                 {
  123.                     db++;                    
  124.                 }
  125.             }
  126.             return (double)db / vehicledata.Count;
  127.         }
  128.         static int FineValue(double speed)
  129.         {
  130.             int fine = 0;
  131.             if (speed >= 104 && speed < 121) fine = 30000;
  132.             if (speed >= 121 && speed < 136) fine = 45000;
  133.             if (speed >= 136 && speed < 151) fine = 60000;
  134.             if (speed >= 151) fine = 200000;
  135.             return fine;
  136.         }
  137.  
  138.  
  139.         static void Main(string[] args)
  140.         {
  141.             // Exercise 1.
  142.             vehicledata = readinput("measurements.txt");
  143.  
  144.             //Exercise 2.
  145.             Console.WriteLine("Exercise 2.");
  146.             Console.WriteLine("The data of {0} vehicles were recorded in the measurement.", vehicledata.Count);
  147.             Console.WriteLine();
  148.            
  149.             //Exercise 3.
  150.             Console.WriteLine("Exercise 3.");
  151.             Console.WriteLine("Before 9 o'clock {0} vehicles passed the exit point recorder.", TrafficCount());
  152.             Console.WriteLine();
  153.            
  154.             //Exercise 4.
  155.             Console.WriteLine("Exercise 4.");
  156.             Console.Write("Enter an hour and minute value: ");
  157.             string hourminute = Console.ReadLine();
  158.             int hour = Convert.ToInt32(hourminute.Split(' ')[0]);
  159.             int minute = Convert.ToInt32(hourminute.Split(' ')[1]);
  160.             int passeddb = PassStartingPoint(hour, minute);
  161.             Console.WriteLine("\ta. The number of vehicle that passed the entry point recorder: {0} ", passeddb.ToString());
  162.             Console.WriteLine("\tb. The traffic intensity: {0}", TrafficIntensity(hourminute).ToString("F1"));
  163.             Console.WriteLine();
  164.  
  165.             //Exercise 5.
  166.             Console.WriteLine("Exercise 5.");
  167.             int max = MaxSpeed();
  168.             double maxv = AverageSpeed(ElapsedTime(max), sectionlength);
  169.             int overtakedb = Overtakes(max);
  170.             Console.WriteLine("The data of the vehicle with the highest speed are \r\n" +
  171.                                   "\tlicense plate number: {0} \r\n" +
  172.                                   "\taverage speed: {1} km/h \r\n" +
  173.                                   "\tnumber of overtaken vehicles: {2}", vehicledata[max].Split(' ')[0], ((int)maxv).ToString(), overtakedb.ToString());
  174.             Console.WriteLine();
  175.  
  176.             //Exercise 6.
  177.             Console.WriteLine("Exercise 6.");
  178.             Console.WriteLine("{0} percent of the vehicles were speeding.", SpeedingPercent().ToString("P"));
  179.             Console.WriteLine();
  180.  
  181.             //Exercise 7.
  182.             FileStream fs = new FileStream("fines.txt", FileMode.Create);
  183.             StreamWriter sw = new StreamWriter(fs);
  184.             for (int i = 0; i < vehicledata.Count; i++)
  185.             {
  186.                 double v = AverageSpeed(ElapsedTime(i), sectionlength);
  187.                 if (104 <= v)
  188.                 {
  189.                     sw.WriteLine("{0}\t{1} km/h\t{2} Ft", vehicledata[i].Split(' ')[0], ((int)v).ToString(), FineValue(v));
  190.                 }
  191.              }
  192.             sw.Close();
  193.             fs.Close();
  194.            
  195.             Console.WriteLine("The file is ready.");
  196.             Console.ReadLine();
  197.         }
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment