[刷题] PTA 7-62 切分表达式 写个tokenizer吧
我的程序:
1 #include<stdio.h>
2 #include<string.h>
3 #define N 50
4 char token[]= {'+','-','*','/','(',')'};
5
6 int istoken(char c) {
7 int i;
8 for(i=0; i<strlen(token); i++) {
9 if(token[i]==c) return 1;
10 }
11 return 0;
12 }
13
14 int main() {
15 int i,j,t=0,flag=0;
16 char s[N],temp[N];
17 scanf("%s",s);
18 int len = strlen(s);
19 for(i=0; i<len; i++) {
20 //如果是减号且前面也是符号,则将减号作为后面数据的一部分
21 if((i==0 && s[i]=='-')||(istoken(s[i-1]) && s[i]=='-')) flag = 1;
22 //如果是符号,且不是上面的情况,则先打印符号前的数据,再打印符号
23 if(istoken(s[i]) && flag==0) {
24 for(j=0; j<t; j++) {
25 printf("%c",temp[j]);
26 }
27 //运算符紧跟括号时,不输出回车
28 if(t!=0)
29 printf("\n");
30 t=0;
31 //如果到末尾,则不输出回车
32 printf("%c",s[i]);
33 if(i!=len-1)
34 printf("\n");
35 } else {
36 temp[t]=s[i];
37 t++;
38 //flag清零
39 flag = 0;
40 //输出末尾的数据
41 if(i==len-1) {
42 for(j=0; j<t; j++) {
43 printf("%c",temp[j]);
44 }
45 }
46 }
47 }
48 }
(最后一个用例提示“格式错误”,没有找出bug)
网友“扯淡”的程序:
1 #include <stdio.h>
2 #include <string.h>
3 int main(){
4 char str[41], ch, flag=0;
5 int i=0;
6 scanf("%s", str);
7 int len=strlen(str);
8 printf("%c", str[0]);
9 for(i=1; i<len; i++){
10 if (str[i]>47 && str[i]<58 || str[i]=='.'){
11 if (flag)
12 printf("\n");
13 printf("%c", str[i]);
14 flag=0;
15 }else{
16 if (str[i-1]=='(' && str[i]=='-'){
17 printf("\n%c", str[i]);
18 flag=0;
19 }else{
20 printf("\n%c", str[i]);
21 flag=1;
22 }
23 }
24 }
25 return(0);
26 }
https://blog.csdn.net/qq_36589706/article/details/81081144
分析:
1、我是一开始就想的比较复杂,然后在测试中不断补漏洞;扯淡是一开始就想的很简单,只分成数值和非数值两种情况
2、程序构造的越抽象,细节越少,越不容易出错
3、从计算机的角度,而不是实际意义的角度出发思考问题
[刷题] PTA 7-62 切分表达式 写个tokenizer吧的更多相关文章
- pta习题集 5-10 切分表达式——写个tokenizer吧
[先说点出题背景] 这个题是为低年级同学.学C语言的同学准备的,因为,对这部分同学,这个题目编写起来略有一点复杂.如果是高年级.学过了正则表达式(Regular Expression)的同学或者学过了 ...
- [刷题] PTA 查验身份证
题目: 7-63 查验身份证 (15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...
- [刷题] PTA 04-树4 是否同一棵二叉搜索树
程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct TreeNode *Tree; 4 struct ...
- [刷题] PTA 03-树2 List Leaves
程序: 1 #include <stdio.h> 2 #include <queue> 3 #define MaxTree 20 4 #define Null -1 5 usi ...
- [刷题] PTA 03-树1 树的同构
程序: 1 #include <stdio.h> 2 #define MaxTree 10 3 #define ElementType char 4 #define Tree int 5 ...
- #leetcode刷题之路10-正则表达式匹配
匹配应该覆盖整个字符串 (s) ,而不是部分字符串.说明:s 可能为空,且只包含从 a-z 的小写字母.p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *. 示例 1:输入:s = & ...
- [刷题] PTA 02-线性结构3 Reversing Linked List
链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...
- [刷题] PTA 03-树3 Tree Traversals Again
用栈实现树遍历 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXSIZE 30 4 5 int Pre[MAXSIZ ...
- [刷题] PTA 7-61 找最长的字符串
程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 81 4 5 int main() { 6 char ch[N ...
随机推荐
- 痞子衡嵌入式:在i.MXRT启动头FDCB里调整Flash工作频率也需同步设Dummy Cycle
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是Flash工作频率与Dummy Cycle的联系. 上一篇文章 <从头开始认识i.MXRT启动头FDCB里的lookupTable ...
- 使用gradle插件发布项目到nexus中央仓库
目录 简介 Gradle Nexus Publish Plugin历史 插件的使用 Groovy DSL Kotlin DSL 插件背后的故事 总结 简介 Sonatype 提供了一个叫做开源软件资源 ...
- 「最强」Lettuce 已支持 Redis6 客户端缓存
Redis 客户端缓存 缓存的解决方案一般有两种: [L1] 内存缓存(如 Caffeine.Ehcache) -- 速度快,进程内可用,但重启缓存丢失,出现缓存雪崩的问题. [L2]集中式缓存(如 ...
- 安装maven工程报错"Failed to execute goal on project...Could not resolve dependencies for project..."
我在qingcheng_interface中Lifecycle目录下执行install命令后报错"Failed to execute goal on project...Could not ...
- Salesforce学习之路(三)利用VS Code结合Git开发Salesforce
在前面说了一些有关Admin的知识,但实际开发运用中,仅凭Admin的配置很难满足项目的定制化需求,因此基于CRM的二次开发则应运而生. 由于国内资料相对较少,所以很多入门新手无处下手,那这里就简单介 ...
- 如何查看显著性SNP在数据中的频率?
我们做完GWAS的关联分析后需要查看显著性SNP在我们数据中的频率分布情况.这时候我们需要用到plink和我们做关系分析所用的二进制文件datas. 第一步,我们用R语言读取分析结果,即*.assoc ...
- Day13_65_线程sleep()方法
线程sleep()方法 * public static void sleep(long millis) throws InterruptedException * Thread.sleep(),该方 ...
- Day11_52_将Set集合转换为List集合
Set集合转换为List集合 ``` import java.util.*; public class SetReverseLsit { public static void main(String[ ...
- CPF 入门教程 - 设计器和模板库的使用(五)
CPF netcore跨平台UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF ...
- Salesforce 集成篇零基础学习(一)Connected App
本篇参考: https://zhuanlan.zhihu.com/p/89020647 https://trailhead.salesforce.com/content/learn/modules/c ...