In algebra, a decimal number can be defined as a number whose whole number part and the fractional part is separated by a decimal point. Write a C++ program to check if a given string is a decimal number or not

  • برمجة سي بلس بلس
  • برمجة

In algebra, a decimal number can be defined as a number whose whole number part and the fractional part is separated by a decimal point. Write a C++ program to check if a given string is a decimal number or not

In algebra, a decimal number can be defined as a number whose whole number part and the fractional part is separated by a decimal point. Write a C++ program to check if a given string is a decimal number or not.

List of characters of a valid decimal number:
Numbers: 0-9
Positive/negative sign - "+"/"-"
Decimal point - "."
Exponent - "e"

Sample Input: s = 9
Sample Output: Is 0 a decimal number? 1

Sample Input: s = abc 123
Sample Output: Is abc 123 a decimal number? 0

Sample Output:

 Is 0 a  decimal number? 1

 Is abc 123 a  decimal number? 0

 Is abc a  decimal number? 0
 Is 0.12 a  decimal number? 1

 Is 123.33 a  decimal number? 1

 Is 76.4e93 a  decimal number? 1

 Is +123 a  decimal number? 1

 Is +-33 a  decimal number? 0

الأجوبة

#include <iostream>

using namespace std;

bool isNumber(string str_num) {
    int i=0;
    int str_len = str_num.size();
    while (i<str_len && str_num[i]==' ')
        i++;
    if (i<str_len && (str_num[i]=='+' || str_num[i]=='-'))
        i++;
    int digits=0, dots=0;
    while (i<str_len && ((str_num[i]>='0' && str_num[i]<='9')||(str_num[i]=='.'))) {
        if (str_num[i]>='0' && str_num[i]<='9')
            digits++;
        else if (str_num[i] == '.')
            dots++;
        i++;
    }
    if (digits == 0 || dots>1)
        return false;
    if (i<str_len && str_num[i]=='e') {
        if (++i<str_len && (str_num[i]=='+' || str_num[i]=='-'))
            i++;
        if (i==str_len || (str_num[i]<'0' || str_num[i]>'9'))
            return false;
        while (i<str_len && (str_num[i]>='0' && str_num[i]<='9'))
            i++;
    }
    while (i<str_len && str_num[i]==' ')  //optional trailing spaces
        i++;
    return (i==str_len); //if at end of string then success
}
int main() {
    string s = "0";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    
    s = "abc 123";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    
    s = "abc";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s);
    s = "0.12";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    
    s = "123.33";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    
    s = "76.4e93";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    	    
    s = "+123";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    	    
    s = "+-33";
    cout << "\n Is " << s << " a  decimal number? " << isNumber(s) << endl;	    	    
    return 0;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...