为了账号安全,请及时绑定邮箱和手机立即绑定

如下,想要求(a+b)和(a-b)的精确值,并显示计算结果?

如下,想要求(a+b)和(a-b)的精确值,并显示计算结果?

C
子衿沉夜 2022-11-18 17:13:45
设输入数据的最大长度不超过70位,且其中不得包含数字以外的其它符号。如果输入的数据不正确则显示“Input error.”。例如,如果输入是:a=1234567890b=112233445566778899则显示结果为:a+b=112233446801346789a-b=-112233444332211009
查看完整描述

3 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

#include <iostream>
#include <algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;
inline int max(int a,int b){
return a>b?a:b;
}
class LONG{
public:
static const int size=1000,base=10;
int len,num[size];//len存储位长,num[i]存储第i位的值
bool flag;//true=+,false=-
friend int cmp(LONG &a,LONG &b);//必须加friend,否则编译器以为cmp为内部函数
LONG()
bool iszero(){
return len==1&&num[0]==0;
}
LONG(char *s){//以char类表示的数值转换为 LONG类;注意基都是base,
//并且s必须以'\0'结尾,因为要用到strlen()函数
memset(num,0,sizeof(num));
int i;
len=strlen(s);  
for(i=0; i<len; i++) 
num[i]=s[len-i-1]-'0';
while(len>0&&num[len-1]==0) len--;
if(len==0)len++;
if(len>1&&(s[0]=='-'||s[0]=='+')){
len--;  
num[len]=0;
}
if(s[0]=='-')
flag=false;
else flag=true;
}
void print(int k=0){//输出
int i;
if(k!=0&&flag)
printf("+");
if(!flag)
printf("-");
printf("%d",num[len-1]);
for(i=len-2; i>=0; i--) printf("%d",num[i]);
}

LONG operator+(LONG &b){
LONG operator-(LONG &b);
if((flag^b.flag)==true){
if(b.flag==false){
LONG c(b);
c.flag=true;
c=*this-c;
return c;
}
else {
LONG c(*this);
c.flag=true;
c=b-c;
return c;
}
}
LONG c;
int i,t;
if(len>b.len) c.len=len; else c.len=b.len;
for(i=t=0; i<c.len; i++){
t+=num[i]+b.num[i];
c.num[i]=t%base;
t/=base;
}
if(t>0)
c.flag=flag;
return c;
}
LONG operator-(LONG &b){
if(b.iszero())
return *this;
if(this->iszero()){
LONG c=b;
c.flag=1^b.flag;
return c;
}  
if((flag^b.flag)==true){
if(b.flag==false){
LONG c(b);
c.flag=true;
return c+*this;
}
else {
LONG c(b);
c.flag=false;
return c+*this;
}
}
LONG c;
int i,t;
int key=cmp(*this,b);
if(key==0)
return LONG("0");
c.len=max(len,b.len);
if(key<0){
c.flag=false;
if(b.flag==false){
for(i=t=0; i<c.len; i++){
t=num[i]-b.num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
else{
for(i=t=0; i<c.len; i++){
t=b.num[i]-num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
}
else {
c.flag=true;
if(flag==false){
for(i=t=0; i<c.len; i++){
t=b.num[i]-num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
else{
for(i=t=0; i<c.len; i++){
t=num[i]-b.num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
}  
while(c.len>1&&c.num[c.len-1]==0)
c.len--;
return c;
}

void chx(){//规格化
while(len>1&&num[len-1]==0)
len--;
}

};
inline int cmp(LONG &a,LONG &b){
if((a.flag^b.flag)==true){
if(a.flag==true)
return 1;
return -1;
}
if(a.flag==true){
if(b.len>a.len)
return -1;
if(b.len<a.len)
return 1;
for(int i=a.len-1;i>=0;i--){
if(b.num[i]>a.num[i])
return -1;
if(b.num[i]<a.num[i])
return 1;
}
}
if(a.flag==false){
if(b.len>a.len)
return 1;
if(b.len<a.len)
return -1;
for(int i=a.len-1;i>=0;i--){
if(b.num[i]>a.num[i])
return 1;
if(b.num[i]<a.num[i])
return -1;
}
}
return 0;


int main(){
char *s1=new char[71];
char *s2=new char[71];//题目要求不超过70位
LONG a,b,c;
for(printf("请输入a、b:\n");scanf("%s%s",s1,s2);printf("\n请输入a、b:\n")){  
a=LONG(s1);
b=LONG(s2);  
printf("a+b=");
c=a+b;
c.print();
printf("\na-b=");
c=a-b;
c.print();
}
return 0;
}


查看完整回答
反对 回复 2022-11-22
?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

建议采用字符串方式的加减法代替整型数据的加减运算,这需要编程实现,并且减法需要考虑借位(先判别2个字符的大小,然后操作),加法需要考虑进位,并判别2字符串a[],b[]的长度:如
if(a[k]<b[k]) { result[k]='9'-b[k]+a[k]+1; carry=TRUE; } //得到结果的第k位ASCII码。
else { result[k]=a[k]-b[k]; carry=FALSE; }

查看完整回答
反对 回复 2022-11-22
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

#include <iostream>
#include <algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;
inline int max(int a,int b){
return a>b?a:b;
}
class LONG{
public:
static const int size=1000,base=10;
int len,num[size];//len存储位长,num[i]存储第i位的值
bool flag;//true=+,false=-
friend int cmp(LONG &a,LONG &b);//必须加friend,否则编译器以为cmp为内部函数
LONG()
bool iszero(){
return len==1&&num[0]==0;
}
LONG(char *s){//以char类表示的数值转换为 LONG类;注意基都是base,
//并且s必须以'\0'结尾,因为要用到strlen()函数
memset(num,0,sizeof(num));
int i;
len=strlen(s);  
for(i=0; i<len; i++) 
num[i]=s[len-i-1]-'0';
while(len>0&&num[len-1]==0) len--;
if(len==0)len++;
if(len>1&&(s[0]=='-'||s[0]=='+')){
len--;  
num[len]=0;
}
if(s[0]=='-')
flag=false;
else flag=true;
}
void print(int k=0){//输出
int i;
if(k!=0&&flag)
printf("+");
if(!flag)
printf("-");
printf("%d",num[len-1]);
for(i=len-2; i>=0; i--) printf("%d",num[i]);
}

LONG operator+(LONG &b){
LONG operator-(LONG &b);
if((flag^b.flag)==true){
if(b.flag==false){
LONG c(b);
c.flag=true;
c=*this-c;
return c;
}
else {
LONG c(*this);
c.flag=true;
c=b-c;
return c;
}
}
LONG c;
int i,t;
if(len>b.len) c.len=len; else c.len=b.len;
for(i=t=0; i<c.len; i++){
t+=num[i]+b.num[i];
c.num[i]=t%base;
t/=base;
}
if(t>0)
c.flag=flag;
return c;
}
LONG operator-(LONG &b){
if(b.iszero())
return *this;
if(this->iszero()){
LONG c=b;
c.flag=1^b.flag;
return c;
}  
if((flag^b.flag)==true){
if(b.flag==false){
LONG c(b);
c.flag=true;
return c+*this;
}
else {
LONG c(b);
c.flag=false;
return c+*this;
}
}
LONG c;
int i,t;
int key=cmp(*this,b);
if(key==0)
return LONG("0");
c.len=max(len,b.len);
if(key<0){
c.flag=false;
if(b.flag==false){
for(i=t=0; i<c.len; i++){
t=num[i]-b.num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
else{
for(i=t=0; i<c.len; i++){
t=b.num[i]-num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
}
else {
c.flag=true;
if(flag==false){
for(i=t=0; i<c.len; i++){
t=b.num[i]-num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
else{
for(i=t=0; i<c.len; i++){
t=num[i]-b.num[i]-t;
if(t>=0) c.num[i]=t,t=0;
else c.num[i]=t+base,t=1;
}
}
}  
while(c.len>1&&c.num[c.len-1]==0)
c.len--;
return c;
}

void chx(){//规格化
while(len>1&&num[len-1]==0)
len--;
}

};
inline int cmp(LONG &a,LONG &b){
if((a.flag^b.flag)==true){
if(a.flag==true)
return 1;
return -1;
}
if(a.flag==true){
if(b.len>a.len)
return -1;
if(b.len<a.len)
return 1;
for(int i=a.len-1;i>=0;i--){
if(b.num[i]>a.num[i])
return -1;
if(b.num[i]<a.num[i])
return 1;
}
}
if(a.flag==false){
if(b.len>a.len)
return 1;
if(b.len<a.len)
return -1;
for(int i=a.len-1;i>=0;i--){
if(b.num[i]>a.num[i])
return 1;
if(b.num[i]<a.num[i])
return -1;
}
}
return 0;


int main(){
char *s1=new char[71];
char *s2=new char[71];//题目要求不超过70位
LONG a,b,c;
for(printf("请输入a、b:\n");scanf("%s%s",s1,s2);printf("\n请输入a、b:\n")){  
a=LONG(s1);
b=LONG(s2);  
printf("a+b=");
c=a+b;
c.print();
printf("\na-b=");
c=a-b;
c.print();
}
return 0;
}

 


查看完整回答
反对 回复 2022-11-22
  • 3 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信