ms_learn_csharp/027_create_methods/reusable_methods/Program.cs

52 lines
1.4 KiB
C#
Raw Normal View History

2024-08-10 21:03:39 -04:00
//using System;
int[] times = {800, 1200, 1600, 2000};
int diff = 0;
/* Format and display medicine times */
void display_times(int[] times){
foreach (int val in times) {
string time = val.ToString();
int len = time.Length;
if (len >= 3) {
time = time.Insert(len - 2, ":");
}
else if (len == 2) {
time = time.Insert(0, "0:");
} else {
time = time.Insert(0, "0:0");
}
Console.Write($"{time} ");
}
Console.WriteLine();
}
void adjust_times(int diff){
// Adjust the times by adding the difference,
// keeping the value within 24 hours
for (int i = 0; i < times.Length; i++) {
times[i] = ((times[i] + diff)) % 2400;
}
}
Console.WriteLine("Enter current GMT");
int current_GMT = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Current Medicine Schedule:");
display_times(times);
Console.WriteLine("Enter new GMT");
int new_GMT = Convert.ToInt32(Console.ReadLine());
if (Math.Abs(new_GMT) > 12 || Math.Abs(current_GMT) > 12) {
Console.WriteLine("Invalid GMT");
} else if (new_GMT <= 0 && current_GMT <= 0 || new_GMT >= 0 && current_GMT >= 0) {
diff = 100 * (Math.Abs(new_GMT) - Math.Abs(current_GMT));
adjust_times(diff);
} else {
diff = 100 * (Math.Abs(new_GMT) + Math.Abs(current_GMT));
adjust_times(diff);
}
Console.WriteLine("New Medicine Schedule:");
display_times(times);