跳到正文

个人记录

垃圾计算器

Scherzo约 7 分钟
原文
文章信息与版本

文章信息

发布
2020年4月24日
收录
2026年5月21日
更新
2026年11月14日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

用于计算一些学校的水题,正在更新

C++
/*
Name: Rabbish_Calculator
Status: updating
Record:
	Rudiment
		2020-4-24
			- normal calculate (+, -, *, /);
			- special calculate (power, square root);
			- adjust decription;

	Version 1
		2020-4-25
			- support multiple root;
			- support multiple calculate;
			- beautify output;
			- debug;
			- adjust description;
*/

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

string calculate_type;

inline void calculate_normal(char symbol,double a,double b) {
	if (symbol=='+') {
		cout<<"The answer is "<<a+b<<endl;
	} else if (symbol=='-') {
		cout<<"The answer is "<<a-b<<endl;
	} else if (symbol=='*') {
		cout<<"The answer is "<<a*b<<endl;
	} else if (symbol=='/') {
		cout<<"The answer is "<<a/b<<endl;
	} else {
		cout<<"Please check your input and type again next time"<<endl;
	}
}

inline void calculate_special(string calculate_type,double pow_a,double pow_b,double sqrt_n,double sqrt_degree) {
	if (calculate_type=="power") {
		cout<<"The answer is "<<pow(pow_a,pow_b)<<endl;
	} else if (calculate_type=="root") {
		cout<<"The answer is "<<pow(sqrt_n,1.0/sqrt_degree)<<endl;
	} else {
		cout<<"Please check your input and type again next time"<<endl;
	}
}

inline void input_equation() {
	cout<<"Please input the calculate type of the equation <'normal'/'special'> ";
	cin>>calculate_type;
	if (calculate_type=="normal") {
		cout<<"Please input the equation <'a' 'operator' 'b'> ";
		char symbol;
		double a,b;
		cin>>a>>symbol>>b;
		calculate_normal(symbol,a,b);
	} else if (calculate_type=="special") {
		string calculate_special_type;
		cout<<"Please input the type of special-calculate equation <'power'/'root'> " ;
		cin>>calculate_special_type;
		if (calculate_special_type=="power") {
			cout<<"Please input the equation <'base' 'index'> ";
			double a,b;
			cin>>a>>b;
			calculate_special(calculate_special_type,a,b,0,0);
		}
		if (calculate_special_type=="root") {
			cout<<"Please input the number and degree of the root <'n' 'degree'> ";
			double n,degree;
			cin>>n>>degree;
			calculate_special(calculate_special_type,0,0,n,degree);
		}
	} else {
		cout<<"Please check your input and type again"<<endl;
		input_equation();
	}
}

inline void termination() {
	char letter;
	cout<<"Thank you for using"<<endl;
	cout<<"input any letter then press <Enter> to end"<<endl;
	cin>>letter;
}

inline void repeat() {
	string calculate_again;
	cout<<"calculate anything else? <'Yes'/'No'> ";
	cin>>calculate_again;
	if (calculate_again=="Yes") {
		cout<<"------------------------------------------------------------------------------------------------------------------"<<endl;
		input_equation();
		repeat();
	} else if (calculate_again=="No") {
		termination();
	} else {
		cout<<"Please check your input and type again"<<endl;
		repeat();
	}

}

int main() {
	cout<<fixed<<setprecision(4);
	cout<<"----------------------------------------------Rabbish Calculator--------------------------------------------------"<<endl;
	cout<<"----------------------------This is an calculator with 4 decimal place for precision------------------------------"<<endl;
	cout<<"------------------------------input given things please, or you need to type again--------------------------------"<<endl;
	cout<<"------------------------------------------------------------------------------------------------------------------"<<endl;
	input_equation();
	repeat();
	return 0;
}