扫描带有空格和符号的行以分隔条目并使用 C 中的结构
Scan a line with spaces and symbols to separate the entries and using structures in C
所以这段代码的目的是扫描一个像这样的句子:
In my house#House#28#-134.943|47.293#21-24
代码必须扫描 6 个以“#”分隔的不同部分。第一个“In my house”是聚会的地点,第二个“House”是聚会地点的类型,第三个“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;
}
对格式进行一些更改(即,lat/long 需要是浮点数,我不确定您打算对 hours
的 int 数组做什么),您可以做类似的事情:
#include <stdio.h>
#define MAX 32
struct loc {
char name[MAX];
char location_type[MAX];
int travel;
float latitude;
float longitude;
char hours[MAX];
};
int
main(void)
{
struct loc Locations[16];
struct loc *t = Locations;
struct loc *e = Locations + sizeof Locations / sizeof *Locations;
char nl;
char fmt[128];
/* Construct fmt string like: %31[^#]#%31[^#]#%d#%f|%f#%31[^\n]%c */
snprintf(fmt, sizeof fmt,
"%%%1$d[^#]#%%%1$d[^#]#%%d#%%f|%%f#%%%1$d[^\n]%%c", MAX - 1);
while( t < e
&& 7 == scanf(fmt,
t->name, t->location_type, &t->travel,
&t->latitude, &t->longitude, t->hours, &nl
) && nl == '\n'
) {
t += 1;
}
for( e = Locations; e < t; e++ ){
; /* Do something with a location */
}
}
尝试编译并运行这个。此代码应拆分您的字符串。您所要做的就是将结果正确地输入到您的结构中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
/*Declare variable */
char *line = malloc(250 * sizeof(char *));
size_t line_size = 250;
char *argument = malloc(250 *sizeof(char *));
/*Get string and save it in line */
getline(&line,&line_size,stdin);
/*Split string */
char corrector[] = "#|\n";
argument = strtok(line,corrector);
while(argument != NULL){
printf("%s\n", argument);
argument = strtok(NULL,corrector);
}
return 0;
}
Input : In my house#House#28#-134.943|47.293#21-24
Output:
In my house
House
28
-134.943
47.293
21-24
所以这段代码的目的是扫描一个像这样的句子:
In my house#House#28#-134.943|47.293#21-24
代码必须扫描 6 个以“#”分隔的不同部分。第一个“In my house”是聚会的地点,第二个“House”是聚会地点的类型,第三个“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;
}
对格式进行一些更改(即,lat/long 需要是浮点数,我不确定您打算对 hours
的 int 数组做什么),您可以做类似的事情:
#include <stdio.h>
#define MAX 32
struct loc {
char name[MAX];
char location_type[MAX];
int travel;
float latitude;
float longitude;
char hours[MAX];
};
int
main(void)
{
struct loc Locations[16];
struct loc *t = Locations;
struct loc *e = Locations + sizeof Locations / sizeof *Locations;
char nl;
char fmt[128];
/* Construct fmt string like: %31[^#]#%31[^#]#%d#%f|%f#%31[^\n]%c */
snprintf(fmt, sizeof fmt,
"%%%1$d[^#]#%%%1$d[^#]#%%d#%%f|%%f#%%%1$d[^\n]%%c", MAX - 1);
while( t < e
&& 7 == scanf(fmt,
t->name, t->location_type, &t->travel,
&t->latitude, &t->longitude, t->hours, &nl
) && nl == '\n'
) {
t += 1;
}
for( e = Locations; e < t; e++ ){
; /* Do something with a location */
}
}
尝试编译并运行这个。此代码应拆分您的字符串。您所要做的就是将结果正确地输入到您的结构中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
/*Declare variable */
char *line = malloc(250 * sizeof(char *));
size_t line_size = 250;
char *argument = malloc(250 *sizeof(char *));
/*Get string and save it in line */
getline(&line,&line_size,stdin);
/*Split string */
char corrector[] = "#|\n";
argument = strtok(line,corrector);
while(argument != NULL){
printf("%s\n", argument);
argument = strtok(NULL,corrector);
}
return 0;
}
Input : In my house#House#28#-134.943|47.293#21-24
Output:
In my house
House
28
-134.943
47.293
21-24