A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/premierconsult/public_html/index.php:65)

Filename: core/Input.php

Line Number: 408

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cookie - headers already sent by (output started at /home/premierconsult/public_html/index.php:65)

Filename: Session/Session.php

Line Number: 143

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/premierconsult/public_html/index.php:65)

Filename: Session/Session.php

Line Number: 143

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/premierconsult/public_html/index.php:65)

Filename: controllers/Questions.php

Line Number: 330

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

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;
}
هل كان المحتوى مفيد؟

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

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