所以我需要知道如何识别一行文本并输出它是什么类型的数据类型,例如如果行说123
,它应该输出为123 int
。如何从文件中识别数据类型
现在,我的程序只识别boolean
,string
和char
。我如何得知它是否是int
或double
?
int main() {
string line;
string arr[30];
ifstream file("pp.txt");
if (file.is_open()){
for (int i = 0; i <= 4; i++) {
file >> arr[i];
cout << arr[i];
if (arr[i] == "true" || arr[i] == "false") {
cout << " boolean" << endl;
}
if (arr[i].length() == 1) {
cout << " character" << endl;
}
if (arr[i].length() > 1 && arr[i] != "true" && arr[i] != "false") {
cout << " string" << endl;
}
}
file.close();
}
else
cout << "Unable to open file";
system("pause");
}
感谢
你可以使用正则表达式?如果它匹配\ d + \。\ d +,那么我们有一个double,如果匹配\ d + $,那么我们有一个int – saml
有一个无限的数字集可以是整数或浮点数。值123可以是浮点值或整数。有些算法使用小数点,所以123是整数,123是浮点数。某些实现需要科学记数法:1.23E + 2。 –