简易计算器

老师布置了个作业:可拓展性或灵活性,指软件可快速改进以满足业务需求的变化,以一个简单系统为例理解在设计上要如何实现这个性能。可以一个简单的计算器为例,使用C++实现。


然后经过各种查,用已懂的知识写了个,下面贴上计算器的代码。


期间遇到错误提示:"A"的初始化操作由"case"标签跳过,这个错误是因为怕用了别的case而导致这个变量不创建,解决方法为把case(0),case(1)...后代码用{}括起来。

#include<iostream>
#include<string>
using namespace std;

//运算类
class COperation{   
private:
	double dNumA; //数字A
	double dNumB; //数字B
public:
	void SetNumA(double n){ //设置数字A
		dNumA = n;
	}
	void SetNumB(double n){ //设置数字B
		dNumB = n;
	}
	double GetNumA(){ //取得数字A
		return dNumA;
	}
	double GetNumB(){ //取得数字B
		return dNumB;
	}
	virtual double GetResult()  // 得到运算结果
	{
		double result = 0;
		return result;
	}
};

//加法类
class COperationAdd :public  COperation  
{
public:
	double  GetResult()override
	{
		double result = 0;
		result = GetNumA() + GetNumB();
		return result;
	}
};

//减法类
class COperationSub :public COperation 
{
public:
	double GetResult()override
	{
		double result = 0;
		result = GetNumA() - GetNumB();
		return result;
	}
};

//乘法类
class COperationMul :public COperation  
{
public:
	double GetResult()override
	{
		double result = 0;
		result = GetNumA() * GetNumB();
		return result;
	}
};

//除法类
class COperationDiv :public COperation 
{
public:
	double GetResult()override
	{
		double result = 0;
		if (GetNumB() == 0){
			std::cout << "除数不能为0。";
			return NULL;
		}
		result = GetNumA() / GetNumB();
		return result;
	}
};

int main()
{
	int n;
	double x, y;

	while(true)
	{
		cout << " == == == == == == == == == == == == == == == == == == ==" << endl;
		cout << "                0. 加法运算" << endl;
		cout << "                1. 减法运算" << endl;
		cout << "                2. 乘法运算" << endl;
		cout << "                3. 除法运算" << endl;
		cout << "                4. 退出    " << endl;
		cout << " == == == == == == == == == == == == == == == == == == ==" << endl;
		cout << "运算类型选择:";
		cin >> n;
		switch(n){
		case(0) : {
					  COperationAdd A;
					  cout << "请输入数据x和y:";
					  cin >> x >> y;
					  A.SetNumA(x);
					  A.SetNumB(y);
					  cout << x << " + " << y << " = " << A.GetResult() << endl; break;
		}
		case(1) : {
					  COperationSub A;
					  cout << "请输入数据x和y:";
					  cin >> x >> y;
					  A.SetNumA(x);
					  A.SetNumB(y);
					  cout << x << " - " << y << " = " << A.GetResult() << endl; break;
		}
		case(2) : {
					  COperationMul A;
					  cout << "请输入数据x和y:";
					  cin >> x >> y;
					  A.SetNumA(x);
					  A.SetNumB(y);
					  cout << x << " * " << y << " = " << A.GetResult() << endl; break;
		}
		case(3) : {
					  COperationDiv A;
					  cout << "请输入数据x和y:";
					  cin >> x >> y;
					  A.SetNumA(x);
					  A.SetNumB(y);
					  cout << x << " / " << y << " = " << A.GetResult() << endl; break;
		}
		case(4):exit(0);
		default: break;
		}
	}
	return 0;
}


package com.softeem.train.calculator; import java.util.Scanner; import java.io.*; public class Calculator { private int m; // 第一个输入的数字 private int n; // 第二个输入的数字 public Calculator() { this.m = 0; this.n = 0; } /* * public void use() { System.out.println("你是否继续使用?继续输入Y,退出输任意键?"); Scanner * in5= new Scanner(System.in); String zz=in5.next(); * if(zz.equals("Y")||zz.equals("y")) this.gui(); else System.exit(0); * * } */ public void graphic() { //3 System.out.println("=========================="); System.out.println("欢迎使用 So easy 计算机简易系统 "); System.out.println(" 请选择计算类型:"); System.out.println(" 1.加法"); System.out.println(" 2.减法"); System.out.println(" 3.乘法"); System.out.println(" 4.除法"); System.out.println(" 5.输入表达式计算"); System.out.println(" 6.退出"); System.out.println("=========================="); Scanner in3 = new Scanner(System.in); int n = in3.nextInt(); switch (n) { //4 case 1: this.add(); //8 break; case 2: this.sub(); break; case 3: this.mul(); break; case 4: this.div(); break; case 5: this.cal(); break; case 6: System.exit(0); default: break; } } public void add() // 实现加法的方法 { //5 System.out.println("欢迎使用加法操作"); this.in(); //7 int sum; sum = this.m + this.n; System.out.println("你输入的数据的和是:" + sum); // this.use(); } public void sub() // 实现减法的方法 { System.out.println("欢迎使用减法操作"); this.in(); int cha; cha = this.m - this.n; System.out.println("你输入的数据的差是:" + cha); // this.use(); } public void mul() // 实现乘法的方法 { System.out.println("欢迎使用乘法操作"); this.in(); int ji; ji = this.m * this.n; System.out.println("你输入的数据的积是:" + ji); // this.use(); } public void div() // 实现除法的方法 { System.out.println("欢迎使用除法操作"); this.in(); if (n == 0) { System.out.println("被除数不能为零!"); this.graphic(); } int shang; shang = this.m / this.n; System.out.println("你输入的数据的和是:" + shang); // this.use(); } public void cal() // 实现表达式运算方法 { String s = ""; // 接收表达式的字符串 String ss1 = ""; // 分离数字m的字符串 String ss2 = ""; // 分离数字n的字符串 char c1 = ' '; // 存放运算符 int m = 0; // 第一个数字 int n = 0; // 第二个数字 System.out.print("请输入一个表达式:"); try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); s = in.readLine(); } catch (IOException e) { } for (int i = 0; i < s.length(); i++) { if ((s.charAt(i) == '+') || (s.charAt(i) == '-') || (s.charAt(i) == '*') || (s.charAt(i) == '/')) { c1 = s.charAt(i); // 第一个数 for (int j = 0; j < i; j++) { ss1 += s.charAt(j); m = Integer.parseInt(ss1); } // 第二个数 for (int j = i + 1; j < s.length(); j++) { ss2 += s.charAt(j); n = Integer.parseInt(ss2); } break; } } if (c1 == '+') System.out.println(m + "+" + n + "=" + (m + n)); if (c1 == '-') System.out.println(m + "-" + n + "=" + (m - n)); if (c1 == '*') System.out.println(m + "*" + n + "=" + (m * n)); if (c1 == '/') System.out.println(m + "/" + n + "=" + (m / n)); } public void in() // 实现接收数据的方法 { //6 System.out.println("请输入第一个数:"); Scanner in1 = new Scanner(System.in); this.m = in1.nextInt(); System.out.println("请输入第二个数:"); Scanner in2 = new Scanner(System.in); this.n = in2.nextInt(); } public static void main(String[] args) throws java.io.IOException { //1 String ifquit = ""; // 用于接收是否退出 Calculator c = new Calculator(); do { //2 c.graphic(); // 调用界面方法 //9 System.out.println("你是否继续使用?继续输入Y,退出输任意键?"); Scanner in5 = new Scanner(System.in); ifquit = in5.nextLine(); //10 } while (ifquit.equals("Y") || ifquit.equals("y")); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值