Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Globalization;
- namespace control
- {
- class control
- {
- static List<string> vehicledata = new List<string>();
- static int sectionlength = 10;
- static List<string> readinput(string filename)
- {
- List<string> vdata = new List<string>(File.ReadAllLines(filename));
- return vdata;
- }
- static int TrafficCount()
- {
- int traffcount = vehicledata.Count;
- int db = 0;
- for (int i = traffcount-1; i >=0; i--)
- {
- if (vehicledata[i].Split(' ')[5] == "9")
- {
- db++;
- }
- }
- return traffcount - db;
- }
- static int PassStartingPoint(int hour, int minute)
- {
- int db = 0;
- for (int i = 0; i < vehicledata.Count; i++)
- {
- if (Convert.ToInt32(vehicledata[i].Split(' ')[1]) == hour && Convert.ToInt32(vehicledata[i].Split(' ')[2]) == minute)
- {
- db++;
- }
- }
- return db;
- }
- static double AverageSpeed(DateTime time, int distance)
- {
- double t = (time.Hour * 3600 + time.Minute * 60 + time.Second + (double)time.Millisecond / 1000);
- return ((double)distance*1000 / t)*3.6;
- }
- static DateTime ElapsedTime(int n)
- {
- return new DateTime() + TimestringToDateTime(vehicledata[n], 1).Subtract(TimestringToDateTime(vehicledata[n], 0));
- }
- static double TrafficIntensity(string hourminute)
- {
- int db = 0;
- for (int i = 0; i < vehicledata.Count; i++)
- {
- int entrysecond = Convert.ToInt32(vehicledata[i].Split(' ')[1]) * 3600 + Convert.ToInt32(vehicledata[i].Split(' ')[2])*60;
- int exitsecond = Convert.ToInt32(vehicledata[i].Split(' ')[5]) * 3600 + Convert.ToInt32(vehicledata[i].Split(' ')[6])*60;
- int enteredtime = Convert.ToInt32(hourminute.Split(' ')[0]) * 3600 + Convert.ToInt32(hourminute.Split(' ')[1])*60;
- if (entrysecond<=enteredtime && enteredtime<=exitsecond)
- {
- db++;
- }
- }
- return (double)db/ 10;
- }
- static DateTime TimestringToDateTime(string datarow, int which)
- {
- int i = 0;
- if (which == 1) i = 4;
- string timestring = datarow.Split(' ')[i+1] + ":" +
- datarow.Split(' ')[i+2] + ":" +
- datarow.Split(' ')[i+3] + "." +
- datarow.Split(' ')[i+4];
- DateTime time = Convert.ToDateTime(timestring);
- return time;
- }
- static int MaxSpeed()
- {
- int max = 0;
- double maxvalue = AverageSpeed(ElapsedTime(max), sectionlength);
- for (int i = 1; i < vehicledata.Count; i++)
- {
- if (maxvalue < AverageSpeed(ElapsedTime(i), sectionlength))
- {
- max = i;
- maxvalue = AverageSpeed(ElapsedTime(max), sectionlength);
- }
- }
- return max;
- }
- static int Overtakes(int n)
- {
- int overtakes = 0;
- for (int i = n - 1; i >= 0; i--)
- {
- if (TimestringToDateTime(vehicledata[n], 1) <= TimestringToDateTime(vehicledata[i], 1))
- {
- overtakes++;
- }
- }
- return overtakes;
- }
- static double SpeedingPercent()
- {
- int db = 0;
- for (int i = 0; i < vehicledata.Count; i++)
- {
- if (90 < AverageSpeed(ElapsedTime(i), sectionlength))
- {
- db++;
- }
- }
- return (double)db / vehicledata.Count;
- }
- static int FineValue(double speed)
- {
- int fine = 0;
- if (speed >= 104 && speed < 121) fine = 30000;
- if (speed >= 121 && speed < 136) fine = 45000;
- if (speed >= 136 && speed < 151) fine = 60000;
- if (speed >= 151) fine = 200000;
- return fine;
- }
- static void Main(string[] args)
- {
- // Exercise 1.
- vehicledata = readinput("measurements.txt");
- //Exercise 2.
- Console.WriteLine("Exercise 2.");
- Console.WriteLine("The data of {0} vehicles were recorded in the measurement.", vehicledata.Count);
- Console.WriteLine();
- //Exercise 3.
- Console.WriteLine("Exercise 3.");
- Console.WriteLine("Before 9 o'clock {0} vehicles passed the exit point recorder.", TrafficCount());
- Console.WriteLine();
- //Exercise 4.
- Console.WriteLine("Exercise 4.");
- Console.Write("Enter an hour and minute value: ");
- string hourminute = Console.ReadLine();
- int hour = Convert.ToInt32(hourminute.Split(' ')[0]);
- int minute = Convert.ToInt32(hourminute.Split(' ')[1]);
- int passeddb = PassStartingPoint(hour, minute);
- Console.WriteLine("\ta. The number of vehicle that passed the entry point recorder: {0} ", passeddb.ToString());
- Console.WriteLine("\tb. The traffic intensity: {0}", TrafficIntensity(hourminute).ToString("F1"));
- Console.WriteLine();
- //Exercise 5.
- Console.WriteLine("Exercise 5.");
- int max = MaxSpeed();
- double maxv = AverageSpeed(ElapsedTime(max), sectionlength);
- int overtakedb = Overtakes(max);
- Console.WriteLine("The data of the vehicle with the highest speed are \r\n" +
- "\tlicense plate number: {0} \r\n" +
- "\taverage speed: {1} km/h \r\n" +
- "\tnumber of overtaken vehicles: {2}", vehicledata[max].Split(' ')[0], ((int)maxv).ToString(), overtakedb.ToString());
- Console.WriteLine();
- //Exercise 6.
- Console.WriteLine("Exercise 6.");
- Console.WriteLine("{0} percent of the vehicles were speeding.", SpeedingPercent().ToString("P"));
- Console.WriteLine();
- //Exercise 7.
- FileStream fs = new FileStream("fines.txt", FileMode.Create);
- StreamWriter sw = new StreamWriter(fs);
- for (int i = 0; i < vehicledata.Count; i++)
- {
- double v = AverageSpeed(ElapsedTime(i), sectionlength);
- if (104 <= v)
- {
- sw.WriteLine("{0}\t{1} km/h\t{2} Ft", vehicledata[i].Split(' ')[0], ((int)v).ToString(), FineValue(v));
- }
- }
- sw.Close();
- fs.Close();
- Console.WriteLine("The file is ready.");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment