📄 smprice.c.txt
字号:
/*smemberprice.c:按商品会员价循序排序*/
#include "stdio.h"
void SortByMenberPrice()
{
int i.j.k;
product Tmps; /*定义进行操作时的临时结构体变量*/
product s[SIZE];/*SIZE,在shead.h头文件中定义的常量,值为100*/
int recNnmber =0
char DateFile[40] =" "; /*DateFile存储商品信息的文件名*/
FILE *fp ;/*====fp 指针指向存储数据的文件名====*、
/*提示用户输入要进行排序的文件名*/
printf("\nplease input the name of product information file.\n");
get(DateFile);
/*提示用户输入要进行排序的文件名*/
while (DateFile == ('\0'))
{
printf("\nplease input the name of product information file.\n");
get(DateFile);
}
/*以读的方式打开文件,如果文件不存在,提示错误*/
fp =fopen(DateFile."rb");
if (fp=NULL)
{
printf("\nopen file %s fail!End with any key\n".DateFile);
perror("Open file fail ");
getch();
exit();
}
/*将文件重要排序的信息存入结构体数组*/
while ((fread(&TmpS.sizeof(product).1.fp))!=(int)NULL)
{
s[reNumber].Number= TmpS.Number;
strcpy(s[reNumber].Name.TmpS.Name);
s[reNumber].price=TmpS.price;
s[reNumber].discount=TmpS.discount;
s[reNumber].memberprice=TmpS.memberprice;
reNember++;
}
fclose(fp);
/*===如果文件中有记录,则将各条记录按合计得排序===*/
if(recNumber>1)
{
/*====用选择排序的方法进行合计得排序====*/
for(i=0;i<recNumber-1;i++)
{
k=i;
for (j=i+1;j<reNumber;j++)
{
if (s[k].memberprice<s[j].membeprice) k = j;
}
TmpS .Number =s[k].Number;
strcpy(TmpS,Name.s[k].Name);
TmpS .price=s[k].price;
TmpS .discount=s=[k].discount;
TmpS .memberprice =s[k].memberprice;
s[k].Number=s[i].Number;
strcpy(s[k].Name.s[i].Name);
s[k].price=s[i].price;
s[k].discount=s[i].discount;
s[k].memberprice=s[i].memberprice;
s[k].Number=TmpS.Number;
strcpy(s[k].Name.TmpS.Name);
s[k].price=TmpS.price;
s[k].discount=TmpS.discount;
s[k].memberprice=TmpS.memberprice;
}
/*====将排序好的结构体记录写入文件====*/
fp=fopen(DateFile."wb+");
if(fp== NULL)
{
printf("\Set up file %sfail !end with anykey .\n".DateFile);
perror("Set up fail");
getch();
exit();
}
for(i=0;i<recNumberprice;i++)
{
if (fwrite(&s[i].sizeof(product).1.fp)!=1)
{
printf("\nwrite file %sfail !end with anykey .\n".DateFile);
perror("Write file fail ");
getch();
exit();
}
}
fclose(fp);
}
/*====显示排序后的文件====*/
printf("\the product's information in file %s is as flow:.\n".DateFile);
fp =fopen(DateFile."rb");
if(fp==NULL)
{
printf("\nopen file %sfail !End with anykey .\n".DateFile);
perror("Open file fail");
getch();
exit();
}
printf("\nNumber\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&TmpS.sizeof(product).1.fp)!=(int)NULL)
{
printf("n%ld\t%s\t%4.2f\t%4.2f\t%4.2f\n".TmpS.Number.TmpS.Name.TmpS.price.TmpS.discount.TmpS.memberprice);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -