zoukankan      html  css  js  c++  java
  • 返回数值类型

    代码
    ///////////////////////////////////////////////////////////////////////////////////////////
    // This program can determin very accurately the nature of the user input,
    // it detects whether it is an integer, a float, a number in scientific notation
    // or simply an invalid input. To be capable of doing this the program uses a simple FSM
    // (FINITE STATE MACHINE) to represent the possible states of the input.( INT, FLOAT,.. )
    // author: Gonzales Cenelia
    ///////////////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using std::cin;
    using std::endl;
    using std::cout;

    //===========================================================================
    // the list of all the possible states for the current FSM
    //===========================================================================
    enum STATE{ START, INT, FLOAT, SCIENTIFIC, EXPONENT, S1, S2, INVALID } state;

    STATE Transition(
    char *str );
    void PrintState( STATE state );

    int main() {
    // declaring buffer variable
    char buffer[32] = {0};
    // geting input from the user
    cout << "\nPlease enter a number: ";
    cin.getline( buffer,
    32 );
    // compute final state
    STATE FINAL_STATE = Transition(buffer);
    // prints the final state
    PrintState(FINAL_STATE);
    return 0;
    }

    //================================================
    // makes the transition from one state to another
    //================================================
    STATE Transition( char *str ) {
    int NEXT_SYMBOL;
    for( ; *str && state != INVALID; str++ ) {
    NEXT_SYMBOL
    = *str;
    switch(state) {
    case START:
    if(isdigit(NEXT_SYMBOL)) {
    state
    = INT;
    }
    else if( NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-' ) {
    state
    = S1;
    }
    else if( NEXT_SYMBOL == '.' ) {
    state
    = FLOAT;
    }
    else {
    state
    = INVALID;
    }
    break;
    case S1:
    if(isdigit(NEXT_SYMBOL)) {
    state
    = INT;
    }
    else if( NEXT_SYMBOL == '.' ) {
    state
    = FLOAT;
    }
    else if(!isdigit(NEXT_SYMBOL)) {
    state
    = INVALID;
    }
    break;
    case INT:
    if( NEXT_SYMBOL == '.' ) {
    state
    = FLOAT;
    }
    else if(!isdigit(NEXT_SYMBOL)) {
    state
    = INVALID;
    }
    break;
    case FLOAT:
    if( NEXT_SYMBOL == 'E' || NEXT_SYMBOL == 'e' ) {
    state
    = S2;
    }
    else if(!isdigit(NEXT_SYMBOL)) {
    state
    = INVALID;
    }
    break;
    case S2:
    if( NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-' ) {
    state
    = EXPONENT;
    }
    else {
    state
    = INVALID;
    }
    break;
    case EXPONENT:
    if(isdigit(NEXT_SYMBOL)) {
    state
    = SCIENTIFIC;
    }
    else {
    state
    = INVALID;
    }
    break;
    case SCIENTIFIC:
    if(!isdigit(NEXT_SYMBOL)) {
    state
    = INVALID;
    }
    break;
    }
    }
    return state;
    }

    //=====================================
    // prints the current state of the FSM
    //=====================================
    void PrintState( STATE state ) {
    cout
    << "\nFSM state: ";
    switch(state) {
    case INT:
    cout
    << "INT ";
    break;
    case FLOAT:
    cout
    << "FLOAT ";
    break;
    case SCIENTIFIC:
    cout
    << "SCIENTIFIC ";
    break;
    case INVALID:
    cout
    << "INVALID ";
    break;
    }
    }







  • 相关阅读:
    DevExpress 最最最基础踩坑(如何设置控件属性)
    Oracle如何创建数据库(使用图形化界面,顺便提一下UTF-8和ZB16GB字符集的问题)
    ElementUI el-dialog中嵌套子页面
    Oracle详细教程(触发器,事务,存储过程,锁,游标)
    和人事交谈下来的几点感悟
    Oracle Groupby分组缺少表达式解决方法
    leetcode973. 最接近原点的 K 个点(谈谈C#中的Icomparer,ComParer,IComparable)
    APP的闪退和无响应
    APP测试的主要内容
    python数据结构
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/1925039.html
Copyright © 2011-2022 走看看