#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[i] - 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>
|