找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1214|回复: 1
打印 上一主题 下一主题
收起左侧

c++商品销售统计程序,有没有大神给我看看呢

[复制链接]
跳转到指定楼层
楼主
ID:426839 发表于 2018-11-15 08:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
c++源程序如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include<fstream>
  5. #define null 0
  6. #include<iostream>
  7. #include <iomanip>
  8. #include<string>     //控制字符串的头文件
  9. using namespace std;
  10. class tong
  11. {
  12. public:
  13. tong();
  14. tong(char co[20],char n[20],
  15. char dw[20],double pr,int qu);
  16. ~tong();
  17. void save();
  18. void take();
  19. void Set();                         //商品信息录入
  20. void Alter();                       //商品信息修改
  21. void list();                        //商品信息显示
  22. void Display();
  23. float Checkout(int quantity);        //单个商品小结
  24. void Total();                        //总计
  25. void buy();                          //商品买入
  26. private :
  27. char code[20];char name[20];char danwei[20];double price;int quantity;
  28. };
  29. tong::tong(){}
  30. tong::tong(char co[20],char n[20],char dw[20],double pr,int qu){
  31. strcpy(code,co);
  32. strcpy(name,n);
  33. strcpy(danwei,dw);
  34. price=pr;
  35. quantity=qu;}
  36. tong::~tong(){}
  37. tong *s[50];
  38. int i=0;
  39. int j=0;
  40. char code[20];char name[20];char danwei[20];double price; int quantity;


  41. void tong::save() //写入数据至文件
  42. {
  43. ofstream outfile;
  44. outfile.open("F:\\商品订购\\商品订购.txt",ios::out);
  45. if(!outfile)
  46. {
  47. cout<<"cannot open the file!"<<endl;
  48. return ;
  49. }
  50. else
  51. outfile<<"商品代码及名称"<<""<<"商品单价"<<""<<"请输入商品数量"<<""<<"计量单位"<<endl;
  52. cout<<"商品代码及名称"<<""<<"商品单价"<<""<<"请输入商品数量"<<""<<"计量单位"<<endl;
  53. for(int k=0;k<i;k++)
  54. {
  55. cout<<setw(10)<<left<<s[k]->code<<setw(10)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(10)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
  56. outfile<<setw(10)<<left<<s[k]->code<<setw(10)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(10)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
  57. }
  58. cout<<"**********************************Save Success!***************************************"<<endl;
  59. outfile.close();
  60. }

  61. void tong::Set()                   //商品信息录入
  62. {
  63. cout<<"请输入商品代码及名称:"<<endl;
  64. cin>>code>>name;
  65. cout<<"请输入计量单位:"<<endl;
  66. cin>>danwei;
  67. cout<<"请输入商品单价:"<<endl;
  68. cin>>price;
  69. cout<<"请输入商品数量:"<<endl;
  70. cin>>quantity;
  71. j++;
  72. s[i]=new tong(code,name,danwei,price,quantity);   //没有循环
  73. i++;
  74. cout<<"信息录入成功!"<<endl;
  75. cout<<"是否继续录入?(y or n)"<<endl;
  76. if(getch()=='y')
  77. Set();
  78. else return;
  79. }

  80. void tong::Alter()//商品信息修改
  81. {
  82. cout<<"请输入您所修改的商品名称:"<<endl;
  83. cin>>name;
  84. for(int h=0;h<i;h++)
  85. if(strcmp(name,s[h]->name)==0)
  86. {
  87. char newcode[20];
  88. char newname[20];
  89. char newdanwei[20];
  90. double newprice;
  91. int newquantity;
  92. int n;
  93. cout<<"你想要修改:代码及名称按3) 、数量(请按4)"<<endl;
  94. cin>>n;
  95. switch(n)
  96. {
  97. case 1:
  98. { cout<<"请输入商品代码及名称:"<<endl;
  99. cin>>newcode[20]>>newname[20];
  100. s[h]->code[20]=newcode[20];
  101. s[h]->name[20]=newname[20];
  102. cout<<"数据修改成功!";
  103. }break;
  104. case 2:            
  105.               {                       
  106.                             cout<<"请输入计量单位:"<<endl;                       
  107.                             cin>>newdanwei;              
  108.                             s[h]->danwei[20]=newdanwei[20];                       
  109.                             cout<<"数据修改成功!";                  
  110.               }break;                    
  111. case 3:                    
  112.               {                       
  113.                             cout<<"请输入单价:"<<endl;                       
  114.                             cin>>newprice;           
  115.                             s[h]->price=newprice;                       
  116.                             cout<<"数据修改成功!";                        
  117.               }break;
  118. case 4:
  119. {
  120. cout<<"请输入数量:"<<endl;
  121. cin>>newquantity;
  122. s[h]->quantity=newquantity;
  123. cout<<"数据修改成功!";
  124. }break;
  125. }
  126. }
  127. else
  128. cout<<"你所要修改的商品的信息不存在,请修正后在更改!"<<endl;
  129. cout<<"是否继续修改?(y/n)"<<endl;
  130. if(getch()=='y')
  131. Alter();
  132. else return ;
  133. }



  134. void tong::list()                  //商品信息显示
  135. {
  136. cout<<"=========================================================="<<endl;
  137. cout<<"===================所有商品信息==================="<<endl;
  138. cout<<"=========================================================="<<endl;
  139. cout<<"货品代码=======货品名称======货品价格======货品数量======计量单位"<<endl;
  140. if(i==0)
  141. cout<<"系统中没有录入商品信息或该商品信息已被删除!"<<endl;
  142. for(int k=0;k<i;k++){
  143. cout<<setw(15)<<left<<s[k]->code<<setw(15)<<left<<s[k]->name<<setw(15)<<left<<s[k]->price<<setw(15)<<left<<s[k]->quantity<<setw(15)<<left<<s[k]->danwei<<endl;
  144. }
  145. }


  146. void tong::buy()                       //商品买入
  147. {
  148. cout<<"请输入你想购买的商品的代码及名称:"<<endl;
  149. cin>>code>>name;
  150. cout<<"请输入您想购买的商品的数量:"<<endl;
  151. cin>>quantity;
  152. Checkout(quantity);
  153. Display();
  154. Total();
  155. }
  156. void tong::Display()
  157. {
  158. int k;
  159. k=atoi(code)-1;
  160. cout<<"货品代码=======货品名称======货品价格======购买数量======计量单位=====小计(元)=="<<endl;
  161. cout<<setw(15)<<left<<s[k]->code
  162. <<setw(15)<<left<<s[k]->name
  163. <<setw(15)<<left<<s[k]->price
  164. <<setw(15)<<left<<quantity
  165. <<setw(15)<<left<<s[k]->danwei
  166. <<setw(15)<<left<<quantity*s[k]->price<<endl;
  167. }


  168. float tong::Checkout(int quantity)                    //单个商品小结
  169. {
  170. int k=0;
  171. float sum(0.0);
  172. sum=s[k]->price * quantity;
  173. return sum;
  174. }


  175. void tong::Total()                            //总结帐
  176. {
  177. float sum = 0;
  178. float factly;
  179. char GoOn;
  180. while(1)
  181. {
  182. cout<<"要结束商品买入请按'N/n',其继续买入请按'Y/y': "<<endl;
  183. cin>>GoOn;
  184. if(GoOn=='Y'||GoOn=='y'){      //int quantity;
  185. cout<<"请输入商品代码:"<<endl;
  186. cin>>code>>name;
  187. cout<<"请输入商品数量:"<<endl;
  188. cin>>quantity;
  189. sum+=Checkout(quantity);
  190. cout<<"你购买的商品为:"<<endl;
  191. Display();
  192. }
  193. else if(GoOn=='N'||GoOn=='n')
  194. break;
  195. }
  196. cout<<"----------------------------------------------------"<<endl;
  197. sum+=Checkout(quantity);
  198. cout<<"你应该付 "<<sum<<"元!"<<endl;
  199. cout<<"你实际付(元): ";
  200. cin>>factly;
  201. cout<<"应该找回你 "<<factly-sum<<"元!"<<endl;     //找零。
  202. }


  203. void caozuoa(int p){
  204. tong t;
  205. switch(p){
  206. case 0:t.Set();break;
  207. case 1:t. Alter ();break;
  208. case 2:t.save();break;
  209. case 8:exit(0);break;
  210. }
  211. }
  212. void caozuob(int p){
  213. tong t;
  214. switch(p){
  215. case 0:t.list();break;
  216. case 1:t.buy();break;
  217. case 8:exit(0);break;
  218. }
  219. }
  220. void main()
  221. {
  222. int a;
  223. char w;
  224. cout<<"*-------------------------------------------------*"<<endl;
  225. cout<<"***************************************************"<<endl;     
  226. cout<<"\n"<<endl;
  227. cout<<"*------------欢迎进入商品订购统计系统-------------*"<<endl;         
  228. cout<<"\n"<<endl;
  229. cout<<"***************************************************"<<endl;   
  230. do{
  231. cout<<"请选择您的操作:商品信息管理系统(M)商品订购系统(S)"<<endl;         
  232. cin>>w;
  233. cout<<"*---------------------菜单选项--------------------*"<<endl;
  234. cout<<"*-------------------------------------------------*"<<endl;
  235. cout<<"*-------------------------------------------------*"<<endl;
  236. cout<<"\n"<<endl;
  237. if(w=='M'||w=='m')
  238. {  cout<<"商品信息录入(请输入0) 修改信息(请输入1) 保存信息(请输入2) "<<endl;
  239. cin>>a;               
  240. caozuoa(a);               
  241. }        
  242. else if(w=='S'||w=='s')
  243. {           
  244.               cout<<"显示商品信息(请输入0)   商品买入(请输入1)  "<<endl;            
  245.               cin>>a;                 
  246.               caozuob(a);               
  247. }               
  248. else
  249. {               
  250.               cout<<"请输入'M' 、'm' 或'S' 、's'"<<endl;}               
  251. cout<<"您想继续进行其他操作吗?(y/n)"<<endl;                 
  252. cin>>w;  
  253. }
  254. while(w=='y');
  255. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-11-15 16:23 | 只看该作者
您这个问题给的信息不全,估计是没人能回答得了的,先转移到冷门区了,自行编辑补充一下吧,审核员会帮你重新转到热门问答区的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表