| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 2195 人关注过本帖
标题:[原创]长整数的加减运算
收藏  订阅  推荐  打印 
starrysky
Rank: 12Rank: 12Rank: 12
来自:华中科技大学EI -T0405
等级:版主
威望:11
帖子:598
积分:6338
注册:2005-9-12
[原创]长整数的加减运算

Typedef struct node {

int data1; //存储系数 0<=data1<=9

unsigned long int data2; // 存储指数0~232-1

struct node *next;

struct node *pre;

}// node, *DuLinkList ;

status CreatList(DuLinkList &L) //长整数的输入与存储

{ char c[];

gets(c);//将长整数以字符串的形式输入

p=L . head; if (a[0]='-' or a[0]='+') { if(a[0]='-') L.head->data1=-1 ;//如果长整数为负,在链表头结点中标记为-1 else L.head->data1=1;//如果长整数为正,在链表头结点中标记为1 for (i=0; a[i]!=''; i++) a[i]= a[i+1]; }

else L.head->data1=1;//如果长整数为正,在链表头结点中标记为1

for(i=0 ; i<strlen(c) ; i++) //从数组转入链表并转化数据类型

{

if(a[i]>' 9 ' or a[i]< ' 0 ') exit ( ERROR INPUT);//出错处理

f(a[i]= ' 0 ') ;//如果当前数位上的数值为0, 则不建立该结点

else

{

r=(node *)malloc(sizeof (node));//分配空间

if(! r) exit (OVERFLOW);//分配失败,则出错

r->data1=a[i]-' 0 '; //数据类型转化 char to int

r->data2= strlen(c)-1-i;

p->next= r; r->pre =p;

p=r;

}

} free(c); //删除数组c return OK;

}// CreatList

void DeletCurElem(*p)//删除当前结点 { h=p->pre; h->next=p->next; p->next->pre=h; free(p); p=h->next; h=NULL; }

void JudgeCurElem(*q) //出理当前数位的进位退位 { h=q->pre; if(q->data1>9) { q->data1-=10; h->data1+=1; JudgeCurElem(h); if(q->data1=0) DeletCurElem(q); q=q->next; } if(0<q->data1<=9) q=q->next; if(q->data1=0) DeletCurElem(q); if(q->data1<0) { q->data1+=10; h->data1-=1; build(h,q);//建立h和p之间的结点 if(h->data1=0) DeletCurElem(h); q=q->next; } h=NULL; }

void build(*p, *q) { t=p; i=p->data2-q->data2-1; for(;i>0;i--)//分配i个空间 { r=(node *)malloc(sizeof (node)); if(! r) exit (OVERFLOW);//分配失败,则出错 r->data1= 9;

r->data2= q->data2+i;

t->next= r; r->pre =t;

t=r;

} t->next=q ; q->pre=t; t=NULL }

void output(DuLinkList &L) //输出函数 { printf("计算结果为:"); p=L.head->next; if(!p) //计算结果为0 printf("0"); else { if L.head->data1=-1 printf("-"); //如果为负数则输出负号 while(p) { printf("%d",p->data1); // 补充为0的数位 if(p->next) { p->data2-- ; while(p->data2!=p->next->data2) { printf("0"); p->data2--; } } if(!p->next&&p->data2!=0) while( p->data2!=0) { printf("0"); p->data2-- ; } p=p->next; } } }

void main() { InitList(La); InitList(Lb); printf(“请输入第一个长整数”); CreatList(La); printf(“请输入第二个长整数”); CreatList(Lb); printf(“请输入运算符”); c=getchar(); if( c= '+ '); if( c= '- ') Lb.head->data1*=-1; else exit(ERROR INPUT); a=La.head->data1; b=Lb.head->data1; qa=La.head->next; qb=Lb.head->next; while(qa and qb) //合并La, Lb { ha=qa->pre; hb=qb->pre; if(qa->data2>qb->data2) qa=qa->next;

if(qa->data2=qb->data2) { qa->data1=(qa->data1*a+qb->data1*b)/a; DeletCurElem(qb); }

if(qa->data2<qb->data2) //将qb插入到qa之前 { qb->data1=qb->data1*b/a; //将qb内数据转换为qa内数据 ha->next=qb; qb->pre=ha; hb->next=qb->next; qb->next=qa; qa->pre=qb; qb=hb->next; } }

if(!ListEmpty(Lb))//Lb 不为空时将其插入到La末尾 { ha->next=qb; qb->pre=ha; qa=ha->next; // qa=qb Lb.head->next=NULL; hb=NULL; while (qa) { qa->data1=qa->data1*b/a; //将qb内数据转换为qa内数据 } qb=NULL; free(Lb.head); } p=La.head->next; if(p->data1<0) { La.head->data1*=-1; while(p) {p->data1*=-1;p=p->next;} } p=La.head->next; while(p) JudgeCurElem(p);//重新遍历La, 进行进位退位处理 output(La); } 上面的算法为长整数的加法和减法运算, 稍微修改一下就可以上机调试.

搜索更多相关主题的帖子: 整数  node  运算  struct  SUP  
2005-9-24 16:22
starrysky
Rank: 12Rank: 12Rank: 12
来自:华中科技大学EI -T0405
等级:版主
威望:11
帖子:598
积分:6338
注册:2005-9-12

改一下定义的结构和部分语句就可以变成象

Typedef struct node {

int data1; //存储万以内的整数 -10000<data1<10000

unsigned int data2; // 存储指数,1 表示10000,2表示 100002,3表示100003,...

struct node *next;

struct node *pre;

}// node, *DuLinkList 这样的结构, 就是某些人的数据结构作业.我就不改了


我的征途是星辰大海
2005-9-24 16:25
starrysky
Rank: 12Rank: 12Rank: 12
来自:华中科技大学EI -T0405
等级:版主
威望:11
帖子:598
积分:6338
注册:2005-9-12

2:题目:长整数四则运算。
问题描述:设计一个实现任意长的整数进行加法运算的演示程序。
基本要求:利用双向循环链表实现长整数的存储,每个结点含一个整形变量。任何整形变量的范围是 -(2^15 - 1)~ (2^15 - 1)。输入和输出形式:按中国对于长整数的表示习惯,每四位一组,组间用逗号隔开。
测试数据:
(1)0;0;应输出“0”。
(2)-2345,6789;-7654,3211;应输出“-1,0000,0000”。
(3)-9999,9999;1,0000,0000,0000;应输出“9999,0000,0001”。
(4)1,0001,0001;-1,0001,0001;应输出“0”。
*****************************************************
这个题目可用楼主的算法完成, 但用第2楼的结构比较好.

我的征途是星辰大海
2005-9-24 16:42
starrysky
Rank: 12Rank: 12Rank: 12
来自:华中科技大学EI -T0405
等级:版主
威望:11
帖子:598
积分:6338
注册:2005-9-12

如果有问题 , 请不吝赐教

我的征途是星辰大海
2005-9-24 17:08
蓝天
Rank: 1
等级:新手上路
帖子:10
积分:200
注册:2005-9-29
我的回复

我是个初学者,以后有时间一定多上来LOOK一下!
2005-9-29 23:54
myajax95
Rank: 12Rank: 12Rank: 12
等级:版主
威望:30
帖子:2978
积分:30680
注册:2006-3-5

好久不写这种程序了,熟熟手。只写了加减法,回头再写乘除。

程序代码:

<BR>#include &lt;iostream&gt;<BR>#include &lt;deque&gt;<BR>#include &lt;string&gt;<BR>#include &lt;sstream&gt;<BR>using namespace std;</P> <P>class CLongInt;<BR>CLongInt abs(CLongInt li);</P> <P>namespace CLongIntDef<BR>{<BR> const int intDataSize = 4;<BR>}</P> <P>class CLongInt<BR>{<BR>public:<BR> CLongInt(string sData);<BR> string ToString() const;<BR> bool operator &gt; (CLongInt li);<BR> CLongInt operator + (CLongInt li);<BR> CLongInt operator - (CLongInt li);<BR> CLongInt operator - ();<BR>private:<BR> deque&lt;int&gt; m_deqData;<BR> bool m_bPositive;<BR>};</P> <P>CLongInt::CLongInt(string sData)<BR>{<BR> string sCurrent;<BR> if (sData.at(0) == '+' || sData.at(0) == '-')<BR> {<BR> m_bPositive = (sData.at(0) == '+');<BR> sData.erase(0, 1);<BR> }<BR> else<BR> m_bPositive = true;</P> <P> while (sData.size() &gt; 0)<BR> {<BR> if ((int)sData.size() &lt; CLongIntDef::intDataSize)<BR> {<BR> m_deqData.push_front(atoi(sData.c_str()));<BR> return;<BR> }<BR> else<BR> {<BR> sCurrent = string(sData, sData.size()-CLongIntDef::intDataSize, sData.size());<BR> sData.erase(sData.size()-CLongIntDef::intDataSize, sData.size());<BR> m_deqData.push_front(atoi(sCurrent.c_str()));<BR> }<BR> }<BR>}</P> <P>string CLongInt::ToString() const<BR>{<BR> stringstream ss;<BR> char charar[5];<BR> int intCount;</P> <P> if (!m_bPositive)<BR> ss &lt;&lt; "-";<BR> for (intCount = 0; intCount &lt; (int)m_deqData.size(); intCount++)<BR> {<BR> if (intCount == 0)<BR> ss &lt;&lt; m_deqData[intCount];<BR> else<BR> {<BR> sprintf(charar, "%04d", m_deqData[intCount]);<BR> ss &lt;&lt; charar;<BR> }<BR> }<BR> return ss.str();<BR>}</P> <P>bool CLongInt::operator &gt; (CLongInt li)<BR>{<BR> int intCount;<BR> if (m_bPositive &gt; li.m_bPositive)<BR> return true;<BR> else if (m_bPositive &lt; li.m_bPositive)<BR> return false;</P> <P> if (m_deqData.size() &gt; li.m_deqData.size())<BR> return m_bPositive;<BR> if (m_deqData.size() &lt; li.m_deqData.size())<BR> return !m_bPositive;</P> <P> for (intCount = 0; intCount &lt; (int)m_deqData.size(); intCount++)<BR> {<BR> if (m_deqData[intCount] &gt; li.m_deqData[intCount])<BR> return m_bPositive;<BR> if (m_deqData[intCount] &lt; li.m_deqData[intCount])<BR> return !m_bPositive;<BR> }</P> <P> return !m_bPositive;<BR>}</P> <P>CLongInt CLongInt::operator + (CLongInt li)<BR>{<BR> CLongInt &amp;liResult = abs(*this) &gt; abs(li) ? abs(*this) : abs(li);<BR> CLongInt &amp;liSmall = abs(*this) &gt; abs(li) ? abs(li) : abs(*this);<BR> int intCount, intData, intFirst, intSecond;</P> <P> for (intCount = 0; intCount &lt; (int)liSmall.m_deqData.size(); intCount++)<BR> {<BR> intFirst = liResult.m_deqData[(int)liResult.m_deqData.size() - 1 - intCount];<BR> intSecond = liSmall.m_deqData[(int)liSmall.m_deqData.size() - 1 - intCount];<BR> if (m_bPositive ^ li.m_bPositive)<BR> intData = intFirst - intSecond;<BR> else<BR> intData = intFirst + intSecond;</P> <P> liResult.m_deqData[(int)liResult.m_deqData.size() - 1 - intCount] = intData%10000;<BR> if (intData &gt;= 10000)<BR> {<BR> if (intCount == liResult.m_deqData.size()-1)<BR> liResult.m_deqData.push_front(1);<BR> else<BR> liResult.m_deqData[(int)liResult.m_deqData.size() - 2 - intCount] += 1;<BR> }<BR> else if (intData &lt; 0)<BR> {<BR> liResult.m_deqData[(int)liResult.m_deqData.size() - 2 - intCount] -= 1;<BR> liResult.m_deqData[(int)liResult.m_deqData.size() - 1 - intCount] = 10000+intData;<BR> }<BR> }<BR> if (!(m_bPositive ^ li.m_bPositive))<BR> liResult.m_bPositive = m_bPositive;<BR> else if (abs(*this) &gt; abs(li))<BR> liResult.m_bPositive = m_bPositive;<BR> else<BR> liResult.m_bPositive = li.m_bPositive;</P> <P> while (liResult.m_deqData[0] == 0)<BR> liResult.m_deqData.pop_front();<BR> return liResult;<BR>}</P> <P>CLongInt CLongInt::operator - (CLongInt li)<BR>{<BR> return *this + (-li);<BR>}</P> <P>CLongInt CLongInt::operator - ()<BR>{<BR> CLongInt liResult(this-&gt;ToString());<BR> liResult.m_bPositive = !liResult.m_bPositive;<BR> return liResult;<BR>}</P> <P>CLongInt abs(CLongInt li)<BR>{<BR> return li &gt; CLongInt("0") ? li : (-li);<BR>}</P> <P>ostream&amp; operator &lt;&lt; (ostream &amp;o, CLongInt &amp;li)<BR>{<BR> return o &lt;&lt; li.ToString();<BR>}</P> <P><BR>int main(int argc, char* argv[])<BR>{<BR> CLongInt in("123456789123"), i2("1111111111111");<BR> cout &lt;&lt; in+i2 &lt;&lt; endl;<BR> cout &lt;&lt; in-i2 &lt;&lt; endl;<BR> return 0;<BR>}<BR>


http://myajax95.bc-cn.net/
2006-6-3 14:43
starrysky
Rank: 12Rank: 12Rank: 12
来自:华中科技大学EI -T0405
等级:版主
威望:11
帖子:598
积分:6338
注册:2005-9-12

,这么早的帖子也给你翻出来了啊.
谢谢给出代码
可惜我学的是C,C++的看不大明白
我可是C++菜鸟级人物

[此贴子已经被作者于2006-6-3 17:41:28编辑过]


我的征途是星辰大海
2006-6-3 17:31
菜鸟上路
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:14
帖子:1120
积分:11366
注册:2006-3-21

顶!


2006-6-3 20:42
vitascl
Rank: 2
等级:注册会员
帖子:29
积分:556
注册:2007-2-7

用楼住所发代码编译出错
错误信息为:
--------------------Configuration: H1 - Win32 Debug--------------------
Compiling...
H1.cpp
C:\Documents and Settings\Administrator\桌面\0313425\H1.cpp(16) : error C2146: syntax error : missing ';' before identifier 'CreatList'
C:\Documents and Settings\Administrator\桌面\0313425\H1.cpp(16) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.

H1.exe - 1 error(s), 0 warning(s)

我现在要用到这段代码 请高人指点!

2007-4-12 21:10
danielliujp
Rank: 2
等级:注册会员
帖子:93
积分:1032
注册:2006-11-30

麻烦加点注释,谢谢了


上有政策 下有对策
2007-4-15 15:52
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.109076 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved