質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/07/29 13:08

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -1,169 +1,169 @@
1
- 参考の質問は複数のcsvを読む作りですが、一度に複数読みたいのでしょうか?
2
-
3
- とりあえず1ファイルの読み込みで作りました。
4
- ファイル全部読むのに`StreamReader`は冗長なだけなので、もっと簡単な方法を使います。
5
-
6
- `Form1`には`button1`・`button2`・`label1`があるとします。
7
- それぞれのボタンにはクリックイベントを付けます。
8
-
9
- csvの例がないので適当にでっち上げましたが、配列のインデックス等は調整してください。
10
-
11
- 注意)エラー処理が一切ありません。
12
- ヘッダー以外にintにならない文字や途中の空行・データが足らない等があると、当然落ちます^^;
13
-
14
- ```C#
15
- using System;
16
- using System.Collections.Generic;
17
- using System.Data;
18
- using System.IO;
19
- using System.Linq;
20
- using System.Windows.Forms;
21
-
22
- namespace Questions374940
23
- {
24
- public partial class Form1 : Form
25
- {
26
- // 「別クラスからどうのこうの」ということがなければ、別にstaticにすることもない
27
- // フィールド(メンバ変数)は、button1_Clickからもbutton2_Clickからもアクセスできます
28
- private List<int[]> csvData = new List<int[]>();
29
-
30
- public Form1()
31
- {
32
- InitializeComponent();
33
- DummyData(); // test.csv作成
34
- }
35
-
36
- private void DummyData()
37
- {
38
- // 内容に特に意味はない(例がなかったので適当)
39
- string s = "No,Prime,Fibonacci\n1,2,0\n2,3,1\n3,5,1\n4,7,2\n5,11,3\n6,13,5\n7,17,8\n8,19,13\n9,23,21\n10,29,34\n";
40
-
41
- // ファイルに書き出し
42
- File.WriteAllText("test.csv", s);
43
- }
44
-
45
- // 読み込みボタン
46
- private void button1_Click(object sender, EventArgs e)
47
- {
48
- // ファイルを開くダイアログ
49
- OpenFileDialog openFileDialog = new OpenFileDialog
50
- {
51
- Filter = "CSVファイル(*.csv)|*.csv",
52
- };
53
-
54
- // ファイルを選ばなかったら処理終わり
55
- if (openFileDialog.ShowDialog() != DialogResult.OK) return;
56
-
57
- // 選んだファイルのパス
58
- string filePath = openFileDialog.FileName;
59
-
60
- // ファイルを1行ずつ読む(けど最初の行はとばす
61
- foreach (string line in File.ReadLines(filePath).Skip(1))
62
- {
63
- // 1行をカンマで分割
64
- string[] strArray = line.Split(',');
65
-
66
- // string[]からint[]に変換
67
- // [C# - C# string配列 を int配列 に変換したい(スッキリとしたコードで)|teratail](https://teratail.com/questions/48472)
68
- int[] intArray = strArray.Select(int.Parse).ToArray();
69
-
70
- // csvDataに追加
71
- csvData.Add(intArray);
72
- }
73
- }
74
-
75
- // 計算ボタン
76
- private void button2_Click(object sender, EventArgs e)
77
- {
78
- // 各行に No,Prime,Fibonacci とある前提
79
-
80
- int sumPrime = 0; // Primeの合計
81
- int sumFibonacci = 0; // Fibonacciの合計
82
-
83
- // csvDataを1件ずつループ
84
- foreach (int[] intArray in csvData)
85
- {
86
- sumPrime += intArray[1];
87
- sumFibonacci += intArray[2];
88
- }
89
-
90
- // 件数・合計の表示
91
- label1.Text = $"{csvData.Count} 件\nPrime合計: {sumPrime}\nFibonacci合計: {sumFibonacci}";
92
- }
93
- }
94
- }
95
- ```
96
- 説明多めにしたつもりですが、わからない点があればコメントしてください。
97
-
98
- ---
99
-
100
- 追記 各行数字ひとつ版
101
-
102
- ```C#
103
- using System;
104
- using System.Data;
105
- using System.IO;
106
- using System.Linq;
107
- using System.Windows.Forms;
108
-
109
- namespace Questions374940
110
- {
111
- public partial class Form1 : Form
112
- {
113
- // button2を先に押されるとエラーになるので無駄だが0個で作っとくw
114
- private int[] intArray = new int[0];
115
-
116
- public Form1()
117
- {
118
- InitializeComponent();
119
- DummyData();
120
- }
121
-
122
- private void DummyData()
123
- {
124
- string s = "13\n14\n34\n45\n45\n70\n46\n113\n133\n";
125
- File.WriteAllText("test.csv", s);
126
- }
127
-
128
- private void button1_Click(object sender, EventArgs e)
129
- {
130
- OpenFileDialog openFileDialog = new OpenFileDialog
131
- {
132
- Filter = "CSVファイル(*.csv)|*.csv",
133
- };
134
- if (openFileDialog.ShowDialog() != DialogResult.OK) return;
135
-
136
- string filePath = openFileDialog.FileName;
137
-
138
- // ファイルを全部読む(最後に改行があるとエラーになるので取っておく)
139
- string text = File.ReadAllText(filePath).Trim();
140
-
141
- // 空白文字(スペースや改行)で分割
142
- string[] strArray = text.Split();
143
-
144
- // string[]からint[]に変換
145
- intArray = strArray.Select(int.Parse).ToArray();
146
- }
147
-
148
- private void button2_Click(object sender, EventArgs e)
149
- {
150
- int sum = Hoge.Sum(intArray);
151
- label1.Text = $"{intArray.Length} 件\n合計: {sum}";
152
- }
153
-
154
- }
155
-
156
- class Hoge
157
- {
158
- public static int Sum(int[] intArray)
159
- {
160
- int sum = 0;
161
- foreach (int i in intArray)
162
- {
163
- sum += i;
164
- }
165
- return sum;
166
- }
167
- }
168
- }
1
+ 参考の質問は複数のcsvを読む作りですが、一度に複数読みたいのでしょうか?
2
+
3
+ とりあえず1ファイルの読み込みで作りました。
4
+ ファイル全部読むのに`StreamReader`は冗長なだけなので、もっと簡単な方法を使います。
5
+
6
+ `Form1`には`button1`・`button2`・`label1`があるとします。
7
+ それぞれのボタンにはクリックイベントを付けます。
8
+
9
+ csvの例がないので適当にでっち上げましたが、配列のインデックス等は調整してください。
10
+
11
+ 注意)エラー処理が一切ありません。
12
+ ヘッダー以外にintにならない文字や途中の空行・データが足らない等があると、当然落ちます^^;
13
+
14
+ ```cs
15
+ using System;
16
+ using System.Collections.Generic;
17
+ using System.Data;
18
+ using System.IO;
19
+ using System.Linq;
20
+ using System.Windows.Forms;
21
+
22
+ namespace Questions374940
23
+ {
24
+ public partial class Form1 : Form
25
+ {
26
+ // 「別クラスからどうのこうの」ということがなければ、別にstaticにすることもない
27
+ // フィールド(メンバ変数)は、button1_Clickからもbutton2_Clickからもアクセスできます
28
+ private List<int[]> csvData = new List<int[]>();
29
+
30
+ public Form1()
31
+ {
32
+ InitializeComponent();
33
+ DummyData(); // test.csv作成
34
+ }
35
+
36
+ private void DummyData()
37
+ {
38
+ // 内容に特に意味はない(例がなかったので適当)
39
+ string s = "No,Prime,Fibonacci\n1,2,0\n2,3,1\n3,5,1\n4,7,2\n5,11,3\n6,13,5\n7,17,8\n8,19,13\n9,23,21\n10,29,34\n";
40
+
41
+ // ファイルに書き出し
42
+ File.WriteAllText("test.csv", s);
43
+ }
44
+
45
+ // 読み込みボタン
46
+ private void button1_Click(object sender, EventArgs e)
47
+ {
48
+ // ファイルを開くダイアログ
49
+ OpenFileDialog openFileDialog = new OpenFileDialog
50
+ {
51
+ Filter = "CSVファイル(*.csv)|*.csv",
52
+ };
53
+
54
+ // ファイルを選ばなかったら処理終わり
55
+ if (openFileDialog.ShowDialog() != DialogResult.OK) return;
56
+
57
+ // 選んだファイルのパス
58
+ string filePath = openFileDialog.FileName;
59
+
60
+ // ファイルを1行ずつ読む(けど最初の行はとばす
61
+ foreach (string line in File.ReadLines(filePath).Skip(1))
62
+ {
63
+ // 1行をカンマで分割
64
+ string[] strArray = line.Split(',');
65
+
66
+ // string[]からint[]に変換
67
+ // [C# - C# string配列 を int配列 に変換したい(スッキリとしたコードで)|teratail](https://teratail.com/questions/48472)
68
+ int[] intArray = strArray.Select(int.Parse).ToArray();
69
+
70
+ // csvDataに追加
71
+ csvData.Add(intArray);
72
+ }
73
+ }
74
+
75
+ // 計算ボタン
76
+ private void button2_Click(object sender, EventArgs e)
77
+ {
78
+ // 各行に No,Prime,Fibonacci とある前提
79
+
80
+ int sumPrime = 0; // Primeの合計
81
+ int sumFibonacci = 0; // Fibonacciの合計
82
+
83
+ // csvDataを1件ずつループ
84
+ foreach (int[] intArray in csvData)
85
+ {
86
+ sumPrime += intArray[1];
87
+ sumFibonacci += intArray[2];
88
+ }
89
+
90
+ // 件数・合計の表示
91
+ label1.Text = $"{csvData.Count} 件\nPrime合計: {sumPrime}\nFibonacci合計: {sumFibonacci}";
92
+ }
93
+ }
94
+ }
95
+ ```
96
+ 説明多めにしたつもりですが、わからない点があればコメントしてください。
97
+
98
+ ---
99
+
100
+ 追記 各行数字ひとつ版
101
+
102
+ ```cs
103
+ using System;
104
+ using System.Data;
105
+ using System.IO;
106
+ using System.Linq;
107
+ using System.Windows.Forms;
108
+
109
+ namespace Questions374940
110
+ {
111
+ public partial class Form1 : Form
112
+ {
113
+ // button2を先に押されるとエラーになるので無駄だが0個で作っとくw
114
+ private int[] intArray = new int[0];
115
+
116
+ public Form1()
117
+ {
118
+ InitializeComponent();
119
+ DummyData();
120
+ }
121
+
122
+ private void DummyData()
123
+ {
124
+ string s = "13\n14\n34\n45\n45\n70\n46\n113\n133\n";
125
+ File.WriteAllText("test.csv", s);
126
+ }
127
+
128
+ private void button1_Click(object sender, EventArgs e)
129
+ {
130
+ OpenFileDialog openFileDialog = new OpenFileDialog
131
+ {
132
+ Filter = "CSVファイル(*.csv)|*.csv",
133
+ };
134
+ if (openFileDialog.ShowDialog() != DialogResult.OK) return;
135
+
136
+ string filePath = openFileDialog.FileName;
137
+
138
+ // ファイルを全部読む(最後に改行があるとエラーになるので取っておく)
139
+ string text = File.ReadAllText(filePath).Trim();
140
+
141
+ // 空白文字(スペースや改行)で分割
142
+ string[] strArray = text.Split();
143
+
144
+ // string[]からint[]に変換
145
+ intArray = strArray.Select(int.Parse).ToArray();
146
+ }
147
+
148
+ private void button2_Click(object sender, EventArgs e)
149
+ {
150
+ int sum = Hoge.Sum(intArray);
151
+ label1.Text = $"{intArray.Length} 件\n合計: {sum}";
152
+ }
153
+
154
+ }
155
+
156
+ class Hoge
157
+ {
158
+ public static int Sum(int[] intArray)
159
+ {
160
+ int sum = 0;
161
+ foreach (int i in intArray)
162
+ {
163
+ sum += i;
164
+ }
165
+ return sum;
166
+ }
167
+ }
168
+ }
169
169
  ```

1

各行数字ひとつ版

2021/12/24 12:09

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -93,4 +93,77 @@
93
93
  }
94
94
  }
95
95
  ```
96
- 説明多めにしたつもりですが、わからない点があればコメントしてください。
96
+ 説明多めにしたつもりですが、わからない点があればコメントしてください。
97
+
98
+ ---
99
+
100
+ 追記 各行数字ひとつ版
101
+
102
+ ```C#
103
+ using System;
104
+ using System.Data;
105
+ using System.IO;
106
+ using System.Linq;
107
+ using System.Windows.Forms;
108
+
109
+ namespace Questions374940
110
+ {
111
+ public partial class Form1 : Form
112
+ {
113
+ // button2を先に押されるとエラーになるので無駄だが0個で作っとくw
114
+ private int[] intArray = new int[0];
115
+
116
+ public Form1()
117
+ {
118
+ InitializeComponent();
119
+ DummyData();
120
+ }
121
+
122
+ private void DummyData()
123
+ {
124
+ string s = "13\n14\n34\n45\n45\n70\n46\n113\n133\n";
125
+ File.WriteAllText("test.csv", s);
126
+ }
127
+
128
+ private void button1_Click(object sender, EventArgs e)
129
+ {
130
+ OpenFileDialog openFileDialog = new OpenFileDialog
131
+ {
132
+ Filter = "CSVファイル(*.csv)|*.csv",
133
+ };
134
+ if (openFileDialog.ShowDialog() != DialogResult.OK) return;
135
+
136
+ string filePath = openFileDialog.FileName;
137
+
138
+ // ファイルを全部読む(最後に改行があるとエラーになるので取っておく)
139
+ string text = File.ReadAllText(filePath).Trim();
140
+
141
+ // 空白文字(スペースや改行)で分割
142
+ string[] strArray = text.Split();
143
+
144
+ // string[]からint[]に変換
145
+ intArray = strArray.Select(int.Parse).ToArray();
146
+ }
147
+
148
+ private void button2_Click(object sender, EventArgs e)
149
+ {
150
+ int sum = Hoge.Sum(intArray);
151
+ label1.Text = $"{intArray.Length} 件\n合計: {sum}";
152
+ }
153
+
154
+ }
155
+
156
+ class Hoge
157
+ {
158
+ public static int Sum(int[] intArray)
159
+ {
160
+ int sum = 0;
161
+ foreach (int i in intArray)
162
+ {
163
+ sum += i;
164
+ }
165
+ return sum;
166
+ }
167
+ }
168
+ }
169
+ ```