亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如下,想要求(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 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號