【问题标题】:Testing if a string is a number or character [duplicate]测试字符串是数字还是字符[重复]
【发布时间】:2012-11-04 20:28:26
【问题描述】:

可能重复:
How to determine if a string is a number with C++?

我用 C++ 编写了一个非常简单的计算器程序。这里是:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   double num1;
   double num2;
   string op;
   double num3;
   int x;
   bool y = false;

   do
   {
      cout<<"Press t to terminate the application"<<endl;

      cout<<"Enter the first number"<<endl;
      cin>>num1;

      cout<<"Enter the operator"<<endl;
      cin>>op;
      cout<<"Enter the next number"<<endl;
      cin>>num2;

      if(op=="/"&&num2==0)
      {
         cout<<"You are attempting to divide by 0. This is impossible and causes the destruction of the universe. However, the answer is infinity"<<endl;
         y = true;
      }

      if(y==false)
      {
         if(op=="+") {
            num3 = num1+num2;
         }
         else if(op=="-") {
            num3 = num1-num2;
         }
         else if(op=="*"||op=="x"||op=="X") {
            num3 = num1*num2;
         }
         else {
            num3 = num1/num2;
         }
         cout<<endl;
         cout<<endl;
         cout<<"Answer:"<<num3<<endl<<endl;
      }
   } while(x!=12);

   return 0;
}

如您所见,我想让人们通过按“t”来终止应用程序。这显然行不通,因为cin 会尝试将一个字母分配给double(如果我按下't',应用程序就会崩溃)。我打算改用字符串来获取输入,但是如何测试字符串是字母还是数字?

【问题讨论】:

  • 我会先检查它是否是“t”,然后使用boost::lexical cast 来检查它是否是一个数字。

标签: c++ calculator


【解决方案1】:
#include <cctype>

并在字符串内容上使用isalhpa()、isdigit()、isalnum()?

【讨论】:

  • 该方法是否检查它是否为数字?
  • 它检查传递的字符是字母、数字还是两者之一。为什么不在获得这些信息后立即查找它们?
  • 你似乎忽略了我想测试 string 而不是 char 的事实
  • @imulsion,您似乎忽略了“字符串内容”部分。
【解决方案2】:

这里是示例和工作代码,只需更改它以适合您的需求

#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>

using namespace std;

bool isNum(char *s) {
    int i = 0,  flag;

    while(s[i]){
            //if there is a letter in a string then string is not a number
        if(isalpha(s[i])){
            flag = 0;
            break;
        }
        else flag = 1;
        i++;
        }
    if (flag == 1) return true;
    else return false;
}


int main(){
    char stingnum1[80], stringnum2[80];
    double doublenum1, doublenum2;
    cin>>stingnum1>>stringnum2;
    if(isNum(stingnum1) && isNum(stringnum2)){
        doublenum1 = atof(stingnum1);
        doublenum2 = atof(stringnum2);
        cout<<doublenum1 + doublenum2 << endl;
    } 
    else cout<<"error";

   return 0;
}

【讨论】:

    【解决方案3】:

    你可以输入一个字符串,然后使用如下函数:

    int atoi ( const char * str );
    

    如果字符串是数字,它会将其转换为整数。

    如果字符串不是数字,它将返回 0:在这种情况下,您可以只检查字符串的第一个字符,如果它为零,则将输入视为 0。如果第一个字符不是零将字符串视为非数字。

    【讨论】:

    • 如果你想走这条路,最好strtol。
    【解决方案4】:

    好吧,如果你只检查“t”,你可以做到既愚蠢又简单的方法。

         if(stringnum1== 't' || stringnum2== 't') {
    
                //terminate
           }
     else {
               doublenum1 = atof(stringnum1)
               doublenum2 = atof(stringnum1)
               // your math operations
               }
    

    更好的方法是:

    if(isalhpa(stringnum1) || isalpha(stringnum2)){
                     //terminate
               }
          else {
               doublenum1 = atof(stringnum1)
               doublenum2 = atof(stringnum2)
               // your math operations
               }
    

    附:

    如果你想测试字符串而不是字符,这里是示例:link 最好的方法是让函数测试给定的字符串是否为数字,如果它是数字,则返回 true,否则返回 false(或其他一路走来)

    【讨论】:

    • 编译器错误:isalhpa 没有在这个范围内声明
    猜你喜欢
    • 2017-02-27
    • 2015-11-12
    • 1970-01-01
    • 2022-09-25
    • 2013-06-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多