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 08:19:29 浏览: 118
这段代码定义了一个名为 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" 不是有效的代码,很可能是一条注释或者是无意义的字符串。
编写一个程序实现北京地铁最短乘坐(站)线路查询,输入为起始站名和目的站名,输出为从起始站到目的站的最短乘坐站换乘线路。 注:1. 要求采用Dijkstra算法实现;2)如果两站间存在多条最短路径,找出其中的一条即可 【输入形式】 文件bgstations.txt为数据文件,包含了北京地铁的线路及车站信息。其格式如下: <地铁线路总条数> <线路1> <线路1站数> <站名1> <换乘状态> <站名2> <换乘状态> ... <线路2> <线路2站数> <站名1> <换乘状态> <站名2> <换乘状态> 从标准输入读入起始站和目的站 输出从起始站到目的站的乘坐信息,要求乘坐站数最少。换乘信息格式如下: SSN-n1(m1)-S1-n2(m2)-...-ESN 其中:SSN和ESN分别为起始站名和目的站名;n为乘坐的地铁线路号,m为乘坐站数;其它字符都是英文字符。 用C语言实现
好的,这是一个比较复杂的问题,需要用到Dijkstra算法进行最短路径的计算。这里提供一份C语言代码供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_STATION 100
typedef struct {
int line_num;
char line_name[MAX_LINE];
int station_count;
char station_name[MAX_STATION][MAX_LINE];
int station_transfer[MAX_STATION];
} subway_line;
typedef struct {
int station_num;
int line_num;
int transfer_count;
} vertex;
typedef struct {
int weight;
int next_vertex;
} edge;
void dijkstra(int start, int end, vertex *vertices, edge *edges, int vertex_count, char station_names[MAX_STATION][MAX_LINE], subway_line *lines, int line_count) {
int i, j, k, min, min_vertex;
int *prev_vertex = (int *)malloc(vertex_count * sizeof(int));
int *dist = (int *)malloc(vertex_count * sizeof(int));
int *visited = (int *)malloc(vertex_count * sizeof(int));
for (i = 0; i < vertex_count; ++i) {
prev_vertex[i] = -1;
dist[i] = -1;
visited[i] = 0;
}
dist[start] = 0;
visited[start] = 1;
int current_vertex = start;
while (current_vertex != end) {
for (i = 0; i < vertex_count; ++i) {
if (visited[i] || !edges[current_vertex * vertex_count + i].weight) {
continue;
}
if (dist[i] == -1 || dist[current_vertex] + edges[current_vertex * vertex_count + i].weight < dist[i]) {
dist[i] = dist[current_vertex] + edges[current_vertex * vertex_count + i].weight;
prev_vertex[i] = current_vertex;
}
}
min = -1;
min_vertex = -1;
for (i = 0; i < vertex_count; ++i) {
if (visited[i] || dist[i] == -1) {
continue;
}
if (min == -1 || dist[i] < min) {
min = dist[i];
min_vertex = i;
}
}
if (min_vertex == -1) {
break;
}
visited[min_vertex] = 1;
current_vertex = min_vertex;
}
if (dist[end] == -1) {
printf("No route found.\n");
} else {
int route[MAX_STATION];
int route_length = 0;
current_vertex = end;
while (current_vertex != start) {
route[route_length++] = current_vertex;
current_vertex = prev_vertex[current_vertex];
}
route[route_length++] = start;
printf("%s-%d(%d)", station_names[vertices[start].station_num], vertices[route[route_length - 1]].line_num, vertices[route[route_length - 1]].transfer_count);
for (i = route_length - 2; i >= 0; --i) {
int line_num1 = vertices[route[i + 1]].line_num;
int line_num2 = vertices[route[i]].line_num;
if (line_num1 != line_num2) {
char line_name[MAX_LINE];
int j;
for (j = 0; j < line_count; ++j) {
if (lines[j].line_num == line_num1) {
strcpy(line_name, lines[j].line_name);
break;
}
}
printf("-%s-%d(%d)", line_name, line_num2, vertices[route[i]].transfer_count);
}
printf("-%s", station_names[vertices[route[i]].station_num]);
}
printf("\n");
}
free(prev_vertex);
free(dist);
free(visited);
}
int main() {
FILE *fp;
char buf[MAX_LINE];
int line_count, station_count;
subway_line *lines;
char station_names[MAX_STATION][MAX_LINE];
vertex vertices[MAX_STATION * MAX_STATION];
edge edges[MAX_STATION * MAX_STATION];
int vertex_count = 0;
fp = fopen("bgstations.txt", "r");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%d", &line_count);
lines = (subway_line *)malloc(line_count * sizeof(subway_line));
for (int i = 0; i < line_count; ++i) {
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%d %s %d", &lines[i].line_num, lines[i].line_name, &lines[i].station_count);
for (int j = 0; j < lines[i].station_count; ++j) {
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%s %d", lines[i].station_name[j], &lines[i].station_transfer[j]);
int found = -1;
for (int k = 0; k < vertex_count; ++k) {
if (strcmp(station_names[vertices[k].station_num], lines[i].station_name[j]) == 0) {
found = k;
break;
}
}
if (found == -1) {
int station_num = vertex_count++;
vertices[station_num].station_num = station_num;
vertices[station_num].line_num = i;
vertices[station_num].transfer_count = lines[i].station_transfer[j];
strcpy(station_names[station_num], lines[i].station_name[j]);
found = station_num;
} else {
if (vertices[found].line_num != i) {
vertices[found].transfer_count = 1;
}
}
if (j > 0) {
int weight = vertices[found].transfer_count == 1 ? 1 : j - 1;
edges[(found - 1) * vertex_count + found].weight = weight;
edges[found * vertex_count + found - 1].weight = weight;
}
if (j < lines[i].station_count - 1) {
int found2 = -1;
for (int k = 0; k < vertex_count; ++k) {
if (strcmp(station_names[vertices[k].station_num], lines[i].station_name[j + 1]) == 0) {
found2 = k;
break;
}
}
if (found2 == -1) {
int station_num = vertex_count++;
vertices[station_num].station_num = station_num;
vertices[station_num].line_num = i;
vertices[station_num].transfer_count = lines[i].station_transfer[j + 1];
strcpy(station_names[station_num], lines[i].station_name[j + 1]);
found2 = station_num;
} else {
if (vertices[found2].line_num != i) {
vertices[found2].transfer_count = 1;
}
}
int weight = vertices[found].line_num == vertices[found2].line_num ? 1 : vertices[found].transfer_count + vertices[found2].transfer_count;
edges[found * vertex_count + found2].weight = weight;
edges[found2 * vertex_count + found].weight = weight;
}
}
}
fclose(fp);
char start_name[MAX_LINE], end_name[MAX_LINE];
int start = -1, end = -1;
printf("Please enter the start station: ");
scanf("%s", start_name);
printf("Please enter the end station: ");
scanf("%s", end_name);
for (int i = 0; i < vertex_count; ++i) {
if (strcmp(station_names[vertices[i].station_num], start_name) == 0) {
start = i;
}
if (strcmp(station_names[vertices[i].station_num], end_name) == 0) {
end = i;
}
}
if (start == -1 || end == -1) {
printf("Invalid station name.\n");
} else {
dijkstra(start, end, vertices, edges, vertex_count, station_names, lines, line_count);
}
free(lines);
return 0;
}
```
这个程序的输入为起始站名和目的站名,输出为从起始站到目的站的最短路径,其中包括经过的站点和换乘信息。程序首先从文件中读取北京地铁的线路及车站信息,并通过Dijkstra算法计算最短路径,最后输出路径信息。
阅读全文