尝试贴一段代码

尝试贴一段代码,看看代码高亮功能能不能正常工作。

//This is a project made by MtZero
//Start at 2015/12/11 21:00

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#include<list>
using namespace std;

#ifndef _CODE_
#define _CODE_                                  0        //只是个标记,无他用
/*错误代码*/
#define CORRECT                                 0        //无错误
#define CONST_NOT_VALID                         1        //常数不合法
#define DIV_0                                   2        //除以0
#define FORMAT_ERROR                            3        //格式错误
#define STACK_ERROR                             4        //堆栈错误
#define MATH_ERROR                              5        //数学错误
#define OPERATION_ERROR                         6        //操作符错误

/*操作符代码*/
#define _OP_CONST                               0        //常数
#define _OP_PLUS                                1
#define _OP_MINUS                               2
#define _OP_MULTI                               3
#define _OP_DIV                                 4
#define _OP_POW                                 5        //乘方
#define _OP_EXT                                 6        //开方
#define _OP_LOG                                 7        //对数
#define _OP_LOG10                               8        //常用对数
#define _OP_LN                                  9        //自然对数
#define _OP_MOD                                 10       //取模
#define _OP_TRUNC                               11       //截取
#define _OP_ROUND                               12       //四舍五入

/*限制*/
#define BIGNUM_MAX_LEN                          65500
#define STACK_MAX_DEPTH                         250

/*常数*/
#define PI                                      3.1415926535897932385
#define N_BASE                                  2.7182818284590452354

/*模式*/
#define _MODE_HI_PRE                            1        //高精度计算模式
#define _MODE_CALC                              2        //浮点计算模式
#define _MODE_PROG                              3        //程序员计算模式
#define _MODE_COMPLEX                           4
#define _MODE_MATRIX                            5
#define _MODE_VECTOR                            6
#define _MODE_EQUATION                          7

#endif

int MODE = 1;

class BigNum {
private:
	vector<short> num;
	int   point;                            //小数点位置
	bool  isPos;                            //是否为非负数
	int   equalTo(string s);
	int   isBigger(BigNum &bn);             //忽视符号判断大小
	int   accessDig(int n, int &size);      //获取大小为size的大数的第n位(从左到右)
	BigNum plus(BigNum &bn);
	BigNum minus(BigNum &bn);
public:
	BigNum();
	BigNum(string s);
	BigNum DIV(BigNum &bn);                 //整数除法
	int iPart();                            //返回整数部分的长度
	int dPart();                            //返回小数部分的长度

	/*操作符重载*/
	BigNum    operator +  (BigNum &bn);
	BigNum    operator -  (BigNum &bn);
	BigNum    operator *  (BigNum &bn);
	BigNum    operator /  (BigNum &bn);     //带小数除法
	BigNum    operator %  (BigNum &bn);
	BigNum    operator += (BigNum &bn);
	BigNum    operator -= (BigNum &bn);
	BigNum    operator *= (BigNum &bn);
	BigNum    operator /= (BigNum &bn);
	BigNum    operator %= (BigNum &bn);
	void      operator =  (string bn);
	void      operator =  (long long sn);
	void      operator =  (BigNum &bn);
	//istream&  operator >> (istream &in);       //HOW?
	//ostream&  operator << (ostream &out);
	bool      operator >  (BigNum &bn);
	bool      operator <  (BigNum &bn);
	bool      operator == (BigNum &bn);
	bool      operator >= (BigNum &bn);
	bool      operator <= (BigNum &bn);
	friend ostream &operator <<(ostream &stream, string s);
	friend istream &operator >>(istream &stream, string s);

	/*
	friend ostream &operator<<(ostream &stream, Rational rational);
	friend istream &operator>>(istream &stream, Rational &rational);
	*/

	/*打印大数*/
	//void echo(int dec = 0);

	/*
	BigNum pow(BigNum bn);
	BigNum ext(BigNum bn);               //开方
	BigNum log(BigNum bn, BigNum base);
	*/
};

int BigNum::iPart() {                    //返回整数部分的长度
	return this->point;
}

int BigNum::dPart() {                    //返回小数部分的长度
	return this->num.size() - this->point;
}

int BigNum::equalTo(string s) {
	/*判断输入的参数是否合法*/
	int pcnt = 0;
	int len = s.length();

	/*判断是否为负数*/
	if (s[0] == '-') {
		isPos = false;
		s.erase(0, 1);
	}
	else isPos = true;

	/*检查非法字符*/
	if (s[0] != '.' && (s[0]<'0' || s[0]>'9')) {
		return CONST_NOT_VALID;
	}

	/*查找是否有小数点*/
	int pPos = -1;
	pPos = s.find('.');
	if (pPos > -1) {
		this->point = pPos;
		s.erase(s.begin() + pPos);
	}

	/*将纯数串写入vector容器*/
	len = s.length();
	this->num.clear();
	for (int i = 0; i < len; i++) {
		this->num.push_back(s[len - i - 1] - 48);    //vector容器的低位相当于大数字的低位
	}

	/*如果没有找到小数点*/
	if (pPos == -1) {
		point = this->num.size();
	}

	return CORRECT;
}

BigNum::BigNum(string s) {
	equalTo(s);
}

BigNum::BigNum() {
	equalTo("0");
}

BigNum BigNum::plus(BigNum &bn) {                    //忽视符号的加法 UNDEBUGGED
	BigNum ans;

	int anslen = max(this->iPart(), bn.iPart()) + max(this->dPart(), bn.dPart()) + 1;
	int thisSize = this->num.size();
	int bnSize = bn.num.size();

	int ansDPart = max(this->dPart(), bn.dPart());

	ans.point = anslen - ansDPart;
	/*用以对齐小数点*/
	int thisDiff = ans.dPart() - this->dPart();
	int bnDiff = ans.dPart() - bn.dPart();

	ans.num.clear();

	ans.num.push_back(this->accessDig(0 - thisDiff, thisSize) + bn.accessDig(0 - bnDiff, bnSize));

	for (int i = 1; i < anslen; i++) {
		ans.num.push_back(this->accessDig(i - thisDiff, thisSize) + bn.accessDig(i - bnDiff, bnSize) + ans.num[i - 1] / 10);
		ans.num[i - 1] %= 10;
	}
	if (ans.num[anslen - 1] == 0) ans.num.erase(ans.num.begin() + anslen - 1);

	return ans;
}

BigNum BigNum::minus(BigNum &bn) {                   //忽视符号的减法(大减小) 未完成
	BigNum ans;
	int anslen = max(this->iPart(), bn.iPart()) + max(this->dPart(), bn.dPart()) + 1;
	int thisSize = this->num.size();
	int bnSize = bn.num.size();

	return ans;
}

void BigNum::operator =(string bn) {
	equalTo(bn);
}

void BigNum::operator =(long long sn) {
	string s;
	stringstream ss;
	ss << sn;
	s = ss.str();
	this->operator =(s);
}

void BigNum::operator =(BigNum &bn) {
	this->num = bn.num;
	this->point = bn.point;
	this->isPos = bn.isPos;
}

int BigNum::accessDig(int n, int &size) {
	if (n >= size || n < 0) return 0;
	else return this->num[n];
}

int BigNum::isBigger(BigNum &bn) {          //大于 -> 1; 等于 -> 0; 小于 -> -1
	if (this->point > bn.point) return 1;
	if (this->point < bn.point) return -1;
	/*以下三行获取两个大数字的位数以及较大的那个位数*/
	int thisSize = this->num.size();
	int bnSize = bn.num.size();
	int maxSize = max(thisSize, bnSize);
	int diff = thisSize - bnSize;           //大数位数之差
	for (int i = 1; i <= maxSize; i++) {
		int thisDig = this->accessDig(thisSize - i, thisSize);
		int bnDig = bn.accessDig(bnSize - i, bnSize);
		if (thisDig > bnDig) return 1;
		if (thisDig < bnDig) return -1;
	}
	return 0;
}

bool BigNum::operator >(BigNum &bn) {       //UNDEBUGGED
	if (this->isPos&&!bn.isPos) {
		return true;
	}
	else if (!this->isPos&&bn.isPos) {
		return false;
	}
	else if (this->isPos&&bn.isPos) {
		return this->isBigger(bn) == 1;
	}
	else return !(this->isBigger(bn) == -1);
}

bool BigNum::operator <(BigNum &bn) {       //UNDEBUGGED
	if (this->isPos&&!bn.isPos) {
		return false;
	}
	else if (!this->isPos&&bn.isPos) {
		return true;
	}
	else if (this->isPos&&bn.isPos) {
		return this->isBigger(bn) == -1;
	}
	else return this->isBigger(bn) == 1;
}

bool BigNum::operator >=(BigNum &bn) {      //UNDEBUGGED
	return !this->operator <(bn);
}

bool BigNum::operator <=(BigNum &bn) {      //UNDEBUGGED
	return !this->operator >(bn);
}

bool BigNum::operator ==(BigNum &bn) {      //UNDEBUGGED
	if (this->isPos != bn.isPos) return false;
	if (this->point != bn.point) return false;
	int thisSize = this->num.size();
	int bnSize = bn.num.size();
	int maxSize = max(thisSize, bnSize);
	for (int i = 1; i <= maxSize; i++) {
		if (this->accessDig(thisSize - i, thisSize) != bn.accessDig(bnSize - i, bnSize)) return false;
	}
	return true;
}

/*
istream& BigNum::operator >>(istream &in) {    //HOW?
	string s;
	in >> s;
	equalTo(s);
	return in;
}

ostream& BigNum::operator <<(ostream &out) {

	return out;
}
*/

/*iostream参考
istream& operator >> (istream &in, bign &x)
{
	string s;
	in >> s;
	x = s.c_str();
	return in;
}

ostream& operator << (ostream &out, const bign &x)
{
	out << x.str();
	return out;
}
friend ostream &operator<<(ostream &stream, Rational rational);
friend istream &operator>>(istream &stream, Rational &rational);
*/

struct bnNode {                                  //高精度节点
	BigNum bn;                                   //高精度整数
	int op;                                      //操作符代码
};

struct Node {                                    //浮点数节点
	long double num;                             //浮点数
	int op;                                      //操作符代码
};

vector<bnNode> bnQue;                            //高精度队列
vector<Node> que;                                //浮点数队列

/*DEBUG AREA*/
BigNum b("3.1415926"), c("3.1415927"), e("3.141592600");
/*DEBUG AREA END*/

int main() {

	/*DEBUG AREA*/
	string s;
	long long d = 9873457;
	//cin >> s;
	//b = s;
	//c = b;
	//c = d;
	//b = "3.1415926";
	//c = "3.1415927";
	//e = "3.141592600";
	cout << (b < c) << endl;
	cout << (b > c) << endl;
	cout << (b <= c) << endl;
	cout << (b >= c) << endl;
	cout << (b < e) << endl;
	cout << (b > e) << endl;
	cout << (b <= e) << endl;
	cout << (b >= e) << endl;
	cout << (b == e) << endl;
	//cin >> c;
	//cout << c;
	/*DEBUG AREA END*/

	return 0;
}

 

发表评论

您的电子邮箱地址不会被公开。