C++ 比吉特加减法
我目前正在做一项学校作业(很抱歉,我的问题是关于我的作业,但我不是在问代码中使用的算法)。我现在正在做算术部分,加法和减法。由于只有两个操作员,因此有8种操作组合。在我的程序中,我可以执行以下四种情况:C++ 比吉特加减法,c++,math,bigint,C++,Math,Bigint,我目前正在做一项学校作业(很抱歉,我的问题是关于我的作业,但我不是在问代码中使用的算法)。我现在正在做算术部分,加法和减法。由于只有两个操作员,因此有8种操作组合。在我的程序中,我可以执行以下四种情况: (+a)+(+b) (-a)+-b) (-a)-(+b) (+a)-(b) 但是我想不出其他四种情况怎么办,, 即 (+a)+(-b) (-a)+(+b) (-a)-(b) (+a)-(+b) 我真诚地希望你们能就如何处理这四起案件提供建议和建议 这是我的密码: linkedListType.h
(+a)+(+b)
(-a)+-b)
(-a)-(+b)
(+a)-(b)
但是我想不出其他四种情况怎么办,,
即
(+a)+(-b)
(-a)+(+b)
(-a)-(b)
(+a)-(+b)
我真诚地希望你们能就如何处理这四起案件提供建议和建议
这是我的密码:
linkedListType.h:
这是一个常见的链表头文件,因此我不在这里发布全部代码
bigInteger.h:
这个函数很长。因此,我没有把它们发出去
#ifndef BIG_INTEGER_H
#define BIG_INTEGER_H
#include <stack>
#include <iostream>
#include "linkedListType.h"
using namespace std;
class bigInteger
{
private:
int sign; // set 0 for positive, 1 for negative
linkedListType<int> digits; // internal linked-list for storing digits in reverse order
public:
bigInteger(); // default constructor
bigInteger(const bigInteger& other); // copy constructor
// Overload constructor
// Use an numerical string to construct this bigInteger
// For negative number, the first char in the string is '-'
// e.g. "-12345"
bigInteger(const string& number);
// overload the assignment operator
const bigInteger& operator= (const bigInteger& other);
// Return a new bigInteger that is equal to *this + other
// The contents of this and other should not be modified
bigInteger& operator+ (bigInteger& other);
// Return a new bigInteger that is equal to *this - other
// The contents of this and other should not be modified
bigInteger& operator- (bigInteger& other);
// Print a big integer
// Since the digits are stored in reverse order in the internal
// list, you should print in reverse order
// Print "undefined" if the digits list is empty
friend ostream& operator<<(ostream& os, bigInteger& n);
};
\ifndef BIG\u INTEGER\u H
#定义大整数
#包括
#包括
#包括“linkedListType.h”
使用名称空间std;
类bigInteger
{
私人:
int-sign;//设置0表示正,设置1表示负
linkedListType digits;//用于按相反顺序存储数字的内部链表
公众:
bigInteger();//默认构造函数
biginger(const biginger&other);//复制构造函数
//重载构造函数
//使用数字字符串构造此bigInteger
//对于负数,字符串中的第一个字符是“-”
//例如“-12345”
bigInteger(常量字符串和数字);
//重载赋值运算符
常量bigInteger和运算符=(常量bigInteger和其他);
//返回一个等于*this+other的新bigInteger
//不应修改此文件和其他文件的内容
bigInteger和运算符+(bigInteger和其他);
//返回一个等于*this-other的新bigInteger
//不应修改此文件和其他文件的内容
bigInteger和运算符-(bigInteger和其他);
//打印一个大整数
//因为数字在内部存储器中以相反的顺序存储
//列表,则应按相反顺序打印
//如果数字列表为空,则打印“未定义”
friend ostream&operator所有的评论都在处理运算符重载的问题,但我怀疑OP是关于如何计算所有用于加法和减法的操作数符号组合
两个补码
大多数整数ALU的HW实现使用2的补码表示有符号的数字。这意味着:
-X = (X^0xFFFFFFFF)+1; // 32 bit example
仅通过反转所有位并递增结果而获得的负值。因此,当符号位仍然MSB时,您可以对所有加法/子加法和符号组合使用单加法器体系结构。此编码处理有符号和无符号操作的方式相同,因此对于加法,您完全没有问题(无需执行任何操作,只需添加)对于减法,只需反转第二个操作数的符号并进行加法
C++实现如下所示:
bool bigInteger::sign() { return MSB_of_this; }
bigInteger bigInteger::operator+ () { bigInteger z=*this; return z; }
bigInteger bigInteger::operator- () { bigInteger z=*this; /* here negate all bits and increment z*/ return z' |z; }
bigInteger bigInteger::operator+ (bigInteger& y)
{
bigInteger z;
// here do z = *this + y as if all operands were positive
return z;
}
bigInteger bigInteger::operator- (bigInteger& y0)
{
bigInteger z;
y=-y0; // invert sign in temp variable to avoid changing input operand value !!!
// here do z = *this + y as if all operands were positive
return z;
}
bigInteger bigInteger::operator+ (bigInteger& y)
{
bigInteger z;
bool sx,sy;
sx=sign();
sy=y.sign();
if (sx==sy) z=uadd(*this,y); // same signs is add
else // not then first operand for (a-b) is the positive one
{
if (sx) return y-(*this);
else return (*this)-y;
}
// here set the sign to the same as operands
z.set_sign(sx);
return z;
}
请注意,我没有更改任何输入操作数,所有返回都是单独的变量(即使对于一元+也不使用此变量)。这是由于操作数的链能力。如果不以这种方式编码,某些编译器很难在一行中菊花链接3个以上的运算符,这意味着您无法编译类似的行
c=a+b-c*a*b/(s-d)+(23*q);
相反,你必须把它分解成更简单的术语
如果不使用二的补码
然后,您必须处理输入操作数的符号。最常见的情况是,您将sign flag作为独立变量,而不是在数字位中。在这种情况下,您需要一些根本不考虑符号的函数
bigInteger uadd(bigInteger &a,bigInteger &b); //=|a|+|b|
bigInteger usub(bigInteger &a,bigInteger &b); //=|a|-|b| but operands must be: |a|>|b|
bool ugeq(bigInteger &a,bigInteger &b); //=|a|>=|b| Unsigned Greater or EQual
这很容易
添加
- 只有当数字是同一个符号时才可以添加数字,如果不是,请将其转换为减法
减法
- 只有当子数是同一符号且第一个操作数大于或等于第二个操作数时,才能使用子数
所以应该是这样的:
bool bigInteger::sign() { return MSB_of_this; }
bigInteger bigInteger::operator+ () { bigInteger z=*this; return z; }
bigInteger bigInteger::operator- () { bigInteger z=*this; /* here negate all bits and increment z*/ return z' |z; }
bigInteger bigInteger::operator+ (bigInteger& y)
{
bigInteger z;
// here do z = *this + y as if all operands were positive
return z;
}
bigInteger bigInteger::operator- (bigInteger& y0)
{
bigInteger z;
y=-y0; // invert sign in temp variable to avoid changing input operand value !!!
// here do z = *this + y as if all operands were positive
return z;
}
bigInteger bigInteger::operator+ (bigInteger& y)
{
bigInteger z;
bool sx,sy;
sx=sign();
sy=y.sign();
if (sx==sy) z=uadd(*this,y); // same signs is add
else // not then first operand for (a-b) is the positive one
{
if (sx) return y-(*this);
else return (*this)-y;
}
// here set the sign to the same as operands
z.set_sign(sx);
return z;
}
由于这是一项作业,我将让-
操作员不带代码
它与+
运算符非常相似,但您需要按绝对大小(使用ugeq
)对输入操作数进行排序,并将结果符号设置为abs较大的一个。如果交换操作数,则反转结果符号。仅当booth操作数是相同符号时,才进行减法,否则将转换为(a+b)
希望对您有所帮助您应该添加一元运算符-例如,请参见。还要注意,它不应该是biginger&operator+(biginger&other);
而是biginger operator+(const biginger&other);
即按值返回并按const
引用获取参数。您可能需要编写biginger&operator+=(const biginger&other);
(和操作符-=
),然后您可以像这样将+
作为非成员函数编写:biginger操作符+(biginger lgs,const biginger&rhs){return lhs+=rhs;}
。同样地,朋友ostream&operator感谢您的建议@TonyD。但是,我可以问一些问题吗?您介意解释一下一元运算符的用法吗?还有,+=和-=?我不理解您的问题。而且,您发布的代码不太可能足以让任何人帮助您。(还有,“反向”@user2847449:当然-欢迎提问。假设您有biginger x;
并编写-x
,如果没有要减去x
的“左手边”,编译器将查找biginger::操作符-()
函数并调用它-它应该返回*this
-的否定,这在链接的问题abo中解释