以下代码是我学习完c++基础知识之后为了练习一下,将所学的内容能够统一的串一遍,特别是练习一下指针的用法所编写的程序,并且有文件操作,现在分享一下,希望对初学者能够有所帮助。
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define LEN sizeof(linklist)
typedef struct consumer
{
char name[10];
char tel[20];
char dining[15];
char travel[15];
char clothing[15];
struct consumer *next;
}linklist;
linklist *insert(linklist *head)
{
linklist *p0,*p1,*p2,*p3;
p3=(linklist*)malloc(LEN);
p1=head;
p0=p3;
while ( strcmp(p0->name,"0")!=0)
{
printf("\nPlease enter a new record.\n");
printf("the name:");
scanf("%s",p0->name);
if (strcmp(p0->name,"0")==0)
break;
printf("the phonenumber:");
scanf("%s",p0->tel);
printf("dining consume:");
scanf("%s",p0->dining);
printf("travel consume:");
scanf("%s",p0->travel);
printf("clothing consume:");
scanf("%s",p0->clothing);
if (head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while (p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
p1->next=p0;
p0->next=NULL;
}
p3=(linklist*)malloc(LEN);
p0=p3;
}