#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#define FALSE 0
#define TURE 1
typedef struct{
int no;
int amount;
int price;
float currency;
}RecType;
typedef struct node{
RecType data;
struct node *lchild,*rchild;
}BSTNode;
typedef BSTNode *BSTree ;
BSTree t;
int inputs(char ch[10]);
int daydata(RecType R[5]);
void Printdata(RecType R[5]);
BSTree Sortedsum(RecType R[5]);
void PrintBST(BSTree t);
BSTree InsertBST(BSTree q,RecType R);
int searchBST(BSTree t,RecType R,BSTree f,BSTree *pp);
int visit(BSTree T);
int menu_select();
main()
{
int ch;
RecType r[5];
clrscr(); /*清屏*/
t==NULL;
do
{ch=menu_select();
switch(ch)
{case 1:daydata(r);break;
case 2:Printdata(r);break;
case 3:t=Sortedsum(r);PrintBST(t);break;
case 0:exit(0);
}}while(1);}
menu_select()
{
char *menu[]={"*************MENU*************",
"~-----------------------------~\n",
" |********收银台********|",
"**************MENU*************",
" |1:输入记录 2:输出记录 |",
" |3:汇总并输出 |",
" |0:退出 |",
"~------------------------------~"
};
char s[3]; /*以字符形式保存选择号*/
int c,i;
gotoxy(1,26); /*移动光标*/
printf("\npress any key enter menu......\n"); /*压任一键进入主菜单*/
getch(); /*输入任一键*/
clrscr(); /*清屏幕*/
gotoxy(1,1);
textcolor(WHITE); /*设置文本显示颜色为白色*/
textbackground(GREEN);
gotoxy(8,2); /*移动光标*/
putch(0xc9); /*输出左上角边框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*输出上边框水平线*/
putch(0xbb);
for(i=3;i<15;i++)
{
gotoxy(8,i);putch(0xba); /*输出左垂直线*/
gotoxy(52,i);putch(0xba);
} /*输出右垂直线*/
gotoxy(8,15);putch(0xc8);
for(i=1;i<44;i++)
putch(0xcd);
putch(0xbc);
window(9,3,51,14); /* 制作显示菜单的窗口,大小根据菜单条数设计*/
clrscr(); /*清屏*/
for(i=0;i<8;i++) /*输出主菜单数组*/
{
gotoxy(8,i+2);
cprintf("%s",menu[i]);
}
textbackground(BLACK);
window(1,1,53,25); /*恢复原窗口大小*/
gotoxy(8,16); /*移动光标*/
do{
printf("\n Enter you choice(0~3):"); /*在菜单窗口外显示提示信息*/
scanf("%s",s); /*输入选择项*/
c=atoi(s); /*将输入的字符串转化为整形数*/
}while(c<0||c>3); /*选择项不在0~3之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
inputs(char ch[10])
{
do{scanf("%s",ch);
if(strlen(ch)>10)
printf("\ntoo long !\n");
}while(strlen(ch)>10);
}
int daydata(RecType R[5])
{ int i=0;
char numstr[20];
printf("请输入5种商品的编号|数量|单价");
for(i=0;i<5;i++)
{ printf("\n输入第%d商品编号:",i+1);
inputs(numstr);
R[i].no=atol(numstr);
if(R[i].no==0)
break;
printf("\n输入第%d商品数量:",i+1);
inputs(numstr);
R[i].amount=atof(numstr);
printf("\n输入第%d商品单价:",i+1);
inputs(numstr);
R[i].price=atof(numstr);
R[i].currency=(R[i].amount)*(R[i].price);
}
return(i);
}
void Printdata(RecType R[5])
{
int i;
printf("记录号\t编号\t数量\t单价\t总额");
for(i=0;i<5;i++)
{printf("\n记录%d\t",i+1);
printf(" %d\t",R[i].no);
printf("%d\t",R[i].amount);
printf("%d\t",R[i].price);
printf("%f\t",R[i].currency);
}
}
BSTree Sortedsum(RecType R[5])
{
int i;
for(i=0;i<5;i++)
{ t=InsertBST(t,R[i]);
}
return t;
}
BSTree InsertBST(BSTree q,RecType R)
{ BSTree *pp,p, s;
pp=&p;
if(!searchBST(q,R,NULL,pp))
{ s=(BSTree *)malloc(sizeof(BSTNode));
s->data=R;
s->lchild=s->rchild=NULL;
if(!p) q=s;
else { if (R.no<p->data.no)
p->lchild=s;
else
p->rchild=s;
}
return(q); }
else {
p->data.amount=(p->data.amount)+(R.amount);
p->data.currency=(p->data.currency)+(R.currency);
return(q);}
}
int searchBST(BSTree t,RecType R, BSTree f,BSTree *p)
{ if(!t)
{ *p=f;
return(FALSE);
}
else {
if(R.no==t->data.no)
{ *p=t;
return (TURE);}
else{
if(R.no<t->data.no)
return(searchBST(t->lchild,R,t,p));
else return( searchBST(t->rchild,R,t,p));
} }}
void PrintBST( BSTree T) /*中序遍历树*/
{
if(T!=NULL)
{PrintBST(T->lchild);
printf("\n %d\t",T->data.no);
printf("%d\t",T->data.amount);
printf("%d\t",T->data.price);
printf("%f\t",T->data.currency);
PrintBST(T->rchild);
}
return;
}
评论0