C++之字符串分割案例---数据分析(1)

包含知识点:

函数的使用、指针变量的使用、字符串查找、子字符串截取、字符串长度等。

查看代码

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

bool findName(int subindex,string substring,string *name){
	subindex=substring.find("我叫");
	//cout<<subindex<<endl;
	//判断是否从 “我叫李宇博” 子字符串中找到 “我叫 ” 关键字 
	if( subindex>=0 && subindex <= substring.size()){	
		*name= substring.substr(subindex+4);		
		return true;
	}
	return false;
	
} 
bool findAge(int subindex,string substring,string *age){
	subindex=substring.find("我今年");
	//cout<<subindex<<endl;
	//判断是否从 “我今年13岁” 子字符串中找到 “我今年 ” 关键字 
	if( subindex>=0 && subindex <= substring.size()){	
		*age= substring.substr(subindex+6);		
		return true;
	}
	return false;	
} 
 int main(){
    // find 查找字符串出现的位置   include <string>
	// substr 截取字符串  include <string>
	// size 获取字符串的长度 include <string>
	// length 获取字符串的长度 include <string>
    string data="我叫李宇博,我今年13岁,我家住在不知道,今天是星期天,"
	"我喜欢吃粑粑,我喜欢做打篮球,我的学校是太康三中,我的生日是1月1号,"
	"我的语文成绩是:0分,我的数学成绩是1分,我今天做了核酸检测,我想去北京旅游,"
	"我中午想吃王颜博推荐的麻辣毛蛋,我晚上想吃黄大山推荐的麻辣毛蛋,我明天想吃刘佳兴爱吃的深山粑粑";
	cout<<data<<endl;
	//姓名 
	string name;
	//年龄
	string age; 
	/*
	1. 按照 , 进行分割 
	*/
	cout<<data.find(",")<<endl;
	//子字符串 
	string substring;
	//子字符串的位置 
	int subindex;
	//从整体的数据源里找到子字符串“我叫李宇博” 
	substring=data.substr(0,data.find(",")); 
	cout<<substring<<endl;		
	if(findName(subindex,substring,&name)){
		//是否  是姓名的数据
				 
	}else if(findAge(subindex,substring,&age)){
		
	}
	cout<<"姓名:" <<name<<endl;
	cout<<"年龄:" <<age<<endl;
	//大数据分析
	/*
	1.姓名:李宇博
	2.年龄:13
	3.家庭住址:不知道
	4.性别:男
	5.健康状态:健康
	6.学校:太康三中
	7.在学校表现如何:差
	8.喜欢吃的食物:粑粑
	9.喜欢做的事情:打篮球
	10.愿望是:去北京旅游
	11.今天中午要吃:王颜博推荐的麻辣毛蛋
	12.今天晚上要吃:黄大山推荐的麻辣毛蛋
	13.明天要吃:刘佳兴爱吃的深山粑粑
	*/
	
    return 1;

 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章