编译原理之常数处理机(二)

Preset.h

#ifndef PRESET_H_INCLUDED
#define PRESET_H_INCLUDED

int Status[10][10]={{},{2,0,0,0,0},{2,3,5,0,8},{4,0,0,0,0},{4,0,5,0,8},{7,0,0,6,0},{7,0,0,0,0},{7,0,0,0,8},{0,0,0,0,0}};

int TranS(int t, char c);   //状态转移
void Act(int t,char c);     //当前状态动作

#endif // PRESET_H_INCLUDED

main.cpp

#include <iostream>
#include <cmath>
#include "Preset.h"

using namespace std;

int N,p,m,e;
double num;

int TranS(int t, char c);
void Act(int t,char c);

int main()
{
    string Str;
    //int a=2147483647;
    //cout << a;
    //a+=1;
    //cout << a;
    while(cout << "Input the num:" << endl,cin >> Str)
    {
        int s=1;
        char ch;
        int Pointer=0;
        while(s!=8 && s!=0)
        {
            Act(s,ch);
            ch=Str[Pointer++];
            s=TranS(s,ch);
        }
        if(s==8)
        {
            Act(s,ch);
            cout << "Your num is:" << endl << num << endl;
        }
        else cout << "error!" << endl;
    }
    return 0;
}

int TranS(int t, char c)
{
    switch(c)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        return Status[t][0];
    case '.':
        return Status[t][1];
    case 'e':
    case 'E':
        return Status[t][2];
    case '+':
    case '-':
        return Status[t][3];
    case 0:
        return Status[t][4];
    }
    return 0;
}

void Act(int t,char c)
{
    switch(t)
    {
    case 1:
        N=p=m=num=0;
        e=1;
        break;
    case 2:
        N=10*N+c-'0';
        break;
    case 4:
        N=10*N+c-'0';
        m++;
        break;
    //case 5:
       // e=1;
       // break;
    case 6:
        if(c=='-')e=-1;
        break;
    case 7:
        p=10*p+c-'0';
        break;
    case 8:
        num=N*pow(10,e*p-m);
        break;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值