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 抛砖引玉

对行数较多的大文件报如下错,还请高手指点

猜你喜欢

转载自blog.csdn.net/m1m2m3mmm/article/details/84331561
今日推荐