#include <conio.h>
#include <stdlib.h>
//#include"FILE.h"
typedef struct shangpin
{
char name[20]
int biaohao
int shuliang
double jiage
}Node
typedef struct list
{
Node data
struct list *next
}List ,*Slist
void charu(Slist &s,Node x)
int caidan()
{
int i
while(1)
{
system("cls")
printf("\n\t\t\t商店销售管理系统\n\n")
printf("\t\t **********************************************\n\n")
printf("\t\t 1--添加商品 2--出售商品\n\n")
printf("\t\t 3--删除商品 4--显示商品\n\n")
printf("\t\t 5--查找商品 0--退出系统\n\n")
printf("\t\t **********************************************\n\n")
printf("请选择(0-5): ")
scanf("%d",&i)
if(i<6&&i>=0)break
}
return i
}
void chushihua(Slist &s)
{
Slist head=new List
head->next=NULL
s=head
}
void tianjia(Slist &s)
{
Node x
Slist r,p,q
int i=0
r=new List
r->next=NULL
q=p=s->next
while(q)
{
i++
q=q->next
}
printf("请输入商品名称、数量以及价格: ")
scanf("%s%d%lf",x.name,&x.shuliang,&x.jiage)
x.biaohao=i+1
r->data=x
s->next=r
r->next=p
}
void duchu(Slist &s)
{
FILE *cp//定义文件指针
int i=0
Node x
if((cp=fopen("shangpin.txt","a+"))==NULL)//打开文件
{printf("文件打开失败!")return }
fseek(cp,0L,SEEK_SET)//将文件指针移动到文件开头
while(!feof(cp))
{
fscanf(cp,"%s%d%lf",x.name,&x.shuliang,&x.jiage)//读取文件的内容
x.biaohao=++i
charu(s,x)//把读到的数据插入到链表上
}
fclose(cp)//关闭文件
}
void charu(Slist &s,Node x)
{
Slist r,p=s->next
r=new List
r->next=NULL
r->data=x
s->next=r
r->next=p
}
void xieru(Slist s)
{
FILE *cp//同上
Slist p=s->next
if((cp=fopen("shangpin.txt","w+"))==NULL)
{printf("文件打开失败!")return }//同上
while(p)
{
fprintf(cp,"%s %d %.2lf ",p->data.name,p->data.shuliang,p->data.jiage)//写入文件中,并且以空格隔开
p=p->next
}
fclose(cp)//关闭文件
}
Slist chazhao(Slist s,int m)
{
Slist p=s->next
if(p==0)return 0
if(p->next==0)return p
if(p->data.biaohao==m)return p
while(p->next)
{
if(p->next->data.biaohao==m)return p
p=p->next
}
return 0
}
void xianshi(Slist s)
{
Slist p
p=s->next
printf("%10s%20s%10s%10s\n","商品编号","商品名称","商品数量","商品价格")
while(p)
{
printf("%10d%20s%10d%10.2f\n",p->data.biaohao,p->data.name,p->data.shuliang,p->data.jiage)
p=p->next
}
}
void shanchu(Slist &s)
{
}
void chushou(Slist &s)
{
}
void main()
{
int num
Slist s
chushihua(s)
duchu(s)
num=caidan()
while(1)
{
switch(num)
{
case 0:exit(0)break
case 1:tianjia(s)break
case 2:chushou(s)break
case 3:shanchu(s)break
case 4:xianshi(s)break
case 5:chazhao(s,1)break
}
printf("按任意键继续!")
getch()
num=caidan()
}
xieru(s)
}
//有些函数我在没写啊,你自己写写吧,文件的读取和写入,还有链表的创建我都给你写好了,也就是上面的1,4我都写好了,
//包括了文件,剩下的2,3,5,你自己试试,并且我也给你写了一个查找函数,你看看能用不!
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct node{
char no[20]//存放编号
char name[40]//存放名称
int reserve//库存
struct node *next
}NODE
typedef struct link{
NODE *front//头指针
NODE *rear//尾指针
}LINK;
NODE *create_node(void){
NODE *node = (NODE *)malloc(sizeof(NODE))
printf("请输入货物编号:")
gets(node->no)
printf("请输入货物名称:")
gets(node->name)
printf("请输入货物名称:")
char ch
while( (ch= getchar()) != '\n')//rewind(stdin)
scanf("%d",&node->reserve)
node->next = NULL
return node
}
void init_link(LINK *link){
link->rear = NULL
link->front = NULL
}
int link_empty(LINK *link){
return link->front == NULL ? 1: 0
}
int node_num(LINK *link){
int num = 0
if( link_empty(link)){
return num
}
num = 1
NODE *node = link->front
while(node != link->rear){
node = node->next
++num
}
return num
}
/*NODE *node_find(LINK *link,const int n){
int num = node_num(link)
if(num <n){
printf("公有节点%d个,无法找到第%d个节点\n",num,n)
}
else{
}
}
*/
void node_push(LINK *link){
NODE *node = create_node()
if(link->front == NULL){
link->front = node
link->rear = node
node->next = NULL
}
else{
link->rear->next = node
link->rear = node
node->next = NULL
}
}
void node_insert(LINK *link,const int n){
int num = 0,i = 1
NODE *node = link->front
NODE *new_node = NULL
if ( link_empty(link) ){
printf("链表为空,将建立链表!\n")
node_push(link)
}
else{
if( n <= 1){
printf("在链表头插入数据\n")
new_node = create_node()
new_node->next = link->front
link->front = new_node
}
else if( n>= num = node_num(link) ){
printf("节点数少于%d,将在末尾插入节点.\n",n)
node_push(link)
}
else{
printf("在第n个节点后插入数据\n")
if(num >= n){
while( i != n){
node = node->next
++i
}
new_node = create_node()
new_node-next = node->next
node->next = new_node
}
}
}
void find_node_insert(LIKNK *link,const char *name){
NODE *node = link->front
if( link_empty(link) )
node_push(link)
else {
while(strcmp(node->name,name) != 0){
if(node != link->rear)
node = node->next
else break
}
if(node != NULL){
NODE *new_node = create_node()
new_node-next = node->next
node->next = new_node
}
else {
printf("没有找到相关货物,将在头节点插入数据\n")
intsert(link,0)
}
}
/*由于我不知到你对货物统计具体实现的要求,关于货物数量统计就你自己写了,应该比较简单。*/
/* 代码没有具体运行过,如果你学过C结构体与指针,就这个代码思路应该看得明白,真正的实现你自己实现吧
这样对你会更好写。可能会有错误的地方,请谨慎。 */
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)