帶符號大整數計算器 C 實現)

2021-08-20 17:52:43 字數 3276 閱讀 3330

為帶符號大整數定義乙個類。將帶符號大整數表示成兩個值:乙個是絕對值,用無符號大整數表示,乙個是符號,用整形數表示,1表示正號,-1表示負號。

過載輸入和輸出運算子》和<<。帶符號大整數以-1234567890,1234567890668這樣的形式來輸入和輸出。注意可能在輸入時包含有正號,所以必須支援像+1234567890這樣的輸入。過載以下所有運算子,:+,-,*,/。

#include#includeusing namespace std;

class biginter

biginter(string a)

else

}friend ostream& operator<<(ostream& out, biginter a);

friend istream& operator>>(istream& in, biginter &a);

biginter operator+(biginter &a);

biginter operator-(biginter &a);

biginter operator*(const biginter &a);

biginter operator/(const biginter &a);

~biginter() {};

};biginter biginter:: operator +(biginter &a)

else if (a.signal == 1 && this->signal == -1)

for ( i = 0; i < tmp.size() && i < tmp1.size(); i++)

else

flag = 0;

} if (tmp.size() == tmp1.size() && flag == 1)

tmp2.push_back('1');

if (isignal == 1)

res.signal = 1;

else

res.signal = -1;

return res;

} if (isignal == 1)

res.signal = 1;

else

res.signal = -1;

return res;

} reverse(tmp2.begin(), tmp2.end());

res.num = tmp2;

if (a.signal == 1 && this->signal == 1)

res.signal = 1;

else

res.signal = -1;

return res;

}biginter biginter:: operator -(biginter &a)

else if (a.signal == 1 && this->signal == -1)

int flag1=0;

if (a.num.size() == this->num.size())

else if (tmp[i] < tmp1[i])

}for (i = 0; i < tmp.size() && i < tmp1.size(); i++)

else

tmp2.push_back(ch);

} for (int i = tmp2.size() - 1; i >= 0; i--)//去除前導0

else

break;

} reverse(tmp2.begin(), tmp2.end());

res.num = tmp2;

if ((a.signal == -1&&flag1==1)||(a.signal==1&&flag1==0))

else

return res;

} if (a.num.size() > this->num.size())

for (i = 0; i < tmp.size() && i < tmp1.size(); i++)

else

tmp2.push_back(ch);

} if (i= 0 && tmp1[i] + flag - '0' <= 9)

else

tmp2.push_back(ch);

i++;

while (i < tmp1.size())

else

tmp2.push_back(ch);

i++;

} for (int i = tmp2.size() - 1; i >= 0; i--)

else

break;

} reverse(tmp2.begin(), tmp2.end());

res.num = tmp2;

if ((this->signal == 1 && a.signal == 1&&flag2==0)||(this->signal == -1 && a.signal == -1 && flag2 == 1))

else

return res; }}

biginter biginter:: operator *(const biginter &a)

for (int j = 0; j < s1.size(); j++)

if (flag != 0)

reverse(tmp.num.begin(), tmp.num.end());

res.num = (res + tmp).num;

tmp.num="";

} res.signal = a.signal*this->signal;

return res;

}biginter biginter:: operator /(const biginter &a)

n1 = n * s2;

s1 = s1 - n1;

if (n != 0)

t = s1.num.size() - s2.num.size() - 1 > 0;

} while ((s1 - s2).signal!=-1)

tmp.signal = this->signal*a.signal;

return tmp;

}istream& operator>>(istream& in, biginter &a)

else if (s[0] == '+')

else

return in;

}ostream& operator<<(ostream& out, biginter a)

else

return out;

}int main()

模組計算器(大整數運算)

問題 這一任務為餘數的核心屬性提供了實踐,在算術中進行運算 通過加法和乘法對餘數進行持久化。這個重要的屬性通常用於檢查計算結果 競爭程式設計 計算校驗和,特別是用於加密。看見模演算法徹底的解釋。我們這裡有一種長的算術計算,我們被問到一些數模的結果。result m 以多種語言編寫 取自codeabb...

yacc學習筆記(四)帶符號表的計算器程式

因為符號表要求語法分析程式和詞法分析程式共享的資料結構,所以我們建立乙個標頭檔案ch3hdr.h vi ch3hdr.h define nsyms 20 maximum number of symbols struct symtab symtab nsyms struct symtab symloo...

計算器c 實現

1.中綴表示式轉字尾表示式 中綴 a b c d e 字尾 abc de 轉換步驟 1 從左到右掃瞄中綴表示式,遇到 轉 6 2 遇到運算元直接輸出 不進棧 3 遇到 則連續出棧輸出,直到遇到 為止 出棧但不輸出 否則 4 若是其它操作符,則與棧頂的操作符比較優先順序 若優先順序小於棧頂的優先順序,...