我在一份档案中有一个汽车清单,其中线形形形色。
car_1_name [car_1_id] & car_2_name [car_2_id]
我想将每辆汽车的姓名和胎盘存放在一列。 为此,我设立了两个机构,称为“Car_”和“_Car_List_”。 然而,我为上述形式下限而写的“间隔”职能似乎并不恰当地摘取汽车名称(我用斜体分解)。 然而,当我用人工方式在名单上添加一辆汽车时,便能正确展示地毯。 下面你可以找到我的最低工作榜样:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _Car_
{
char* name_;
int id_;
struct _Car_* parent_1_;
struct _Car_* parent_2_;
};
struct _Car_List_
{
struct _Car_* car_list[100];
int number_of_cars;
};
struct _Car_* createCar(char* name, int id)
{
struct _Car_* car = malloc(sizeof(struct _Car_));
if (car == NULL)
return NULL;
car -> name_ = name;
car -> id_ = id;
return car;
}
void addCar(struct _Car_List_* my_list, struct _Car_* car)
{
my_list -> car_list[my_list -> number_of_cars] = car;
my_list -> number_of_cars = my_list -> number_of_cars + 1;
}
int parse(struct _Car_List_* my_list)
{
char string[] = "Ferrari [3] & Porsche [6]
";
char* car_1_string = strtok(string, "&");
char* car_2_string = strtok(NULL, "&");
char* car_1_name = strtok(car_1_string, "[");
int car_1_id = atoi(strtok(NULL, "]"));
char* car_2_name = strtok(car_2_string, "[");
int car_2_id = atoi(strtok(NULL, "]"));
struct _Car_* car_1 = createCar(car_1_name, car_1_id);
struct _Car_* car_2 = createCar(car_2_name, car_2_id);
addCar(my_list, car_1);
addCar(my_list, car_2);
return 0;
}
void printList(struct _Car_List_* my_list)
{
int i = 0;
struct _Car_* car = my_list -> car_list[i];
while (car != NULL)
{
printf("- %s [%d]
", car -> name_, car -> id_);
i = i + 1;
car = my_list -> car_list[i];
}
}
int testAdd(struct _Car_List_* my_list)
{
struct _Car_* car = createCar("Renault", 1);
addCar(my_list, car);
return 0;
}
int main()
{
struct _Car_List_* my_list = malloc(sizeof(struct _Car_List_));
my_list -> number_of_cars = 0;
for (int i = 0; i < 100; i = i + 1)
my_list -> car_list[i] = NULL;
testAdd(my_list);
parse(my_list);
printf("Below the printed list:
");
printList(my_list);
return 0;
}
请告诉我我我我我,我是错了吗?