C++ 理解C+中的数据解析+;(对于较新的程序员)
我有以下格式的数据: (数据可以作为字符串输入,也可以从文件输入,这是我们的选择,但我更喜欢字符串) 姓名姓氏编号日期持续时间 例如: John Doe 5593711872 09122010 12 我需要解析这些数据,然后将其放入一个“Contacts”类中,每个条目一个 然后我需要将其中的5个条目放入堆栈中,并将它们打印出来 我知道如何处理它,但如何解析这些数据并将其拆分为相关部分 (编辑,添加我所拥有的小代码片段。我希望更多的是理解而不是代码,或者这不是这个网站的工作方式?) 联系人:h:C++ 理解C+中的数据解析+;(对于较新的程序员),c++,string,parsing,stack,C++,String,Parsing,Stack,我有以下格式的数据: (数据可以作为字符串输入,也可以从文件输入,这是我们的选择,但我更喜欢字符串) 姓名姓氏编号日期持续时间 例如: John Doe 5593711872 09122010 12 我需要解析这些数据,然后将其放入一个“Contacts”类中,每个条目一个 然后我需要将其中的5个条目放入堆栈中,并将它们打印出来 我知道如何处理它,但如何解析这些数据并将其拆分为相关部分 (编辑,添加我所拥有的小代码片段。我希望更多的是理解而不是代码,或者这不是这个网站的工作方式?) 联系人: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;
};
#如果没有联系#
#定义联系人
#include//这是我们需要“重载”和中断的一组函数
使用std::ostream//使用两个输出部分
使用stf::istream//和输入一半
#包括
使用std::string;
班级联系
{
friend ostream&operator(istream&,Contact&);//同样来自powerpoint 3中的示例11.3
私人:
先串;
最后一串;
字符串编号;
字符串日期;
通话时间;
};
电话
#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
\ifndef电话
#定义电话号码
#include//这是我们需要“重载”和中断的一组函数
使用std::ostream//使用两个输出部分
使用stf::istream//和输入一半
#包括
使用std::string;
班级电话
{
friend ostream&operator(istream&电话&);
私人:
字符串区域;//区号段
string threes;//然后是三位数部分
string fours;//最后是四位数部分
}; //电话课到此结束
日期: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;
}
#如果取消日期#
#定义日期
#include//这是我们需要“重载”和中断的一组函数
使用std::ostream//使用两个输出部分
使用stf::istream//和输入一半
#包括
使用std::string;
上课日期
{
friend ostream&operator(istream&,Date&);//也来自powerpoint 3中的示例11.3
私人:
弦月;
弦日;
弦年;
}; //这就结束了日期存储类
main.cpp
#包括
#包括
#包括
#包括
#包括“contact.h”
#包括“phone.h”
#包括“date.h”
int main()
{
//这应该是抓取数字,然后我需要一些东西抓取文本并将其拆分
字符行_one[]=“Alex Liu 5592780000 061820115”;
char*pEnd;
长整数电话号码、日期、分钟;
电话号码=strtol(第1行和第10行);
日期=标准(待决,待决,10);
分钟=strtol(待定,空,10);
printf(“我们得到的号码是:%ld,%ld,%ld.\n”,电话号码,日期,分钟);
返回0;
}
strtol是这里使用的错误函数吗
是的,这是家庭作业的一部分。请不要以为我在找免费的施舍,我真的不想学这个。谢谢,伙计们(还有女孩!)无论你解析字符串还是文件,如果你确定没有收到格式错误的输入,你都可以尝试使用相应的流类。无论你解析字符串还是文件,你都可以试着使用相应的流类,如果您确定没有收到格式错误的输入。那么基本上您希望读取两个字符串和三个整数。在C++中实现这一点的基本方法是使用流:
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\u stream
可以是从作为输入的字符串初始化的std::istringstream
,也可以是读取文件内容的std::ifstream
。因此基本上您需要读取两个字符串和三个整数。在C++中实现这一点的基本方法是使用流:
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\u stream
可以是从作为输入的字符串初始化的std::istringstream
,也可以是读取文件内容的std::ifstream
。解析的简单方法是使用:
解析的简单方法是使用:
你有代码让我们使用吗?如果你没有给我们任何东西,那就是免费的施舍。你有什么代码让我们使用吗?如果你不给我们任何东西,那它就是免费分发的。你确定
5593711872
会适合一个整数吗?@cnicutar:好的,我会修正它。+1,虽然如果你提到格式错误的输入处理/检测会很好。你确定5593711872
会适合一个整数吗?@cnicutar:好的,我会修正它。+1,不过,如果您提到格式错误的输入处理/检测,那就太好了