数据结构课程设计大数问题.docx

上传人:b****8 文档编号:10530543 上传时间:2023-02-21 格式:DOCX 页数:11 大小:15.85KB
下载 相关 举报
数据结构课程设计大数问题.docx_第1页
第1页 / 共11页
数据结构课程设计大数问题.docx_第2页
第2页 / 共11页
数据结构课程设计大数问题.docx_第3页
第3页 / 共11页
数据结构课程设计大数问题.docx_第4页
第4页 / 共11页
数据结构课程设计大数问题.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

数据结构课程设计大数问题.docx

《数据结构课程设计大数问题.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计大数问题.docx(11页珍藏版)》请在冰豆网上搜索。

数据结构课程设计大数问题.docx

数据结构课程设计大数问题

大数运算问题的程序设计

例如:

输入第一个数为:

1234567890987654321,输入第二个数为:

1239874355878921367863

则输出结果:

1234567890987654321*1239874355878921367863=正确答案。

源程序:

#include"stdio.h"

#include"malloc.h"

#include"string.h"

#defineMAX20

typedefcharDataType;

typedefstruct

{

DataTypedata[MAX];

inttop;

}SeqStack,*PSeqStack;

intStackEmpty(PSeqStackS)

{

returnS->top==-1;

}

intStackFull(PSeqStackS)

{

returnS->top==MAX-1;

}

voidPush(PSeqStackS,DataTypex)

{

if(StackFull(S))

printf("栈中元素已经满了!

请先出栈或把栈置空!

\n");

S->data[++S->top]=x;

}

DataTypePop(PSeqStackS)

{

if(StackEmpty(S))

printf("栈为空,无法出栈,请检查a!

\n");

returnS->data[S->top--];

}

DataTypeStackTop(PSeqStackS)

{

if(StackEmpty(S))

printf("栈为空,无法出栈,请检查!

\n");

returnS->data[S->top];

}

voidAdd(PSeqStackM,PSeqStackN,PSeqStackaddnumber)//加法函数

{

intzongshu,shiweishu=0;

if(M->top==N->top)

{

while(M->top!

=-1)

{

zongshu=Pop(M)-48+Pop(N)-48+shiweishu;

shiweishu=zongshu/10;

Push(addnumber,(zongshu%10+48));

}

}

elseif(M->top>N->top)

{

while(N->top!

=-1)

{

zongshu=Pop(M)-48+Pop(N)-48+shiweishu;

shiweishu=zongshu/10;

Push(addnumber,(zongshu%10+48));

}

while(M->top!

=-1)

{

zongshu=Pop(M)-48+shiweishu;

shiweishu=zongshu/10;

Push(addnumber,(zongshu%10+48));

}

}

elseif(M->toptop)

{

while(M->top!

=-1)

{

zongshu=Pop(M)-48+Pop(N)-48+shiweishu;

shiweishu=zongshu/10;

Push(addnumber,(zongshu%10+48));

}

while(N->top!

=-1)

{

zongshu=Pop(N)-48+shiweishu;

shiweishu=zongshu/10;

Push(addnumber,(zongshu%10+48));

}

}

}

intMinus(PSeqStackM,PSeqStackN,PSeqStackaddnumber)//减法函数

{

intj=0;

intkk;

if(M->top==N->top)

{

for(j=0;j<=M->top;j++)

{

if(M->data[j]>N->data[j])

{

while(N->top!

=-1)

{

if(Pop(M)>=Pop(N))

{

kk=(Pop(M)-48)-(Pop(N)-48);

Push(addnumber,(kk+48));

}

else

{

Push(addnumber,((Pop(M)-48)+10-(Pop(N)-48)+48));

Push(M,(Pop(M)-1));

}

}

j=N->top+1;

}

elseif(M->data[j]data[j])

{

printf("被减数比减数小!

请检查a!

\n");

j=N->top+1;

}

}

Push(addnumber,0);

return1;

}

elseif(M->top>N->top)

{

while(N->top!

=-1)

{

if(Pop(N)<=Pop(M))

{

kk=(Pop(M)-48)-(Pop(N)-48);

Push(addnumber,(kk+48));

}

else

{

Push(addnumber,(Pop(M)-48+10-(Pop(N)-48)+48));//先要把字符转成数字,相减后再把数字转成字符

Push(M,(Pop(M)-1));

}

}

while(M->top!

=-1)

{

Push(addnumber,Pop(M));

}

return1;

}

elseif(M->toptop)

{

printf("被减数比减数小!

请检查!

\n");

return0;

}

}

voidchange1(int(*P1)[40],chars,charP2[20])//以下为乘法函数

{

staticintn=0;

intj,i,m=0;

for(i=(strlen(P2)-1),j=39-n;i>=0;i--,j--)

{

P1[n][j]=((P2[i]-48)*(s-48)+m)%10;

m=((P2[i]-48)*(s-48)+m)/10;

}

if(m!

=0)

{

P1[n][j]=m;

}

n++;

}

 

voidchange2(int(*P)[40],int*Q,ints)

{

inti,j,m=0;

for(j=39;j>=0;j--)

{

for(i=0;i

{

Q[j]+=P[i][j];

}

Q[j]=Q[j]+m;

m=Q[j]/10;

Q[j]=Q[j]%10;

}

}

 

voidputs(int*Q)

{

inti;

for(i=0;i<40;i++)

{

if(Q[i]!

=0)//在Q数组中前面的可能存在的0不用输出.

printf("%d",Q[i]);

}

}

 

voidMultiply(char*A,char*B)

{

intD[20][40]={0},C[40]={0};

inti;

for(i=(strlen(B)-1);i>=0;i--)

{

change1(D,B[i],A);

}

change2(D,C,strlen(B));

puts(C);

}

 

voidmain()

{

SeqStackM,N,addnumber;

intselect;

size_ti;

//chary;

charA[MAX],B[MAX];

M.top=-1;

N.top=-1;

addnumber.top=-1;

printf("***************请输入第一个大数**************\n");

printf("该数应该长度应小于%d位\n",MAX);

gets(A);

for(i=0;i

{

Push(&M,A[i]);

}

//printf("%c",StackTop(&M));

printf("***************请输入第二个大数**************\n");

printf("该数应该长度应小于%d位\n",MAX);

gets(B);

for(i=0;i

{

Push(&N,B[i]);

}

//printf("%c",StackTop(&N));

printf("select1:

Add()\n");//选择加法

printf("select2:

Minus()\n");//选择减法

printf("select3:

Multiply()\n");//选择乘法

printf("select0:

exit\n");//选择退出

printf("inputayourselect(0-4):

\n");

scanf("%d",&select);

if(select!

=0)

{

switch(select)

{

case1:

Add(&M,&N,&addnumber);

printf("大数加法计算的结果是:

\n");

printf("%s+%s=",A,B);

while(addnumber.top!

=-1)

{

printf("%c",Pop(&addnumber));

}

printf("\n");

break;

case2:

if(Minus(&M,&N,&addnumber))//如果返回值为0则说明被减数比减数小,这样就不用再执行下面的语句了;

{

printf("大数减法计算的结果是:

\n");

printf("%s-%s=",A,B);

while(addnumber.top!

=-1)

{

printf("%c",Pop(&addnumber));

}

printf("\n");

}

break;

case3:

printf("大数乘法计算的结果是:

\n");

printf("%s*%s=",A,B);

Multiply(A,B);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1