了解c++中的数据解析(供新程序员使用)

Understanding parsing of data in C++ (For a newer programmer)

本文关键字:程序员 c++ 数据 了解      更新时间:2023-10-16

我有以下格式的数据:(数据可以作为字符串输入,也可以从文件输入,我们选择,但我更喜欢字符串)

First_Name Last_Name Number日期持续时间

如:无名氏5593711872 09122010 12

我需要解析这些数据,然后将其放入"Contacts"类中,每个条目一个。

然后我需要将其中的5个条目放入堆栈中,并将它们打印出来。

我知道如何处理它,但是我如何解析并将这些数据分割成相关的部分呢?

(编辑,添加什么小代码片段,我已经得到。我正在寻找更多的理解而不是代码,或者这不是这个网站的工作方式?)

contact.h:

 #ifndef contact_h
#define contact_h
#include <iostream> //this is the set of functions we need to "overload" and break out
using std::ostream; //use both output halves
using stf::istream; //and input halves
#include <string>
using std::string;

class Contact
{
      friend ostream &operator<<( ostream &, const Contact & ); //from example 11.3 in powerpoint 3
      friend ostream &operator>>( istream &, Contact & ); //also from example 11.3 in powerpoint 3
private:
        string first;
        string last;
        string number;
        string date;
        int talk_time;
};

Phone.h

#ifndef phone_h
#define phone_h
#include <iostream> //this is the set of functions we need to "overload" and break out
using std::ostream; //use both output halves
using stf::istream; //and input halves
#include <string>
using std::string;
class Phone
{
      friend ostream &operator<<( ostream &, const Phone & ); 
      friend ostream &operator>>( istream &, Phone & ); 
private:
        string area; //area code segment
        string threes; //then the three-digit section
        string fours; //and lastly the four-digit section
};  //and this wraps up the phone class

date.h

#ifndef date_h
#define date_h
#include <iostream> //this is the set of functions we need to "overload" and break out
using std::ostream; //use both output halves
using stf::istream; //and input halves
#include <string>
using std::string;

class Date
{
      friend ostream &operator<<( ostream &, const Date & ); //from example 11.3 in powerpoint 3
      friend ostream &operator>>( istream &, Date & ); //also from example 11.3 in powerpoint 3
private:
        string month;
        string day;
        string year;
};  //and this wraps up the date storage class
main.cpp
    #include <iomanip>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "contact.h"
    #include "phone.h"
    #include "date.h"
    int main()
    {
//this is supposed to grab the numbers, and then i need something to grab the text and split it up
      char line_one[] = "Alex Liu 5592780000 06182011 15";
      char * pEnd;
      long int phone_number, date, minutes;
      phone_number = strtol (line_one,&pEnd,10);
      date = strtol (pEnd,&pEnd,10);
      minutes = strtol (pEnd,NULL,10);
      printf ("The numbers we get are: %ld, %ld, %ld.n", phone_number, date, minutes);
      return 0;
    }

在这里使用strtol是错误的函数吗?

是的,这是家庭作业的一部分。请不要以为我在找免费的讲义,我真的很想学这个。谢谢大家!

基本上你想读取两个字符串和三个整数。在c++中实现这一点的基本方法是使用streams:

std::string first_name, last_name;
std::int64_t number;
std::int32_t date, duration;
input_stream
    >> first_name >> last_name
    >> number >> date >> duration;

其中input_stream可以是从输入字符串初始化的std::istringstream,也可以是读取文件内容的std::ifstream

简单的解析方法是使用streams:

std::ifstream fin("my_file.txt");
std::string name1, name2;
int number1, number2, number3;
fin >> name1 >> name2 >> number1 >> number2 >> number3;

无论您是解析字符串还是文件,如果您确定不会得到格式错误的输入,您都可以尝试使用相应的流类。