使用C#编制一个控制台应用-竖曲线标高计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace 竖曲线计算
{
    class Program
    {
        static void Main(string[] args)
        {
            string folder = Directory.GetCurrentDirectory();
            string file_path = Path.Combine(folder, "data.txt");
            double[,] arr = Toolbox.readData(file_path);
            if (arr != null)
            {
                string sInput = "";
                bool endApp = false;
                while (!endApp)
                {
                    Console.Write("请输入桩号:");
                    sInput = Console.ReadLine();
                    double dMile = 0;
                    while (!double.TryParse(sInput, out dMile))
                    {
                        Console.Write("This is not valid input. Please enter an integer value: ");
                        sInput = Console.ReadLine();
                    }
                    double h = Toolbox.getElevation(arr, dMile);
 
                    Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                    if (Console.ReadLine() == "n") endApp = true;
                }
 
            }
        }
 
 
    }
    class Toolbox
    {
        public static double getElevation(double[,] arrIn, double dMile)
        {
            double[,] arrOut = Toolbox.getData(arrIn);
            double h = 0;
 
            for (int i = 1; i < arrOut.GetLength(0) - 1; i++)
            {
                double dPrevMile = arrOut[i - 1, 0];
                double dCurtMile = arrOut[i, 0];
                double dNextMile = arrOut[i + 1, 0];
                double dPrevElevation = arrOut[i - 1, 1];
                double dCurtElevation = arrOut[i, 1];
                double dNextElevation = arrOut[i + 1, 1];
                //if (dMile == dPrevMile)
                //{
                //    Console.WriteLine("the elevation is :{0:N5}", dPrevElevation);
                //    break;
                //}
                //else if (dMile == dCurtMile)
                //{
                //    Console.WriteLine("the elevation is :{0:N5}", dCurtElevation);
                //    break;
                //}
                //else if (dMile == dNextMile)
                //{
                //    Console.WriteLine("the elevation is :{0:N5}", dNextElevation);
                //    break;
                //}
                if ((dMile > dPrevMile) && (dMile < dNextMile))
                {
                    double dPrevTag = arrOut[i - 1, 5];
                    double dCurtTag = arrOut[i, 5];
                    double dNextTag = arrOut[i + 1, 5];
                    double dRadius = arrOut[i, 2];
                    double kL = arrOut[i, 3];
                    double kR = arrOut[i, 4];
                    double omgea = kR - kL;
                    if ((dPrevTag == 0 && dCurtTag == -1 && dNextTag == 0) || (dPrevTag == 1 && dCurtTag == -1 && dNextTag == 0))/// left line right arc
                    {
                        if (dMile < dCurtMile)
                        {
                            h = dCurtElevation + kL * (dMile - dCurtMile);
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
                        else if (dMile < dNextMile)
                        {
                            h = dCurtElevation + kL * (dMile - dCurtMile) + Math.Sign(omgea) * Math.Pow((dMile - dCurtMile), 2) / 2 / dRadius;
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
 
                    }
                    if (dPrevTag == -1 && dCurtTag == 0 && dNextTag == 1)///either arc
                    {
                        if (dMile < dCurtMile)
                        {
                            h = dCurtElevation + kL * (dMile - dCurtMile) + Math.Sign(omgea) * Math.Pow((dMile - dPrevMile), 2) / 2 / dRadius;
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
                        else if (dMile < dNextMile)
                        {
                            h = dCurtElevation + kR * (dMile - dCurtMile) + Math.Sign(omgea) * Math.Pow((dMile - dNextMile), 2) / 2 / dRadius;
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
                    }
                    if ((dPrevTag == 0 && dCurtTag == 1 && dNextTag == -1) || (dPrevTag == 0 && dCurtTag == 1 && dNextTag == -1))///left arc right line
                    {
                        if (dMile < dCurtMile)
                        {
                            h = dCurtElevation + kR * (dMile - dCurtMile) + Math.Sign(omgea) * Math.Pow((dMile - dCurtMile), 2) / 2 / dRadius;
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
                        else if (dMile < dNextMile)
                        {
                            h = dCurtElevation + kR * (dMile - dCurtMile);
                            Console.WriteLine("the elevation is {0:N5}", h);
                            break;
                        }
                    }
 
                    //Console.WriteLine("neither");
                }
 
            }
            return h;
        }
        public static double[,] getData(double[,] arrIn)
        {
            int iRowsCount = arrIn.GetLength(0);
            int iColsCount = arrIn.GetLength(1);
            double[,] arrOut = new double[(iRowsCount - 1) * 3 + 2, 6];
            int iOutRowscount = arrOut.GetLength(0);
 
            arrOut[0, 0] = arrIn[0, 0];
            arrOut[0, 1] = arrIn[0, 1];
            arrOut[0, 2] = arrIn[0, 2];
            arrOut[0, 3] = 0;
            arrOut[0, 4] = 0;
            arrOut[0, 5] = 0;
 
            for (int i = 1; i < iRowsCount - 1; i++)
            {
                double dMile = arrIn[i, 0];
                double dElevation = arrIn[i, 1];
                double dRadius = arrIn[i, 2];
                double dPreMile = arrIn[i - 1, 0];
                double dPreElevation = arrIn[i - 1, 1];
                double dNextMile = arrIn[i + 1, 0];
                double dNextElevation = arrIn[i + 1, 1];
                double dPreSlope = -(dElevation - dPreElevation) / (dPreMile - dMile);
                double dNextSlope = (dNextElevation - dElevation) / (dNextMile - dMile);
                double dArcLength = Math.Abs(dRadius * (dNextSlope - dPreSlope));
                double dTangentLength = dArcLength / 2;
                arrOut[3 * i - 2, 0] = dMile - dTangentLength;
                arrOut[3 * i - 1, 0] = dMile;
                arrOut[3 * i - 0, 0] = dMile + dTangentLength;
                arrOut[3 * i - 2, 1] = dElevation - dTangentLength * dPreSlope;
                arrOut[3 * i - 1, 1] = dElevation;
                arrOut[3 * i - 0, 1] = dElevation + dTangentLength * dNextSlope;
                arrOut[3 * i - 2, 1] = dElevation - dTangentLength * dPreSlope;
                arrOut[3 * i - 1, 1] = dElevation;
                arrOut[3 * i - 0, 1] = dElevation + dTangentLength * dNextSlope;
                arrOut[3 * i - 2, 1] = dElevation - dTangentLength * dPreSlope;
                arrOut[3 * i - 1, 1] = dElevation;
                arrOut[3 * i - 0, 1] = dElevation + dTangentLength * dNextSlope;
                arrOut[3 * i - 2, 2] = dRadius;
                arrOut[3 * i - 2, 3] = dPreSlope;
                arrOut[3 * i - 2, 4] = dNextSlope;
                arrOut[3 * i - 2, 5] = -1;
                arrOut[3 * i - 1, 2] = dRadius;
                arrOut[3 * i - 1, 3] = dPreSlope;
                arrOut[3 * i - 1, 4] = dNextSlope;
                arrOut[3 * i - 1, 5] = 0;
                arrOut[3 * i - 0, 2] = dRadius;
                arrOut[3 * i - 0, 3] = dPreSlope;
                arrOut[3 * i - 0, 4] = dNextSlope;
                arrOut[3 * i - 0, 5] = 1;
 
            }
 
            arrOut[iOutRowscount - 1, 0] = arrIn[iRowsCount - 1, 0];
            arrOut[iOutRowscount - 1, 1] = arrIn[iRowsCount - 1, 1];
            arrOut[iOutRowscount - 1, 2] = arrIn[iRowsCount - 1, 2];
            arrOut[iOutRowscount - 1, 3] = 0;
            arrOut[iOutRowscount - 1, 4] = 0;
            arrOut[iOutRowscount - 1, 5] = 0;
            return arrOut;
 
        }
        public static double[,] readData(string file_path)
        {
            try
            {
                //string[] lines = IO.File.ReadAllLines(file_path);
                string line;
                int counter = 0;
                int arrRowsCount = 0;
                char[] delimiterChars = { '\t' };
                StreamReader sr = new StreamReader(file_path);
                line = sr.ReadLine();
                arrRowsCount = int.Parse(line);
                //read the first line
                double[,] arr = new double[arrRowsCount, 3];
                //read the rest lines untill the end
                while (line != null && counter < arrRowsCount)
                {
                    line = sr.ReadLine();
                    Console.WriteLine(line);
                    string[] numbers = line.Split(delimiterChars);
                    arr[counter, 0] = double.Parse(numbers[0]);
                    arr[counter, 1] = double.Parse(numbers[1]);
                    arr[counter, 2] = double.Parse(numbers[2]);
                    counter++;
                }
                sr.Close();
                return arr;
                //Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                return null;
            }
            finally
            {
                //Console.WriteLine("Executing finally block.");
            }
 
        }
    }
}

  

posted on   风中狂笑  阅读(89)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示