【发布时间】:2020-02-13 11:07:26
【问题描述】:
我的任务是我必须创建一个计算器,它可以处理最长为 256 个字符的大整数。我遇到了错误,无法将某些减法数字如何工作(例如 23456 - 13141= 10315)进行倍数以及奇怪的问题,但是任何其他减法(例如 6 - 5)都不起作用。我很困惑为什么会这样。
完整代码如下
#include <iostream>
#include <string>
#include "Bigint.h"
using namespace std;
Bigint::Bigint()
{
for (int i = DIGITS-1; i >= 0; --i) {
digits_[i] = 0;
}
radix = 10;
}
Bigint::Bigint(int r)
{
for (int i = DIGITS-1; i >= 0; --i) {
digits_[i] = 0;
}
radix = r;
}
ostream& operator<< (ostream& out, const Bigint& n)
{
string s = "";
int leadingZeros = 0;
for (int i = DIGITS-1; i >= 0 ; --i) {
if(n.digits_[i] != 0){
leadingZeros = 1;
}
if(leadingZeros == 1){
s += char(n.digits_[i] + '0');
}
}
return out << s;
}
istream& operator>> (istream& in, Bigint& n)
{
string s;
if (in >> s) {
int length = s.length();
int i;
for (i = 0; i < DIGITS && i < length; ++i) {
n.digits_[i] = int(s[length-1 - i] - '0');
}
}
return in;
}
Bigint operator+ (const Bigint& n1, const Bigint& n2)
{
Bigint add;
int carry = 0, sum = 0;
for(int i=0; i < DIGITS; ++i){
sum = n1.digits_[i] + n2.digits_[i] + carry;
add.digits_[i] = sum % 10;
carry = sum / 10;
}
return add;
}
Bigint operator- (const Bigint& n1, const Bigint& n2)
{
Bigint sub;
int carry = 0;
for (int i=0; i< DIGITS; i++)
{
int val = n1.digits_[i] - n2.digits_[i] -carry;
if (val < 0)
{
val += 10;
carry = 1;
}
else
carry = 0;
sub.digits_[i] = val;
}
return sub;
}
Bigint Bigint :: multiplyBigint(const Bigint& num, const int n, int count){
Bigint result;
int carry =0;
for(int i=0; i< DIGITS; ++i){
int val = (num.digits_[i] * n) + carry;
result.digits_[i+count] = val % 10;
carry = val / 10;
}
return result;
}
Bigint operator* (const Bigint& n1, const Bigint& n2)
{
Bigint multiply;
Bigint temp;
int count =0;
for(int i=0; i< DIGITS; ++i){
temp = Bigint :: multiplyBigint(n1, n2.digits_[i], count);
count++;
multiply = multiply + temp;
}
return multiply;
}
Bigint operator/ (const Bigint& n1, const Bigint& n2)
{
Bigint divide;
Bigint temp = n1;
while(temp > n2){
divide = divide + 1;
temp = temp - n2;
}
return divide;
}
Bigint operator+ (const Bigint& n1, int n2)
{
Bigint add;
int carry = 0, sum = 0;
for(int i=0; i < DIGITS; ++i){
sum = n1.digits_[i] + (n2 % 10) + carry;
n2 = n2 / 10;
add.digits_[i] = sum % 10;
carry = sum / 10;
}
return add;
}
bool operator> (const Bigint& n1, const Bigint& n2){
for(int i= DIGITS - 1; i >= 0; --i){
if(n1.digits_[i] > n2.digits_[i])
return true;
}
return false;
}
头文件
#ifndef BIGINT_H_ //This checks to whether the given token has been defined somewhere else in the file.
#define BIGINT_H_
#define DIGITS 256 //increases the array size to 256 digits.
class Bigint
{
public: // this makes the members of the public accessible from anywhere in the project.
/**
* Creates a Bigint initialised to 0.
*/
Bigint();
Bigint(int r);
/**
* Inserts n into stream or extracts n from stream.
*/
friend std::ostream& operator<< (std::ostream &out, const Bigint& n);
friend std::istream& operator>> (std::istream &in, Bigint& n);
/**
* Returns the sum, difference, product, or quotient of n1 and n2 and compares them.
*/
friend Bigint operator+ (const Bigint& n1, const Bigint& n2);
friend Bigint operator- (const Bigint& n1, const Bigint& n2);
friend Bigint operator* (const Bigint& n1, const Bigint& n2);
friend Bigint operator/ (const Bigint& n1, const Bigint& n2);
friend Bigint operator+ (const Bigint& n1, int n2);
friend bool operator> (const Bigint& n1, const Bigint& n2);
private: //making this only accessible within other members of this same class.
int digits_[DIGITS];
int radix;
static Bigint multiplyBigint(const Bigint& num, const int n, int count);
};
#endif // BIGINT_H_
Main.cpp
#include <iostream> //provides basic input and output services such as char.
#include "Bigint.h" //provides access and link to header file.
using namespace std;
int findRadix(string value){ //uses the Radix sort alogrithm to sort each value.
if(value.length() == 3){
if(value[0] == '-' && value[1] == 'r'){
if(value[2] >= '2' && value[2] <= '9')
return value[2] - '0';
else if(value[2] >= 'A' && value[2] <= 'Z')
return (value[2] - 'A') + 10;
}
}
return 10;
}
int main(int argc, char *argv[])
{
int radix;
if(argc ==2){
radix = findRadix(argv[1]);
}
Bigint n1(radix), n2(radix); //This compares n1 and n2 to each other to give a result.
char op;
while (cin >> n1 >> op >> n2) {
switch (op) {
case '+' :
cout << n1 + n2 << endl;
break;
case '-' :
cout << n1 - n2 << endl;
break;
case '*' :
cout << n1 * n2 << endl;
break;
case '/' :
cout << n1 / n2 << endl;
break;
}
}
return 0;
}
【问题讨论】:
-
How to debug small programs。老实说,像您这样的程序需要您学习如何使用调试器。此外,如果
6 - 5给出了问题,那么您应该将6和5硬编码到程序中并跳过argc和argv的内容并将op硬编码为减法。 -
我很困惑为什么会这样。 使用调试器一次单步执行 1 行算法。查看每一步的变量。