#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm> class Users{
private:
std::map<std::string,std::string> m;
public:
Users(){m["boss"]=std::string("123");}
bool addUser(std::string username,std::string password)
{
auto it=m.find(username);
if(it!=m.end()) return false;
m[username]=password;
return true;
}
bool check(std::string username,std::string password)
{
auto it = m.find(username);
if(it==m.end()) return false;
if(it->second!=password) return false;
return true;
}
bool login()
{
puts("输入用户名:");
std::string uname;
std::cin >> uname;
puts("输入密码:");
std::string passw;
std::cin >> passw;
if (!check(uname, passw))
{
puts("用户名或密码错误");
system("sleep 0.6");
return false;
}
puts("登录成功");
system("sleep 0.6");
system("cls");
return true;
}
}users; class CandyList{
private:
std::map<std::string,int> list;//名字到id的映射
std::vector<std::string> nameList;
public:
CandyList(){nameList.resize(1);}
int queryId(std::string name)//传入名字,返回id,不存在返回0
{
auto it = list.find(name);
if(it!=list.end())
return it->second;
return 0;
}
std::string queryName(int id)
{
if(id>nameList.size()) return std::string("");
return nameList[id];
}
int addCandyKind(std::string name)//无论有没有,都返回id
{
int id=queryId(name);
if(id) return id;
id=list.size()+1;
// printf("&&DEBUG&&\n%s %d\n\n",name.c_str(),id);
list[name]=id;
nameList.push_back(name);
return id;
}
}candyList; class Store{//仓库
private:
std::vector<std::pair<int, int> > s; //数量、单价
int sum;
public:
Store()
{
s.clear();
s.resize(100);
sum=0;
}
int getSum(){return sum;}
int getPrice(int id){return s[id].second;}
void displayCandy()
{
// std::cout<<s.size()<<" ***"<<std::endl;
std::cout << "编号\t品名\t剩余数量\t单价" << std::endl;
for(int i=0;i<s.size();i++)
{
if(s[i].first>0)
{
std::cout<<i<<"\t"<<candyList.queryName(i)<<"\t"<<s[i].first<<"\t"<<s[i].second<<std::endl;
}
}
}
void addCandy(std::string name,int num,int price=-1)
{
bool ok=1;
if(num<1)
ok=0,puts("数量错误");
if(price<-1||price==0)
ok=0,puts("价格错误");
if(!ok) return;
int id=candyList.addCandyKind(name);
// if(s.capacity()<id-1)
// {
// // std::cout<<"haha"<<std::endl;
// if(price==-1)
// {
// puts("新品必须定价");
// return;
// }
// s.resize(id+1);//仓库扩容
// s[id]=std::make_pair(num,price);
// return;
// }
s[id].first+=num;
sum+=num;
if(~price) s[id].second=price;//更新价格
}
int rmCandy(int id,int num,int pay)//卖糖
{
if(!id)
{
puts("查无此糖");
return 0;
}
if(s[id].first<num)
{
puts("数量不足");
return 0;
}
int totPrice=num*s[id].second;
if(pay<totPrice)
{
puts("钱不够");
return 0;
}
s[id].first-=num;
sum-=num;
return pay-totPrice;
}
}store; class FrontPage{//前端
private:
inline void read(int &x)
{
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
x = s * w;
} public:
FrontPage(){store.addCandy(std::string("默认糖"),10,10);}
void run()
{
printf("启动中");
for(int i=1;i<=5;i++)
system("sleep 0.1"),printf(".");
while(1)
{
system("cls");
if(store.getSum()) printf("营业中!\n买糖请输入1,");
else printf("缺货,");
puts("加糖请输入2");
int type=0;
read(type);//提高容错性
if(type==2)
{
if(!users.login()) continue;
puts("请输入品名");
std::string name;
std::cin>>name;
int id=candyList.queryId(name);
puts("请输入数量");
int num,price;
bool transPrice=0;
read(num);
if(!id)
puts("输入定价(分/个)"),read(price),transPrice=1;
else
{
printf("要更改该商品定价请输入1,保持原定价%d 分/个 请输入0\n",store.getPrice(id));
int temp;
read(temp);
if(temp)
{
puts("输入新定价(分/个)"),read(price),transPrice=1;
}
}
if(transPrice) store.addCandy(name,num,price);
else store.addCandy(name,num);
continue;
}
if(type==1)
{
puts("欢迎光临");
store.displayCandy();
puts("输入编号");
int id;
read(id);
puts("输入数量");
int num;
read(num);
puts("输入付款数量");
int pay;
read(pay);
int temp=store.rmCandy(id,num,pay);
if(temp) std::cout<<"给你找零"<<temp<<"分"<<std::endl<<"欢迎再次光临";
system("sleep 2");
system("cls");
continue;
}
puts("输入错误");
system("sleep 2");
}
}
}; int main()
{
FrontPage temp;
temp.run();
return 0;
}

C++自动糖果贩卖机的更多相关文章

  1. 自动贩卖机VS无人门店:谁是真正的零售新风口?

    ​ ​原本在线上不断发力,让实体店几乎凋敝的电商,却忽然对线下兴趣大增.阿里疯狂入股.收购线下商超:京东要在全国范围内开设百万家便利店,仅在农村就将开设50万家--这一股浪潮,或将直接改变整个百货零售 ...

  2. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  3. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

  4. 有限状态机FSM(自动售报机Verilog实现)

    有限状态机FSM(自动售报机Verilog实现) FSM 状态机就是一种能够描述具有逻辑顺序和时序顺序事件的方法. 状态机有两大类:Mealy型和Moore型. Moore型状态机的输出只与当前状态有 ...

  5. AOI自动光学检测机技术在电路板检查中的应用

    1.简述 AOI技术在许多不同的制造业领域使用,自从电子影像技术开始发展,就被各种人利用在不同的应用领域.大家最熟悉的数字相机.数字摄影机是大家生活中最常用到的器材之一,而工业产品的生产也大量使用这些 ...

  6. 做一个自动修改本机IP和mac的bat文件

    原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...

  7. BZOJ_4590_[Shoi2015]自动刷题机_二分答案

    BZOJ_4590_[Shoi2015]自动刷题机_二分答案 Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题 ...

  8. SMD 自动点料机维修

    SMD 自动点料机维修 这个工具是一个好帮手,但是过完年回来发现坏了. 设置了数量不会自动停,按停止键没有反应,一定要按打印键才能停止. 这可愁死我了. 正常情况下开机设置好数量,然后开始点数,点到数 ...

  9. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置)

    目录 1. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置) 1.1. 需要的软件 1.2. 启动 proxy dhcp 服务 1.3. 关键的几个配置文件 PXE 自动安装物理机 ( ...

随机推荐

  1. C++练习 | 类的继承与派生练习(1)

    #include <iostream> #include <cmath> #include <cstring> #include <string> #i ...

  2. python PEP8常用规范(看完你会感谢我的!)

    完整的规范移步传送门 pep8规范 官方文档:[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/p ...

  3. spring boot入门与进阶教程

    SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spring Boot集 ...

  4. whistle 前端工具之抓包利器

    一.业务场景 前端本地开发的场景中,我们需要频繁的改动代码,并需要实时看到效果,并且在一些开发场景中,我们需要将特定的请求代理到特定的IP.本地文件等,所以使用fiddler或whistle等本地.真 ...

  5. 使用python的selenium库刷超星网课

    网课很多看不完呀 所以动手做了一个基础的自动答题和下一节的程序 用到了python 3 selenium Chrome 如何自动化Chrome?https://www.cnblogs.com/eter ...

  6. mongodb的简单操作记录

    由于项目上需要对mongodb进行监控,所以需要先熟悉下什么是mongobd以及mongodb的简单操作 mongodb的安装: curl -O https://fastdl.mongodb.org/ ...

  7. 18、nginx优化

    一.性能优化概述 基询imm能优化,那么在性能优化这一章,我们将分为如下几个方面做介绍 1.首先我们需要了解性能优化要考虑哪些方面. 2.然后我们需要了解性能优化必须要用到的压力测试工具ab. 3.最 ...

  8. The Linux Kernel 4.15.0官方文档内核语言风格解读(留)

    https://www.kernel.org/doc/html/v4.15/translations/zh_CN/coding-style.html 1.缩进 制表符是 8 个字符,所以缩进也是 8 ...

  9. Java入门指南-03 操作符与表达式

    一.赋值操作符 在 Java 语言里,等号称为赋值操作符.例:a = b + 100;注意,不要把 Java 语言理解为数学.在 Java 里,这个等号的作用是“赋值”,即右侧的值赋给左边的变量. 要 ...

  10. .htaccess 一段神奇的跳转代码

    <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_REFERER} ^.*(google|ask|yahoo|you ...