public class Sscanf { public static Object[] scan(String source, String format, Object... params) { List<Object>outs = new ArrayList<Object>(); SscanfFormat sf = new SscanfFormat(source, format); for(Object param : params) { Object o = parse(sf, param); if(o == null) break; else outs.add(o); } return outs.toArray(); }
时间: 2024-02-14 20:19:29 浏览: 130
这段代码定义了一个名为 Sscanf 的类,其中包含一个名为 scan 的静态方法,其返回类型为 Object 数组。scan 方法接受三个参数:一个字符串 source,一个字符串 format 和一个可变参数列表 params。在方法内部,它首先创建了一个空的 List 对象 outs,用于存储解析出来的参数。然后创建了一个 SscanfFormat 对象 sf,用于解析 source 字符串中的内容,其解析规则由 format 字符串指定。接着使用 for-each 循环遍历 params 数组中的每一个参数,调用 parse 方法对其进行解析,并将解析结果存储到 outs 列表中。如果 parse 方法返回 null,则说明解析失败,退出循环。最后将 outs 列表转换成数组并返回。
相关问题
package com.xinchao.glomis.nonsecs.driver; import java.util.ArrayList; import java.util.List; public class Sscanf { public static Object[] scan(String source, String format, Object... params) { List<Object>outs = new ArrayList<Object>(); SscanfFormat sf = new SscanfFormat(source, format); for(Object param : params) { Object o = parse(sf, param); if(o == null) break; else outs.add(o); } return outs.toArray(); } corporation realistic moderate public static int scan2(String source, String format, Object params[]) { SscanfFormat sf = new SscanfFormat(source, format); int parseCount = 0; for(int i = 0; i < params.length; ++i) { params[i] = parse(sf, params[i]); if(params[i] == null) break; else ++parseCount; } return parseCount; } private static Object parse(SscanfFormat sf, Object param) { if(!sf.prepareNextParseParam()) { return null; } Object o = null; if (param instanceof Number) { if (param instanceof Integer) { o = sf.parse((Integer) param); } else if (param instanceof Long) { o = sf.parse((Long) param); } else if (param instanceof Double) { //o = sf.parse((Double) param); } else if (param instanceof Float) { //o = sf.parse((Float) param); } else { //o = sf.parse((Number)param); } } else if (param instanceof Character) { o = sf.parse((Character) param); } else { o = sf.parse(param.toString()); } return o; } }
这段代码定义了一个名为 Sscanf 的类,其中包含两个静态方法:scan 和 scan2。scan 方法与前面提到的一样,它用于解析字符串 source 中的内容,解析规则由 format 字符串指定,然后返回解析结果。scan2 方法与 scan 方法类似,但返回的是解析成功的参数个数。这两个方法内部都使用了一个名为 SscanfFormat 的类,它用于解析字符串。parse 方法用于解析单个参数,它根据参数类型的不同使用不同的 parse 方法进行解析,并返回解析结果。如果解析失败,则返回 null。
代码最后一行 "corporation realistic moderate" 不是有效的代码,很可能是一条注释或者是无意义的字符串。
#include <stdio.h> #include <stdlib.h> static char buff[256]; static char* string; int main() { printf("Please input your string:"); gets(string); printf("\nYour string is:%s\n",string); }
这段代码是一个简单的C程序,它的功能是从标准输入中读取一行字符串并输出。
然而,这段代码存在一个严重的问题:它使用了不安全的gets函数来读取输入,可能会导致缓冲区溢出漏洞。正确的做法是使用fgets函数来代替gets函数,限制读取的字符数量。
另外,这段代码声明了一个静态的字符数组buff,但是却没有使用它。而指针变量string却没有被初始化,使用时会导致未定义的行为。应该将string指向buff数组,或者使用动态内存分配函数malloc来为string分配内存。
改进后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 256
int main() {
char buff[MAX_LEN];
char *string = (char*)malloc(MAX_LEN * sizeof(char));
printf("Please input your string:");
fgets(buff, MAX_LEN, stdin);
sscanf(buff, "%s", string); // 去掉字符串末尾的换行符
printf("\nYour string is:%s\n", string);
free(string);
return 0;
}
```
阅读全文
相关推荐















