程序,,不过现在还是有bug,有些运算结果不太对,
在调试,调好的结果会再发一份代码
// name: bigint.h
// author: amrzs
// date: 2014/03/11
#ifndef BIGINT_H
#define BIGINT_H
#include <string>
using namespace std;
class Bigint{
private:
static const int MAX_SIZE = 500;
int size;
int arr[MAX_SIZE];
public:
Bigint();
Bigint(string &s); //initialize with a string num
~Bigint();
int getSize();
int *getNum();
Bigint getPow10(int);
Bigint operator+(Bigint &);
Bigint operator-(Bigint &);
Bigint operator*(Bigint &);
Bigint operator/(Bigint &);
bool operator>=(Bigint &);
void clear();
void printNum();
};
#endif // BIGINT_H
// name: bigint.cpp
// author: amrzs
// date: 2014/03/11
#include <cstring>
#include <iostream>
#include "bigint.h"
using namespace std;
Bigint::Bigint(){
//make sure to be zero(0)
size = 1;
arr[0] = 0;
}
Bigint::Bigint(string &s){
size = s.length();
if (size > MAX_SIZE){
//something wrong
cout << "too many numbers" << endl;
}
for (int i = size-1; i >= 0; i--)
arr[i] = s[size-i-1] - '0';
}
Bigint::~Bigint(){
//to do sth
}
int Bigint::getSize(){
return size;
}
int *Bigint::getNum(){
return arr;
}
Bigint Bigint::operator+(Bigint &x){
Bigint result;
result.size = max(size, x.size);
for (int i = 0; i < result.size; i++)
result.arr[i] = arr[i] + x.arr[i];
for (int i = 0; i < result.size; i++)
if (result.arr[i] > 9){
result.arr[i+1] += result.arr[i] / 10;
result.arr[i] %= 10;
}
return result;
}
Bigint Bigint::operator-(Bigint &x){
Bigint result;
result.size = max(size, x.size);
for (int i = 0; i < result.size; i++)
result.arr[i] = arr[i] - x.arr[i];
for (int i = 0; i < result.size-1; i++)
if (result.arr[i] < 0){
result.arr[i] += 10;
result.arr[i+1]--;
}
while(result.size > 1 && 0 == result.arr[result.size-1])
result.size--;
return result;
}
Bigint Bigint::operator*(Bigint &x){
Bigint result;
result.clear(); //make sure arr[i] of result must be 0
result.size = size + x.size - 1;
for (int i = 0; i < size; i++)
for (int j = 0; j < x.size; j++){
result.arr[i+j] += arr[i] * x.arr[j];
if (result.arr[i+j] > 9){
result.arr[i+j+1] += result.arr[i+j] / 10;
result.arr[i+j] %= 10;
}
}
if (result.arr[result.size+1] > 0)
result.size++;
return result;
}
Bigint Bigint::operator/(Bigint &x){
Bigint result;
result.clear(); //make result being zero
if(size < x.size)
return result;
Bigint y = *this, z;
int pow = y.size - x.size;
while(y >= x){
z = x.getPow10(pow);
while(y >= z){
y = y - z;
result.arr[pow]++;
}
pow--;
}
result.size = size - x.size + 1;
while(result.size > 1 && 0 == result.arr[result.size-1])
result.size--;
return result;
}
Bigint Bigint::getPow10(int n){
if(size+n >= MAX_SIZE || n < 0)
cout << "Out of range" << endl;
Bigint x;
x.clear();
for(int i = 0; i < size; i++)
x.arr[i+n] = arr[i];
x.size = size + n;
return x;
}
bool Bigint::operator>=(Bigint &x){
if(size < x.size)
return false;
if(size == x.size){
for(int i = size-1; i >= 0; i--)
if(arr[i] > x.arr[i])
return true; //greater
else if(arr[i] < x.arr[i])
return false;
return true; //equal
}
return true; //greater
}
void Bigint::clear(){
size = 1;
memset(arr, 0, sizeof(*arr) * MAX_SIZE);
}
void Bigint::printNum(){
cout << size << endl;
for (int i = size-1; i >= 0; i--)
cout << arr[i];
cout << endl;
}
// name: main.cpp
// author: amrzs
// date: 2014/03/11
#include <string>
#include <iostream>
#include "bigint.h"
using namespace std;
/*
Bigint calc(string s1, string s2, char c){
Bigint a(s1), b(s2), result;
switch(c){
'+':
result = a + b;
break;
'-':
result = a - b;
break;
'*':
result = a * b;
break;
'/':
result = a / b;
break;
default:
break;
}
return result;
}
*/
int main(){
string s1, s2;
char c;
cin >> s1 >> s2; // 2 operands and 1 operator
//Bigint result = calcs1, s2, c);
Bigint a(s1), b(s2);
a.printNum();
b.printNum();
Bigint result = a / b;
result.printNum();
//result.printNum();
return 0;
}
上面switch case语法居然都忘了,学了语言多一点就容易混淆。。