一道機試題

#include <iostream>
#include <string>
#include <cctype>
#include <regex>

using namespace std;

void end_check_2 (const string& str, unsigned& index) {
    if (index == str.size ()) {
        return;
    }
    else if ((str[index] == 'l' || str[index] == 'L') && index == str.size () - 1) {
        return;
    }
    else {
        throw (string (""));
    }
}

void end_check_1 (const string& str, unsigned& index) {
    if (index == str.size ()) {
        return;
    }
    else if (str[index] == 'u' || str[index] == 'U') {
        end_check_2 (str, ++index);
    }
    else
    {
        end_check_2 (str, index);
    }
}

void oct_check (const string& str, unsigned& index) {
    while ((str[index] >= '0' and str[index] <= '7')) {
        ++index;
    }
    end_check_1 (str, index);
}

void hex_check (const string& str, unsigned& index) {
    if (isdigit (str[index]) or (str[index] >= 'a' and str[index] <= 'f')) {
        ++index;
    }
    else {
        throw string ("");
    }
    while (isdigit (str[index]) or (str[index] >= 'a' and str[index] <= 'f')) {
        ++index;
    }
    end_check_1 (str, index);
}

void oct_hex_check (const string& str, unsigned& index) {
    if (str[index] == 'x' or str[index] == 'X') {
        hex_check (str, ++index);
    }
    else
        oct_check (str, ++index);
}

void dec_check (const string& str, unsigned& index) {
    while (isdigit (str[index])) {
        ++index;
    }
    end_check_1 (str, index);
}

void my_Check (const string& str, unsigned& index) {
    if (str[index] == '0') {
        ++index;
        oct_hex_check (str, index);
    }
    else if (isdigit (str[index])) {
        ++index;
        dec_check (str, index);
    }
    else {
        throw(string (""));
    }
}

bool numCheck (const string& str) {
    unsigned index = 0;
    try {
        my_Check (str, index);
    }
    catch (string&) {
        return false;
    }
    return true;
}

int main ()
{
    string str;
    while (getline (cin, str)) {
        cout << numCheck (str) << endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章