如何判断数字
本帖最后由 bin554385863 于 2019-8-17 12:07 编辑#include <iostream>
#include <vector>
#include <cctype>
/*输入10个数字使用迭代器翻倍输出*/
int main(int argc, char const *argv[])
{
using namespace std;
vector<int> intvec;
int c = 0;
int num;
while (c != 10)
{
c++;
cin >> num;
/*
if()
{这里如何判断输入的是数字而不是其他字符,数字大小有可能超出Char类型的最大值}
*/
intvec.push_back((int)num);
}
for (vector<int>::iterator it = intvec.begin(); it != intvec.end(); it++)
{
*it = *it * 2;
cout << *it << " ";
}
return 0;
}
---------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-2li3xujz.b4x --stdout=Microsoft-MIEngine-Out-irxnjzh0.lkv --stderr=Microsoft-MIEngine-Error-mit5ew1z.1bb --pid=Microsoft-MIEngine-Pid-u3m1o4fz.0wb "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
1234 2345 32 1 2 3 4 5 6 7
2468 4690 64 2 4 6 8 10 12 14
E:\Users\86184\Documents\Code>
---------------------------------------------------------------------------------------------------------
c++;
cin >> num;
/*
if()
{这里如何判断输入的是数字而不是其他字符,数字大小有可能超出Char类型的最大值}
*/
intvec.push_back((int)num);
}
for (vector<int>::iterator it = intvec.begin(); it != intvec.end(); it++)
{
求解,isdigit()没卵用 lz理解错了,你以int的方式cin 那么接受到的肯定是int的,输入非int类型的会跳过的非法输入的
建议自己用ide试一试调试一次你就懂了 #include <iostream>
#include <vector>
#include <cctype>
#include <cmath>
/*判断字符串是不是一个整数*/
bool isnumber(std::string &strargs)
{
bool flag = true;
for (char cargs : strargs)
{
if (isdigit(cargs) == false)
{
flag = false;
break;
}
}
return flag;
}
/*将数字字符串转换成整数*/
std::vector<int> tonumber(std::string &strargs)
{
std::vector<int> vecargs;
const int size = strargs.size();
double result = 0;
for (size_t i = 0; i < size; i++)
{
result += (strargs - 48) * pow(10, size - 1 - i);
}
vecargs.push_back(result);
result = 0;
return vecargs;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <cctype>
#include <cmath>
#include "E:\Users\86184\Documents\Code\Study\0_0_0_MyC++func.cpp"
/*输入10个数字使用迭代器翻倍输出*/
int main(int argc, char const *argv[])
{
using namespace std;
double ynum = 0;
string wnum;
vector<int> intnum;
cout << "请输入数字" << endl;
while (true)
{
cin >> wnum;
if (isnumber(wnum) == false)
{
cout << "请不要输入其他字符" << endl;
continue;
}
intnum = tonumber(wnum);
vector<int>::iterator it = intnum.begin();
for (; it != intnum.end(); ++it)
{
*it = *it * 2;
}
for (int num : intnum)
{
cout << wnum << " X 2 = " << num << endl;
}
}
}
----------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-04zfharl.h0u --stdout=Microsoft-MIEngine-Out-kyram3nf.npq --stderr=Microsoft-MIEngine-Error-xe4arxc5.rb2 --pid=Microsoft-MIEngine-Pid-a2qdg1ow.uxz "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
请输入数字
a
请不要输入其他字符
+
请不要输入其他字符
12
12 X 2 = 24
1236
1236 X 2 = 2472
96544
96544 X 2 = 193088
E:\Users\86184\Documents\Code>
页:
[1]