运用c语言程序写一个超市收银情况
答案:1 悬赏:20 手机版
解决时间 2021-04-07 10:38
- 提问者网友:酱爆肉
- 2021-04-06 20:39
运用c语言程序写一个超市收银情况
最佳答案
- 五星知识达人网友:污到你湿
- 2021-04-06 21:27
#include
#include
#define DIS 0.7
typedef struct buyrecond{
int id; //序号
char name[20]; //商品名称
float unitprice; //单价
int num; //数量
} BuyRecond;
typedef struct buylist{
BuyRecond *list;
int maxSize;
int size;
} BuyList;
//建立线性表
BuyList *createList(int maxsize);
void destroyList(BuyList *list);
//向表内添加元素
int appendRecond(BuyList *list,BuyRecond *recond);
//显示线性表
void displayList(BuyList *list);
int main()
{
int e;
BuyList *buyTab;
buyTab = createList(3);
e=1;
while(e!=0){
BuyRecond rec;
printf("输入第%d种商品名称(输入-1结束输入):",e);
gets(rec.name);
if(rec.name[0]=='-' && rec.name[1]=='1' && rec.name[2]=='\0')
break;
printf("输入第%d种商品单价:",e);
scanf("%f",&rec.unitprice);
printf("输入第%d种商品数量:",e);
scanf("%d",&rec.num);
getchar();
if(!appendRecond(buyTab,&rec))
break;
}
displayList(buyTab);
destroyList(buyTab);
getchar();
return 0;
}
BuyList *createList(int maxsize)
{
BuyList *pl = (BuyList *)malloc( sizeof(BuyList) );
pl->list = (BuyRecond *)calloc(maxsize, sizeof(BuyRecond));
pl->size = 0;
pl->maxSize = maxsize;
return pl;
}
void destroyList(BuyList *list)
{
free( list->list );
free( list );
}
int appendRecond(BuyList *list,BuyRecond *recond)
{
if(list->size == list->maxSize){
BuyRecond *p = (BuyRecond *)calloc(list->size * 2, sizeof(BuyRecond));
if(p==NULL){
printf("内存分配失败!无法添加元素。\n");
return 0;
}
memcpy(p, list->list, list->size*sizeof(BuyRecond));
list->maxSize = list->size * 2;
free(list->list);
list->list = p;
}
recond->id=list->size+1;
list->list[list->size]=*recond;
list->size++;
return 1;
}
void displayList(BuyList *list)
{
int i;
float x;
printf("\n%-4s %c%-20s %10s %10s %10s %10s\n",
"项次",'|',"商品名称","单价","数量","小计","优惠价");
for(i=0;isize;++i){
x=(list->list[i].num)*(list->list[i].unitprice);
printf("%-4d %c%-20s %10.0f %10d %10.2f %10.2f\n",
list->list[i].id,'|',list->list[i].name,
list->list[i].unitprice,list->list[i].num,
x,x*DIS);
}
printf("总计(%d项)\n",list->size);
}
#include
#define DIS 0.7
typedef struct buyrecond{
int id; //序号
char name[20]; //商品名称
float unitprice; //单价
int num; //数量
} BuyRecond;
typedef struct buylist{
BuyRecond *list;
int maxSize;
int size;
} BuyList;
//建立线性表
BuyList *createList(int maxsize);
void destroyList(BuyList *list);
//向表内添加元素
int appendRecond(BuyList *list,BuyRecond *recond);
//显示线性表
void displayList(BuyList *list);
int main()
{
int e;
BuyList *buyTab;
buyTab = createList(3);
e=1;
while(e!=0){
BuyRecond rec;
printf("输入第%d种商品名称(输入-1结束输入):",e);
gets(rec.name);
if(rec.name[0]=='-' && rec.name[1]=='1' && rec.name[2]=='\0')
break;
printf("输入第%d种商品单价:",e);
scanf("%f",&rec.unitprice);
printf("输入第%d种商品数量:",e);
scanf("%d",&rec.num);
getchar();
if(!appendRecond(buyTab,&rec))
break;
}
displayList(buyTab);
destroyList(buyTab);
getchar();
return 0;
}
BuyList *createList(int maxsize)
{
BuyList *pl = (BuyList *)malloc( sizeof(BuyList) );
pl->list = (BuyRecond *)calloc(maxsize, sizeof(BuyRecond));
pl->size = 0;
pl->maxSize = maxsize;
return pl;
}
void destroyList(BuyList *list)
{
free( list->list );
free( list );
}
int appendRecond(BuyList *list,BuyRecond *recond)
{
if(list->size == list->maxSize){
BuyRecond *p = (BuyRecond *)calloc(list->size * 2, sizeof(BuyRecond));
if(p==NULL){
printf("内存分配失败!无法添加元素。\n");
return 0;
}
memcpy(p, list->list, list->size*sizeof(BuyRecond));
list->maxSize = list->size * 2;
free(list->list);
list->list = p;
}
recond->id=list->size+1;
list->list[list->size]=*recond;
list->size++;
return 1;
}
void displayList(BuyList *list)
{
int i;
float x;
printf("\n%-4s %c%-20s %10s %10s %10s %10s\n",
"项次",'|',"商品名称","单价","数量","小计","优惠价");
for(i=0;i
x=(list->list[i].num)*(list->list[i].unitprice);
printf("%-4d %c%-20s %10.0f %10d %10.2f %10.2f\n",
list->list[i].id,'|',list->list[i].name,
list->list[i].unitprice,list->list[i].num,
x,x*DIS);
}
printf("总计(%d项)\n",list->size);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯