【编译原理】无符号数的识别


#include<iostream>
#include<ctype.h>
#include<math.h>
#include<string>
using namespace std;

#define LETTER 0
#define DIGIT  1
#define POINT  2
#define OTHER  3
#define POWER  4
#define PLUS   5
#define MINUS  6
#define EndState -1

//12.34567e-123
int w;//1234567
int p;//123
int n;//5
int e;//-1
int d;//记录数字
double result;//记录最终结果

char* all[] = {
	"12","34.567","89.",".345",".","12E34","12e34","12E+34",
	"12e+34","12e-34","12.3E4","12.3e4","12.3E+4","12.3e+4",
	"12.3E-4","12.3e-4",".38E4",".3e45",".38E+4",".3e+45",
	".38E-4",".3e-45","3.E45","38.e4","3.E+45","38.e+4",
	"3.E-45","38.e-4"
};

int isNumber(char *w);//判断字符串w是不是无符号数
int getChar(int c);//判断当前字符是数字,点,Ee,+,-
void excute(int& CurrentState,int ch);//根据当前状态和当前字符类型来进入下一个状态

int main(){
	int i = -1;
	for(;++i<sizeof(all)/sizeof(char*);){
		cout<<all[i]<<" : ";
		if(isNumber(all[i])){
			cout<<"YES  ";
			cout<<result;
		}
		else{
			cout<<"NO";
		}
		cout<<endl;
	}
	return 0;
}

//判断字符串w是不是无符号数
int isNumber(char *w){
	string s(w);
	int i = -1;
	int ch;
	int currentState = 0;
	::w = 0;
	p = 0;//123
	n = 0;//5
	e = 1;//-1
	result = 0.0;
	for(;++i<s.size();){
		ch = getChar(s[i]);
		excute(currentState,ch);
		if(currentState == EndState){
			return 0;
		}
	}
	switch(currentState){
	case 1:
	case 2:
	case 6:
		result = ::w * pow(10,e*p-n);
		return 1;
	default:return 0;
	};

}

/**
 * 1. c是数字,返回1 
 * 2. c是点,返回2
 * 3. c是E,返回4
 * 4. c是+号,返回5
 * 5. c是-号,返回6
 */
//判断当前字符是数字,点,Ee,+,-
int getChar(int c){
	switch(c){
	case '.':return POINT;
	case 'E':
	case 'e':return POWER;
	case '+':return PLUS;
	case '-':return MINUS;
	default:
		if(isdigit(c)){
			d = c - 48;
			return DIGIT;
		}
		break;
	}
	return OTHER;
}

/**
 * 状态有
 * -1: EndState
 * 0 :
 *   读取 数字 -> 1
 *   读取 点   -> 3
 *
 * 1 :
 *   读取 数字 -> 1
 *   读取 '.'  -> 2
 *   读取 'E'  -> 4
 * 2 :
 *   读取 数字 -> 2
 *   读取 E    -> 4
 * 3:
 *   读取 数字 -> 2
 * 4:
 *   读取 +/-  -> 5
 *   读取 数字 -> 6
 * 5:
 *   读取 数字 -> 6
 * 6:
 *   读取 数字 -> 6
 */
//根据当前状态和当前字符类型来进入下一个状态
void excute(int& currentState,int ch){
	switch(currentState){
	case 0:
		switch(ch){
		case DIGIT:
			w = w * 10 + d;
			currentState = 1;
			break;
		case POINT:
			currentState = 3;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 1:
		switch(ch){
		case DIGIT:
			w = w * 10 + d;
			currentState = 1;
			break;
		case POINT:
			currentState = 2;
			break;
		case POWER:
			currentState = 4;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 2:
		switch(ch){
		case DIGIT:
			w = w * 10 + d;
			++n;
			currentState = 2;
			break;
		case POWER:
			currentState = 4;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 3:
		switch(ch){
		case DIGIT:
			w = w * 10 + d;
			++n;
			currentState = 2;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 4:
		switch(ch){
		case DIGIT:
			p = p * 10 + d;
			currentState = 6;
			break;
		case PLUS:
			currentState = 5;
			break;
		case MINUS:
			e = -1;
			currentState = 5;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 5:
		switch(ch){
		case DIGIT:
			p = p * 10 + d;
			currentState = 6;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	case 6:
		switch(ch){
		case DIGIT:
			p = p * 10 + d;
			currentState = 6;
			break;
		default:
			currentState = EndState;
			break;
		};
		break;
	};
}


第一列 待判定的数字字符串

第二列 是否为无符号数

第三列 输出double FCON = w * pow( 10 , e * p – n); 即将字符串转换为double




  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值