用c或c編寫 一千位的正整數的加法

時間 2021-12-18 21:49:28

1樓:e網e友

//本程式在vc上通過除錯,完全正確

#include

#include

#include

void main()

else

len1=strlen(sum);

len2=strlen(addshort);

carry=0;//進製標誌清理

for(i=1;i<=len2;i++)

}sum[len1-i]=sum[len1-i]+carry;

printf("兩數之和為:%s\n",sum);

getch();

} 翠花姐姐 9:59:16

#include

#include

#include

void main()

else

len1=strlen(sum);

len2=strlen(addshort);

carry=0;//進製標誌清理

for(i=1;i<=len2;i++)

}//以下程式處理

for(;i<=len1;i++)

}if(sum[0]==48)

//去除可能的首元素0

printf("兩數之和為:%s\n",sum);

getch();}

2樓:匿名使用者

#include

void add_thousand(char * add1, char * add2, char * sum)

len1++;

}while(add2[len2]!='\0')len2++;

}if(len2

carry = 0;

for(i=0;i

else

sum[len2-i] = bit_sum + '0';

}sum[0]=carry+'0';

return;

}main()

(用c++輸入輸出語句)程式設計序。輸入乙個3位的正整數,輸出其各位上的數字。

3樓:老馮文庫

c++程式:

#include

using namespace std;

int main()

{int num;

int a, b, c;

printf("請輸入乙個三位數:");

cin>>num;

a = num / 100;

b = num / 10 % 10;

c = num % 10;

cout<<"三位數字分別是:"<

執行測試:

請輸入乙個三位數:123

三位數字分別是:1 2 3

4樓:草叢裡的落葉

void main()

c++實現超長正整數的加法,跪求修改

5樓:杭州離天堂70碼

cout<<"輸入長度"<>len;

int ilen = atoi(len);

char* buff1 = new char[ilen + 1];

char* buff2 = new char[ilen + 1];

memset(buff1 , 0 , ilen + 1);

memset(buff2 , 0 , ilen + 1);

cout<<"輸入數字1"<>buff1;

cout<<"輸入數字2"<>buff2;

listtemp1;

listtemp2;

for (int i = 0 ; i < ilen ; ++i)

if (0 != buff2[i])

}listte***;

int ten = 0;

char flag = 1;

flag |= 1 << 1;

while(true)

else

int arg2 = 0;

list::iterator ite2 = temp2.begin();

if (ite2 == temp2.end())

else

if (0 == flag)

int rt = arg1 + arg2 + ten;

ten = 0;

int one = rt % 10;

ten = rt / 10;

char crt = 0;

itoa(one , &crt , 10);

te***.push_front(crt);

crt = 0;

}if (0 != ten)

char* outputbuff = new char[te***.size() + 1];

memset(outputbuff , 0 , te***.size() + 1);

int index = 0;

for (list::iterator ite = te***.begin();

ite != te***.end();

++ite)

cout<<"兩數相加"<

直接寫在main函式裡,退出的時候有錯,我沒處理,但是邏輯就是這樣。要用list。

我簡單的測試了下沒問題。

思路就是用兩個list儲存每個位置上的數量,然後就和我們小學學數學那樣從最後一位乙個乙個的向前加,用乙個數字來儲存進製資訊,每次相加就加上這個進製資訊,結果放在第三個list裡。

6樓:匿名使用者

我覺得大整數不應該用double做為輸入,應該用字串,另外,為什麼int a[n]裡乙個int只存放一位數字哪?這個感覺也不太好,我覺得應該可以放更大的數字。我把**簡單的調整了下:

#include

#include

#include

using namespace std;

const int m = 4;

const int n = 99;

void sort(int a, string z)else }}

int main()

, b[n] = , c[n] = ;

int carry = 0;

int tmp = 0;

int num = 0;

sort(a, x);

sort(b, y);

for (int i = 0; i < n; ++i)for (int i = num; i >= 0; --i)cout<

7樓:冰旋華影

大數運算

兩個99位的大數不能用double臨時儲存,只能用陣列來存。

8樓:

首先大整數我覺得不能用double,因為可能超出,所以存只能乙個字元乙個字元存,然後就是進製的問題,我重寫了乙個,你可以看一下,n就是你給的n,每乙個整數以#結尾,表示輸入完了

char a[n]=;

char b[n]=;

int c[n]=;

int ia,ib,ic;

int carry=0; //進製標誌

for(ia=1;a[ia-1]!='#';++ia)for(ib=1;b[ib-1]!='#';++ib)ia=ia-2;

ib=ib-2;

for(ic=0;ia>=0&&ib>=0;++ic,--ia,--ib)

else

if(c[ic]>=10)

}for(int i=ic-1;i>=0;--i)cout<

system("pause");

return 0;

9樓:匿名使用者

要考慮進製問題,且需逆序輸出

用c語言編寫 檔案讀寫,用C語言編寫 檔案讀寫

第一種 include include include struct student 結構體s 50 void daoru struct student s,int n 檔案匯入函式 else fclose p void paixu struct student s,int n 排序函式 int i...

用c語言編寫宿舍管理查詢系統,用C語言編寫宿舍管理查詢系統

定義結構或者類,類屬性包括姓名 學號 房間號 include stdio.h include stdlib.h 標準庫函式 include ctype.h 字元操作函式 include string.h typedef struct studentsaddress 以下是函式原型 int enter...

用c編寫賓館客房管理系統,用C 編寫賓館客房管理系統

void inquire printf n printf n k 0 printf 下列房間已預訂 n for j 0 j 80 j k 0 printf n printf n printf 下列房間有人入住 n for j 0 j 80 j printf n printf n else if in...

用c語言編寫隨機點名程式,用C語言編寫乙個隨機點名程式

include include include define stu num max 4struct studentinfo 學生資訊結構stu stu num max void writedata 寫入學生資訊if fp fopen myfile.dat ab null for int j 0 j...

用c 編寫分數計算器,用C 編寫分數計算器

貌似樓上的是大神,linux下編寫的哦。如何用c 寫乙個簡單的計算器程式 include using std cin using std cout int main return 0 如果不限定for或while,還有兩種簡單思路 輸入乙個string遍歷字元,搜尋符號 2.while cin in...