我在这里的这个程序不能正确打印出来。这是输入:
有多少员工?2
输入员工 1 信息:月/日/年、年龄、身高、姓名:3/21/34、43、3.4、hdsfgdf
输入员工 2 信息:月/日/年、年龄、身高、姓名:4/44/44、44、6.2、dfgtesas
这是输出:
员工1信息:0/-1081689528/134514548, 16564212, 0.0, ��
员工 2 信息:0/1/14608664, -1217230008, 0.0, ��������
我唯一的猜测是我没有正确填充数组,或者我打印的是地址而不是数据。我这样假设是对的吗?任何一点建议都会有所帮助。非常感谢!
我的代码在 3 个文件中。
这是主要的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "personal.c"
LIST *start, *end;
int main(void)
{
int i, numEmp;
PERSON person;
start=end=NULL;
printf("How many employees? ");
scanf("%d", &numEmp);
PERSON employees[numEmp];
for (i = 0; i < numEmp; i++)
{
printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
scanf("%d/%d/%d,%d,%f,%s", &person.bday.month, &person.bday.day,
&person.bday.year, &person.age, &person.height, person.name);
add(&start, &end, person);
}
for (i = 0; i < numEmp; i++)
{
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, employees[i].bday.month, employees[i].bday.day, employees[i].bday.year, employees[i].age, employees[i].height, employees[i].name);
delete(&start, &end);
}
这是具有结构的列表:
#ifndef LIST_H_ /* to prevent re-definitions */
#define LIST_H_ /* that cause errors */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct DATE
{
int month;
int day;
int year;
} DATE;
typedef struct PERSON
{
char name[41];
int age;
float height;
DATE bday;
} PERSON;
typedef struct list
{
PERSON data;
struct list *next;
} LIST;
#endif
这就是我所有方法的所在:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
int delete (LIST **head, LIST **tail){
LIST *temp;
if (*head == NULL)
return -1;
PERSON retVal = (*head)->data;
if(*head==*tail){
free(*head);
*head=*tail=NULL;
}
else{
temp=(*head)->next;
free(*head);
*head=temp;
}
//return retVal;
}
void add(LIST **head, LIST **tail, PERSON data){
if(*tail==NULL){
*head=*tail=(LIST *) malloc(sizeof(LIST));
(*head)->data=data;//use arrow when struct is pointer. Use . if have direct access to struct
(*head)->next=NULL;
}
else{
(*tail)->next= (LIST *) malloc(sizeof(LIST));
*tail=(*tail)->next;
(*tail)->data=data;
(*tail)->next=NULL;
}
}