poj 3332-判断字符串是否合法


C - Parsing Real Numbers
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Write a program that read a line of input and checks if the line contains a valid real number. Real numbers may have a decimal point, an exponent (starting with the character e or E), or both. Additionally, it has the usual collection of decimal digits. If there is a decimal point, there must be at least one digit on each side of the point. There may be a plus or minus sign in front of the number, or the exponent, or both (without any blank characters after the sign). Exponents are integers (not having decimal points). There may be blank characters before or after a number, but not inside it. Note that there is no bound on the range of the numbers in the input, but for the sake of simplicity, you may assume the input strings are not longer than 1000 characters.

Input

The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the input line for a test case.

Output

The output contains T lines, each having a string which is LEGAL or ILLEGAL.

Sample Input

2
  1.5e+2
3.

Sample Output

LEGAL
ILLEGAL

模拟:


#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdio.h>
#include <ctype.h>

using namespace std;

char s[1200];
int main() {
	int T;
	scanf("%d", &T);
	while (T --) {
		scanf(" %s", s);
		if(s[0] == '+' || s[0] == '-' || isdigit(s[0])) {
			int c1 = 0;
			int c2 = 0;
			int i;
			for (i = 1; s[i] != '\0'; i ++) {
				if(s[i] == '.') {
					c1 ++;
					if(!isdigit(s[i - 1]) || !isdigit(s[i + 1])) c1 = 2; 
					if(c1 > 1 || c2 > 0) {
						c1 = 2;
						break;
					}
				}
				else if(s[i] == 'E' || s[i] == 'e') {
					c2 ++;
					if(!isdigit(s[i-1]))
						c2 = 2;
					if(c2 > 1) {
						c1 = 2;
						break;
					}
				}
				else if(!isdigit(s[i])) {
					if(!isdigit(s[i-1]) && s[i-1] != '.'&& s[i-1] != 'E' && s[i-1]!='e') {
						c1 = 2;
						break;
					}
				}
			}
			if(!isdigit(s[i-1]))
				c1 = 2;
			if(c1==2)
				puts("ILLEGAL");
			else
				puts("LEGAL");
		}
		else
			puts("ILLEGAL");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值