[英]How to check whether an input is integer, string or float in C++
我想知道的是,如果我要求用戶輸入一些東西,如果輸入是整數、字符串或浮點值,我將如何輸出。 我想要一些方法來檢查 C++14 中輸入的數據類型。
例如。
如果輸入是“Hello world”
輸出應該是:“輸入是字符串”
如果輸入是“134”
輸出應該是:“輸入是整數”
如果輸入是“133.23”
輸出應為:“輸入為浮點數”
讀取字符串。
在
<string><\/code>中,標准庫提供了一組函數,用於從字符串或 wstring 中的字符表示中提取數值。
使用
x=stoi(s,p)<\/code> 。
檢查
p<\/code> - 如果整個字符串被讀取 - 它是整數。
對
x=stof(s,p)<\/code>或
x=stod(s,p)<\/code> 、
x=stold(s,p)<\/code>執行相同操作以檢查 float\/double\/long double。
如果一切都失敗了 - 它是字符串。
用戶將始終輸入一個字符串,您可以嘗試將其轉換為浮點數,如果成功則可能是浮點數或整數。 如果浮點轉換不成功,那么它可能不是數字。
"#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <sstream>
using myvariant = boost::variant<int, float, std::string>;
struct emit : boost::static_visitor<void>
{
void operator()(int i) const {
std::cout << "It's an int: " << i << '\n';
}
void operator()(float f) const {
std::cout << "It's a float: " << f << '\n';
}
void operator()(std::string const& s) const {
std::cout << "It's a string: " << s << '\n';
}
};
auto parse(const std::string& s) -> myvariant
{
char* p = nullptr;
auto i = std::strtol(s.data(), &p, 10);
if (p == s.data() + s.size())
return int(i);
auto f = std::strtof(s.data(), &p);
if (p == s.data() + s.size())
return f;
return s;
}
void test(const std::string& s)
{
auto val = parse(s);
boost::apply_visitor(emit(), val);
}
int main()
{
test("Hello world");
test("134");
test("133.23");
}
輸入是一個字符串。 如果沒有額外的約定,你怎么可能知道用戶想要 "1" 是包含字符 '1' 的字符串還是整數 1 的字符串表示形式?
如果你決定“如果它可以被解釋為一個 int,那么它就是一個 int。如果它可以是一個雙精度,那么它就是一個雙精度。否則它是一個字符串”,那么你可以只做一系列的轉換,直到一個工作,或進行一些格式檢查,也許使用正則表達式。
由於所有整數都可以轉換為雙精度,並且如果您關心差異,則雙精度的字符串表示可以轉換為整數(可能還有一些垃圾),您可能需要檢查它是否是雙精度的指示符(數字可能一個 . 在其中,可能是一個“e”,后面可能有 +\/-。等等。您可以在 Internet 上找到正則表達式,具體取決於您要允許的內容、前導 +、電子符號等。
如果是 int,則可以使用正則表達式 ^\\d+$,否則如果是 double,則 [+-]?(?:0|[1-9]\\d*)(?:.\\d*)?(? :[eE][+-]?\\d+)? 否則它是一個字符串。
這是一些似乎有效的代碼。 :)
#include <iostream>
#include <string>
#include <regex>
using namespace std;
void handleDouble(double d) {
std::cout << "Double = " << d << "\n";
}
void handleInt(int i) {
std::cout << "Int = " << i << "\n";
}
void handleString(std::string const & s) {
std::cout << "String = " << s << "\n";
}
void parse(std::string const& input) {
static const std::regex doubleRegex{ R"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)" };
static const std::regex intRegex{ R"(\d+)"};
if (std::regex_match(input, intRegex)){
istringstream inputStream(input);
int i;
inputStream >> i;
handleInt(i);
}
else if (std::regex_match(input, doubleRegex)) {
istringstream inputStream(input);
double d;
inputStream >> d;
handleDouble(d);
}
else {
handleString(input);
}
}
int main()
{
parse("+4.234e10");
parse("1");
parse("1.0");
parse("123abc");
}
您可以輕松地將
string.strof<\/code>包裝為
float<\/code>或
string.stroi<\/code>包裝為
int<\/code>在 try-catch 塊中:
#include <string>
bool isFloat(string str) {
try {
float floatCheck = stof(str);
return true;
}
catch (...) {
return false;
}
}
bool isInt(string str) {
try {
int intCheck = stoi(str);
return true;
}
catch (...) {
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.