1. 程式人生 > >C#程式設計學習(01):北斗時轉日曆時的小程式

C#程式設計學習(01):北斗時轉日曆時的小程式

需求說明:給定nav格式的北斗時檔案從中提取出北斗周和周內秒資訊,將該資訊轉化為日曆時

軟體介面:

設計流程:

1. 變數定義:

批量處理: 開啟,btn_openFile; 轉換, btn_timeTrans

單一轉換:北斗周對應文字框,txb_bdsWeek;周內秒對應的文字框:txb_weekInnerSec;轉換結果對應的文字框,txb_resault;轉換按鈕:btn_singleTrans;清空按鈕,btn_clear

2. 介面設計

將對話方塊的最大化按鈕設為False的兩種方式:

1)在 InitializeComponent()函式中手寫程式碼:this.MaximizeBox = false;

2)在屬性MaximizeBox設為false

將對話方塊設定為大小不可調整:屬性FormBorderStyle設為FixedDialog

3. 程式碼實現

3.1 批量處理-->開啟按鈕

private void btn_openFile_Click(object sender, EventArgs e)
        {
            //彈出檔案對話方塊
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            txtPath = ofd.FileName;

            if (txtPath.Split('.')[1] != "nav")
            {
                MessageBox.Show("開啟檔案應為nav格式檔案,請重新選擇開啟", "錯誤提示");
            }
        }

3.2 批量處理-->轉換

 private void btn_timeTrans_Click(object sender, EventArgs e)
        {
            if (txtPath == null)
            {
                MessageBox.Show("請點選 開啟 按鈕,選擇要轉換的檔案", "錯誤提示");
                return;
            }

            string outputFilePath = txtPath.Split('.')[0] + ".bds";
            System.IO.StreamWriter file = new System.IO.StreamWriter(outputFilePath, false);
            file.Write("北斗周 周內秒 年 月 日 時 分 秒\r\n");
            //開啟檔案開始轉換
            System.IO.StreamReader inputFile = System.IO.File.OpenText(txtPath); 
            string nextLine; 
            while ((nextLine = inputFile.ReadLine()) != null) 
            {
                string[] substrtmp = nextLine.Split(';');
                string[] substrs = substrtmp[1].Split(',');
                //得到北斗周和周內秒
                int bdsWeek = int.Parse(substrs[0]);
                double weekInnerSec = double.Parse(substrs[1]);
                CalendarTime ct = gps2CalendarTime(bdsWeek, weekInnerSec);
                file.Write("{0:D} {1:N} {2:D} {3:D} {4:D} {5:D} {6:D} {7:N}\r\n", 
                    bdsWeek, weekInnerSec, ct.year, ct.month, ct.day, ct.hour, ct.minus, ct.second);
            } 

            //關閉檔案
            file.Close();
            //釋放物件
            inputFile.Dispose();
            file.Dispose();
            MessageBox.Show("輸出路徑為:" + outputFilePath, "轉換成功");
        }

3.3 北斗時到日曆時轉換函式

private CalendarTime gps2CalendarTime(int weekno, double gpstow)
        {
            CalendarTime ct = new CalendarTime();
            int[] dinmth={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            double isecs = gpstow;
            double fsec = gpstow - isecs;
            int dayofw = (int)(isecs / 86400);
            isecs = isecs - 86400 * dayofw;

            int h = (int)(isecs / 3600);
            isecs = isecs - 3600 * h;

            int m = (int)(isecs / 60);             
            double s = isecs - 60 * m + fsec;

            //輸出時分秒
            ct.hour = h;
            ct.minus = m;
            ct.second = s;

            int ttlday = dayofw + 7 * weekno;
            ttlday -= 360;
            int yr = 1981;

            while(ttlday > 366)
            {
                ttlday = ttlday - 365;
                if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0)
                {
                    ttlday -= 1;
                }
                yr++;
            }
            if (ttlday == 366)
            {
                if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0)
                {
                    ct.year = yr;
                    dayofw = 366;
                }
                else
                {
                    ct.year = yr + 1;
                    dayofw = 1;
                }
            }

            if (ttlday < 366)
            {
                ct.year = yr;
                dayofw = ttlday;
            }
            int mon = 0;
            foreach (int i in dinmth)
            {
                mon += 1;
                if (ttlday <= i && ttlday > 0)
                {
                    ct.day = ttlday;
                    ct.month = mon;
                    ttlday = 0;
                }
                else if (mon == 2)
                {
                    if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0)
                    {
                        if (ttlday > 29)
                        {
                            ttlday -= 29;
                        }
                        else
                        {
                            ct.day = 29;
                            ct.month = 2;
                            ttlday = 0;
                        }
                    }
                    else
                    {
                        ttlday -= 28;
                    }
                }
                else
                {
                    ttlday -= dinmth[mon];
                }
                if (ttlday == 0) break;
            }
            
            return ct;
        }

3.4 單一處理-->轉換

private void btn_ouputFile_Click(object sender, EventArgs e)
        {
            int bdsWeek = int.Parse(txb_bdsWeek.Text);
            double weekInnerSec = double.Parse(txb_weekInnerSec.Text);
            CalendarTime ct = gps2CalendarTime(bdsWeek, weekInnerSec);
            txb_resault.Text = string.Format("{0:D} {1:D} {2:D} {3:D} {4:D} {5:N}", ct.year, ct.month, ct.day, ct.hour, ct.minus, ct.second);
        }

3.5 單一處理-->清除

private void btn_clear_Click(object sender, EventArgs e)
        {
            txb_weekInnerSec.Text = null;
            txb_resault.Text = null;
            txb_bdsWeek.Text = null;
        }

4 拋磚引玉

對行數較多的大檔案報如下錯,還請高手指點