❶ 用c語言編寫大數運算,求程序!!!!
我覺得你在存儲大數的時候,可以考慮鏈表的存儲方法存儲大數,每個結點中只存放一位數字,這樣就不會存在存儲空間不夠的問題,在運算的時候,只要在一個結點中出現雙位數,就向後一個結點加個一,也就是對高位進個一,我曾今做過這樣一個程序,時間久了,忘了具體的代碼,只提供個想法,樓主鑽研吧呵呵
❷ 用鏈表法做大整數四則運算C語言程序
只做了加法,思路應該都是這個
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}List,*Link_list;
Link_list create_list(void)
{
Link_list h=NULL;
Link_list s=NULL;
char data=0;
h=(Link_list)malloc(sizeof(List));
h->next=NULL;
h->data=0;
while((data=getchar())=='\n')
{
}
while(1)
{
if(data=='\n')
{
break;
}
s=(Link_list)malloc(sizeof(List));
s->data=data-'0';
s->next=h->next;
h->next=s;
data=getchar();
}
return h;
}
void out_put(Link_list h)
{
Link_list s=NULL;
s=h->next;
while(s!=NULL)
{
printf("%d",s->data);
s=s->next;
}
printf("\n");
}
Link_list add(void)
{
Link_list h=NULL;
Link_list s1=NULL;
Link_list s2=NULL;
Link_list p=NULL;
printf("輸入第一個數據以回車結束:\n");
s1=create_list();
s1=s1->next;
printf("輸入第二個數據以回車結束:\n");
s2=create_list();
s2=s2->next;
h=(Link_list)malloc(sizeof(List));
h->data=0;
h->next=NULL;
while(s1!=NULL && s2!=NULL)
{
p=(Link_list)malloc(sizeof(List));
p->data=s1->data+s2->data+h->data;
h->data=0;
if(p->data>9)
{
p->data=p->data-10;
h->data=1;
}
p->next=h->next;
h->next=p;
s1=s1->next;
s2=s2->next;
}
while(s1!=NULL)
{
p=(Link_list)malloc(sizeof(List));
p->data=s1->data+h->data;
h->data=0;
if(p->data>9)
{
p->data=p->data-10;
h->data=1;
}
p->next=h->next;
h->next=p;
s1=s1->next;
}
while(s2!=NULL)
{
p=(Link_list)malloc(sizeof(List));
p->data=s2->data+h->data;
h->data=0;
if(p->data>9)
{
p->data=p->data-10;
h->data=1;
}
p->next=h->next;
h->next=p;
s2=s2->next;
}
if(h->data==1)
{
p=(Link_list)malloc(sizeof(List));
p->data=1;
p->next=h->next;
h->next=p;
}
return h;
}
int main()
{
int select = 0;
Link_list h=0;
printf("請選擇功能:\n");
printf("1 - 相加\n");
printf("2 - 相減\n");
printf("3 - 相乘\n");
printf("4 - 相除\n");
printf("0 - 退出\n");
while(1)
{
printf("選擇功能\n");
scanf("%d",&select);
if(0 == select)
{
break;
}
switch(select)
{
case 1:
h=add();
printf("所求的和:\n");
out_put(h);
break;
case 2:
break;
case 3:
break;
case 4:
break;
default :
printf("輸入有誤\n");
break;
}
}
}
❸ C語言:用鏈表表示多項式並完成輸入、運算和輸出。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedefintElemType;
/*單項鏈表聲明*/
typedefstructPolynNode{
intcoef;//系數
intexpn;//指數
structPolynNode*next;
}PolynNode,*PolynList;
/*位序(插表尾)輸入n元素值建立帶表結構單鏈線性表*/
/*指數系數輸入*/
voidCreatePolyn(PolynList&L,intn)
{
inti;
PolynListp,q;
L=(PolynList)malloc(sizeof(PolynNode));//結點
L->next=NULL;
q=L;
printf("輸入%d數據 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn);//指數系數輸入
q->next=p;
q=q->next;
}
p->next=NULL;
}
//初始條件:單鏈表L已存
//操作結:依L每數據元素調用函數vi()旦vi()失敗,則操作失敗
voidPolynTraverse(PolynListL,void(*vi)(ElemType,ElemType))
{
PolynListp=L->next;
while(p)
{
vi(p->coef,p->expn);
if(p->next)
{
printf("+");//+號輸項面沒+
}
p=p->next;
}
printf(" ");
}
/*ListTraverse()調用函數(類型要致)*/
voidvisit(ElemTypec,ElemTypee)
{
if(c!=0)
{
printf("%dX^%d",c,e);//格式化輸項式每項
}
}
/*項式相加原理:歸並*/
/*參數:兩已經存項式*/
/*返值:歸並新項式結點*/
PolynListMergeList(PolynListLa,PolynListLb)
{
PolynListpa,pb,pc,Lc;
pa=La->next;
pb=Lb->next;
Lc=pc=La;//用La結點作Lc結點
while(pa&&pb)
{
if(pa->expn<pb->expn)
{
pc->next=pa;//指數相等pc指針連指數結點
pc=pa;
pa=pa->next;//指向該結點指針移
}
elseif(pa->expn>pb->expn)
{
pc->next=pb;//pc指針連指數結點
pc=pb;
pb=pb->next;//指向該結點指針移
}
else//(pa->expn=pb->expn)
{
pa->coef=pa->coef+pb->coef;//指數相等系數相加
pc->next=pa;
pc=pa;
pa=pa->next;//兩指針都往移
pb=pb->next;
}
}
pc->next=pa?pa:pb;//插入剩餘段
returnLc;
}
voidmain()
{
PolynListha,hb,hc;
printf("非遞減輸入項式ha");
CreatePolyn(ha,5);//位序輸入n元素值
printf("非遞減輸入項式hb");
CreatePolyn(hb,5);//位序輸入n元素值
printf("項式ha:");
PolynTraverse(ha,visit);
printf(" ");
printf("項式hb:");
PolynTraverse(hb,visit);
printf(" ");
hc=MergeList(ha,hb);
PolynTraverse(hc,visit);
}
❹ 用c語言編寫基於鏈表的長整數的加減乘除法運算的程序
#include "iostream.h"
void main()
{
int x,y;
cout<<"請輸入兩個整數,整數之間用空格隔開:"<<endl;
cin>>x>>y;
int he=x+y;
int ji=x*y;
int jian=x-y;
double chu=x/y;
cout<<he<<ji<<jian<<chu;
}
❺ c語言大整數運算問題高分求助各位幫忙,不勝感激 寫了一個利用循環鏈表進行超大整數四則運算的c程序
某些即使小數字運算時卻出現內存錯誤的提示
造成這樣的原因很簡單,那就是你用小數字運算或無需進位時,運行到這一步時
if(i!=0){
t=insert_after(t,i); /*處理最後一次進位*/
t->next=s; /*指向頭結點*/
}
由於不需要進位,所以沒執行,導致這一步沒執行 「t->next=s; /*指向頭結點*/」
所以當執行到這一步
oid printint(NODE *s)
{
if(s->next->data!=-1) /*若不是表頭,則輸出*/ 由於指針沒有回指到頭節點,所以這一句會報錯
❻ 要求用C語言編寫程序實現大整數的四則運算(加、減、乘、除)
如果只是實現(加、減、乘、除)那麼,直接用文本文件保存就好,雖然效率略低,但可以保證精確度。
直接按照小學教的加減乘除原理,逐位進行計算,保證無線精確(內存不夠大、cpu不夠快不算)。 這個我就能做。
❼ C語言求單鏈表運算,急
你的這個要求是不可能有人滿足你的。因為關於 C 語言中的各種鏈表的編程(單鏈表、雙鏈表、樹形結構等)以及調試,和 C 語言中別的內容相比起來,可以說是在整個 C 語言編程中最困難的。原因如下:
(1)、由於鏈表的編程必然需要涉及到 C 語言中最難於理解的內容:指針!!而指針內容是 C 語言中功能最為靈活強大、但是同時也是最為抽象、且最難於調試的代碼;
(2)、鏈表還不只是一個 C 語言的問題,它更多的是屬於《數據結構》課程中的一個部分。而《數據結構》課程又是計算機軟體專業中比較難於掌握的一門課程;
(3)、C 語言源代碼的調試過程,它不是只要在紙上寫出單鏈表程序代碼,就能夠保證程序一定能夠運行出正確結果。它必須要在一個編程環境下(例如:Microsoft Visual Studio C++)經過親自上機編程、編譯、鏈接、運行這幾步才能夠把程序的最終正確結果調試出來。
以上就是我多年的 C 語言編程的親身體會。