基于状态机的四则运算计算器

最近都在准备考试,所以没时间写博客了,今天刚考完试,就把之前写的一个四则运算计算器放上来

界面是用的QT画的,比较简单,就只把核心部分的代码放上来

由于想得比较简单,各个字符的输入转换成对应的数字


符号代号对应表
0 0
1~9 1~9
10 .
11,12 +-
13,14 */
15 =

int MainWindow::sign_to_int(QString input){
    if (input == "+") {
        return 11;
    }else if (input == "-") {
        return 12;
    }else if (input == "*") {
        return 13;
    }else if (input == "/") {
        return 14;
    }else if (input == "=") {
        return 15;
    }
}

QString MainWindow::int_to_QString(int input){
    if (input == 10) {
        return ".";
    }else if (input == 11) {
        return "+";
    }else if (input == 12) {
        return "-";
    }else if (input == 13) {
        return "*";
    }else if (input == 14) {
        return "/";
    }else if (input == 15) {
        return "=";
    }else {
        return QString::number(input);//int字符转QString
    }
}


这个计算器中的变量主要有4个,数一,数二,符号,结果,根据这几个变量设定状态


状态对应表
1表示该变量有值,0表示无值
状态 result number2 symbol number1
0 0 0 0 0
1 0 0 0 1
2 0 0 1 1
3 0 1 1 1


状态跳转表为


该状态LCD显示 输入后状态跳转
状态 display 0 1~9 . +- */ =
0 0 0 1 1 0 0 0
1 number1 1 1 1 2 2 输出result
2 number1 3 3 3 2 2 2
3 number2 3 3 3 3 3 输出result


代码为


QString MainWindow::compute(){
    number1 = numberstr1.right(5).toFloat();
    number2 = numberstr2.right(5).toFloat();
    switch (sign_to_int(symbol)) {
    case 11 : result = number1 + number2; break;
    case 12 : result = number1 - number2; break;
    case 13 : result = number1 * number2; break;
    case 14 :
        if (number2 == 0){
            return "EEEEE";
        }else{
            result = number1 / number2; break;
        }
    default : break;
    }
    return QString("%1").arg(result).left(5);

}

int MainWindow::handle_input(int sign)//该函数注意分case阅读,每一个case表示计算器的一种状态
{
    switch (now){
    case 0:
        if (sign == 0 || (sign >= 13 && sign <= 15)) {//input 0,*,/
            show_LCD("0");
            next = 0;
        }else if (sign >= 1 && sign <= 10) {//input 1~9,.
            if (sign == 10) {//如果输入为小数点.则将小数点标志位置1
                flag = 1;
                numberstr1 += int_to_QString(sign);
            }else{
                numberstr1 = int_to_QString(sign);
            }
            show_LCD(numberstr1);
            next = 1;
        }else {//输入+-则表示该数为正或者负
            numberstr1 = int_to_QString(sign);
            show_LCD(numberstr1);
            next = 1;
        }
        break;
    case 1:
        if (sign >= 0 && sign <= 10) {//输入0~10,.
            if (sign != 10 || flag == 0) {//如果输入的不是小数点或者小数点标志不为1,则直接在数后加上.
                if (sign == 10) {//如果输入为小数点,则将小数点标志置1
                    flag = 1;
                }
                numberstr1 += int_to_QString(sign);
            }
            show_LCD(numberstr1);//该状态显示number1
            next = 1;
        }else if (sign >= 11 && sign <= 14) {//参照状态跳转表
            symbol = int_to_QString(sign);
            show_LCD(numberstr1);
            next = 2;
        }else {//输入“=”,直接输出结果,初始化所有变量,准备下一次计算
            R = numberstr1;
            show_LCD(R);
            init_all();
        }
        break;
    case 2:
        flag = 0;
        if (sign == 13 || sign == 14) {//输入“*/”则直接给symbol赋值
            symbol = int_to_QString(sign);
            show_LCD(numberstr1);
            next = 2;
        }else if (sign == 15) {//输入“0,=”不改变状态
            show_LCD(numberstr1);
            next = 2;
        }else if (sign >= 0 && sign <= 10) {//与状态0相似,只不过number1换成了number2
            if (sign == 10) {
                flag = 1;
                numberstr2 += int_to_QString(sign);
            }else{
                numberstr2 = int_to_QString(sign);
            }
            show_LCD(numberstr2);
            next = 3;
        }else {
            numberstr2 = int_to_QString(sign);
            show_LCD(numberstr2);
            next = 3;
        }
        break;
    case 3:
        if (sign == 15) {//输入“=”,则输出结果
            R = compute();
            show_LCD(R);
            init_all();
        }else if(sign >= 11 && sign <= 14){
            show_LCD(numberstr2);
            next = 3;
        }else {//
            if (sign != 10 || flag == 0) {//与状态1相似,只不过number1换成了number2
                if (sign == 10) {
                    flag = 1;
                }
                numberstr2 += int_to_QString(sign);
            }
            show_LCD(numberstr2);
            next = 3;
        }
        break;
    default://输入出错
        show_LCD("FFFFF");
        init_all();
        break;
    }
    now = next;
    cout << "now = " << now << endl;
    return 0;
}


好了差不多就这样,也没什么时间改了,就之前花了几个小时写的,就当练练QT还有状态机吧

基于状态机的四则运算计算器_第1张图片



你可能感兴趣的:(数据结构)