了解c++中的数据解析(供新程序员使用)
Understanding parsing of data in C++ (For a newer programmer)
我有以下格式的数据:(数据可以作为字符串输入,也可以从文件输入,我们选择,但我更喜欢字符串)
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;
无论您是解析字符串还是文件,如果您确定不会得到格式错误的输入,您都可以尝试使用相应的流类。
相关文章:
- 为什么程序员同时使用 std::bad_alloc 和 std::exception.是否 std::例外 仅是不够的
- C++std::atomic在程序员级别保证了什么
- 如何检查程序员在C++中提供的两种不同格式的输入
- 为什么 C++ 程序员更喜欢前缀 ++,而 Java 程序员更喜欢后缀 ++?
- 程序员可以用 C++ 编写多少个嵌套结构
- 如果/else语句未显示/可变分配问题的输出;初学者程序员
- 安装/理解c++库的问题(新手程序员)
- 初学者程序员 c++(局部与全局变量声明)
- 工资计算器(加班工资)需要高级程序员的帮助
- 是否有任何合理的场景,程序员可能希望避免布尔表达式的短路计算
- 面向C++程序员的 SQL 简介
- 我想知道根据程序员的哲学在哪里声明变量
- c#程序员在c++中尝试事件
- 新的Linux程序员 - 尝试开发库
- 有没有一种非重复的方法允许程序员在成员初始化的复制和移动语义之间进行选择
- 作为一名初级程序员,我该如何开始制作桌面应用程序
- 返回 1 与返回 0(用户和程序员)
- 视觉C++:我可以强制我的程序员为每个类定义复制构造函数吗?
- 使用OOP是否C++成为专业C++程序员的任务
- 为什么程序员说"pass by reference"真的很"passing references by value?"为什么这很重要?