首页 > 解决方案 > 从 .csv 文件中读取以在 c# 中查找 x,y 坐标数据集的所有峰和谷

问题描述

我需要从 csv 文件中的数据中返回所有的峰和谷。当我运行程序告诉我字符串格式不正确时,我不断收到异常。我不知道我写错了什么。我将值传递给 testX 和 testY,然后在我的 while 循环中比较它们。当我将 testY 转换为双精度时,while 循环是我得到错误的地方。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        double firstY = 0.0;

        string testX;
        string testY;

        string[] xpoint = new string[5000];
        string[] ypoint = new string[5000];

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var reader = new StreamReader(@"D:\data.csv"))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    var values = line.Split(',');

                    testX = values[0];
                    testY = values[1];

                    while (Convert.ToDouble(testY) <= firstY)
                    {
                        firstY = Convert.ToDouble(testY);

                        if (firstY == Convert.ToDouble(testY))
                        {
                            break;
                        }
                    }

                    if (Convert.ToDouble(testY) > firstY)
                    {
                        listBox1.Items.Add(Convert.ToDouble(testX) + "," + firstY);
                    }

                    while (Convert.ToDouble(testY) >= firstY)
                    {
                        firstY = Convert.ToDouble(testY);

                        if (firstY == Convert.ToDouble(testY))
                        {
                            break;
                        }
                    }

                    if (Convert.ToDouble(testY) < firstY)
                    {
                        listBox2.Items.Add(Convert.ToDouble(testX) + "," + firstY);
                    }

                    Convert.ToString(testX);
                    Convert.ToString(testY);
                }
            }
        }
    }
}

标签: c#

解决方案


推荐阅读