表格数据存储,只含有字母,数字和12个特殊字符,逗号分隔,输出每个字段
作者:互联网
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string zifu = "!@#$%^&*()\",";
bool istrue(string strr)
{
for (int i = 0; i < strr.size(); i++)
{
if (strr[i] >= 'a'&&strr[i] <= 'z')
{
}
else if (strr[i] >= 'A'&&strr[i] <= 'Z')
{
}
else if (strr[i] >= '0'&&strr[i] <= '9')
{
}
else
{
string::size_type idx;
idx = zifu.find(strr[i]);
if (idx == string::npos)
return false;
}
}
return true;
}
void func1(string str)
{
if (istrue(str))
{
vector<int> vfyin;
vector<int> danyin;
vector<int> dou;
vector<string> chuan;
vector<int>::iterator iter;
int lenth = str.length();
for (int i = 0; i < lenth; i++)
{
if (str[i] == '"')
{
vfyin.push_back(i);
}
}
for (int i = 1; i < vfyin.size() - 1; i++)
{
if ((vfyin[i + 1] - vfyin[i]) == 1)
{
danyin.push_back(vfyin[i]);
danyin.push_back(vfyin[i+1]);
i += 2;
}
}
for (iter = vfyin.begin(); iter != vfyin.end(); )
{
bool sta = false;
for (int i = 0; i < danyin.size(); i++)
{
if (*iter == danyin[i])
{
sta = true;
iter = vfyin.erase(iter);
}
}
if (sta == false)
iter++;
}
for (int i = 0; i < lenth; i++)
{
if (str[i] == ',')
{
bool stt = false;
for (int j = 0; j < vfyin.size()-1;j+=2)
{
if (i>vfyin[j] && i < vfyin[j + 1])
{
stt = true;
}
}
if (stt == false)
{
dou.push_back(i);
}
}
}
string strtemp;
strtemp = str.substr(0, dou[0]);
chuan.push_back(strtemp);
for (int i = 0; i < dou.size()-1; i++)
{
strtemp = str.substr(dou[i] + 1, dou[i + 1] - dou[i] - 1);
chuan.push_back(strtemp);
}
strtemp = str.substr(dou[dou.size() - 1]+1, lenth - dou[dou.size() - 1]);
chuan.push_back(strtemp);
cout << chuan.size() << endl;
for (int i = 0; i < chuan.size(); i++)
{
strtemp = chuan[i];
if (strtemp == "")
{
cout << "__" << endl;
}
else if (strtemp[0] == '"'&&strtemp[strtemp.length() - 1] == '"')
{
string temp;
for (int i = 1; i < strtemp.length() - 1; i++)
{
if (strtemp[i] == '"'&&strtemp[i + 1] == '"')
{
temp += strtemp[i];
i++;
}
else
{
temp += strtemp[i];
}
}
cout << temp<<endl;
}
else
{
cout << strtemp << endl;
}
}
}
else
{
cout << "ERROR" << endl;
}
}
int main()
{
int num;
/*while (cin >> num)
{
func(num);
}*/
string str;
cin >> str;
func1(str);
system("pause");
return 0;
}
标签:12,int,vfyin,逗号,++,strtemp,str,特殊字符,dou 来源: https://blog.csdn.net/qq_32091317/article/details/99634228