C 用順序表實現兩個大整數求和

2021-10-02 12:02:40 字數 3216 閱讀 7596

來自大二上學期實驗報告1,請多指教。

一、實驗任務:

用順序表實現兩個大整數求和

二、**:

#include

using

namespace std;

const

int maxsize =

100;

template

<

class

datatype

>

class

seqlist

seqlist

(datatype array,

int n);~

seqlist()

intgetlength()

datatype getelement

(int i)

;int

getlocal

(datatype x)

;void

insert

(int i, datatype x)

; datatype delete

(int i)

;void

printseqlist()

;private

: datatype data[maxsize]

;int length;};

template

<

class

datatype

>

seqlist

::seqlist

(datatype array,

int n)

for(

int i =

0; i < n; i++

) length = n;

}template

<

class

datatype

>

datatype seqlist

::getelement

(int i)

else

}template

<

class

datatype

>

int seqlist

::getlocal

(datatype x)

}return0;

}template

<

class

datatype

>

void seqlist

::insert

(int index, datatype x)

if(index<

1|| index>length +1)

for(

int j = length; j >= index; j--

) data[index -1]

= x;

length++;}

template

<

class

datatype

>

datatype seqlist

::delete

(int index)

else

length--;}

return x;

}template

<

class

datatype

>

void seqlist

::printseqlist()

else

cout << endl;}}

seqlist<

int>

add(seqlist<

int>a, seqlist<

int>b)

for(

; i < alength; i++

)for

(; i < blength; i++)if

(flag ==1)

return c;

}int

main()

;int array2=

; seqlist<

int>a = seqlist<

int>

(array1,9)

; cout <<

"請輸入第乙個大整數:"

<< endl;

a.printseqlist()

; seqlist<

int>b = seqlist<

int>

(array2,8)

; cout <<

"請輸入第二個大整數:"

<< endl;

b.printseqlist()

; seqlist<

int>c =

add(a, b)

; cout <<

"兩個大整數的和是:"

<< endl;

c.printseqlist()

;return0;

}

三、執行結果:

2020.3.19更新:

更改了一下main函式,達到了執行時輸入整數的效果:

int

main()

cout <<

"請輸入第二個大整數的位數:"

<< endl;

cin >> t2;

int* array2 =

newint

[t2]

;for

(int i =

0; i < t2; i++

) seqlist<

int>a = seqlist<

int>

(array1, t1)

; a.

printseqlist()

; seqlist<

int>b = seqlist<

int>

(array2, t2)

; b.

printseqlist()

; seqlist<

int>c =

add(a, b)

; cout <<

"兩個大整數的和是:"

兩個大整數相加 Java實現

本文分析和實現了兩個大整數相加的思路和做法 1.首先,由於計算機表示的整數範圍有限,當加數的位數超過計算機所能表示的範圍時就需要用到這裡的演算法。由於整數過大,不能一次完成相加,我們猜想能否將兩個整數儲存到陣列中,然後逐位相加。當然,這裡實現的方法就是基於此。如下 public class bigi...

用單鏈表實現兩個大整數相加運算

用單鏈表實現兩個大整數相加運算 目的 深入掌握單鏈表應用的演算法設計 主要功能 1 將使用者輸入的十進位制整數字串轉化為帶頭結點的單鏈表,每個結點存放乙個整數字 2 求兩個整數單鏈表相加的結果單鏈表 3 求結果單鏈表的中間位,如123的中間位為2,1234的中間位為2 date 2018 01 14...

用單鏈表實現兩個大型整數的相加

最近幾天一直在忙老師布置的作業,現在小有成就,和大家分享一下。include include define null 0 define len sizeof struct lnode 巨集定義結構體的大小 typedef struct lnode lnode list 定義兩個節點型別,乙個是普通的...