关闭

c++写的一个简单的管理系统

标签: c++管理系统指针问价操作
670人阅读 评论(0) 收藏 举报
分类:

以下代码是我学习完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;
}
return(head); 


void display(linklist *head) 
{
linklist *p; 
p=head; 
p=p->next;
printf("*******************************************************************************\n"); 
printf("name\tphonenumber\t\tdining consume\ttravel consume\tclothing consume\n"); 
printf("*******************************************************************************\n"); 
while(p!=NULL) 

printf("%s\t",p->name);
printf("%s\t\t",p->tel);
printf("%s\t\t",p->dining);
printf("%s\t\t",p->travel);
printf("%s\t\t",p->clothing);
printf("\n");
p=p->next;



void show(linklist *p)
{
printf("*******************************************************************************\n"); 
printf("name\tphonenember\t\tdining consume\ttravel consume\tclothing consume\n"); 
printf("*******************************************************************************\n"); 
printf("%s\t",p->name);
printf("%s\t\t",p->tel);
printf("%s\t\t",p->dining);
printf("%s\t\t",p->travel);
printf("%s\t\t",p->clothing);
}

linklist *del(linklist *l) 
{

linklist *p1,*p2;
char name[10];
p1=l;
printf("please input the name which will be deleted : "); 
scanf("%s",name); 
while(strcmp(p1->name,name) != 0 && p1->next != NULL)  
{
p2=p1; 
p1=p1->next;  

if(strcmp(p1->name,name)==0) 
{
p2->next=p1->next;
free(p1);
printf("delete success\n");
}
else
printf("can not search the record\n");
return p1;


linklist *search(linklist *l) 
{  
char name[15];
linklist *p;
printf("please input the name which will be searched:");
scanf("%s",name);
p=l->next;
    while ( p!=NULL && p->next!=NULL && strcmp(name,p->name)!=0 )
{
p=p->next;

if(strcmp(name,p->name)==0)
{
show(p);
printf("\n");
}
else
printf("can not search the record!\n");
return p;
}


void save(linklist *p)
{
FILE *fp;
if((fp=fopen("information.txt","w"))==NULL)
{
printf("cannot write the file!\n");
exit(0);
}
while ( p!=NULL&&p->next!=NULL)
{
p=p->next;
fprintf(fp,"%s %s %s %s %s\n",p->name,p->tel,p->dining,p->travel,p->clothing);
}
fclose(fp);
}

void read()
{
FILE *fp;  
char c;
fp= fopen("information.txt","r");
if(fp==NULL)
{
printf("Cannot open file strike any key exit!");
exit(0);
}
c=fgetc(fp);
while (!feof(fp))  
{
putchar(c); 
c=fgetc(fp); 
}
fclose(fp);
}

linklist *change(linklist *p)
{
char name[10];
printf("please input the name which will be changed:");
scanf("%s",name);
while(p != NULL)
{
if(strcmp(p->name,name)==0)
{
printf("name:");
scanf("%s",p->name);
printf("phonenumber:");
scanf("%s",p->tel); 
printf("dining consume:");
scanf("%s",p->dining);
printf("traveling consume:");
scanf("%s",p->travel);
printf("clothing consume:");
scanf("%s",p->clothing);
printf("don't foget save!\n");
}
p=p->next;
}
return p;
}

void menu_select()
{
printf("\t*************************************************************\n"); 
printf("\twelcome to \n");
printf("\t\t\tthe system of consumer information manage\n"); 
printf("\t*******************MENU**************************************\n"); 
printf("\t\t0.Quit \n");
printf("\t\t1.Increase record \n"); 
printf("\t\t2.Disply record \n"); 
printf("\t\t3.Delete record \n"); 
printf("\t\t4.Search record \n");
printf("\t\t5.Save the information \n");
printf("\t\t6.Change the information \n");
printf("\t\t7.Read the information \n");
}


void main()
{
char cmd='1'; 
linklist *top; 
       top=(linklist*)malloc(LEN);
top->next=NULL;
while(cmd)
{
menu_select();
printf("\nplese select the function which you want:");
scanf("%s",&cmd);
switch(cmd)
{
case '0':
{
printf("Exit!Good-bye\n");
exit(0);
break;
}
case '1':
{
insert(top);
break;
}
case '2':
{
display(top);
break;
}
case '3':
{
del(top);
break;
}
case '4':
{
search(top);
break;
}
case '5':
{
save(top);
break;
}
case '6':
{
change(top);
break;
}
case '7':
{
read();
break;
}
default :
{
printf("TThe choice is between 1 to 7! \a \n");
break;
}
}
}
}






















































0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82146次
    • 积分:1403
    • 等级:
    • 排名:千里之外
    • 原创:68篇
    • 转载:27篇
    • 译文:1篇
    • 评论:1条
    最新评论