#include<stdio.h>
struct gupiao /*股票的结构声明*/
{
int num;
char name[15];
char zhangdie;
float xianjia;
float zuigao;
float zuidi;
float jinkai;
float zuoshou;
float bianhua;
float baifenbi;
}; /*定义6个股票的信息:*/
#define GUP_NUMBER 6 /*宏定义股票数为6*/
struct gupiao Gup[GUP_NUMBER]={{601766,"zgnc", 'Z', 6.38,6.38,6.38,6.38,5.80,0,0},
{601288,"nyyh", 'Z', 3.71,3.77,3.50,3.55,3.54,0,0},
{600601,"fzkj", 'D', 4.68,4.70,4.61,4.69,4.72,0,0},
{601588,"bcsy", 'Z', 4.79,4.79,4.30,4.34,4.35,0,0},
{600321,"gdjs", 'D', 3.83,3.88,3.73,3.84,3.88,0,0},
{601618,"zgzy", 'Z', 5.05,5.15,4.96,5.07,5.02,0,0} };
int SelectMainMenu(); /*显示并选择主菜单。返回:被选择的菜单项*/
/*所有的函数声明:*/
void Show(); /*股票查询模块*/
void Modify(); /*股票修改模块*/
void Stat(); /*股票统计模块*/
void StatAve(float *Avexianjia,float *Avezuigao,float *Avezuidi,float *Avejinkai,float *Avezuoshou);/*统计平均数值*/
int Findgupiao(int num); /*根据代码查找某一股票*/
void SetAllTotle(); /*计算所有股票*/
void SetTotle(int No); /*计算第No个股票涨跌变化*/
void ShowAllgupiao(); /*显示所有股票*/
void ShowTitle(); /*显示表头信息*/
void Showgupiao(int No); /*显示第No个的股票信息*/
void Buggupiao(); /*购买股票*/
int main()
{
int isend=0,password=0;/*程序结束标志量,0表示不结束,1表示结束*/
SetAllTotle();/*计算所有学生总分*/
while(password!=667788)
{printf("请输入密码:\n");
scanf("%d",&password);
}
while(1)
{
switch(SelectMainMenu())/*显示并选择主菜单*/
{
case 1:/*股票查询*/
Show();/*调用股票查询函数*/
break;
case 2:/*股票修改*/
Modify();/*调用股票修改函数*/
break;
case 3:/*股票统计*/
Stat();/*调用股票统计函数*/
break;
case 4:/*股票购买*/
Buggupiao();
break;
case 5:/*退出系统*/
printf("\n谢谢您的使用,再见!\n\n");
isend=1;/*设置程序结束标志*/
break;
}
if(isend)break;/*程序结束*/
}
}
int SelectMainMenu()/*显示并选择主菜单。返回:被选择的菜单项*/
{
int menu_no=0;
do/*显示并选择菜单*/
{
printf("=================================\n");
printf(" <股票管理系统>\n");
printf(" 作者:洪增辉\n");
printf("=================================\n");
printf(" 主菜单:\n");
printf(" 1 - 股票查询\n");
printf(" 2 - 股票修改\n");
printf(" 3 - 股票统计\n");
printf(" 4 - 购买股票\n");
printf(" 5 - 退出系统\n");
printf(" 请选择(1-5):");
scanf("%d",&menu_no);
}while(menu_no<1 && menu_no>5);/*直到选择出正确的序号*/
return menu_no;
}
void Show() /*股票查询模块*/
{
int n,i;
while(1)
{
printf("\n====股票查询====\n");
printf("请输入代码(输入 0 退出股票,输入 1 查询全部):");
scanf("%d",&n);
if(n==0)break;/*退出股票查询模块*/
if(n==1)ShowAllgupiao();/*显示所有股票*/
else if((i=Findgupiao(n))>=0)/*找到代码为n的股票*/
{
ShowTitle();/*显示表头信息*/
Showgupiao(i);/*显示该股票信息*/
}
else printf("----找不到代码----\n");
}
}
void Modify()/*股票修改模块*/
{
int n,i,a;
while(1)
{
printf("\n====股票修改====\n");
printf("请输入代码(输入 0 退出股票修改):");
scanf("%d",&n);
if(n==0)break;/*退出股票修改模块*/
if((i=Findgupiao(n))>=0)/*找到代码为n的股票*/
{
ShowTitle();/*显示表头信息*/
Showgupiao(i);/*显示该股票信息*/
printf("请输入要修改的价格:\n");
printf("1 现价\n");
printf("2 最高价格\n");
printf("3 最低价格\n");
printf("4 今开价格\n");
printf("5 昨收价格\n");
scanf("%d",&a);
switch(a)
{case 1:printf("请输入现价:\n");
scanf("%f",&Gup[i].xianjia);break;
case 2:printf("请输入最高价格:\n");
scanf("%f",&Gup[i].zuigao);break;
case 3:printf("请输入最低价格:\n");
scanf("%f",&Gup[i].zuidi);break;
case 4:printf("请输入今开价格:\n");
scanf("%f",&Gup[i].jinkai);break;
case 5:printf("请输入昨收价格:\n");
scanf("%f",&Gup[i].zuoshou);break;
default:printf("输入错误");
}
SetTotle(i);/*计算该股票总分*/
printf("----修改成功----\n");
}
else printf("----找不到代码----\n");
}
}
void Stat()/*股票统计模块*/
{
float Avexianjia, Avezuigao, Avezuidi, Avejinkai,Avezuoshou;
StatAve(&Avexianjia,&Avezuigao,&Avezuidi,&Avejinkai,&Avezuoshou);/*统计各个参数平均数*/
printf("\n====股票统计====\n");
printf("股票数目:%d\n",GUP_NUMBER);
printf("现价:%6.2f\n",Avexianjia);
printf("最高:%6.2f\n",Avezuigao);
printf("最低:%6.2f\n",Avezuidi);
printf("今开:%6.2f\n",Avejinkai);
printf("昨收:%6.2f\n",Avezuoshou);
}
/*统计各个参数平均数,用指针参数返回*/
void StatAve(float *Avexianjia,float *Avezuigao,float *Avezuidi,float *Avejinkai,float *Avezuoshou)
{
int i;
float sumxianjia=0,sumzuigao=0,sumzuidi=0,sumjinkai=0,sumzuoshou=0;
for(i=0;i<GUP_NUMBER;i++)
{
sumxianjia+=Gup[i].xianjia;
sumzuigao+=Gup[i].zuigao;
sumzuidi+=Gup[i].zuidi;
sumjinkai+=Gup[i].jinkai;
sumzuoshou+=Gup[i].zuoshou;
}
*Avexianjia=sumxianjia/GUP_NUMBER;
*Avezuigao=sumzuigao/GUP_NUMBER;
*Avezuidi=sumzuidi/GUP_NUMBER;
*Avejinkai=sumjinkai/GUP_NUMBER;
*Avezuoshou=sumzuoshou/GUP_NUMBER;
}
/*根据代码查找某一股票。返回:-1没找到,>=0该股票所在数组的下标*/
int Findgupiao(int num)
{
int i;
for(i=0;i<GUP_NUMBER;i++)
if(Gup[i].num==num)break;
if(i<GUP_NUMBER)return i;/*找到*/
else return -1;/*没找到*/
}
void SetAllTotle()/*计算所有股票*/
{
int i;
for(i=0;i<GUP_NUMBER;i++)
SetTotle(i);
}
void SetTotle(int No)/*计算第No个股票*/
{
Gup[No].bianhua=Gup[No].xianjia-Gup[No].zuoshou;
Gup[No].baifenbi=(Gup[No].bianhua/Gup[No].zuoshou)*100;
}
void ShowAllgupiao()/*显示所有股票*/
{
int i;
ShowTitle();
for(i=0;i<GUP_NUMBER;i++)
Showgupiao(i);
}
void ShowTitle()/*显示表头信息*/
{
printf("\n%12s%12g%12g%10g%10g%10g%10g%10g\n","dai ma","name","zhang die",
"xianjia","zuigao","zuidi","jinkai","zuoshou","bian hua","bianfenbi");
}
void Showgupiao(int No)/*显示第No个的股票信息*/
{
printf("%12d%6s%6c%6g%6g%6g%6g%6g%10.2g%10g\n",
Gup[No].num,Gup[No].name,Gup[No].zhangdie,Gup[No].xianjia,
Gup[No].zuigao,Gup[No].zuidi,Gup[No].jinkai,Gup[No].zuoshou,Gup[No].bianhua,Gup[No].baifenbi);
}
void Buggupiao() /*股票购买模块*/
{
int n,i,m,s,p,sm;
while(1)
{
printf("\n====股票购买====\n");
printf("请输入代码(输入 0 退出股票)和拥有金钱:");
scanf("%d%d",&n,&m);
if(n==0)break;/*退出股票购买模块*/
else if((i=Findgupiao(n))>=0)/*找到代码为n的股票*/
{
s=(m/Gup[i].xianjia)/100;
p=s*100;
sm=m-Gup[i].xianjia*p;
printf("可购买数量%d剩余金钱%d",p,sm);
}
else printf("----找不到代码----\n");
}
}