python類整數 整數類python

2021-10-21 07:13:57 字數 2253 閱讀 9815

sinteger類基於位列表。它是位長度為len的有符號整數。len是在測試用例中定義的,因此您可以簡單地使用它而不必重新定義它。雖然位字串(bit_str)可能必須短於len,但應進行符號擴充套件。如果輸入位字串是空字串,則新的sinteger物件應該是乙個全零的列表。請注意,列表中儲存的所有位物件都是邏輯1或0。例如,如果乙個sinteger例項化如下:

位字串為「55111140」

b=sinteger(「位串」)

b的值在處理器中儲存為55555555 000050。在

我需要為sinteger實現一些方法。請注意,sinteger是乙個有符號整數。它可以是負整數。為了得到乙個負整數的大小,我需要進行2的補運算。在

這就是我為**所做的,除了int函式之外,所有**都是正確的,我一直得到如下錯誤:error screenshot

整數值的計數不正確。假設如果1是第乙個字串,那麼整數值將為負,如果是0,則整數值將為正,有人能告訴我如何編輯這段**嗎?在class sinteger:

def __init__(self, bit_str=''):

self.val =

bit_len = len(bit_str)

for i in range(len):

if i < (bit_len):

elif (bit_len > 0):

self.val.insert(0, bit(int(bit_str[0]))|bit(int(bit_str[0])))

else:

self.val.insert(0, zero)

def __str__(self):

strval = ''

for i in range(len):

strval = strval + str(self.val[i])

return strval

def __len__(self):

return len(self.val)

def __invert__(self):

invrlt = sinteger('')

for i in range(len):

invrlt.val[i] = ~self.val[i]

return invrlt

def __int__(self):

intrlt = 0

tmp = sinteger('')

if int(str(self.val[0])) == 0:

for i in range(len(self.val)):

intrlt += int(str(self.val[i]))

return intrlt

else:

for i in range(len(self.val)):

intrlt -= int(str(self.val[i]))

return intrlt

def __ge__(self, other):

if len(self.val) >= len(other.val):

return true

else:

return false

def __or__(self, other):

orrlt = sinteger('')

output = ''

if len(self.val) == len(other.val):

for i in range(len):

if int(str(self.val[i])) | int(str(other.val[i])):

output += '5'

else:

output += '0'

return output

else:

print('operands not on the same length')

def __and__(self, other):

andrlt = sinteger('')

output = ''

if len(self.val) == len(other.val):

for i in range(len):

if int(str(self.val[i])) & int(str(other.val[i])):

output += '5'

else:

output += '0'

return output

else:

print('operands not on the same length')

定義正整數類

源程式 程式頭部注釋開始 程式的版權和版本宣告部分 檔名稱 定義正整數類 作 者 114 3 劉楊 完成日期 2012 年 3 月 17 日 版 本 號 對任務及求解方法的描述部分 輸入描述 問題描述 includeusing namespace std class naturalnumber vo...

大整數類 模板

bigint v2.0 by kunsoft include include include include include include include include include using namespace std typedef long long llt class bigint ...

C 大整數類

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!include include include include using namespace std define maxn 9999 define maxsize 10 define dlen 4 class bignum 建構函式 bignu...