鱼C论坛

 找回密码
 立即注册
查看: 1773|回复: 2

如何判断数字

[复制链接]
发表于 2019-8-16 23:19:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 bin554385863 于 2019-8-17 12:07 编辑
  1. #include <iostream>
  2. #include <vector>
  3. #include <cctype>
  4. /*输入10个数字使用迭代器翻倍输出*/
  5. int main(int argc, char const *argv[])
  6. {
  7.     using namespace std;
  8.     vector<int> intvec;
  9.     int c = 0;
  10.     int num;
  11.     while (c != 10)
  12.     {
  13.         c++;
  14.         cin >> num;
  15.         /*
  16.         if()
  17.         {这里如何判断输入的是数字而不是其他字符,数字大小有可能超出Char类型的最大值}
  18.         */
  19.         intvec.push_back((int)num);  
  20.     }
  21.     for (vector<int>::iterator it = intvec.begin(); it != intvec.end(); it++)
  22.     {
  23.         *it = *it * 2;
  24.         cout << *it << " ";
  25.     }
  26.     return 0;
  27. }
复制代码

---------------------------------------------------------------------------------------------
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>

---------------------------------------------------------------------------------------------------------
  1.        c++;
  2.         cin >> num;
  3.         /*
  4.         if()
  5.         {这里如何判断输入的是数字而不是其他字符,数字大小有可能超出Char类型的最大值}
  6.         */
  7.         intvec.push_back((int)num);  
  8.     }
  9.     for (vector<int>::iterator it = intvec.begin(); it != intvec.end(); it++)
  10.     {
复制代码

求解,isdigit()没卵用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 23:36:01 | 显示全部楼层
lz理解错了,你以int的方式cin 那么接受到的肯定是int的,输入非int类型的会跳过的非法输入的
建议自己用ide试一试调试一次你就懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-17 03:16:48 | 显示全部楼层
  1. #include <iostream>
  2. #include <vector>
  3. #include <cctype>
  4. #include <cmath>
  5. /*判断字符串是不是一个整数*/
  6. bool isnumber(std::string &strargs)
  7. {
  8.     bool flag = true;
  9.     for (char cargs : strargs)
  10.     {
  11.         if (isdigit(cargs) == false)
  12.         {
  13.             flag = false;
  14.             break;
  15.         }
  16.     }
  17.     return flag;
  18. }
  19. /*将数字字符串转换成整数*/
  20. std::vector<int> tonumber(std::string &strargs)
  21. {
  22.     std::vector<int> vecargs;
  23.     const int size = strargs.size();
  24.     double result = 0;
  25.     for (size_t i = 0; i < size; i++)
  26.     {
  27.         result += (strargs[i] - 48) * pow(10, size - 1 - i);
  28.     }
  29.     vecargs.push_back(result);
  30.     result = 0;
  31.     return vecargs;
  32. }
复制代码

---------------------------------------------------------------------------------------------------
  1. #include <iostream>
  2. #include <vector>
  3. #include <cctype>
  4. #include <cmath>
  5. #include "E:\Users\86184\Documents\Code\Study\0_0_0_MyC++func.cpp"
  6. /*输入10个数字使用迭代器翻倍输出*/
  7. int main(int argc, char const *argv[])
  8. {
  9.     using namespace std;
  10.     double ynum = 0;
  11.     string wnum;
  12.     vector<int> intnum;
  13.     cout << "请输入数字" << endl;
  14.     while (true)
  15.     {
  16.         cin >> wnum;
  17.         if (isnumber(wnum) == false)
  18.         {
  19.             cout << "请不要输入其他字符" << endl;
  20.             continue;
  21.         }
  22.         intnum = tonumber(wnum);
  23.         vector<int>::iterator it = intnum.begin();
  24.         for (; it != intnum.end(); ++it)
  25.         {
  26.             *it = *it * 2;
  27.         }
  28.         for (int num : intnum)
  29.         {
  30.             cout << wnum << " X 2 = " << num << endl;
  31.         }
  32.     }
  33. }

复制代码

----------------------------------------------------------------------------------------------------------------------
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>

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 06:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表