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;

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