【发布时间】:2021-04-04 12:55:21
【问题描述】:
所以这段代码的目的是扫描一个类似这样的句子:
在我家#House#28#-134.943|47.293#21-24
代码必须扫描由“#”分隔的 6 个不同部分。第一个“在我家”是聚会的地点,第二个“房子”是聚会的地点类型,第三个“28”是到达那里的步行时间,第四个“-134.943|47.293”是用“|”分隔的经纬度(查找地点),最后,“21-24”是聚会开始和结束的时间。前面的所有参数都必须保存在不同的变量中,这些变量的结构如下所示:
typedef struct {
char name[MAX]; //name of the location
char location_type[MAX]; //type of location
int travel; //time to arrive
int latitude;
int longitude;
int hours[5]; //when the party starts and ends
} Locations;
然后,为每个聚会地点整齐地存储所有内容。
所以,我要求一个函数来询问和存储我们之前看到的结构中的所有信息(由“#”和“|”分隔)。这个函数是这样的:
int AddLocation(Locations location[]) {
int i = 0;
i = num_locations;
printf ("Location information: ");
// here should be the scanf and this stuff
i++;
return i;
}
【问题讨论】:
-
通过
fgets()读取行,通过strtok()拆分并解析每个部分。
标签: c string data-structures scanf separator