C#, "Xiaobai Learning Program" Lesson 8: List (List) Second, compile "High-speed Rail Train Timetable" and time DateTime

1 text format

/// <summary>
/// Station information class
/// </summary>
public class Station
{     /// <summary>     /// number     /// </summary>     public int Id { get; set; } = 0;     /// <summary>     /// station name     /// </summary>     public string Name { get; set; } = string.Empty;







    public Station(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 

// The initial value of the list
List<Station> stations = new List<Station>() {     new Station(1,"Beijing"),     new Station(2,"Shijiazhuang"),     new Station(3,"Xianghe"),     new Station(4,"Tangshan"),     new Station(5,"Beidaihe"),     new Station(6,"Qinhuangdao"),     new Station(7,"Langfang"),     new Station(8,"Tianjin"),}; /// <summary> /// Lesson 8 of "Xiaobai Learning Programs": Application of List (List) 2——Compile high-speed rail train timetable /// Each line of the train timetable is generally: Station Arrival Time Departure Time /// The travel time between two stations time = distance / speed /// Departure Time = Arrival Time + Stop Time /// </summary> /// <param name="sender"> </param> /// <param name="e"></param> private void button8_Click(object sender, EventArgs e) {     // #1 create list (train information)





















    List<Station> G103 = new List<Station>();

    // #2 Add node (station information)
    G103.Add(stations[0]);
    G103.Add(stations[2]);
    G103.Add(stations[3]);
    G103.Add(stations[4]);
    G103.Add(stations[5]);

    // #3 Time-related information
    // Distance between stations (kilometers)
    // Each data represents the distance from the previous station; the starting station is of course 0;
    double[] distance = new double[] { 0, 50, 100, 140, 20 };
    // The stop time of each station (minutes)
    // Each data represents the stop time, the start station and the end station are not required!
    double[] stop = new double[] { 0, 2, 2, 2, 0 };
    // average train speed (km/h, km/h)
    double speed = 160;

    StringBuilder sb = new StringBuilder();

    // #4 Calculation start
    // Set the departure time (10:01)
    DateTime start = DateTime.Parse("10:01:00");
    for (int i = 0; i < 5; i++)
    {         if ( i == 0)         {             // starting station; no arrival time; only need to output departure time;             sb.AppendLine(G103[i].Name + " " + start.ToString("HH:mm") + " departure<br >");         }         else if (i == 4)         {             // Terminal; no departure time, only arrival time needs to be calculated;             // Calculated is to list running time (hours), and the following line is converted to seconds             double t = distance[i] / speed;             t = t * 60 * 60;             // Arrival time = departure time of the previous station + departure time             // This function is unified (calculated in seconds)             DateTime arrive = start.AddSeconds(t);














            sb.AppendLine(G103[i].Name + " " + arrive.ToString("HH:mm") + "arrival<br>"); }
        else
        {
        //             other stations; need to calculate arrival time and departure time             double t = distance[i] / speed;             t = t * 60 * 60;             // 1 arrival time;             DateTime arrive = start.AddSeconds(t);             sb.AppendLine(G103[i].Name + " " + arrive.ToString( "HH:mm") + "Arrival");             // 2 departure time (minutes, converted to seconds)             start = arrive.AddSeconds(stop[i] * 60);             sb.AppendLine(start.ToString("HH:mm ") + " Depart<br>");         }     }










    webBrowser1.DocumentText = sb.ToString();
}
 

2 code format

/// <summary>
/// 车站信息类 class
/// </summary>
public class Station
{
    /// <summary>
    /// 编号
    /// </summary>
    public int Id { get; set; } = 0;
    /// <summary>
    /// 车站名
    /// </summary>
    public string Name { get; set; } = string.Empty;

    public Station(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

// 列表的初值
List<Station> stations = new List<Station>() {
    new Station(1,"北京"),
    new Station(2,"石家庄"),
    new Station(3,"香河"),
    new Station(4,"唐山"),
    new Station(5,"北戴河"),
    new Station(6,"秦皇岛"),
    new Station(7,"廊坊"),
    new Station(8,"天津"),
};


/// <summary>
/// 《小白学程序》第八课:列表(List)应用之二————编制高铁列车时刻表
/// 列车时刻表的每一行一般都是:车站 到达时间 出发时间
/// 两个车站之间的开行时间 time = distance / speed
/// 出发时间 = 到达时间 + 停靠时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
    // #1 创建列表(列车车次信息)
    List<Station> G103 = new List<Station>();

    // #2 添加节点(车站信息)
    G103.Add(stations[0]);
    G103.Add(stations[2]);
    G103.Add(stations[3]);
    G103.Add(stations[4]);
    G103.Add(stations[5]);

    // #3 时间相关的信息
    // 各车站之间的距离(公里)
    // 每个数据表示与前面一个车站的距离;起点站当然为0;
    double[] distance = new double[] { 0, 50, 100, 140, 20 };
    // 每个车站的停靠时间(分钟)
    // 每个数据表示停靠时间,起点站 与 终点站  不需要!
    double[] stop = new double[] { 0, 2, 2, 2, 0 };
    // 列车的平均时速(公里/小时,km/h)
    double speed = 160;

    StringBuilder sb = new StringBuilder();

    // #4 计算开始
    // 设定开行时间(10点01分)
    DateTime start = DateTime.Parse("10:01:00");
    for (int i = 0; i < 5; i++)
    {
        if (i == 0)
        {
            // 起点站;没有到达时间;只需要输出出发时间;
            sb.AppendLine(G103[i].Name + " " + start.ToString("HH:mm") + " 出发<br>");
        }
        else if (i == 4)
        {
            // 终点站;没有出发时间,只需要计算到达时间;
            // 算出的是列出运行时间(小时),后面一行转为 秒
            double t = distance[i] / speed;
            t = t * 60 * 60;
            // 到达时间 = 上一站出发时间 + 开行时间
            // 本函数统一(按秒计算)
            DateTime arrive = start.AddSeconds(t);
            sb.AppendLine(G103[i].Name + " " + arrive.ToString("HH:mm") + " 到达<br>");
        }
        else
        {
            // 其他车站;需要计算到达时间与出发时间
            double t = distance[i] / speed;
            t = t * 60 * 60;
            // 1 到达时间;
            DateTime arrive = start.AddSeconds(t);
            sb.AppendLine(G103[i].Name + " " + arrive.ToString("HH:mm") + " 到达 ");
            // 2 出发时间(分钟,转为秒)
            start = arrive.AddSeconds(stop[i] * 60);
            sb.AppendLine(start.ToString("HH:mm") + " 出发<br>");
        }
    }

    webBrowser1.DocumentText = sb.ToString();
}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132523160