【发布时间】:2016-08-17 05:48:41
【问题描述】:
所以我需要知道如何识别一行文本并输出它是什么样的数据类型,比如如果该行显示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+ 那么我们有一个双精度,如果它匹配 \d+$ 那么我们有一个 int
-
有一组无限的数字,可以是整数或浮点数。值 123 可以是浮点值或整数。有些算法使用小数点,所以 123 是整数,123. 是浮点数。一些实现需要科学记数法:1.23E+2。
标签: c++ string types int identify