note int price

本文介绍了一个MySQL表tmp15的创建过程及字段说明,并通过多个示例演示了如何使用SQL进行算术运算、比较运算、逻辑运算等操作,包括求和、平均值计算、条件判断等。

创建表 tmp15 其中包括varchar类型的字段note 和int类型的字段price ,练习运算符(时间:60分钟)

5.1.1 任务描述

创建表 tmp15 其中包括varchar类型的字段note 和int类型的字段price ,使用运算符对表中不同的字段进行计算;使用逻辑操作符对数据进行逻辑操作;





mysql> create database yunsuanfu;

Query OK, 1 row affected (0.05 sec)

mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| csdn              |
| csdn3g            |
| csdn9              |
| fenzu              |
| gujianpeng        |
| mysql              |
| performance_schema |
| shujuku            |
| student            |
| waijian            |
| webnews            |
| yunsuanfu          |
| zuoye              |
+--------------------+
14 rows in set (0.00 sec)

mysql> use yunsuanfu;
Database changed
mysql> create table temp15(note varchar(20),price int(20));
Query OK, 0 rows affected (0.86 sec)

mysql> insert into temp15 values('张三','50');
Query OK, 1 row affected (0.09 sec)

mysql> insert into temp15 values('李四','40');
Query OK, 1 row affected (0.08 sec)

mysql> insert into temp15 values('王五','40');
Query OK, 1 row affected (0.11 sec)

mysql> insert into temp15 values('梨园春','65');
Query OK, 1 row affected (0.33 sec)

mysql> insert into temp15 values('水立方','65');
Query OK, 1 row affected (0.08 sec)

mysql> desc temp15;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| note  | varchar(20) | YES  |    | NULL    |      |
| price | int(20)    | YES  |    | NULL    |      |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> select * from temp15;
+-----------+-------+
| note      | price |
+-----------+-------+
| 张三      |    50 |
| 李四      |    40 |
| 王五      |    40 |
| 梨园春    |    65 |
| 水立方    |    65 |
+-----------+-------+
5 rows in set (0.00 sec)

mysql> select note,sum(price) `价格和` from temp15 group by note;
+-----------+-----------+
| note      | 价格和    |
+-----------+-----------+
| 张三      |        50 |
| 李四      |        40 |
| 梨园春    |        65 |
| 水立方    |        65 |
| 王五      |        40 |
+-----------+-----------+
5 rows in set (0.00 sec)



mysql> select note,max(price) `最高价格`,sum(price) `价格和`, avg(price) `平均工
资` from temp15 group by note;
+-----------+--------------+-----------+--------------+
| note      | 最高价格    | 价格和    | 平均工资    |
+-----------+--------------+-----------+--------------+
| 张三      |          50 |        50 |      50.0000 |
| 李四      |          40 |        40 |      40.0000 |
| 梨园春    |          65 |        65 |      65.0000 |
| 水立方    |          65 |        65 |      65.0000 |
| 王五      |          40 |        40 |      40.0000 |
+-----------+--------------+-----------+--------------+
5 rows in set (0.00 sec)






第一大题的答案

1.对tmp15表中的整数值字段price进行算术运算 
求和  平均值


价格的总和
mysql> select sum(price) as '总的价格和' from temp15;
+-----------------+
| 总的价格和      |
+-----------------+
|            260 |
+-----------------+
1 row in set (0.00 sec)



平均值的和
mysql> select avg(price) as '平均值' from temp15;
+-----------+
| 平均值    |
+-----------+
|  52.0000 |
+-----------+
1 row in set (0.00 sec)






第二大题的答案

对tmp15中的整型数值字段price进行比较运算,和60比较

mysql> select note,price,(price>60) from temp15;
+-----------+-------+------------+
| note      | price | (price>60) |
+-----------+-------+------------+
| 张三      |    50 |          0 |
| 李四      |    40 |          0 |
| 王五      |    40 |          0 |
| 梨园春    |    65 |          1 |
| 水立方    |    65 |          1 |
+-----------+-------+------------+
5 rows in set (0.00 sec)

mysql>








第三大题的答案

3.判断price值是否落在30~80区间;返回与70和30之间相比最大的值,判断price是否为IN列表(10, 20, 50, 35)中的某个值


判断price值是否落在30~80区间;
mysql> select  (price>=30 and price<=80) from temp15;
+---------------------------+
| (price>=30 and price<=80) |
+---------------------------+
|                        1 |
|                        1 |
|                        1 |
|                        1 |
|                        1 |
+---------------------------+
5 rows in set (0.00 sec)



返回与70和30之间相比最大的值

mysql> select  max(price) from temp15 where price between 30 and 70;
+------------+
| max(price) |
+------------+
|        65 |
+------------+
1 row in set (0.05 sec)




判断price是否为IN列表(10, 20, 50, 35)中的某个值

mysql> select * from  temp15  price where price in(10,20,50,35);
+--------+-------+
| note  | price |
+--------+-------+
| 张三  |    50 |
+--------+-------+
1 row in set (0.00 sec)










第四大题的答案
1--》  对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空;
2--》  使用LIKE判断是否以字母'd'开头;
3--》  使用REGEXP判断是否以字母'y'尾;
4--》  判断是否包含字母'g'或者'm'

填入的新数据

mysql> select * from temp15;
+-----------+-------+
| note      | price |
+-----------+-------+
| 张三      |    50 |
| 李四      |    40 |
| 王五      |    40 |
| 梨园春    |    65 |
| 水立方    |    65 |
| 18        |    90 |
| dd        |    77 |
| liy      |    77 |
| ggg      |    77 |
| mmm      |    77 |
+-----------+-------+
10 rows in set (0.00 sec)




















1--》的答案


插入一个新的数据
mysql> insert into temp15 values (18,90);
Query OK, 1 row affected (0.23 sec)

mysql> select * from temp15;
+-----------+-------+
| note      | price |
+-----------+-------+
| 张三      |    50 |
| 李四      |    40 |
| 王五      |    40 |
| 梨园春    |    65 |
| 水立方    |    65 |
| 18        |    90 |
+-----------+-------+
6 rows in set (0.00 sec)

1--》  对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空;
mysql> select note,note>8 from temp15;
+-----------+--------+
| note      | note>8 |
+-----------+--------+
| 张三      |      0 |
| 李四      |      0 |
| 王五      |      0 |
| 梨园春    |      0 |
| 水立方    |      0 |
| 18        |      1 |
+-----------+--------+
6 rows in set, 5 warnings (0.00 sec)



判断表tmp15中note字段是否为空;

mysql> select note,note<=>null from temp15;
+-----------+-------------+
| note      | note<=>null |
+-----------+-------------+
| 张三      |          0 |
| 李四      |          0 |
| 王五      |          0 |
| 梨园春    |          0 |
| 水立方    |          0 |
| 18        |          0 |
+-----------+-------------+
6 rows in set (0.10 sec)






2--》  使用LIKE判断是否以字母'd'开头;

mysql> select note,note like '%d' from temp15;
+-----------+----------------+
| note      | note like '%d' |
+-----------+----------------+
| 张三      |              0 |
| 李四      |              0 |
| 王五      |              0 |
| 梨园春    |              0 |
| 水立方    |              0 |
| 18        |              0 |
| dd        |              1 |
| liy      |              0 |
| ggg      |              0 |
| mmm      |              0 |
+-----------+----------------+
10 rows in set (0.00 sec)





3--》  使用REGEXP判断是否以字母'y'尾;


mysql> select note,note regexp 'y$' from temp15;
mysql> select note,note regexp 'y$' from temp15;
+-----------+------------------+
| note      | note regexp 'y$' |
+-----------+------------------+
| 张三      |                0 |
| 李四      |                0 |
| 王五      |                0 |
| 梨园春    |                0 |
| 水立方    |                0 |
| 18        |                0 |
| dd        |                0 |
| liy      |                1 |
| ggg      |                0 |
| mmm      |                0 |
+-----------+------------------+
10 rows in set (0.00 sec)

mysql>








4--》  判断是否包含字母'g'或者'm'



mysql> select note,note regexp 'g|f' from temp15;
+-----------+-------------------+
| note      | note regexp 'g|f' |
+-----------+-------------------+
| 张三      |                0 |
| 李四      |                0 |
| 王五      |                0 |
| 梨园春    |                0 |
| 水立方    |                0 |
| 18        |                0 |
| dd        |                0 |
| liy      |                0 |
| ggg      |                1 |
| mmm      |                0 |
+-----------+-------------------+
10 rows in set (0.00 sec)

mysql>





第五大题的答案

5.将price字段值与NULL,0进行逻辑运算



mysql> select note,price||0 from temp15;
+-----------+----------+
| note      | price||0 |
+-----------+----------+
| 张三      |        1 |
| 李四      |        1 |
| 王五      |        1 |
| 梨园春    |        1 |
| 水立方    |        1 |
| 18        |        1 |
| dd        |        1 |
| liy      |        1 |
| ggg      |        1 |
| mmm      |        1 |
+-----------+----------+
10 rows in set (0.01 sec)

mysql>






mysql> select note,price&&0 from temp15;
+-----------+----------+
| note      | price&&0 |
+-----------+----------+
| 张三      |        0 |
| 李四      |        0 |
| 王五      |        0 |
| 梨园春    |        0 |
| 水立方    |        0 |
| 18        |        0 |
| dd        |        0 |
| liy      |        0 |
| ggg      |        0 |
| mmm      |        0 |
+-----------+----------+
10 rows in set (0.00 sec)

mysql>






mysql> select note,price&&null from temp15;
+-----------+-------------+
| note      | price&&null |
+-----------+-------------+
| 张三      |        NULL |
| 李四      |        NULL |
| 王五      |        NULL |
| 梨园春    |        NULL |
| 水立方    |        NULL |
| 18        |        NULL |
| dd        |        NULL |
| liy      |        NULL |
| ggg      |        NULL |
| mmm      |        NULL |
+-----------+-------------+
10 rows in set (0.00 sec)

mysql>






mysql> select note,price||null from temp15;
+-----------+-------------+
| note      | price||null |
+-----------+-------------+
| 张三      |          1 |
| 李四      |          1 |
| 王五      |          1 |
| 梨园春    |          1 |
| 水立方    |          1 |
| 18        |          1 |
| dd        |          1 |
| liy      |          1 |
| ggg      |          1 |
| mmm      |          1 |
+-----------+-------------+
10 rows in set (0.00 sec)

mysql>
// Use DBML to define your database structure // Docs: https://dbml.dbdiagram.io/docs Table follows { following_user_id integer followed_user_id integer created_at timestamp } Table users { id integer [primary key] username varchar role varchar created_at timestamp } Table posts { id integer [primary key] title varchar body text [note: 'Content of the post'] user_id integer [not null] status varchar created_at timestamp } Ref user_posts: posts.user_id > users.id // many-to-one Ref: users.id < follows.following_user_id Ref: users.id < follows.followed_user_id Table "authority" { "id" int [pk, not null, increment, note: 'ID'] "name" varchar(32) [not null, note: '名称'] "description" varchar(64) [not null, note: '描述'] Indexes { name [type: btree, unique, name: "name"] } Note: '权限' } Table "comments" { "id" int [pk, not null, increment, note: '评论ID'] "news_id" int [not null, note: '新闻ID'] "author" varchar(255) [default: '匿名用户', note: '评论作者'] "content" text [not null, note: '评论内容'] "created_at" datetime [default: `CURRENT_TIMESTAMP`, note: '评论时间'] Indexes { news_id [type: btree, name: "news_id"] } Note: '评论' } Table "hero" { "id" int [pk, not null, increment, note: '英雄ID'] "name" varchar(64) [not null, note: '英雄名称'] "avatar" varchar(256) [default: NULL, note: '英雄头像(存储头像图片的URL)'] "description" text [note: '英雄描述'] "created_at" datetime [default: `CURRENT_TIMESTAMP`, note: '创建时间'] "updated_at" datetime [default: `CURRENT_TIMESTAMP`, note: '更新时间'] Note: '英雄信息' } Table "match_rounds" { "id" int [pk, not null, increment, note: '轮次ID'] "match_id" int [not null, note: '比赛ID'] "round_number" int [not null, note: '轮次序号(如第1局、第2局)'] "team1_score" int [default: NULL, note: '队伍1的得分'] "team2_score" int [default: NULL, note: '队伍2的得分'] Indexes { match_id [type: btree, name: "match_id"] } Note: '比赛轮次' } Table "matches" { "id" int [pk, not null, increment, note: '比赛ID'] "status" varchar(20) [not null, note: '比赛状态(如已结束、进行中)'] "name" varchar(100) [not null, note: '比赛名称'] "stage" varchar(50) [not null, note: '比赛阶段(如小组赛、淘汰赛)'] "date" datetime [not null, note: '比赛时间'] "team1_id" int [not null, note: '队伍1的ID'] "team2_id" int [not null, note: '队伍2的ID'] "team1_score" int [default: NULL, note: '队伍1的得分'] "team2_score" int [default: NULL, note: '队伍2的得分'] "event_type" varchar(50) [not null, note: '比赛类型(如全球总决赛、LPL)'] Indexes { team1_id [type: btree, name: "team1_id"] team2_id [type: btree, name: "team2_id"] } Note: '比赛' } Table "news" { "id" int [pk, not null, increment, note: '新闻ID'] "title" varchar(255) [not null, note: '新闻标题'] "author" varchar(100) [default: NULL, note: '作者名称'] "publish_date" datetime [not null, note: '发布时间'] "content" text [not null, note: '新闻内容'] "views" int [default: 0, note: '浏览次数'] "image" varchar(255) [default: NULL, note: '新闻图片'] Note: '新闻' } Table "orders" { "id" int [pk, not null, increment] "user_id" int [not null] "match_id" int [not null] "ticket_id" int [not null] "quantity" int [not null] "total_price" decimal(10,2) [default: NULL] "order_time" timestamp [default: `CURRENT_TIMESTAMP`] "status" varchar(20) [default: '已下单'] Indexes { ticket_id [type: btree, name: "fk_ticket_id"] } } Table "player_stats" { "id" int [pk, not null, increment, note: '记录ID'] "round_id" int [not null, note: '轮次ID'] "team_member_id" int [not null, note: '选手ID'] "hero_id" int [not null, note: '英雄ID'] "kills" int [default: 0, note: '击杀数'] "deaths" int [default: 0, note: '死亡数'] "assists" int [default: 0, note: '助攻数'] "cs" int [default: 0, note: '补刀数(Creep Score)'] "damage" int [default: 0, note: '伤害值'] "gold" int [default: 0, note: '经济(金币)'] "vision" int [default: 0, note: '视野控制分'] Indexes { round_id [type: btree, name: "round_id"] team_member_id [type: btree, name: "team_member_id"] hero_id [type: btree, name: "hero_id"] } Note: '比赛轮次选手数据' } Table "referee" { "id" int [pk, not null, increment, note: 'ID'] "username" varchar(255) [default: NULL] "refereeName" varchar(32) [default: NULL, note: '裁判姓名'] "level" varchar(64) [default: NULL, note: '裁判等级'] "experience" text [note: '执裁经历'] Indexes { username [type: btree, name: "fk_username"] } Note: '裁判' } Table "report" { "id" int [pk, not null, increment, note: 'ID'] "refereeName" varchar(64) [default: NULL, note: '裁判姓名'] "reporterName" varchar(64) [default: NULL, note: '举报人姓名'] "contact" varchar(64) [default: NULL, note: '联系方式'] "title" varchar(128) [default: NULL, note: '举报标题'] "content" varchar(2048) [default: NULL, note: '举报内容'] "response" varchar(2048) [default: NULL, note: '裁判回复'] "status" varchar(64) [default: '待处理', note: '举报状态'] Note: '裁判举报' } Table "role" { "id" int [pk, not null, increment, note: 'ID'] "name" varchar(32) [not null, note: '名称'] "description" varchar(64) [not null, note: '描述'] Indexes { name [type: btree, unique, name: "name"] } Note: '角色' } Table "roles_authorities" { "id" int [pk, not null, increment, note: 'ID'] "roleId" int [not null, note: '角色ID'] "authorityId" int [not null, note: '权限ID'] Note: '角色权限' } Table "rules" { "id" int [pk, not null, increment, note: '赛事规则ID'] "title" varchar(255) [default: NULL, note: '赛事规则标题'] "publisher" varchar(100) [default: NULL, note: '发布人'] "description" text [note: '赛事规则详细描述'] "created_at" datetime [default: `CURRENT_TIMESTAMP`, note: '规则创建时间'] "updated_at" datetime [default: `CURRENT_TIMESTAMP`, note: '规则最后更新时间'] Note: '赛事规则' } Table "team" { "id" int [pk, not null, increment, note: '队伍ID'] "team_name" varchar(255) [not null, note: '队伍名称'] "team_avatar" varchar(255) [default: NULL, note: '队伍头像URL'] "team_description" text [note: '队伍描述'] "created_at" datetime [default: `CURRENT_TIMESTAMP`, note: '创建时间'] "updated_at" datetime [default: `CURRENT_TIMESTAMP`, note: '更新时间'] Note: '队伍' } Table "team_member" { "id" int [pk, not null, increment, note: '队员ID'] "name" varchar(64) [not null, note: '队员姓名'] "avatar" varchar(256) [default: NULL, note: '队员头像(存储头像图片的URL)'] "team_id" int [not null, note: '所属战队ID'] "position" varchar(64) [default: NULL, note: '游戏位置(例如:上单、中单、打野、辅助等)'] "kills" int [default: 0, note: '击杀数'] "assists" int [default: 0, note: '助攻数'] "survival_time" int [default: 0, note: '生存时间(单位:秒)'] "damage" int [default: 0, note: '伤害值'] "gold" int [default: 0, note: '经济(例如:金币数量)'] "vision" int [default: 0, note: '视野(例如:视野控制次数)'] "match_record" text [note: '比赛记录(可以是JSON或文本描述)'] "created_at" datetime [default: `CURRENT_TIMESTAMP`, note: '创建时间'] "updated_at" datetime [default: `CURRENT_TIMESTAMP`, note: '更新时间'] Indexes { team_id [type: btree, name: "fk_team_member_team_id"] } Note: '队员信息' } Table "team_member_heroes" { "team_member_id" int [not null, note: '队员ID'] "hero_id" int [not null, note: '英雄ID'] Indexes { (team_member_id, hero_id) [pk, type: btree] hero_id [type: btree, name: "hero_id"] } } Table "tickets" { "id" int [pk, not null, increment] "match_id" int [not null] "ticket_type" varchar(50) [default: NULL] "price" decimal(10,2) [default: NULL] "total_quantity" int [default: NULL] "sold_quantity" int [default: 0] "create_time" timestamp [default: `CURRENT_TIMESTAMP`] Indexes { match_id [type: btree, name: "match_id"] } } Table "user" { "id" int [pk, not null, increment, note: 'ID'] "name" varchar(32) [default: NULL, note: '名称'] "username" varchar(16) [not null, note: '账号'] "password" varchar(512) [not null, note: '密码'] "avatar" varchar(128) [default: NULL, note: '头像'] "token" varchar(512) [default: NULL, note: '令牌'] "roleId" int [not null, note: '角色ID'] "loggedAt" datetime [default: NULL, note: '登录于'] "deletedAt" datetime [default: NULL, note: '删除于'] "registeredAt" datetime [not null, note: '注册于'] Indexes { username [type: btree, unique, name: "username"] } Note: '用户' } Ref "comments_ibfk_1":"news"."id" < "comments"."news_id" [update: cascade, delete: cascade] Ref "match_rounds_ibfk_1":"matches"."id" < "match_rounds"."match_id" [update: cascade, delete: cascade] Ref "matches_ibfk_1":"team"."id" < "matches"."team1_id" [update: cascade, delete: cascade] Ref "matches_ibfk_2":"team"."id" < "matches"."team2_id" [update: cascade, delete: cascade] Ref "fk_ticket_id":"tickets"."id" < "orders"."ticket_id" [update: cascade, delete: cascade] Ref "player_stats_ibfk_1":"match_rounds"."id" < "player_stats"."round_id" [update: cascade, delete: cascade] Ref "player_stats_ibfk_2":"team_member"."id" < "player_stats"."team_member_id" [update: cascade, delete: cascade] Ref "player_stats_ibfk_3":"hero"."id" < "player_stats"."hero_id" [update: cascade, delete: cascade] Ref "fk_username":"user"."username" < "referee"."username" [update: restrict, delete: restrict] Ref "fk_team_member_team_id":"team"."id" < "team_member"."team_id" [update: cascade, delete: cascade] Ref "team_member_heroes_ibfk_1":"team_member"."id" < "team_member_heroes"."team_member_id" [update: cascade, delete: cascade] Ref "team_member_heroes_ibfk_2":"hero"."id" < "team_member_heroes"."hero_id" [update: cascade, delete: cascade] Ref "tickets_ibfk_1":"matches"."id" < "tickets"."match_id" [update: restrict, delete: restrict] 生成er图
05-12
#include <iostream> #include <string> #include <fstream> #include <vector> #include <algorithm> #define potprice 10 using namespace std; //基本信息 class flower { protected: string name; string kinds; string num; //条形码 int price; int stock; int sold; public: flower(string a,string b,string c,int q,int x) { name=a; kinds=b; num=c; price=q; stock=x; sold=0; } virtual double totalprice(int buy) = 0; //虚函数,总价 virtual ostream& save(ostream& os) const //虚函数,保存到文件 { os<<name<<" "<<kinds<<" "<<num<<" "<<price<<" "<<stock<<" "<<sold; return os; } virtual istream& load(istream& is) //虚函数,读取文件 { is>>name>>kinds>>num>>price>>stock>>sold; return is; } void purchase(int buy) //进行购买,并更新库存信息 { if(stock>=buy) { stock=stock-buy; sold=sold+buy; } else cout<<"库存中该花的数量不足。"<<endl; } string getnum(){return num;} string getkinds(){return kinds;} string getname(){return name;} int getsold(){return sold;} //输出基本信息 int getprice(){return price;} int getstock(){return stock;} }; class commonflower:public flower { public: commonflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override //重写虚函数进行计算普通鲜花总价 { return price*buy; } }; class potflower:public flower { public: potflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy) { return (price+potprice)*buy; } }; class giftflower:public flower //未完成 { public: giftflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override { return buy*price*0.9; } }; class importantflower:public flower { public: importantflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override { return buy*price*1.1; } }; ostream& operator<<(ostream& os, const flower& flower) { return flower.save(os); } istream& operator>>(istream& is, flower& flower) { return flower.load(is); } void loadflowerfromfile(vector<flower*>& flowers) { ifstream file("flower.txt"); string name,kinds,num; int price; int stock; while(file>>name>>kinds>>num>>price>>stock) { if(kinds=="普通鲜花") { flowers.push_back(new commonflower(name,kinds,num,price,stock)); } else if(kinds=="盆栽花") { flowers.push_back(new potflower(name,kinds,num,price,stock)); } else if (kinds=="礼品鲜花") { flowers.push_back(new giftflower(name,kinds,num,price,stock)); } else if (kinds=="稀有进口花卉") { flowers.push_back(new importantflower(name,kinds,num,price,stock)); } } file.close(); } void saveflowertofile(const vector<flower*>& flowers) { ofstream file("flower.txt"); for (const auto& flower : flowers) { file<<flower; } file.close(); } // 购买鲜花:按条形码查找,处理支付、找零 void buyflower(vector<flower*>& flowers) { string num; int buy; double change; cout << "请输入条形码:"; cin >> num; cout << "请输入购买数量:"; cin >> buy; for(auto& flower: flowers) { if(flower->getnum()==num) { double total=flower->totalprice(buy); cout << "需付金额:" << total << endl; int paytype; //支付方式 cout << "支付方式:1(电子)/2(纸币)~"; cin >> paytype; if (paytype==1) { cout<<"支付成功"<<endl; } else if (paytype==2) { double pay; cout << "输入实收金额:"; cin >> pay; change = pay - total; // 找零算法:先大面值再小面值 cout << "找零:" << change<<"元"<<endl; } vector<int>notes; notes.push_back(100); notes.push_back(50); notes.push_back(20); notes.push_back(10); notes.push_back(5); notes.push_back(1); for(int note:notes) { if(change>=note) { int count; count=change/note; cout<<"明细:"<<count<<"张"<<note<<"元"; change=change-count*note; } } cout<<endl; flower->purchase(buy); //更新库存 cout << "购买成功!" << endl; return; } } cout << "未找到该条形码对应的鲜花!" << endl; } void editflower(vector<flower*>& flowers) // 编辑鲜花:新增/删除 { int choice; cout<<"1(新增鲜花)/2(删除鲜花)"<<endl; cin>>choice; if(choice==1) { string name,kinds,num; int price; int stock; cout<<"输入名称、类别、条形码、单价、库存:"; cin>>name>>kinds>>num>>price>>stock; bool exists=false; // 检查条形码是否已存在,布尔 for (auto& flower: flowers) { if (flower->getnum()==num) { // 已存在则修改数量 flower->purchase(-stock); // 库存增加(反向调用 purchase,实际需优化逻辑) exists=true; break; } } if (!exists) { // 不存在则新增子类对象 if (kinds=="普通鲜花") { flowers.push_back(new commonflower(name,kinds,num,price,stock)); } else if (kinds=="盆栽花") { flowers.push_back(new potflower(name,kinds,num,price,stock)); } else if (kinds=="礼品鲜花") { flowers.push_back(new giftflower(name,kinds,num,price,stock)); } else if (kinds=="稀有进口花卉") { flowers.push_back(new importantflower(name,kinds,num,price,stock)); } } cout << "编辑成功!" << endl; } else if(choice==2) { string num; cout << "请输入要删除的条形码:"; cin >>num; auto it=find_if(flowers.begin(),flowers.end(),[num](flower*f){return f->getnum()==num;}); if(it!=flowers.end()) { delete *it; // 释放内存 flowers.erase(it); cout << "删除成功!" << endl; return; } } } void statsflower(const vector<flower*>& flowers) { double totalrevenue = 0; for (flower*f : flowers) { totalrevenue += f->getprice() * f->getsold(); } cout << "当日营业额:" << totalrevenue << endl; // 拷贝原始指针容器用于排序 vector<flower*> sorted = flowers; // 按销量降序排序 sort(sorted.begin(), sorted.end(), [](flower* a, flower* b) { return a->getsold() > b->getsold(); // 降序排列 }); cout << "销量排序(降序):" << endl; for (const auto& f : sorted) { cout << f->getname() << " 销量:" << f->getsold() << endl; } } int main() { vector<flower*> flower; // 程序启动时加载数据 loadflowerfromfile(flower); int choice; while (true) { cout<<"\n=== 鲜花销售管理系统 ==="<<endl; cout<<"1(购买鲜花)/2(编辑鲜花)/3(统计分析)/4(退出)"<<endl; cout<<"请选择操作:"; cin>>choice; if(choice==1) { buyflower(flower); } else if(choice==2) { editflower(flower); } else if(choice==3) { statsflower(flower); } else if(choice==4) { // 退出时保存数据 saveflowertofile(flower); break; } else { cout <<"无效选项,请重新选择!"<<endl; } } return 0; } 这个程序中为什么无法将输入内容保存到文件中
07-03
#include <iostream> #include <string> #include <fstream> #include <vector> #include <algorithm> #define potprice 10 using namespace std; //基本信息 class flower { protected: string name; string kinds; string num; //条形码 int price; int stock; int sold; public: flower(string a,string b,string c,int q,int x) { name=a; kinds=b; num=c; price=q; stock=x; sold=0; } virtual double totalprice(int buy) = 0; //纯虚函数声明,总价 virtual ostream& save(ostream& os) const //虚函数,保存到文件 { os<<name<<" "<<kinds<<" "<<num<<" "<<price<<" "<<stock<<" "<<sold; return os; } virtual istream& load(istream& is) //虚函数,读取文件 { is>>name>>kinds>>num>>price>>stock>>sold; return is; } void purchase(int buy) //进行购买,并更新库存信息 { if(stock>=buy) { stock=stock-buy; sold=sold+buy; } else cout<<"库存中该花的数量不足。"<<endl; } string getnum(){return num;} string getkinds(){return kinds;} string getname(){return name;} int getsold(){return sold;} //输出基本信息 int getprice(){return price;} int getstock(){return stock;} friend ofstream& operator<<(ofstream& os, const flower& flower) { os << flower.name <<" "<< flower.kinds<<" "<<flower.num<<" "<<flower.price<<" "<<flower.stock; return os; }; }; class commonflower:public flower { public: commonflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override //重写虚函数进行计算普通鲜花总价 { return price*buy; } }; class potflower:public flower { public: potflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy) { return (price+potprice)*buy; } }; class giftflower:public flower //未完成 { public: giftflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override { return buy*price*0.9; } }; class importantflower:public flower { public: importantflower(string a,string b,string c,int q,int x):flower(a,b,c,q,x){} double totalprice(int buy)override { return buy*price*1.1; } }; ostream& operator<<(ostream& os, const flower& flower) { return flower.save(os); } istream& operator>>(istream& is, flower& flower) { return flower.load(is); } void loadflowerfromfile(vector<flower*>& flower) { ifstream file("flower.txt"); string name,kinds,num; int price; int stock; while(file>>name>>kinds>>num>>price>>stock) { if(kinds=="普通鲜花") { flower.push_back(new commonflower(name,kinds,num,price,stock)); } else if(kinds=="盆栽花") { flower.push_back(new potflower(name,kinds,num,price,stock)); } else if (kinds=="礼品鲜花") { flower.push_back(new giftflower(name,kinds,num,price,stock)); } else if (kinds=="稀有进口花卉") { flower.push_back(new importantflower(name,kinds,num,price,stock)); } } file.close(); } void saveflowertofile(const vector<flower*>& flowers) { ofstream file("flower.txt"); for (const auto& flower : flowers) { file<<flower<<endl; } file.close(); } // 购买鲜花:按条形码查找,处理支付、找零 void buyflower(vector<flower*>& flowers) { string num; int buy; double change; cout << "请输入条形码:"; cin >> num; cout << "请输入购买数量:"; cin >> buy; for(flower*f : flowers) { if(f->getnum()==num) { double total=f->totalprice(buy); cout << "需付金额:" << total << endl; int paytype; //支付方式 cout << "支付方式:1(电子)/2(纸币)"; cin >> paytype; if (paytype == 2) { double pay; cout << "输入实收金额:"; cin >> pay; change = pay - total; // 找零算法:先大面值再小面值 cout << "找零:" << change<< endl; } cout<<"明细:"; vector<int>notes; notes.push_back(100); notes.push_back(50); notes.push_back(20); notes.push_back(10); notes.push_back(5); notes.push_back(1); for(int note:notes) { if(change>=note) { int count; count=change/note; cout<<count<<"张"<<note<<"元"; change=change-count*note; } } cout<<endl; f->purchase(buy); //更新库存 cout << "购买成功!" << endl; return; } } cout << "未找到该条形码对应的鲜花!" << endl; } void editflower(vector<flower*>& flowers) // 编辑鲜花:新增/删除 { int choice; cout<<"1(新增鲜花)/2(删除鲜花)"<<endl; cin>>choice; if(choice==1) { string name,kinds,num; int price; int stock; cout<<"输入名称、类别、条形码、单价、库存:"; cin>>name>>kinds>>num>>price>>stock; bool exists=false; // 检查条形码是否已存在,布尔 for (flower *f: flowers) { if (f->getnum()==num) { // 已存在则修改数量 f->purchase(-stock); // 库存增加(反向调用 purchase,实际需优化逻辑) exists=true; break; } } if (!exists) { // 不存在则新增子类对象 if (kinds=="普通鲜花") { flowers.push_back(new commonflower(name,kinds,num,price,stock)); } else if (kinds=="盆栽花") { flowers.push_back(new potflower(name,kinds,num,price,stock)); } else if (kinds=="礼品鲜花") { flowers.push_back(new giftflower(name,kinds,num,price,stock)); } else if (kinds=="稀有进口花卉") { flowers.push_back(new importantflower(name,kinds,num,price,stock)); } } cout << "编辑成功!" << endl; } else if(choice==2) { string num; cout << "请输入要删除的条形码:"; cin >>num; auto it=find_if(flowers.begin(),flowers.end(),[num](flower*f){return f->getnum()==num;}); if(it!=flowers.end()) { delete *it; // 释放内存 flowers.erase(it); cout << "删除成功!" << endl; return; } } } void statsflower(const vector<flower*>& flowers) { double totalrevenue = 0; for (flower*f : flowers) { totalrevenue += f->getprice() * f->getsold(); } cout << "当日营业额:" << totalrevenue << endl; // 拷贝原始指针容器用于排序 vector<flower*> sorted = flowers; // 按销量降序排序 sort(sorted.begin(), sorted.end(), [](flower* a, flower* b) { return a->getsold() > b->getsold(); // 降序排列 }); cout << "销量排序(降序):" << endl; for (const auto& f : sorted) { cout << f->getname() << " 销量:" << f->getsold() << endl; } } int main() { vector<flower*> flower; // 程序启动时加载数据 loadflowerfromfile(flower); int choice; while (true) { cout<<"\n=== 鲜花销售管理系统 ==="<<endl; cout<<"1(购买鲜花)/2(编辑鲜花)/3(统计分析)/4(退出)"<<endl; cout<<"请选择操作:"; cin>>choice; if(choice==1) { buyflower(flower); } else if(choice==2) { editflower(flower); } else if(choice==3) { statsflower(flower); } else if(choice==4) { // 退出时保存数据 saveflowertofile(flower); // 释放内存 for (auto& flower : flower) { delete flower; } flower.clear(); break; } else { cout <<"无效选项,请重新选择!"<<endl; } } return 0; } 代码中有错误吗
07-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值