У меня возникли проблемы с программой со связанным списком, в которой strcmp никогда не возвращает 0, однако они равны.Я попробовал функцию strcpy (), чтобы поместить temp -> name в строку, но она тоже не сработала.Я пытаюсь назначить среднее значение для temp -> average.
Вот входные данные:
vloz Mrkvicka Jozko 1 1.25
vloz Hrusticka Ferko 2 1.5
vloz Kalerab Jurko 1 2.14
vloz Hrusticka Ferko 1 2.8
vloz Zeler Misko 1 4.12
vypis
zmen Hrusticka Ferko 3.0
vypis
Мой вывод должен выглядеть следующим образом:
firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25
firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=3.00 //current 2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=3.00 //current 1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25
Мой вывод:
firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25
firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25
Вот мой полный код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char firstname[50];
char lastname[50];
int year;
double average;
struct student *next;
};
struct database
{
struct student *first;
};
struct database *create_head ()
{
struct database *head = (struct database *)malloc(sizeof(struct database));
head -> first = NULL;
return head;
}
void vloz (struct database *head)
{
struct student *temp = (struct student *) malloc (sizeof(struct student));
temp -> next = NULL;
char firstname[50];
char lastname[50];
int year;
double average;
scanf("%s %s %d %lf\n", firstname, lastname, &year, &average);
strcpy(temp -> firstname, firstname);
strcpy(temp -> lastname, lastname);
temp -> year = year;
temp -> average = average;
temp -> next = head -> first;
head -> first = temp;
return;
}
void zmen (struct database *head)
{
char firstname[50];
char lastname[50];
double average;
scanf("%s %s %lf", lastname, firstname, &average);
struct student *temp = head -> first;
while (temp != NULL)
{
if (strcmp(temp -> firstname, firstname) == 0)
{
printf("TTT\n");
temp -> average = average;
}
temp = temp -> next;
}
}
void vypis (struct database *head)
{
struct student *temp = head -> first;
while (temp != NULL)
{
printf("lastname=%s, firstname=%s, year=%d, average=%.2lf\n", temp -> firstname, temp -> lastname, temp -> year, temp -> average);
temp = temp -> next;
}
printf("\n");
}
int main()
{
char prikaz[20];
struct database *head = create_head();
while (scanf("%s", prikaz) > 0)
{
if (strcmp(prikaz, "vloz") == 0)
{
if (head->first != NULL)
{
struct database *head = create_head();
}
vloz (head);
}
if (strcmp(prikaz, "vypis") == 0)
vypis (head);
if (strcmp(prikaz, "zmen") == 0)
zmen (head);
}
return 0;
}