C++入门程序——goto loop做简易计算器

C++入门程序——goto loop做简易计算器

本文以计算器程序为主体,为C++初学者带来goto loop的巧妙的实际运用。

(文中计算器的功能仅涉及加法、减法、乘法、除法)

#include "pch.h"
#include <iostream>
using namespace std;
void main()
{
loop:
	{
		cout << "请输入操作数一:" << '\n' << "请输入一个运算符:" << '\n' << "请操作数二:" << endl;
		char op;
		double x, y;
		cin >> x;
		cin >> op;
		cin >> y;
		cout << "输入的运算符为:" << op << endl;
		switch (op)
		{
		case '+':cout << "加和为:" << x + y << endl;
			break;
		case '-':cout << "相减为:" << x - y << endl;
			break;
		case '*':cout << "乘积为:" << x * y << endl;
			break;
		case '/':if (y != 0)
			cout << "做商为:" << x / y << endl;
				 else
			cout << "被除数不能为0!" << endl;
			break;
		default: cout << "请确保您输入的操作符是+-*/之一!" << endl;
		}
		cout << '\n' << "输入L离开该运行窗口;输入C继续进行运算" << endl;
		char vv;
		cin >> vv;
		if (vv == 'L')
			cout << "感谢使用该简易计算器!" << endl;

		else if (vv == 'C')
			goto loop;
		else
			cout << "请输入提示字符(L或S)!" << endl;
	}
}

     上述代码在vs17环境下编译运行通过。

 

     我的GitHub链接https://github.com/TYduoduo

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的turtle计算器程序: ```python import turtle # 初始化turtle窗口 turtle.setup(width=600, height=400) turtle.title("Calculator") # 定义画布和画笔 canvas = turtle.Screen() pen = turtle.Turtle() pen.penup() # 定义计算器的状态变量 operator = "" num1 = "" num2 = "" result = "" # 定义数字按钮的点击事件 def onButtonClick(num): global operator, num1, num2, result if operator == "": num1 += str(num) pen.goto(-200, 0) pen.write(num1, font=("Arial", 24, "normal")) else: num2 += str(num) pen.goto(0, 0) pen.write(num2, font=("Arial", 24, "normal")) # 定义运算符按钮的点击事件 def onOperatorClick(op): global operator, num1, num2, result operator = op pen.goto(100, 0) pen.write(op, font=("Arial", 24, "normal")) # 定义等号按钮的点击事件 def onEqualClick(): global operator, num1, num2, result if operator == "+": result = str(int(num1) + int(num2)) elif operator == "-": result = str(int(num1) - int(num2)) elif operator == "*": result = str(int(num1) * int(num2)) elif operator == "/": result = str(int(num1) / int(num2)) pen.goto(200, 0) pen.write(result, font=("Arial", 24, "normal")) # 定义清除按钮的点击事件 def onClearClick(): global operator, num1, num2, result operator = "" num1 = "" num2 = "" result = "" pen.clear() pen.write("0", font=("Arial", 24, "normal")) # 画出计算器的数字和运算符按钮 pen.goto(-240, 80) pen.write("1", font=("Arial", 24, "normal")) pen.goto(-180, 80) pen.write("2", font=("Arial", 24, "normal")) pen.goto(-120, 80) pen.write("3", font=("Arial", 24, "normal")) pen.goto(-240, 20) pen.write("4", font=("Arial", 24, "normal")) pen.goto(-180, 20) pen.write("5", font=("Arial", 24, "normal")) pen.goto(-120, 20) pen.write("6", font=("Arial", 24, "normal")) pen.goto(-240, -40) pen.write("7", font=("Arial", 24, "normal")) pen.goto(-180, -40) pen.write("8", font=("Arial", 24, "normal")) pen.goto(-120, -40) pen.write("9", font=("Arial", 24, "normal")) pen.goto(-240, -100) pen.write("0", font=("Arial", 24, "normal")) pen.goto(0, 80) pen.write("+", font=("Arial", 24, "normal")) pen.goto(0, 20) pen.write("-", font=("Arial", 24, "normal")) pen.goto(0, -40) pen.write("*", font=("Arial", 24, "normal")) pen.goto(0, -100) pen.write("/", font=("Arial", 24, "normal")) pen.goto(120, 0) pen.write("=", font=("Arial", 24, "normal")) pen.goto(240, 0) pen.write("C", font=("Arial", 24, "normal")) # 给按钮绑定点击事件 canvas.onclick(lambda x, y: onButtonClick(1) if -240 <= x <= -180 and 60 <= y <= 100 else None) canvas.onclick(lambda x, y: onButtonClick(2) if -180 <= x <= -120 and 60 <= y <= 100 else None) canvas.onclick(lambda x, y: onButtonClick(3) if -120 <= x <= -60 and 60 <= y <= 100 else None) canvas.onclick(lambda x, y: onButtonClick(4) if -240 <= x <= -180 and 0 <= y <= 40 else None) canvas.onclick(lambda x, y: onButtonClick(5) if -180 <= x <= -120 and 0 <= y <= 40 else None) canvas.onclick(lambda x, y: onButtonClick(6) if -120 <= x <= -60 and 0 <= y <= 40 else None) canvas.onclick(lambda x, y: onButtonClick(7) if -240 <= x <= -180 and -60 <= y <= 0 else None) canvas.onclick(lambda x, y: onButtonClick(8) if -180 <= x <= -120 and -60 <= y <= 0 else None) canvas.onclick(lambda x, y: onButtonClick(9) if -120 <= x <= -60 and -60 <= y <= 0 else None) canvas.onclick(lambda x, y: onButtonClick(0) if -240 <= x <= -180 and -120 <= y <= -80 else None) canvas.onclick(lambda x, y: onOperatorClick("+") if -60 <= x <= 60 and 60 <= y <= 100 else None) canvas.onclick(lambda x, y: onOperatorClick("-") if -60 <= x <= 60 and 0 <= y <= 40 else None) canvas.onclick(lambda x, y: onOperatorClick("*") if -60 <= x <= 60 and -60 <= y <= 0 else None) canvas.onclick(lambda x, y: onOperatorClick("/") if -60 <= x <= 60 and -120 <= y <= -80 else None) canvas.onclick(lambda x, y: onEqualClick() if 60 <= x <= 120 and -40 <= y <= 40 else None) canvas.onclick(lambda x, y: onClearClick() if 180 <= x <= 240 and -40 <= y <= 40 else None) # 运行程序 canvas.mainloop() ``` 程序运行后,你可以在窗口中看到一个简单的计算器界面。你可以通过点击数字和运算符按钮来输入计算式,并且点击等号按钮来计算结果。点击清除按钮可以清空所有状态变量,重新开始计算。 注意,这个程序只支持整数计算。如果你需要支持浮点数计算,可以将 `int()` 函数改为 `float()` 函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值