日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

大数相加c语言思路,大数相加

發(fā)布時間:2023/12/2 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大数相加c语言思路,大数相加 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓

#include

#include

/**

*

* 定義雙向節(jié)點

* 數(shù)據(jù)區(qū)為一個整型數(shù)字

*

**/

struct Node {

int number;//數(shù)據(jù)區(qū),僅一個數(shù)字,保存一位數(shù)字

struct Node * next;

struct Node * prev;

};

typedef struct Node Node;

/**

* 創(chuàng)建頭節(jié)點

* param:number,此數(shù)字賦值給創(chuàng)建出的節(jié)點的數(shù)字區(qū)

* return:創(chuàng)建出來的頭結點

*

*

**/

Node * create_head(int number){

Node * head = (Node *)malloc(sizeof(Node));

head->number = number;

//只有一個節(jié)點的時候,此節(jié)點的next和prev均為NULL

head->next = NULL;

head->prev = NULL;

return head;

}

/**

* 創(chuàng)建一個新的節(jié)點,并將此節(jié)點追加到鏈表的最末端

* param:**tail鏈表尾指針的指針,因為方法中需要修改尾指針,因為你懂的原因,這里使用了指針的指針

* number整型數(shù)字,賦值給新節(jié)點的數(shù)據(jù)區(qū)

**/

void create_node_to_tail(Node ** tail,int number){

//為新節(jié)點開辟空間

Node * node = (Node *)malloc(sizeof(Node));

node->number = number;

//因為新節(jié)點要加到尾部,所以它的next變?yōu)镹ULL,prev指向原有的尾部

node->next = NULL;

node->prev = *tail;

//原有的尾部的next指向最新的節(jié)點

(*tail)->next = node;

//新節(jié)點到鏈表尾部之后,新節(jié)點成為鏈表的尾部

*tail = node;

}

/**

* 創(chuàng)建一個新的節(jié)點,并將此節(jié)點追加到鏈表的最前端

* param**head鏈表頭指針的指針,原因同上

* number同上

*

**/

void create_node_to_head(Node ** head,int number){

//為新節(jié)點開辟空間

Node * node = (Node *)malloc(sizeof(Node));

node->number = number;

//新節(jié)點加到頭前面,所以它的prev變?yōu)镹ULL,next指向原有的頭部

node->prev = NULL;

node->next = *head;

//原有的頭部的prev指向最新的節(jié)點

(*head)->prev = node;

//最新的節(jié)點成為新的頭部

*head = node;

}

/**

* 大數(shù)相加

* param:number1_head加數(shù)1的頭指針

* number2_head加數(shù)2的頭指針

* return:計算結果的尾指針,主要是為了方便輸出

*

**/

Node * add_big(Node * number1_head,Node * number2_head){

//進位

int carry = 0;

Node * number1_current = number1_head;

Node * number2_current = number2_head;

//保存計算結果的鏈表的頭指針和尾指針

Node * result_head = NULL;

Node * result_tail = NULL;

//循環(huán)時,只要有一個加數(shù)沒有結束,則繼續(xù)循環(huán)

while(number1_current != NULL || number2_current != NULL){

//加數(shù)1,如果節(jié)點為NULL,則此加數(shù)為0,應對兩個加數(shù)不一樣長的情況

int number1 = (number1_current == NULL)?0:number1_current->number;

//加數(shù)2,如果節(jié)點為NULL,則此加數(shù)為0,應對兩個加數(shù)不一樣長的情況

int number2 = (number2_current == NULL)?0:number2_current->number;

//計算加數(shù)1+加數(shù)2+進位

int result = number1 + number2 + carry;

//更新進位

carry = result/10;

//獲取當前位除掉進位后的值

result = result % 10;

//如果尚未創(chuàng)建鏈表,說明當前是第一次進入循環(huán),則創(chuàng)建頭節(jié)點

if(result_head == NULL){

result_head = create_head(result);

result_tail = result_head;

}else{

//進入這里,說明鏈表已創(chuàng)建,可以直接創(chuàng)建新節(jié)點,并追加到原鏈表后面

create_node_to_tail(&result_tail,result);

}

//移動指針,進入下一次循環(huán),加入非空判斷,應對兩個加數(shù)不一樣長的情況

if(number1_current != NULL){

number1_current = number1_current->next;

}

if(number2_current != NULL){

number2_current = number2_current->next;

}

}

//結束循環(huán)后,如果進位大于0,則還需要再創(chuàng)建一個節(jié)點

if(carry > 0){

create_node_to_tail(&result_tail,carry);

}

//返回尾指針

return result_tail;

}

/**

* 輸入大數(shù)

* return:輸入后產生的鏈表的頭指針

*

**/

Node * input_number(){

printf("%s","請輸入加數(shù)\n");

//保存輸入的數(shù)字的頭指針和尾指針

Node * head = NULL;

Node * tail = NULL;

while(1){

//每次輸入一個字符

char c;

scanf("%c",&c);

//0-9對應的asicc碼值為48-57,在此范圍內,說明輸入的是數(shù)字

if(c >= 48 && c <= 57) {

//減去48,將char型轉為數(shù)字

int number = c - 48;

//如果尚未創(chuàng)建鏈表,說明是第一次進入循環(huán),則創(chuàng)建頭節(jié)點

if(head == NULL){

head = create_head(number);

tail = head;

}else{

//進入到這里,說明鏈表已創(chuàng)建,可以直接創(chuàng)建新節(jié)點,并加到原鏈表的前面

//注:鏈表保存的數(shù)字,低位在前,高位在后,而輸入數(shù)字時,高位先輸入,低位后輸入

//所以這里鏈表的結點要加到鏈表的最前面

create_node_to_head(&head,number);

}

}

//如果輸入的是回車,則退出循環(huán)

else if(c == '\n'){

break;

}else{

//如果輸入的不是上兩者,則返回NULL

printf("%s","只可以輸入數(shù)字\n");

return NULL;

}

}

return head;

}

/**

* 輸出大數(shù)

* param:tail要輸出的鏈表的尾指針,因為存放數(shù)據(jù)的時候,低位在前,高位在后,為了正常輸出,需要從尾向頭輸出

*

**/

void output_number(Node * tail){

Node * current = tail;

//從尾到頭,一個一個輸出數(shù)字

while(current != NULL){

printf("%d",current->number);

current = current->prev;

}

}

void main(){

//依次輸入加數(shù),如果返回為NULL,則說明輸入不合法,退出

Node * number1_head = input_number();

if(number1_head == NULL)

return;

Node * number2_head = input_number();

if(number2_head == NULL)

return;

//計算結果,并根據(jù)返回的尾指針輸出數(shù)字

Node * result_tail = add_big(number1_head,number2_head);

output_number(result_tail);

}

總結

以上是生活随笔為你收集整理的大数相加c语言思路,大数相加的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。