Advertisement
Levi0227

transponder

Oct 26th, 2022 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.91 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.  
  8. namespace transponder
  9. {
  10.     class transponder
  11.     {
  12.         struct ST {
  13.             public DateTime Time;
  14.             public int x;
  15.             public int y;
  16.         }
  17.         static List<ST> ls = new List<ST>();
  18.         static ST ls1;
  19.  
  20.         static int n = 2;
  21.         static void exn(){
  22.             if (n > 2) { Console.WriteLine(); }
  23.             if (n == 3) n++;
  24.             Console.WriteLine($"Exercise {n++}.");
  25.         }
  26.  
  27.         static void ex1() {
  28.             StreamReader sr = new StreamReader("signal.txt");
  29.  
  30.             while (!sr.EndOfStream) {
  31.                 var line = sr.ReadLine().Split();
  32.  
  33.                 ls1.Time = new DateTime(1,1,1,Convert.ToInt16(line[0]),   //hour
  34.                                               Convert.ToInt16(line[1]),   //min
  35.                                               Convert.ToInt16(line[2]));  //sec
  36.                 ls1.x = Convert.ToInt16(line[3]);
  37.                 ls1.y = Convert.ToInt16(line[4]);
  38.  
  39.                 ls.Add(ls1);
  40.             }
  41.             sr.Close();
  42.  
  43.             //foreach (var e in ls)
  44.             //{
  45.             //    Console.WriteLine($"{e.Time.ToLongTimeString()} {e.x} {e.y}");
  46.             //}
  47.         }
  48.         static void ex2() {
  49.             exn();
  50.             Console.Write("Give the ordinal number of the signal: ");
  51.             int numb = Convert.ToInt16(Console.ReadLine());
  52.             Console.WriteLine($"x={ls[numb - 1].x} y={ls[numb - 1].y}");
  53.         }
  54.         static int interval(DateTime time1, DateTime time2) {
  55.             //int timediff = Convert.ToInt16(time2 - time1).TotalSeconds;
  56.             //return timediff;
  57.             return (int)(time2 - time1).TotalSeconds;
  58.         }
  59.         static void ex4() {
  60.             exn();
  61.             DateTime first = ls[0].Time;
  62.             DateTime last = ls[ls.Count-1].Time;
  63.             int h, m, s, totalsec;
  64.             totalsec = interval(first, last);
  65.             h = totalsec / 3600;
  66.             totalsec -= h * 3600;  
  67.             m = totalsec / 60;
  68.             totalsec -= m * 60;
  69.             s = totalsec;
  70.             Console.WriteLine($"Time difference: {h}:{m}:{s}");
  71.         }
  72.         static void ex5() {
  73.             exn();
  74.             //int minx, maxx, miny, maxy;
  75.             //maxx=minx = ls[0].x;
  76.             //maxy=miny = ls[0].y;
  77.             //foreach (var e in ls){
  78.             //    if (e.x<minx) minx = e.x;
  79.             //    if (e.x>maxx) maxx = e.x;
  80.             //    if (e.y<miny) miny = e.y;
  81.             //    if (e.y>maxy) maxy = e.y;
  82.             //}
  83.             //Console.WriteLine($"Bottom left: {minx};{miny}, top right: {maxx};{maxy}");
  84.             Console.WriteLine($"Bottom left: {ls.Min(c => c.x)},{ls.Min(c => c.y)} top right: {ls.Max(c => c.x)};{ls.Max(c => c.y)}");
  85.         }
  86.         static void ex6() {
  87.             exn();
  88.             double distance = 0;
  89.             for (int i = 0; i < ls.Count-1; i++){
  90.                 double dx = (ls[i].x - ls[i+1].x);
  91.                 double dy = (ls[i].y - ls[i+1].y);
  92.                 distance += Math.Sqrt(dx*dx+dy*dy);
  93.             }
  94.             Console.WriteLine($"Total distance: {distance:F3} units");
  95.         }
  96.         static void ex7() {
  97.             StreamWriter sw = new StreamWriter("missing.txt");
  98.             int timediff, corddiffx, corddiffy, timecount, cordcount;
  99.             for (int i = 0; i < ls.Count - 1; i++){
  100.                 timediff = interval(ls[i].Time, ls[i + 1].Time)/60;
  101.                 corddiffx = (ls[i].x - ls[i + 1].x);
  102.                 corddiffy = (ls[i].y - ls[i + 1].y);
  103.                 timecount = cordcount = 0;
  104.                 if (timediff > 5) {
  105.                     timecount += (timediff-1) / 5;
  106.                 }
  107.                 if (corddiffx>10){
  108.                     cordcount += (corddiffx-1) / 10;
  109.                 }
  110.                 if (corddiffy>10){
  111.                     cordcount += (corddiffy - 1) / 10;
  112.                 }
  113.                 if (timecount != 0 || cordcount != 0)
  114.                 {
  115.                     sw.Write(ls[i+1].Time.ToString("%H %m %s "));
  116.                     if (timecount>cordcount){
  117.                         sw.WriteLine($"time difference {timecount}");
  118.                     }
  119.                     else
  120.                     {
  121.                         sw.WriteLine($"coordinate difference {cordcount}");
  122.                     }
  123.                     sw.WriteLine();
  124.                 }
  125.             }
  126.             sw.Close();
  127.         }
  128.  
  129.         static void Main(string[] args)
  130.         {
  131.             ex1();
  132.             ex2();
  133.             //ex3();
  134.             ex4();
  135.             ex5();
  136.             ex6();
  137.             ex7();
  138.  
  139.             Console.ReadKey();
  140.         }
  141.     }
  142. }
  143. /*
  144. 3 11 19 122 644
  145. 3 16 19 120 643
  146. 3 21 19 126 639
  147. 3 26 19 131 641
  148. 3 27 55 124 651
  149. 3 31 50 134 649
  150. 3 36 50 126 650
  151. 3 41 50 129 648
  152. 3 46 50 129 647
  153. 3 50 22 119 653
  154. 3 55 22 111 662
  155. 3 55 33 101 669
  156. 4 0 33 99 677
  157. 4 5 33 91 672
  158. 4 10 33 89 670
  159. 4 15 33 85 668
  160. 4 25 33 86 671
  161. 4 30 33 83 680
  162. 4 35 33 80 677
  163. 4 40 33 88 686
  164. 4 45 33 87 690
  165. 4 50 33 81 698
  166. 4 55 33 74 678
  167. 5 5 33 78 684
  168. 5 10 33 73 690
  169. 5 12 42 70 680
  170. 5 17 42 75 687
  171. 5 22 42 75 695
  172. 5 27 42 68 701
  173. 5 32 42 65 694
  174. 5 37 42 72 698
  175. 5 42 42 76 692
  176. 5 47 42 71 699
  177. 5 52 42 71 701
  178. 5 57 42 77 704
  179. 6 2 42 73 709
  180. 6 7 42 76 713
  181. 6 22 42 94 724
  182. 6 32 42 82 698
  183. 6 37 42 74 691
  184. 6 42 42 81 695
  185. 6 47 42 85 692
  186. 6 52 42 88 684
  187. 6 57 42 95 679
  188. 7 2 42 96 682
  189. 7 7 42 99 678
  190. 7 12 42 104 676
  191. 7 17 42 104 682
  192. 7 22 42 103 676
  193. 7 27 42 94 670
  194. 7 31 44 88 660
  195. 7 36 44 87 653
  196. 7 41 44 85 660
  197. 7 46 44 93 656
  198. 7 51 44 100 656
  199. 7 56 44 96 660
  200. 8 1 44 95 662
  201. 8 6 44 94 664
  202. 8 11 44 87 673
  203. 8 16 44 94 665
  204. 8 21 44 95 663
  205. 8 26 44 104 663
  206. 8 31 44 104 658
  207. 8 36 44 104 655
  208. 8 37 26 112 665
  209. 8 42 26 112 674
  210. 8 43 29 102 683
  211. 8 48 29 110 687
  212. 8 53 29 114 692
  213. 8 58 29 112 692
  214. 9 3 29 112 691
  215. 9 8 29 110 693
  216. 9 10 1 120 693
  217. 9 15 1 111 691
  218. 9 20 1 109 685
  219. 9 24 10 99 682
  220. 9 29 10 100 682
  221. 9 30 27 110 690
  222. 9 35 27 117 684
  223. 9 40 27 119 675
  224. 9 45 27 118 682
  225. 9 50 27 121 684
  226. 9 55 27 126 690
  227. 10 0 27 123 689
  228. 10 5 27 119 694
  229. 10 10 27 127 685
  230. 10 15 27 121 694
  231. 10 20 27 125 700
  232. 10 25 27 129 703
  233. 10 30 27 125 702
  234. 10 35 27 123 707
  235. 10 38 59 113 705
  236. 10 43 59 109 700
  237. 10 44 45 111 690
  238. 10 46 26 121 690
  239. 10 47 50 111 696
  240. 10 48 17 121 687
  241. 10 51 38 131 696
  242. 10 56 38 135 698
  243. 11 1 38 140 693
  244. 11 6 38 141 694
  245. 11 11 38 143 696
  246. 11 16 38 136 688
  247. 11 21 38 132 687
  248. 11 26 38 128 679
  249. 11 31 38 122 683
  250. 11 34 19 132 674
  251. 11 39 19 139 665
  252. 11 40 48 147 675
  253. 11 43 45 137 673
  254. 11 48 45 133 678
  255. 11 49 59 143 679
  256. 11 54 59 136 683
  257. 11 56 52 126 682
  258. 12 1 52 134 676
  259. 12 2 46 124 674
  260. 12 7 46 123 681
  261. 12 12 46 116 681
  262. 12 17 46 109 672
  263. 12 22 46 114 675
  264. 12 26 29 113 665
  265. 12 31 29 116 663
  266. 12 36 29 112 662
  267. 12 41 29 106 664
  268. 12 41 32 113 674
  269. 12 41 48 123 683
  270. 12 46 48 114 687
  271. 12 51 39 104 677
  272. 12 56 39 103 686
  273. 13 1 39 109 690
  274. 13 5 53 119 699
  275. 13 9 40 109 706
  276. 13 14 40 112 708
  277. 13 19 40 110 699
  278. 13 24 40 113 692
  279. 13 29 40 114 700
  280. 13 34 40 108 691
  281. 13 39 40 99 682
  282. 13 44 40 95 680
  283. 13 48 25 89 690
  284. 13 53 25 84 687
  285. 13 58 25 79 685
  286. 14 3 25 83 678
  287. 14 8 25 83 672
  288. 14 11 49 83 662
  289. 14 16 49 92 663
  290. 14 21 49 100 660
  291. 14 26 49 97 661
  292. 14 31 49 100 658
  293. 14 36 49 104 666
  294. 14 41 49 110 664
  295. 14 46 49 102 663
  296. 14 51 49 102 659
  297. 14 51 50 108 669
  298. 14 56 50 107 672
  299. 14 58 31 103 682
  300. 15 3 31 95 682
  301. 15 8 31 99 678
  302. 15 13 31 98 680
  303. 15 18 31 101 673
  304. 15 23 31 96 666
  305. 15 26 18 92 676
  306. 15 31 18 100 679
  307. 15 31 51 98 689
  308. 15 36 1 95 679
  309. 15 41 1 87 676
  310. 15 46 1 88 675
  311. 15 51 1 89 671
  312. 15 56 1 86 677
  313. 16 1 1 88 682
  314. 16 5 16 83 672
  315. 16 10 16 82 664
  316. 16 15 16 81 666
  317. 16 20 16 74 661
  318. 16 25 16 67 661
  319. 16 27 36 69 651
  320. 16 32 36 74 642
  321. 16 37 36 70 649
  322. 16 42 36 76 656
  323. 16 47 36 73 659
  324. 16 52 36 82 662
  325. 16 57 36 78 662
  326. 17 2 36 72 662
  327. 17 6 34 69 672
  328. 17 11 34 78 675
  329. 17 15 7 75 685
  330. 17 16 19 85 683
  331. 17 21 19 85 688
  332. 17 24 20 76 678
  333. 17 29 20 71 671
  334. 17 30 3 72 661
  335. 17 31 4 82 670
  336. 17 36 4 89 664
  337. 17 41 4 83 666
  338. 17 46 4 77 670
  339. 17 51 4 85 671
  340. 17 56 4 77 673
  341. 18 1 4 70 670
  342. 18 5 52 73 680
  343. 18 10 52 69 688
  344. 18 15 52 70 680
  345. 18 20 52 68 682
  346. 18 25 52 65 685
  347. 18 30 52 58 692
  348. 18 35 52 62 689
  349. 18 40 52 57 694
  350. 18 44 20 52 684
  351. 18 49 20 46 682
  352. 18 54 20 45 681
  353. 18 59 20 49 672
  354. 19 4 20 44 678
  355. 19 9 20 42 669
  356. 19 14 20 35 662
  357. 19 19 20 30 665
  358. 19 22 45 20 671
  359. 19 27 45 14 670
  360. 19 32 45 6 673
  361. 19 37 45 6 669
  362. 19 42 45 5 676
  363. 19 47 45 12 681
  364. 19 52 45 20 689
  365. 19 57 45 26 685
  366. 20 0 34 27 695
  367. 20 4 31 17 700
  368. 20 9 31 13 699
  369. 20 14 31 17 699
  370. 20 19 31 20 705
  371. 20 24 31 26 712
  372. 20 29 31 25 708
  373. 20 33 24 20 718
  374. 20 38 24 29 725
  375. 20 43 24 29 723
  376. 20 48 24 20 726
  377. 20 53 24 23 717
  378. 20 55 40 17 727
  379. 21 0 40 21 721
  380. 21 5 40 13 727
  381. 21 10 40 4 722
  382. 21 15 40 11 721
  383. 21 20 40 12 718
  384. 21 25 40 19 711
  385. 21 30 40 13 720
  386. 21 33 27 21 710
  387. 21 38 27 27 719
  388. 21 43 27 29 714
  389. 21 48 27 31 708
  390. 21 50 21 40 698
  391. 21 53 59 50 706
  392. 21 58 59 44 713
  393. 22 3 59 36 713
  394. */
Tags: help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement