PKU 3332 Parsing Real Numbers 解题

很烦人的字符串处理。
需要考虑的东西很多
用c++写比较麻烦。
自己用c++写的wa了3次才过。
java的话直接正则表达式就可以了
下面分别有c++和java的代码
c++的代码属于自己原创
java是转来别人的了

 1#include<stdio.h>
 2int main()
 3{
 4    int KASE,f;
 5    char ch;
 6    scanf("%d",&KASE);
 7    scanf("%c",&ch);
 8    while(KASE--)
 9    {
10        f=0;
11        while(scanf("%c",&ch))
12        {
13            if(ch==10)break;
14            while(ch==' ')
15            {
16                scanf("%c",&ch);
17                if(ch==10)break;
18            }

19            if(ch==10)break;
20            if(ch=='+'||ch=='-')scanf("%c",&ch);
21            if(ch>='0' && ch<='9')f=1;
22                    //    printf("%d\n",f);
23            while(ch>='0' && ch<='9')
24            {
25                scanf("%c",&ch);
26                if(ch==10)break;
27            }

28            if(ch==10)break;
29            if(!f)goto abc;
30            if(ch!='.' && ch!='e' && ch!='E'){f=0;goto abc;}
31            if(ch=='.')
32            {
33                //printf("asdasdas");
34                f=0;scanf("%c",&ch);
35                if(ch==10)break;
36                if(ch>='0' && ch<='9')f=1;
37                while(ch>='0' && ch<='9')
38                {
39                    scanf("%c",&ch);
40                    if(ch==10)break;
41                }

42            }

43            if(!f)goto abc;
44            if(ch==10)break;
45            if(ch!='e'&&ch!='E'){f=0;goto abc;}
46            if(ch=='e'||ch=='E')
47            {
48                f=0;scanf("%c",&ch);
49                if(ch==10)break;
50                if(ch=='+'||ch=='-')scanf("%c",&ch);
51                if(ch==10)break;
52                if(ch>='0' && ch<='9')f=1;
53                while(ch>='0' && ch<='9')
54                {
55                    scanf("%c",&ch);
56                    if(ch==10)break;
57                }

58                if(ch!=10)f=0;
59                if(ch==10)break;
60            }

61            if(ch==10)break;
62abc:        while(scanf("%c",&ch))if(ch==10)break;
63            if(ch==10)break;
64        }

65        if(f)printf("LEGAL\n");
66        else printf("ILLEGAL\n");
67    }

68    return 0;
69}

70


java代码转自http://hi.baidu.com/ecchi/blog/item/822201247e8a2d29d40742ad.html
 1import java.util.regex.*;
 2import java.util.*;
 3
 4public class Main {
 5      public static void main(String[] args){
 6          String str;
 7          int n;
 8          Scanner cin = new Scanner(System.in);
 9          n=cin.nextInt();
10          str=cin.nextLine();
11          Pattern pattern = Pattern.compile("(\\s)*(\\+|\\-)?(\\d)+(\\.(\\d)+)?((e|E)(\\+|\\-)?(\\d)+)?");
12          int i;
13          for(i=0;i< n;++i){
14              str=cin.nextLine();
15              Matcher matcher = pattern.matcher(str);
16              if(matcher.matches()){
17                  System.out.println("LEGAL");
18              }

19              else{
20                  System.out.println("ILLEGAL");
21              }

22          }

23      }

24}

25

posted on 2008-07-16 14:45 gong 阅读(349) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(6)

随笔档案

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜