#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. SpringDataRedis配置

    1.父pom.xml配置依赖包 <dependencyManagement> <dependencies> <dependency> <groupId> ...

  2. 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)

    题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...

  3. [BJOI2014]大融合(Link Cut Tree)

    [BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...

  4. expect批量分发密钥对

    vim shell.exp #!/usr/bin/expect set timeout 10 set hostname [lindex $argv 0] set username [lindex $a ...

  5. 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。

    今天使用npm安装插件时出现了以下错误: 经查,原因:现用执行策略是 Restricted(默认设置) 解决办法: 1.win+X键,使用管理员身份运行power shell 2.输入命令:set-e ...

  6. [转载]汇编语言assume伪指令的作用

    原文:https://blog.csdn.net/u010234808/article/details/38366943 摘出关键部分: 编写程序,是写给编译软件的.由编译软件,编译成机器码,再去控制 ...

  7. Java中字符串排序

    package com.fs.test; import java.util.ArrayList; import java.util.Collections; import java.util.List ...

  8. 87. Scramble String (Java)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 设置Windows静态IP+动态IP

    静态IP 设置以太网属性 进入IPv4属性 设置IPv4 动态IP 同上方法,只不过选成了自动

  10. 【异常】org.apache.phoenix.exception.PhoenixIOException: SYSTEM:CATALOG

    1 详细异常信息 rror: SYSTEM:CATALOG (state=,code=) org.apache.phoenix.exception.PhoenixIOException: SYSTEM ...