300分代改C语言程序(链表)

300分代改C语言程序(链表),第1张

修改后:

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<malloc.h>

#define NULL 0

#define LEN sizeof(struct good)

struct good

{

long number

char name[20]

int bianhao

struct good*next

}

int m

void creat(struct good *head)

{

struct good*p1,*p2

m=1

printf("商品信息:商品号 进货数量没唯 商品名称\n")

p2=head

do

{

p1=(struct good*)malloc(LEN)

p1->next = NULL

scanf("%ld,%d,%s",&p1->number,&p1->bianhao,p1->枯晌培name)

p2->next=p1

p2=p1

m++

}while(p1->number!=0)

}

void print(struct good *head)

{

struct good*p

printf("库存情况为:\n")

p=head->next

while(p!=NULL)

{

printf("%ld,%s,%d\n",p->number,p->name,p->bianhao)

p=p->next

}

}

void sell(struct good *head,long num,int q)

{

struct good *p1,*p2

if(head->next==NULL)

{

printf("抱歉,数据出错")

return

}

p2=head

p1=head->next

while((num!=p1->number)&&(p1->next!=NULL))

{

p2=p1

p1=p1->next

}

if(num==p1->number)

{

p1->bianhao=(p1->bianhao-q)

if(p1->bianhao<=0)

{

p2->next=p1->next

free(p1)

}

}

else

{

printf("抱歉,你搜索的商品并不存在,请重新输入")

}

}

void input(struct good *head,int q,struct good *goo)

{

struct good*p1,*p2,*p0

if(head->next == NULL)

{

return

}

p1=head->next

p2=head

while((goo->number != p1->number) &&(p1->next!=NULL))

{

p2=p1

p1=p1->next

}

if(goo->number==p1->谨桥number)

{

p1->bianhao=(p1->bianhao+q)

free(goo)

return

}

p1=head->next

p2=head

p0=goo

while((p0->number>p1->number)&&(p1->next!=NULL))

{

p2=p1

p1=p1->next

}

p2->next=p0

p0->next=p1

m=m+1

}

void del(struct good*head,long num)

{

struct good*p1,*p2

if(head->next==NULL)

{

printf("抱歉,你搜索的商品并不存在,请重新输入")

return

}

p1=head->next

while((num!=p1->number)&&(p1->next!=NULL))

{

p2=p1

p1 = p1->next

}

if(num==p1->number)

{

p2->next=p1->next

free(p1)

m=m-1

}

else

{

printf("抱歉,你搜索的商品并不存在,请重新输入")

}

}

void delAll(struct good*head)

{

struct good*p1,*p2

if(head->next==NULL)

{

printf("抱歉,你搜索的商品并不存在,请重新输入")

return

}

p1=head->next

while(p1->next!=NULL)

{

p2=p1

p1 = p1->next

free(p2)

p2=NULL

m=m-1

}

}

int main()

{

int n,c,j

struct good *head,*goo

long del_num

head = (struct good*)malloc(LEN)/*开辟一个新单元*/

head->next = NULL

do

{

printf(" ☆菜单☆\n 1.输入商品信息\n 2.销售\n 3.进货\n 4.列举商品信息\n 5.清除指定商品\n 6.退出\n请输入您想选择的项目对应的数字编号:\n")

scanf("%d",&n)

switch(n)

{

case 1:

printf("请输入商品信息:\n")

creat(head)

break

case 2:

printf("销售\n")

printf("请输入商品编号:")

scanf("%ld",&c)

printf("请输入销售数量:")

scanf("%d",&j)

sell(head,c,j)

print(head)

break

case 3:

printf("进货\n")

goo = (struct good *)malloc(sizeof(struct good))

printf("商品信息:商品号 进货数量 商品名称\n")

scanf("%ld,%d,%s",&goo->number,&goo->bianhao,goo->name)

input(head,goo->bianhao,goo)

break

case 4:

printf("列举商品信息")

print(head)break

case 5:

printf("请输入您想删除的商品的对应编号\n")

scanf("%ld",&del_num)

del(head,del_num)

print(head)

break

case 6:

printf("感谢您的使用,欢迎提出意见和建议,我们致力于为您生活更美好而奋斗,再见!\n")

break

default:

printf("抱歉,没有这个选项,我们将通知程序设计者\n")

break

}

}while(n!=6)

delAll(head)

free(head)

return(0)

}

有三种方法可以实现,分别如下:

第1种、将文件中数据读入内存中,修改后,清空源文件,存入新数据。

第2种、以读写的方式打开文件,将文件指针移动到要修改的地方,写入新数据。新数据将会覆盖掉旧数据。

第3种、以读写的方式打开文件,将文件指针定位到需要修改数据的末尾,然后删除需要修改的数据(通过循环n次执行fputc(8,fp),直到清空需要修改的数据为止,8为退格键对应的ascii)。 然后计算需要加入的新数据长度,通过fputc(32,fp)来添加空格到文件中(32为空格键的ascii). 然后根据指针位置,填入数据覆盖掉这些空格。

注意:第1种方法适合数据量较小的情况,第2种、第3种适合数据量较大的情况。

提供一个参考代码,如下:

#include "stdio.h"

main()

{

    FILE *in=fopen("f:\\test.txt","r")

    FILE *out=fopen("f:\\back.txt","w")  /*out是充当临时文件作用*/

    int ch=0

   友让空 if(!in)

    {

 滑瞎       printf("cann't open test.txt\n")

        exit(1)

    }

    if(!out)

    {

        printf("cann't create back.txt\n")

        exit(1)

    }

        /*开始复制*/

    while(!feof(in))

    {

       ch=fgetc(in)

    好瞎   if(ch=='a') ch='p'

       fputc(ch,out)

    }

    fclose(in)  fclose(out)  

    unlink("f:\\test.txt") /*删除test.txt*/

    rename("f:\\back.txt","test.txt") /*改名*/

    printf("ok!!!!!!\n")

 

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12473879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存