⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2266.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 2266 on 2006-08-19 at 22:23:53 */ 
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

const int N = 64, F = 10;

class BigInt {
private:
	int n[N], len;
	bool pos;
public:
	BigInt(int);
	BigInt(char*);
	bool operator <(const BigInt&) const;
	bool operator >(const BigInt& b) const { return b < *this; }
	bool operator >=(const BigInt& b) const { return !(*this < b); }
	void operator +=(const BigInt&);
	void operator -=(const BigInt&);
	BigInt operator +(const BigInt&) const;
	void print() const;
};
BigInt::BigInt(int k = 0) {
	pos = (k >= 0); k = abs(k);
	memset(n, 0, sizeof(n));
	for(len = 1; k != 0; len++, k /= F) n[len-1] = k%F;
	while(len > 1 && n[len-1] == 0) len--;
}
BigInt::BigInt(char* str) {
	pos = (*str != '-');
	if(!isdigit(*str)) str++;
	memset(n, 0, sizeof(n));
	int sl = strlen(str);
	char* sp = str+sl;
	for(len = 1; sp != str; len++, sp--) n[len-1] = *(sp-1) - '0';
	while(len > 1 && n[len-1] == 0) len--;
}
void BigInt::print() const {
	if(!pos) putchar('-');
	for(int i = len-1; i >= 0; i--) printf("%d", n[i]);
	putchar('\n');
}
bool BigInt::operator <(const BigInt& b) const {
	if(b.len != len) return len < b.len;
	for(int i = len-1; i >= 0; i--)
		if(n[i] != b.n[i]) return n[i] < b.n[i];
	return false;
}
void BigInt::operator +=(const BigInt& b) {
	len >?= b.len;
	int r = 0;
	for(int i = 0; i < len; i++) {
		n[i] += b.n[i]+r;
		r = n[i]/F; n[i] %= F;
	}
	while(r != 0) { n[len++] = r%F; r /= F; }
}
void BigInt::operator -=(const BigInt& b) {
	for(int i = 0; i < len; i++) {
		n[i] -= b.n[i];
		if(n[i] < 0) { n[i] += F; n[i+1]--; }
	}
	while(len > 1 && n[len-1] == 0) len--;
}
BigInt BigInt::operator +(const BigInt& b) const {
	BigInt r;
	if(pos == b.pos) { r = *this; r += b; }
	else {
		if(*this >= b) { r = *this; r -= b; }
		else { r = b; r -= *this; }
		if((!pos && *this > b) || (pos && *this < b)) r.pos = false;
		else r.pos = true;
	}
	return r;
}

int main()
{
	char dem[N], num[N];
	
	while(gets(dem) != NULL) {
		char* ep = strstr(dem, "E"), *pd = dem;
		if(ep == NULL) ep = strstr(dem, "e");
		if(ep == NULL) { strcat(dem, "E0"); ep = strstr(dem, "E"); }
		else *ep = 'E';
		sscanf(dem, "%*[^E]E%s", num); *ep = 0;
		if(*pd == '-') putchar('-');
		if(*pd == '+' || *pd == '-') pd++;
		while(*pd == '0') pd++;
		char *pp = strstr(pd, ".");
		if(pp == NULL) { strcat(pd, "."); pp = strstr(pd, "."); }
		int pmn = pp-pd-1;
		if(pp == pd)
			for(pd++; *pd == '0'; pd++, pmn--);
		bool prtp = false;
		for(; *pd != 0; pd++) {
			if(*pd == '.') continue;
			putchar(*pd);
			if(!prtp) { prtp = true; putchar('.'); }
		}
		BigInt r = BigInt(num)+BigInt(pmn);
		putchar('E'); r.print();
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -