C++自动糖果贩卖机
#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++自动糖果贩卖机的更多相关文章
- 自动贩卖机VS无人门店:谁是真正的零售新风口?
原本在线上不断发力,让实体店几乎凋敝的电商,却忽然对线下兴趣大增.阿里疯狂入股.收购线下商超:京东要在全国范围内开设百万家便利店,仅在农村就将开设50万家--这一股浪潮,或将直接改变整个百货零售 ...
- 【BZOJ-4590】自动刷题机 二分 + 判定
4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 156 Solved: 63[Submit][Status ...
- BZOJ4590 自动刷题机
Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...
- 有限状态机FSM(自动售报机Verilog实现)
有限状态机FSM(自动售报机Verilog实现) FSM 状态机就是一种能够描述具有逻辑顺序和时序顺序事件的方法. 状态机有两大类:Mealy型和Moore型. Moore型状态机的输出只与当前状态有 ...
- AOI自动光学检测机技术在电路板检查中的应用
1.简述 AOI技术在许多不同的制造业领域使用,自从电子影像技术开始发展,就被各种人利用在不同的应用领域.大家最熟悉的数字相机.数字摄影机是大家生活中最常用到的器材之一,而工业产品的生产也大量使用这些 ...
- 做一个自动修改本机IP和mac的bat文件
原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...
- BZOJ_4590_[Shoi2015]自动刷题机_二分答案
BZOJ_4590_[Shoi2015]自动刷题机_二分答案 Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题 ...
- SMD 自动点料机维修
SMD 自动点料机维修 这个工具是一个好帮手,但是过完年回来发现坏了. 设置了数量不会自动停,按停止键没有反应,一定要按打印键才能停止. 这可愁死我了. 正常情况下开机设置好数量,然后开始点数,点到数 ...
- PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置)
目录 1. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置) 1.1. 需要的软件 1.2. 启动 proxy dhcp 服务 1.3. 关键的几个配置文件 PXE 自动安装物理机 ( ...
随机推荐
- python 爬虫--下载图片,下载音乐
#下载图片 imgUrl='http://www.pptbz.com/pptpic/UploadFiles_6909/201211/2012111719294197.jpg' r=requests.g ...
- jQuery Mobile Slider Widget 使用js控制
jQuery Mobile 滑动条控件 基本用法不用多说了,看这里: http://www.runoob.com/jquerymobile/jquerymobile-form-sliders.html ...
- luogu题解 P2419 【牛大赛Cow Contest】传递丢包
题目链接: https://www.luogu.org/problemnew/show/P2419 分析: "在交际网络中,给定若干元素和若干对二元关系,且关系具有传递性. 通过传递性推导出 ...
- MySQL两种内核对比
MySQL内核 https://blog.csdn.net/baichoufei90/article/details/83504446 关键字:全文索引 索引外置 两种内核:MyISAM 和InnoD ...
- Maven之私服配置
一.配置从私服下载 从私服下载主要是将 central 库的下载地址从https://repo1.maven.org/maven2/修改为私服地址,比如http://localhost:8081/re ...
- 小程序之textarea层级最高问题
1.textarea位于底部固定定位按钮下方,会导致点击底部按钮,textarea获取到焦点. 解决方法如下 view与textarea之间在聚焦和失去焦点进行切换 cursor-spacing是te ...
- 改变font-weight的数值,样式并不会改变的原因
通常情况下,一个特定的字体仅会包含少数的可用字重.若所指定的字重不存在直接匹配,则会通过字体匹配算法规则匹配使用邻近的可用字重.这也就是为什么我们有时候使用特定字重时没有“生效”,看起来跟其它字重差不 ...
- Linux使用Docker启动Elasticsearch并配合Kibana使用,安装ik分词器
注意事项 这里我的Linux虚拟机的IP地址是192.168.1.3 Docker运行Elasticsearch容器之后不会立即有反应,要等一会,等待容器内部启动Elasticsearch,才可以访问 ...
- win7-32位安装mysql-5.7.27
下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 参考链接 https://blog.csdn.net/qq_41307443/a ...
- pyquery:轻松、灵活的处理html
介绍 pyquery是一个专门用来解析html的库,从名字很容易想到jQuery,没错,这完全是仿照jQuery的语法实现的.如果用过jQuery,那么pyquery也很容易上手 初始化html py ...