实现函数“
update_age
”,用于更改数组中某个现有记录的年龄(“newage
”)。要更改的记录由ID(“p_id
”)标识。如果找不到具有匹配ID的记录,则不会更改任何内容。如果进行了更改,则函数返回1;如果未找到匹配的记录,则返回0。您可以假设没有多个具有相同ID的条目。实现函数“
update_entrydate
”,用于更改数组中某个现有记录的年龄(“newdate
”)。要更改的记录由ID(“p_id
”)标识。如果找不到具有匹配ID的记录,则不会更改任何内容。如果进行了更改,则函数返回1;如果未找到匹配的记录,则返回0。您可以假设没有多个具有相同ID的条目。
这是我在这里的任务,所以这两个更新功能几乎相同,在这里我试着写它们,我只是无法理解这里出了什么问题?也许我需要在这些函数中使用某种循环? main
必须保持不变,并且add_record
功能很好,所以不要介意。
我的代码:
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
// Single animal entry
struct animal {
char id[7]; // animal ID (6 characters) + terminating '\0'
char *name; // animals name
char *species; // animal species (string)
unsigned char age; // animals age
struct date entrydate; // date of the animals arrival
};
struct animal *add_record(struct animal *array, unsigned int size, struct animal newanimal)
{
int n = size + 1;
struct animal *newarray = realloc(array, n*sizeof(*newarray));
if (!newarray) {
perror("realloc");
exit(EXIT_FAILURE);
}
array = newarray;
strcpy(array[n - 1].id, newanimal.id);
array[n - 1].name = malloc(strlen(newanimal.name) + 1);
strcpy(array[n - 1].name, newanimal.name);
array[n - 1].species = malloc(strlen(newanimal.species) + 1);
strcpy(array[n - 1].species, newanimal.species);
array[n - 1].age = newanimal.age;
array[n - 1].entrydate = newanimal.entrydate;
return array;
int update_age(struct animal *array, unsigned int size, const char *p_id,
unsigned char newage) {
for (int i = 0; !array; ++i) {
if (strcmp(array[i], p_id)==0) {
array[i] = newage;
return 1
}
}
return 0;
}
}
int update_entrydate(struct animal *array, unsigned int size, const char *p_id,
struct date newdate) {
int n = size + 1;
if (array[n-1].id == p_id) {
array[n-1].entrydate = newdate;
return 1;
}
if (array[n].id == p_id) {
array[n].entrydate = newdate;
return 1;
}
if (array[n + 1].id == p_id) {
array[n + 1].entrydate = newdate;
return 1;
}
if (array[n + 2].id == p_id) {
array[n + 2].entrydate = newdate;
return 1;
}
else
return 0;
}
int main()
{
/*making new animal zoo which I want to which all of them I need to
add to array in function add_record*/
struct animal zoo[] = {
{ "123456", "Winnie-the-Pooh", "Bear", 94,{ 1,1,1924 } },
{ "555666", "Eeyore", "Donkey", 92,{ 1,1,1926 } },
{ "773466", "Piglet", "Very Small Animal", 30,{ 31, 12, 2015 } },
{ "234567", "Roo", "Kangaroo", 5,{ 31, 12, 2015 } }
};
//this below is for the function add_record so do not mind
struct animal *array = NULL;
unsigned int len = 0;
for (unsigned int i = 0; i < sizeof(zoo) / sizeof(struct animal); i++) {
struct animal *newarray = add_record(array, len, zoo[i]);
len++;
array = newarray;
}
//from here starts the part of this question
struct date day1 = { 0, 0, 0 };
if (!update_age(array, len, "555666", 50))
printf("failed '555666'\n");
if (!update_age(array, len, "23456677", 3))
printf("failed '234567', invalid id\n");
if (!update_entrydate(array, len, "123456", day1))
printf("failed '111111'\n");
if (!update_entrydate(array, len, "999999", day1))
printf("failed '234567', no course\n");
return 0;
}