C#文件操作
2020年全国大学生数学建模竞赛D题接触式轮廓仪的自动标注。读取附件1的数据,完成问题1中的计算。在窗口中画出图形,并标注结果。必要时可以转换文件格式。
参考网址:
1、2020年数学建模题目,http://www.mcm.edu.cn/upload_cn/node/585/05k6B8WT92d3955f5c5e95dd086e59163e5f584b.rar
2、往年优秀论文展示,http://dxs.moe.gov.cn/zx/qkt/sxjm/lw/2019qgdxssxjmjslwzs/
第一个表格文件效果
第二个表格文件效果
代码
部分代码
.
// An highlighted block
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public struct Point
{
//定义一个点
public double X;
public double Y;
}
public double x;
public double r;
public double c;
public double z;
//private void button1_Click(object sender, EventArgs e)
public void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.Filter = "txt文件|*.txt|所有文件|*.*";
chart1.ChartAreas[0].AxisY.Minimum = -10.0;
chart1.ChartAreas[0].AxisY.Maximum = 2.0;
chart1.ChartAreas[0].AxisY.Interval = 0.5;
chart1.ChartAreas[0].AxisX.Minimum = 40;
chart1.ChartAreas[0].AxisX.Maximum = 120;
chart1.ChartAreas[0].AxisX.Interval = 10;
if(ofd.ShowDialog()==DialogResult.OK)
{
textBox1.Text = ofd.FileName;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string[] lines = File.ReadAllLines(textBox1.Text);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] v = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);//StringSplit0ptions.RemoveEmptyEntries
if (v[0] == "!")
{
continue;
}
else if (v[0] == "#")
{
break;
}
else
{
Point p;
p.X = double.Parse(v[0]);
p.Y = double.Parse(v[1]);
chart1.Series["Series1"].Points.AddXY(p.X, p.Y);
chart1.Series["Series2"].Points.AddXY(p.X, -3.3);
//X
if (i == 6300) { x = p.X; }
if (i == 12330) {
x = p.X - x;
x1.Text = Convert.ToString(x);
R1.Text = Convert.ToString(x/8);
}
if (i == 12330) { x = p.X; }
if (i == 21900)
{
x = p.X - x;
x2.Text = Convert.ToString(x);
}
if (i == 21900) { x = p.X; }
if (i == 27100)
{
x = p.X - x;
x3.Text = Convert.ToString(x);
R2.Text = Convert.ToString(x / 8);
}
if (i == 27100) { x = p.X; }
if (i == 36080)
{
x = p.X - x;
x4.Text = Convert.ToString(x);
}
if (i == 36080) { x = p.X; }
if (i == 40420)
{
x = p.X - x;
x5.Text = Convert.ToString(x);
R3.Text = Convert.ToString(x / 8);
}
if (i == 40420) { x = p.X; }
if (i == 49100)
{
x = p.X - x;
x6.Text = Convert.ToString(x);
}
if (i == 71000) { x = p.X; }
if (i == 76300)
{
x = p.X - x;
x8.Text = Convert.ToString(x);
}
if (i == 76300) { x = p.X; }
if (i == 80100)
{
x = p.X - x;
R4.Text = Convert.ToString(x/2);
}
if (i == 80100) { x = p.X; }
if (i == 81500)
{
x = p.X - x;
x9.Text = Convert.ToString(x);
}
if (i == 81500) { x = p.X; }
if (i == 86000)
{
x = p.X - x;
R5.Text = Convert.ToString(x / 2);
}
if (i == 86000) { x = p.X; }
if (i == 94300)
{
x = p.X - x;
x10.Text = Convert.ToString(x);
}
if (i == 94300) { x = p.X; }
if (i == 111200)
{
x = p.X - x;
R6.Text = Convert.ToString(x/2);
}
if (i == 111200) { x = p.X; }
if (i == 119000)
{
x = p.X - x;
x12.Text = Convert.ToString(x);
}
if (i == 119000) { x = p.X; }
if (i == 135000)
{
x = p.X - x;
R7.Text = Convert.ToString(x/2);
}
if (i ==9300 ) { c = p.X; }
if (i == 24500)
{
c = p.X - c;
c1.Text = Convert.ToString(c);
}
if (i ==24500 ) { c = p.X; }
if (i ==38000 )
{
c = p.X - c;
c2.Text = Convert.ToString(c);
}
if (i == 38000) { c = p.X; }
if (i == 78000)
{
c = p.X - c;
c3.Text = Convert.ToString(c);
}
if (i == 78000) { c = p.X; }
if (i == 84000)
{
c = p.X - c;
c4.Text = Convert.ToString(c);
}
if (i == 84000) { c = p.X; }
if (i == 102000)
{
c = p.X - c;
c5.Text = Convert.ToString(c);
}
if (i == 102000) { c = p.X; }
if (i == 127000)
{
c = p.X - c;
c6.Text = Convert.ToString(c);
}
if (i == 60700) { z = p.Y; }
if (i == 49100)
{
z = z-p.Y;
z1.Text = Convert.ToString(z);
}
}
}
}
}
}
#转载请注明原作者,谢谢!