首页 > 其他分享 >2.3 电子秤模拟——代码实现

2.3 电子秤模拟——代码实现

时间:2022-11-20 14:33:44浏览次数:50  
标签:apple weight 0.0 电子秤 float price 2.3 banana 模拟

代码实现

#include <iostream>
using namespace std;

int main() {
	float apple_price = 3.5; // 苹果单价
	float banana_price = 4.2; // 香蕉单价
	float apple_weight = 0.0; // 苹果重量
	float banana_weight = 0.0; // 香蕉重量
	float total = 0.0; // 总价

	cout << "请输入苹果重量" << endl;
	cin >> apple_weight;
	cout << "请输入香蕉重量" << endl;
	cin >> banana_weight;
	total =  apple_price * apple_weight + banana_price * banana_weight;
	cout << "应付款" << total << endl;
	return 0;
}

运行结果

image

局限

  • 只能称两种水果
  • 无法调整水果的价格

程序中蕴含的道理

  • 实际中卖的水果种类有限
  • 水果价格在一定时期内基本固定

image

代数思维

用变量来思考问题,变量之间的运算来表达一定的关系、规律

标签:apple,weight,0.0,电子秤,float,price,2.3,banana,模拟
From: https://www.cnblogs.com/caihong2022/p/16908426.html

相关文章