要求识别非标准格式的浮点数
时间: 2023-10-09 11:08:17 浏览: 36
对于非标准格式的浮点数,我们可以使用有限状态机来进行识别。以下是一个可以识别多种浮点数表示方法的示例代码:
```c
#include <stdio.h>
#include <ctype.h>
// 定义状态类型
typedef enum {
START,
SIGN,
INT,
DOT,
DEC,
EXP,
EXP_SIGN,
EXP_INT,
END
} State;
int main() {
char input[100];
int i = 0;
State state = START;
printf("请输入一个字符串:");
scanf("%s", input);
while (input[i] != '\0') {
switch (state) {
case START:
if (input[i] == '+' || input[i] == '-') {
state = SIGN;
} else if (isdigit(input[i])) {
state = INT;
} else {
printf("输入不合法!\n");
return 0;
}
break;
case SIGN:
if (isdigit(input[i])) {
state = INT;
} else {
printf("输入不合法!\n");
return 0;
}
break;
case INT:
if (isdigit(input[i])) {
i++;
} else if (input[i] == '.') {
state = DOT;
i++;
} else if (input[i] == 'e' || input[i] == 'E') {
state = EXP;
i++;
} else {
state = END;
}
break;
case DOT:
if (isdigit(input[i])) {
state = DEC;
i++;
} else {
printf("输入不合法!\n");
return 0;
}
break;
case DEC:
if (isdigit(input[i])) {
i++;
} else if (input[i] == 'e' || input[i] == 'E') {
state = EXP;
i++;
} else {
state = END;
}
break;
case EXP:
if (input[i] == '+' || input[i] == '-') {
state = EXP_SIGN;
i++;
} else if (isdigit(input[i])) {
state = EXP_INT;
i++;
} else {
printf("输入不合法!\n");
return 0;
}
break;
case EXP_SIGN:
if (isdigit(input[i])) {
state = EXP_INT;
i++;
} else {
printf("输入不合法!\n");
return 0;
}
break;
case EXP_INT:
if (isdigit(input[i])) {
i++;
} else {
state = END;
}
break;
default:
printf("输入不合法!\n");
return 0;
}
}
if (state == INT || state == DOT || state == DEC || state == EXP_INT) {
printf("输入合法!\n");
} else {
printf("输入不合法!\n");
}
return 0;
}
```
这个程序使用有限状态机来识别浮点数,其中状态转移规则如下:
- START:起始状态,可以是正负号或数字
- SIGN:已经读入正负号,后面必须是数字
- INT:已经读入整数部分,后面可以是小数点、数字或指数符号
- DOT:已经读入小数点,后面必须是数字
- DEC:已经读入小数点和小数部分,后面可以是数字或指数符号
- EXP:已经读入指数符号,后面可以是正负号或数字
- EXP_SIGN:已经读入指数符号和正负号,后面必须是数字
- EXP_INT:已经读入指数符号和数字,后面必须是数字
如果状态机最终停留在 INT、DOT、DEC 或 EXP_INT 状态,则认为输入合法;否则认为输入不合法并退出程序。
需要注意的是,这个程序只是一个简单的示例,还有许多浮点数表示方法没有考虑到,如浮点数前后的空格、多个小数点等。如果需要更完善的识别功能,需要对程序进行进一步扩展。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)