输出正则向新文件C++
我正在使用正则表达式从名为grades.txt的文件中取出整数(随附列表,不,它们不是实际成绩)。当我执行 std::cout<< 时匹配[0] << std::endl; 代码按照文档中的顺序输出所有整数。但是,我需要将这些数字转换为整数,以便我可以读取它们并找到成绩最高的学生、成绩最低的学生以及班级的平均成绩,并将它们输出到新文件中。
示例:
Average grade: 280
John: 398
Tracy: 130
name grade_total
Scarlet 201
Gary 277
Shad 291
Kuame 219
Beau 263
Cecilia 269
Amir 301
Thane 396
Porter 267
Aimee 330
Wang 265
Arsenio 312
Vivien 233
Gwendolyn 349
Malcolm 285
Maia 248
Lamar 263
Kimberly 263
India 283
Hakeem 397
Raymond 165
Christopher 290
Xyla 279
Xandra 313
Barry 218
Yoko 327
Rama 280
Ciara 294
Mariko 360
Quinn 397
Laurel 386
Larissa 267
Lavinia 263
Amber 280
Reese 308
Phyllis 319
Rhona 217
Stella 339
Irma 323
Urielle 298
Ahmed 176
Jemima 237
Donovan 352
Eric 235
Akeem 350
Doris 288
Sawyer 329
David 283
Kato 300
Alma 294
Declan 309
Octavia 248
Nichole 218
Vance 217
Macaulay 312
Burton 239
Rhoda 367
Melyssa 288
Sebastian 326
Felix 384
Connor 422
Winifred 399
Camille 205
Ivana 285
Cameran 325
Lila 342
Shoshana 320
Amos 308
Alfonso 253
Carter 340
Roary 303
Amaya 212
Carolyn 332
Ebony 260
Idona 255
Paul 264
Selma 198
Salvador 213
Yeo 294
Derek 338
Myra 310
Fleur 356
Isabella 308
Zenaida 315
Noah 208
Dale 264
Lester 297
Cynthia 380
Robert 207
Hayfa 315
Blake 256
Cyrus 232
Ivy 360
Mason 231
Brielle 368
Amanda 303
Merritt 252
Aretha 225
Germaine 309
Dalton 397
Brenden 253
Uta 255
Juliet 318
Audrey 220
Francesca 359
Nathaniel 316
Zachary 350
Leila 299
Eve 238
Knox 348
Briar 359
Leroy 304
Kathleen 183
Jennifer 284
Delilah 265
Olympia 302
Nomlanga 303
Joseph 230
Carson 360
Alvin 262
Georgia 338
Uriel 303
Mari 185
Whilemina 396
Quail 330
Palmer 263
Alec 295
Quyn 324
Wyatt 333
Holly 344
Kiayada 346
May 387
Sigourney 257
Bruno 252
Timon 207
Linda 236
Omar 331
Brynne 182
Roth 303
Rashad 254
Sybill 348
Ulysses 305
Morgan 326
Sasha 282
Piper 414
Florence 272
Colt 343
Trevor 385
Hollee 365
Galena 287
Brent 298
Ali 332
Zephr 364
Zachery 321
Jerry 195
Chadwick 209
Joan 296
Brenda 283
Christine 336
Shelly 411
Vincent 301
Eleanor 305
Kameko 282
Jayme 302
Price 210
Yen 231
Cairo 335
Hashim 239
Charles 259
Chester 366
Riley 391
Elton 290
Chandler 222
Giacomo 343
Raja 256
Lars 256
Oren 405
Amal 266
Eaton 442
Venus 247
Patience 321
Haviva 293
Dylan 259
Basil 332
Finn 310
McKenzie 319
Malik 358
Vladimir 271
Davis 328
Macy 313
Hiroko 305
Mira 392
Zoe 271
Scarlet 329
Caesar 302
Harding 453
Abigail 379
Elmo 336
Aquila 265
Samuel 283
Ivan 279
Maxine 293
Reagan 250
Cade 310
Melvin 350
Cheryl 300
Adena 265
Garrison 295
Barclay 320
Kelsie 327
Quentin 358
Callie 332
Cooper 322
Gregory 255
Veda 345
Geoffrey 392
Althea 282
Risa 269
Sacha 347
Kitra 340
Cullen 242
Chancellor 332
Frances 356
Hall 338
Montana 298
Jesse 272
Cain 197
Elijah 283
Sylvester 333
Jacqueline 339
Gloria 334
Sade 280
Miriam 256
Reece 329
Daniel 319
Lane 311
Kennan 351
Ronan 291
MacKensie 224
Claudia 396
Kalia 295
Nigel 315
Walter 243
Hector 304
Chanda 224
Idola 347
Thaddeus 349
Wyoming 285
Andrew 283
Fatima 257
Sylvia 254
Cole 314
Ira 268
Molly 323
Lillian 309
Belle 328
Xander 371
Odessa 352
Wendy 360
Demetrius 261
Gisela 375
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <regex>
int main()
{
//Use Regex to pull only Integers from document
std::ifstream f;
f.open("grades.txt");
if(f.is_open())
{
std::string line;
getline(f, line);
while(getline(f,line))
{
std::stringstream ss(line);
//Pulls only the integers out of the string
std::smatch match;
std::regex re("\\d+");
regex_search(line, match, re);
while (regex_search(line, match, re))
{
std::cout << match[0] << std::endl;
line = match.suffix();
}
}
}
return 0;
}
我尝试将 match[0]
输出到另一个文件,以便我可以获取这些数字并使它们成为整数,但它只输出最后一个数字。
I am using regex to take the integers out of a file called grades.txt (attached list, no they're not actual grades). When I do std::cout<< match[0] <<std::endl;
the code outputs all integers in the order they were in the document. However I need to turn these numbers into integers so I can read them and find the student with the maximum grade, the student with the minimum grade and the average grade for the class and output these in a new file.
Example:
Average grade: 280
John: 398
Tracy: 130
name grade_total
Scarlet 201
Gary 277
Shad 291
Kuame 219
Beau 263
Cecilia 269
Amir 301
Thane 396
Porter 267
Aimee 330
Wang 265
Arsenio 312
Vivien 233
Gwendolyn 349
Malcolm 285
Maia 248
Lamar 263
Kimberly 263
India 283
Hakeem 397
Raymond 165
Christopher 290
Xyla 279
Xandra 313
Barry 218
Yoko 327
Rama 280
Ciara 294
Mariko 360
Quinn 397
Laurel 386
Larissa 267
Lavinia 263
Amber 280
Reese 308
Phyllis 319
Rhona 217
Stella 339
Irma 323
Urielle 298
Ahmed 176
Jemima 237
Donovan 352
Eric 235
Akeem 350
Doris 288
Sawyer 329
David 283
Kato 300
Alma 294
Declan 309
Octavia 248
Nichole 218
Vance 217
Macaulay 312
Burton 239
Rhoda 367
Melyssa 288
Sebastian 326
Felix 384
Connor 422
Winifred 399
Camille 205
Ivana 285
Cameran 325
Lila 342
Shoshana 320
Amos 308
Alfonso 253
Carter 340
Roary 303
Amaya 212
Carolyn 332
Ebony 260
Idona 255
Paul 264
Selma 198
Salvador 213
Yeo 294
Derek 338
Myra 310
Fleur 356
Isabella 308
Zenaida 315
Noah 208
Dale 264
Lester 297
Cynthia 380
Robert 207
Hayfa 315
Blake 256
Cyrus 232
Ivy 360
Mason 231
Brielle 368
Amanda 303
Merritt 252
Aretha 225
Germaine 309
Dalton 397
Brenden 253
Uta 255
Juliet 318
Audrey 220
Francesca 359
Nathaniel 316
Zachary 350
Leila 299
Eve 238
Knox 348
Briar 359
Leroy 304
Kathleen 183
Jennifer 284
Delilah 265
Olympia 302
Nomlanga 303
Joseph 230
Carson 360
Alvin 262
Georgia 338
Uriel 303
Mari 185
Whilemina 396
Quail 330
Palmer 263
Alec 295
Quyn 324
Wyatt 333
Holly 344
Kiayada 346
May 387
Sigourney 257
Bruno 252
Timon 207
Linda 236
Omar 331
Brynne 182
Roth 303
Rashad 254
Sybill 348
Ulysses 305
Morgan 326
Sasha 282
Piper 414
Florence 272
Colt 343
Trevor 385
Hollee 365
Galena 287
Brent 298
Ali 332
Zephr 364
Zachery 321
Jerry 195
Chadwick 209
Joan 296
Brenda 283
Christine 336
Shelly 411
Vincent 301
Eleanor 305
Kameko 282
Jayme 302
Price 210
Yen 231
Cairo 335
Hashim 239
Charles 259
Chester 366
Riley 391
Elton 290
Chandler 222
Giacomo 343
Raja 256
Lars 256
Oren 405
Amal 266
Eaton 442
Venus 247
Patience 321
Haviva 293
Dylan 259
Basil 332
Finn 310
McKenzie 319
Malik 358
Vladimir 271
Davis 328
Macy 313
Hiroko 305
Mira 392
Zoe 271
Scarlet 329
Caesar 302
Harding 453
Abigail 379
Elmo 336
Aquila 265
Samuel 283
Ivan 279
Maxine 293
Reagan 250
Cade 310
Melvin 350
Cheryl 300
Adena 265
Garrison 295
Barclay 320
Kelsie 327
Quentin 358
Callie 332
Cooper 322
Gregory 255
Veda 345
Geoffrey 392
Althea 282
Risa 269
Sacha 347
Kitra 340
Cullen 242
Chancellor 332
Frances 356
Hall 338
Montana 298
Jesse 272
Cain 197
Elijah 283
Sylvester 333
Jacqueline 339
Gloria 334
Sade 280
Miriam 256
Reece 329
Daniel 319
Lane 311
Kennan 351
Ronan 291
MacKensie 224
Claudia 396
Kalia 295
Nigel 315
Walter 243
Hector 304
Chanda 224
Idola 347
Thaddeus 349
Wyoming 285
Andrew 283
Fatima 257
Sylvia 254
Cole 314
Ira 268
Molly 323
Lillian 309
Belle 328
Xander 371
Odessa 352
Wendy 360
Demetrius 261
Gisela 375
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <regex>
int main()
{
//Use Regex to pull only Integers from document
std::ifstream f;
f.open("grades.txt");
if(f.is_open())
{
std::string line;
getline(f, line);
while(getline(f,line))
{
std::stringstream ss(line);
//Pulls only the integers out of the string
std::smatch match;
std::regex re("\\d+");
regex_search(line, match, re);
while (regex_search(line, match, re))
{
std::cout << match[0] << std::endl;
line = match.suffix();
}
}
}
return 0;
}
I have tried to output match[0]
to another file so I can take those numbers and make them integers but it only outputs the last number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++ 及其原理将为您提供帮助。
您的数据非常简单,您可以使用简单的
格式化输入函数读取任何行。 “name”将被转换为字符串,ant“score”将被转换为int。
接下来,您应该为您的学生设计一个类/结构。它只包含“名称”和“分数”。然后,您将覆盖提取运算符
>>
和插入运算符<<
。这非常简单,正如您在下面的示例代码中看到的那样。请注意:只有“Student”类/结构才知道如何读取或写入其数据。类/结构之外的任何函数都不应负责。这将允许稍后轻松修改提取/插入功能,而不影响外部功能。
对于许多“学生”,我们还将定义一个类/结构并使用相同的方法。
对于“Students”类/结构,我们还将添加所需的最小/最大/平均值函数。
所有这些看起来如下所示:
请注意。由于使用了格式化输入函数,因此仅当名称字段中没有空格时才有效。
如果有的话,那么我们只需修改“Student”类/结构的提取函数,其他一切都将继续工作。
使用此功能
将使事情变得更加健壮。
C++ and its principles will help you.
You data is that simple, that you can read any line with a simple
formatted input function. "name" will be converterted to a string ant "score" will be converted to an int.
Next, you should design a class/struct for your Student. It will just contain the "name" and a "score". Then, you will overwrite the extraction operator
>>
and the inserter operator<<
. That is very simple, as you can see in the example code below.Please note: Only the "Student" class/struct shall know how to read or write its data. No function outside the class/struct shall be responsible. This will allow for an easy modification of the extraction/insertion function later, without an effect on the outside functionality.
For many "Students" we will also define a class/struct and use the same approach.
For the "Students" class/struct we will also add the required min/max/average function.
All this can then look like the below:
Please note. Because of the usage of the formatted input function, this works only, as long as there are no spaces in the name field.
If there would be, then we could just modify the extraction function of the "Student" class/struct and everything else would continue to work.
Using this function
will make things more robust.