大数相加c语言思路,大数相加
該樓層疑似違規(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语言思路,大数相加的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言中变量的值十进制,C语言中介绍的整
- 下一篇: 四阶龙格库塔c语言,四阶龙格库塔算法的C