用 C++实现的简单物流信息存储

说明:c++简单实现 信息添加、显示、删除


// wuliu.cpp : by lip
//

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <cstring>
#include <iomanip>




struct Product {
	char name[20];
	int id; 
	struct Product *next;
};

struct Product *Head;

struct Product *add(struct Product *h);
void show();
void save(struct Product *h);
void del();
void choice();
void main_init();

struct Product *add(struct Product *h){
	system("cls");
	int n=0;
	int i=0;
	h = NULL;
	struct Product *p;
	printf("\n");
	 printf("请输入总物流记录数:");
	 scanf("%d",&n);   
	 for (i=0;i<n;i++)
	 {
		p=(struct Product *)malloc(sizeof(struct Product));
		printf("名称:");
		scanf("%s",&p->name);
		printf("编号:");
		scanf("%d",&p->id);	 
		printf("  --------------   \n");
		p-> next = NULL;   
		if(h==NULL)	  
			h=p;   
		else     
		{
			struct Product *q=h; 	
			while(q->next!=NULL)	  
				q=q->next;   		
			q->next=p; 	
		}
	 
	 
	 }
	 return(h);
}

void main_init(){
	
	printf("----   -----   ----   ----\n");
	printf("* 1.添加  2.显示  3.删除 *\n");
	printf("----   ----    ----   ----\n");

	struct Product *p;
	p=(struct Product *)malloc(sizeof(struct Product));

	int choice;
	printf("请选择:");
	scanf("%d",&choice);
	switch(choice){
	case 1:
		p = add(p);
		save(p);
		break;
	case 2:
		show();break;
	case 3:
		del();break;
	default:
		exit(1);break;
	
	}
	

}

void show(){

	system("cls");
	printf("正在读取,请稍后...\n");		
	
			struct Product *p;
	p=(struct Product *)malloc(sizeof(struct Product));
	
	struct Product *q;
	q=(struct Product *)malloc(sizeof(struct Product));

	ifstream read("wuliu.txt",ios::in);
    if(!read)
       {
          cout<<"can not rean wuliu.txt"<<endl;
          exit(1);
       }
	printf("\n");
    cout<<"名称\t"<<"编码"<<endl;
	cout<<"---------------"<<endl;
    while(read.good()&&!read.eof())
    {        
        read>>p->name>>p->id;
		if(read.fail()){
			break;
		}
		cout<<p->name<<'\t'<<p->id<<endl;
    }
    p->next=NULL;
    read.close();


	printf("\n");
	printf("读取完毕!\n");
	choice();
}


void del(){
		
	system("cls");
	struct Product *p;
	p=(struct Product *)malloc(sizeof(struct Product));
	
	struct Product *q;
	q=(struct Product *)malloc(sizeof(struct Product));//保存用户要删除物流信息

	ifstream read("wuliu.txt",ios::in);//读入原文件
	if(!read)
    {
		cout<<"can not read wuliu.txt"<<endl;	exit(1);
    }
	ofstream  save("wuliutemp.txt",ios::out|ios::app);//新文件
	if(!save)
	{
		cout<<"can not read wuliutemp.txt"<<endl;exit(1);   
	}


	cout<<"请输入要删除的物流信息..."<<endl;
	cout<<"名称:";
	cin>>q->name;



	while(read.good()&&!read.eof())
    {        
        read>>p->name>>p->id;
		if(read.fail()){//防止读取时最后一行重复
			break;
		}
		if( strcmp(p->name,q->name)!=0 ){
				save<<p->name<<'\t'<<p->id<<endl;	
		}

		/** 修改操作可执行以下代码
		if(strcmp(p->name,q->name)==0 ){
			save<<q->name<<'\t'<<q->id<<endl;	//保存新值
		}else{
			save<<p->name<<'\t'<<p->id<<endl;	
		}	
		*/
    }

	save.close();

	read.seekg(0,ios::end);
	streampos oldt = read.tellg(); 

	ifstream rn("wuliutemp.txt",ios::in);//
	if(!rn)
    {
		cout<<"can not read wuliutemp.txt"<<endl;	exit(1);
    }
	rn.seekg(0,ios::end);
	streampos newt = rn.tellg(); 

	if(oldt==newt){
		cout<<"没有该数据!"<<endl;		
	}else{
		cout<<"删除成功!"<<endl;
	}
	rn.close();
	read.close();
	

	remove("wuliu.txt");//删除源文件
	rename("wuliutemp.txt","wuliu.txt");//修改临时文件名=源文件名称
	
	
	choice();
}


void save(struct Product *h){
	
	printf("\n");
	cout<<"正在存储,请稍后...\n"<<endl;
	struct Product *p;
	p=(struct Product *)malloc(sizeof(struct Product));
	p=h;
	ofstream save("wuliu.txt",ios::out|ios::app);
    if(!save)
    {
		cout<<"can not save wuliu.txt"<<endl;
        exit(1);
    }
 
    while(p!=NULL)
    {   
        save<<p->name<<'\t'<<p->id<<endl;
        p=p->next;        
    }
    save.close();
	printf("存储完成...\n");
	choice();


}

void choice(){

	int choice;
	cout<<("\n");
	cout<<"请选择 1.返回主菜单  2.退出 :"<<endl;
	scanf("%d",&choice);
	switch(choice){
	case 1:
		main_init();
		break;
	case 2:
		exit(1);break;
	default:
		exit(1);break;

	
	}


}
void main(){
	printf("\n");
	cout<<"欢迎使用"<<endl;
	main_init();

}






  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值