C语言大作业

仓库管理系统

这是本人有了两个星期写出来的代码,也是有一些小bug的。
1.名字会重复。
2.保存没有注释(打开文件之后用户可能无法识别保存的是什么)。
3.提示一下,我上面写的提示性词语都是本社团的人员,我只不过是开开玩笑,各位可以在用时进行删改。、

这是我的录入功能

//保存 
int insert ( link* pList )
{
	int n;
	link* temp=pList;
	int count;
	char name[Q]; 
	char num[Q];
	link* p=creation();
	printf("请输入想要增加的货物的数量");
	scanf("%d",&n); 
	
    for(int i=1;i<n;i++){
    printf("请输入货物名称:");
	scanf("%s",name);

	printf("请输入货物的数量:");
	scanf("%d",&count);
	getchar();
	
		
	p->data.count=count;
	strcpy(p->data.name,name);
	}
	p->next=temp->next;
	temp->next=p;
	}
   
}

这是删除利用strcpy函数来实现找到目标进行删除
int delete_from_list(link* plist){
	if(plist==NULL||plist->next==NULL){
		printf("社长家也没有余粮了!!!");
		return ERROR; 
	}
	
	char name[Q];
	printf("请输入货物的名字\r\n");
	scanf("%s",name);
	
	link* p=plist->next;
	link* e=plist;
	
	while(p!=NULL){
		
		if(strcmp(p->data.name,name)==0){
			e->next=p->next;
			free(p);
			p=NULL;
			printf("兵哥已经帮你删除(暗示兵哥扫地扫的很好)\r\n",name);
			return OK;
		}
		else{
			printf("琛哥翻遍脑子也找不到你要删除的货物");
			return ERROR; 
		}
		p->next=p;
		e->next=e;
	
	}
	    
} 
这是一整套文件功能进行录入商品到指定文件中
void Stores ( link* pList )
{
	
	if ( NULL == pList
	||   NULL == pList->next)
	{
		printf ( "没有货物可以保存r\n" );
		return ;
	}
	FILE *fp =NULL;
	fp = fopen ( "迪哥真帅.txt", "w+" );
	if ( NULL == fp )
	{
		printf( "迪哥不帅了\r\n" );
		return ;
	}
	
	link *p = NULL;
	int count = 0;

	while ( NULL != pList->next )
	{
		p = pList->next;
		pList->next = p->next;

		
		fprintf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);
	
		free (p);
		p = NULL;
		p=p->next;
		count++;
	}
	
	free ( pList );
	pList = NULL;

	fclose ( fp );
	printf ( "已经保存 %d 种商品信息\r\n" , count );

	menu4();
}

//读取
void read(link* pList){
	
	if ( NULL == pList )
	{
		printf ( "读取数据失败!\r\n" );
		return ;
	}
	
	FILE *fp = fopen ( "迪哥真帅.txt", "rb+" );
	
	if ( NULL == fp )
	{
		printf( "注意:您的数据库内没有商品信息\r\n" );
		return ;
	}

	int count = 0;
	
	int tmp = 1;
	link *p =  NULL;

	p = creation ( );
	fscanf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);

	while ( !feof(fp) )
	{
			//头插法插入节点
			p->next = pList->next;
			pList->next = p;
			count++;

			//创建一个结点
			p = creation ( );
			//赋值
			fscanf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);
	}
	printf ( "数据库中有 %d 种商品信息\r\n", count );
	fclose ( fp );

	return;
} 


排序使用的是选择排序,利用遍历找出最小数量的商品,与其它商品比较,从大到小,每循环一次就找到剩余最小数量的商品,循环知道所有商品都排序完成

 int sort(link* pList){
	link* head,*p1=NULL,*p2=NULL;
	int count1=0,count2=0;
	
	if(pList->next==NULL||pList==NULL){
		printf("没有货物去排列");
		return ERROR;
	}
	
	head=pList;
	while(head!=NULL){
		p1=head->next;
		p2=head->next;
		count1=head->data.count;
		count2=count1;
		while(p1!=NULL){
			if(count1>p1->data.count){
				count1=p1->data.count;
				p2=p1;
			         }
			    p1=p1->next;
	}
		if(count2!=count1){
				p2->data.count=count2;
				head->data.count=count1;
				
				char name[Q];
				char num[Q];
				strcpy(name,head->data.name);
				strcpy(head->data.name,p2->data.name);
				strcpy(p2->data.name,name);
				
			}
	
		head=head->next;
	}
	printf("排序成功"); 
} 


这是有记忆的登录,利用文件保存自己注册的密码。
void happy(){	
    int i;
	system("pause");
	system("cls");
	FILE* fp=fopen("兵哥真帅.txt","r+");
	if(fp==NULL){
		printf("打开失败"); 
		return;
	}
	printf("\n\n\t\t\t欢迎使用仓库管理系统\n\n");
	//三次登录验证
	for(i=1;i<=3;i++)
	{
	    char a[Q];
		char b[Q];
		printf("请输入账号:\n");
		scanf("%s",a); 
		printf("请输入密码:\n");
		scanf("%s",b);
		fscanf(fp,"%s\n%s",newname,password); 
		if(strcmp(a,newname)==0&&strcmp(b,password)==0)
		{
			printf("\n\n\t\t登录成功,欢迎使用腾讯QQ系统\n\n");
		    return; 
		}
		else
		{
			printf("\n\n\t\t登录失败,请重新登录,您还有%d次机会\n\n",3-i);
		}
		
	}
	
}

//注册
void fun(){
	FILE* fp;
	fp=fopen("兵哥真帅.txt","w");
	if(fp==NULL){
		printf("兵哥不帅了");
	}
	
	printf("请注册账号(不能多于19个字符):");
	scanf("%s",newname);
	printf("请注册密码(不能多于19个字符):"); 
	scanf("%s",password);
	fprintf(fp,"%s\n%s",newname,password);
	fclose(fp);
	char a=menu4();
	if(a=='1')
	{
		happy();
	}

	
   //exit(1);
} 

整体

#include<string.h>
#include<stdlib.h>


#define Q 20

char newname[Q];
char password[Q];

enum judge
{
	ERROR = -1,
	OK,
};


typedef struct commmodity{
	char name[Q];

	
	int count;
	
}goods;


typedef struct link{
	goods data;
	struct link* next;
}link;

link lis; 


link * creation ( )
{
	link *pList = NULL;
	pList = ( link * ) malloc ( sizeof ( link ) );
	if ( NULL == pList )
	{
		return NULL;
	}
	memset ( pList, 0, sizeof ( link ) );
	return pList;
}

//保存 
int insert ( link* pList )
{
	int n;
	link* temp=pList;
	int count;
	char name[Q]; 
	char num[Q];
	link* p=creation();
	printf("请输入想要增加的货物的数量");
	scanf("%d",&n); 
	
    for(int i=1;i<n;i++){
    printf("请输入货物名称:");
	scanf("%s",name);

	printf("请输入货物的数量:");
	scanf("%d",&count);
	getchar();
	
		
	p->data.count=count;
	strcpy(p->data.name,name);
	}
	p->next=temp->next;
	temp->next=p;
	}
   
}

//删除 
int delete_from_list(link* plist){
	if(plist==NULL||plist->next==NULL){
		printf("社长家也没有余粮了!!!");
		return ERROR; 
	}
	
	char name[Q];
	printf("请输入货物的名字\r\n");
	scanf("%s",name);
	
	link* p=plist->next;
	link* e=plist;
	
	while(p!=NULL){
		
		if(strcmp(p->data.name,name)==0){
			e->next=p->next;
			free(p);
			p=NULL;
			printf("兵哥已经帮你删除(暗示兵哥扫地扫的很好)\r\n",name);
			return OK;
		}
		else{
			printf("琛哥翻遍脑子也找不到你要删除的货物");
			return ERROR; 
		}
		p->next=p;
		e->next=e;
	
	}
	    
} 


//查询 
int find(link* plist){
	if(plist==NULL||plist->next==NULL){
		printf("兵哥说都没有货物查什么????");
	}
	
	link* p=plist->next;
	char name[Q];
	printf("请输入货物的名字:");
	scanf("%s",name);
	
	
	while(p!=NULL)
	{
		if(strcmp(p->data.name,name)==0)
		{
		    printf("货物的名字是%s\r\n",p->data.name);
	    	printf("货物的数量是%d\r\n",p->data.count);
		}
		else{
	               printf("迪哥叹气,虚拟机自闭,没有查到你想要查询的货物");
	              return ERROR;
		}
		p=p->next;
	}

}


//修改 
int Modify(link* plist){
	if(plist==NULL||plist->next==NULL){
		printf("行哥说了:“没有东西可以修改”");
		return ERROR;
	}
	link* p=plist->next;
	
	char name[Q];
	printf("请输入想要修改的货物的名字:");
	scanf("%s",name);
	
	while(p!=NULL){
		if(strcmp(p->data.name,name)==0){
			printf("%d",p->data.count);
			printf("请输入想要修改的数量:");
			int count;
			scanf("%d",&count);
			p->data.count=count;
			printf("修改完成");
			return OK;
		}
		p=p->next;
	}
	printf("修改失败,请过几天再来r\n");
	return ERROR;
}

int menu(){
	
	printf ( "	===欢迎来到商品管理系统===\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "  1-------------------进货\r\n" );
	printf ( "  2-------------------出货\r\n" );
	printf ( "  3-------------------查询\r\n" );
	printf ( "  4-------------------排序\r\n" );
	printf ( "  5-------------------保存并退出\r\n");
	printf ( "*******************************************\r\n");
	printf ( "  6------------------------迪哥功能\r\n");
	
	printf ( "*-*请输入你的选择\r\n" );
	char choice[Q];
	scanf ( "%s", choice );

	return choice[0];
}

int menu1 ( )
{
	
	printf ( "	===欢迎来到商品管理系统===\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "  1-------------------新增商品和数量\r\n" );
	printf ( "  2-------------------修改商品数量\r\n" );
	printf ( "*******************************************\r\n");

	printf ( "*-*请输入你的选择*\r\n" );
	char choice[Q];
	scanf ( "%s", choice );
	
	return choice[0];
}
int menu2 ( )
{

	printf ( "	===欢迎来到商品管理系统===\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "  1-------------------减少库存\r\n" );
	printf ( "  2-------------------删除商品\r\n" );
	printf ( "*******************************************\r\n");

	printf ( "*-*请输入你的选择\r\n" );
	char choice[Q];
	scanf ( "%s", choice );
	
	return choice[0];
}
int menu3( )
{
	
	printf ( "	===欢迎来到商品管理系统===\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "  1-------------------查询当前库存列表\r\n" );
	printf ( "  2-------------------根据货物的名字查询商品\r\n" );
	printf ( "*******************************************\r\n");

	printf ( "*-*请输入你的选择\r\n" );
	char choice[Q];
	scanf ( "%s", choice );
	return choice[0];
}

int menu4(){
	fflush(stdin);
	printf ( "	===欢迎来到商品管理注册系统===\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "  1-------------------登录\r\n" );
	printf ( "  2-------------------注册\r\n" );
	printf ( "*******************************************\r\n");
	printf ( "*-*请输入你的选择\r\n" );
	char a[Q];
	scanf ( "%s", a );
	return a[0];
} 


//显示储存 
int show ( link *pList )
{

	if ( NULL == pList
	||   NULL == pList->next )
	{
		printf ( "没货显摆什么?兵哥怒气冲天!!!!r\n" );
		return ERROR;
	}

	int count = 0;
	
	link *p = pList->next;
	while ( NULL != p )
	{
		count++;
		printf ( "货物的名字是%s\r\n", p->data.name );
		printf ( "货物的数量是%d\r\n", p->data.count );
		printf ( "***********************************\r\n" );
		p = p->next;
	}
	printf ( " 货物的种类有%d种。 \r\n", count );
	return count;
}




//保存 
void Stores ( link* pList )
{
	
	if ( NULL == pList
	||   NULL == pList->next)
	{
		printf ( "没有货物可以保存r\n" );
		return ;
	}
	FILE *fp =NULL;
	fp = fopen ( "迪哥真帅.txt", "w+" );
	if ( NULL == fp )
	{
		printf( "迪哥不帅了\r\n" );
		return ;
	}
	
	link *p = NULL;
	int count = 0;

	while ( NULL != pList->next )
	{
		p = pList->next;
		pList->next = p->next;

		
		fprintf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);
	
		free (p);
		p = NULL;
		p=p->next;
		count++;
	}
	
	free ( pList );
	pList = NULL;

	fclose ( fp );
	printf ( "已经保存 %d 种商品信息\r\n" , count );

	menu4();
}

//读取
void read(link* pList){
	
	if ( NULL == pList )
	{
		printf ( "读取数据失败!\r\n" );
		return ;
	}
	
	FILE *fp = fopen ( "迪哥真帅.txt", "rb+" );
	
	if ( NULL == fp )
	{
		printf( "注意:您的数据库内没有商品信息\r\n" );
		return ;
	}

	int count = 0;
	
	int tmp = 1;
	link *p =  NULL;

	p = creation ( );
	fscanf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);

	while ( !feof(fp) )
	{
			//头插法插入节点
			p->next = pList->next;
			pList->next = p;
			count++;

			//创建一个结点
			p = creation ( );
			//赋值
			fscanf(fp,"货物的名称是%s货物的数量是%d\n",p->data.name,p->data.count);
	}
	printf ( "数据库中有 %d 种商品信息\r\n", count );
	fclose ( fp );

	return;
} 

//排序
    int sort(link* pList){
	link* head,*p1=NULL,*p2=NULL;
	int count1=0,count2=0;
	
	if(pList->next==NULL||pList==NULL){
		printf("没有货物去排列");
		return ERROR;
	}
	
	head=pList;
	while(head!=NULL){
		p1=head->next;
		p2=head->next;
		count1=head->data.count;
		count2=count1;
		while(p1!=NULL){
			if(count1>p1->data.count){
				count1=p1->data.count;
				p2=p1;
			         }
			    p1=p1->next;
	}
		if(count2!=count1){
				p2->data.count=count2;
				head->data.count=count1;
				
				char name[Q];
				char num[Q];
				strcpy(name,head->data.name);
				strcpy(head->data.name,p2->data.name);
				strcpy(p2->data.name,name);
				
			}
	
		head=head->next;
	}
	printf("排序成功"); 
} 


//登录 
void happy(){	
    int i;
	system("pause");
	system("cls");
	FILE* fp=fopen("兵哥真帅.txt","r+");
	if(fp==NULL){
		printf("打开失败"); 
		return;
	}
	printf("\n\n\t\t\t欢迎使用仓库管理系统\n\n");
	//三次登录验证
	for(i=1;i<=3;i++)
	{
	    char a[Q];
		char b[Q];
		printf("请输入账号:\n");
		scanf("%s",a); 
		printf("请输入密码:\n");
		scanf("%s",b);
		fscanf(fp,"%s\n%s",newname,password); 
		if(strcmp(a,newname)==0&&strcmp(b,password)==0)
		{
			printf("\n\n\t\t登录成功,欢迎使用腾讯QQ系统\n\n");
		    return; 
		}
		else
		{
			printf("\n\n\t\t登录失败,请重新登录,您还有%d次机会\n\n",3-i);
		}
		
	}
	
}

//注册
void fun(){
	FILE* fp;
	fp=fopen("兵哥真帅.txt","w");
	if(fp==NULL){
		printf("兵哥不帅了");
	}
	
	printf("请注册账号(不能多于19个字符):");
	scanf("%s",newname);
	printf("请注册密码(不能多于19个字符):"); 
	scanf("%s",password);
	fprintf(fp,"%s\n%s",newname,password);
	fclose(fp);
	char a=menu4();
	if(a=='1')
	{
		happy();
	}

	
   //exit(1);
} 

	



//迪哥功能 
int chen(int n){
	while(n){
		printf("陈迪是混子!!!!\n"); 
	}
} 

int main(){
	while(1){
		system("pause");
		int c2;
	loop:
		fflush(stdin);
		char a=menu4();
	    
		switch(a){
			case '1':
				happy();
				system("pause");
				system("cls");
				break;
			case '2':
			     fun();
			     system("pause");
				 system("cls");
				 break; 
				 
			 default:
					//提示
					printf ("输入的信息有误,请重新输入!!!\r\n");
					system("pause");
					system("cls");
					goto loop;
	
		}
		break;
	} 
    link *pList = NULL;
	pList = creation ();

	//读数据
	read ( pList );
	
	while ( 1 )
	{
		char choice = menu();
		int c1;
		//从缓存区读取一个字节
		getchar();

		switch ( choice )
		{
			case '1':
				//增加商品信息
				c1= menu1();
				if(c1=='1'){
					insert ( pList );
					system("pause");
					system("cls");
					break;
				} 
				else if(c1=='2'){
				    Modify(pList);
				    system("pause");
					system("cls");
					break;
				}
				else if(c1=='3')
				{
					system("pause");
					system("cls");
					break;
				}
				
			case '2':
					//删除商品信息
				c1= menu2();
				if(c1=='1') {
					Modify( pList );
					system("pause");
					system("cls");
					break;
				}
				else if(c1=='2') {
					delete_from_list ( pList );
					system("pause");
					system("cls");
					break;
				}
				
				else{
					system("pause");
					system("cls");
					break;
				} 
					
					
			case '3':
					//查找商品信息
			  
			    c1= menu3();
			   if(c1=='1'){
					show(pList);
					system("pause");
					system("cls");
					break;
				} 
				else if(c1=='2'){
					find (pList);
					system("pause");
					system("cls");
					break;
				}
				else if(c1=='3'){
					
					goto loop1;
					loop1:menu();
					break;
				}
				else{
					system("pause");
					system("cls");
					break;
				}
				
			
                
			case '5':
				
					Stores ( pList );
					system("pause");
					system("cls");
					return 0;
					
			case '4':
					sort(pList);
					break;
					
			case '6':
					chen(1);
					break;
			default:
					//提示
					printf ("输入的信息有误,请重新输入!!!\r\n");
					system("pause");
					system("cls");
					break;
		}
	}
	return 0;
} 


				 
				
		
  
 
	


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值