📄 main.cpp
字号:
#include"iostream.h"
#include"math.h"
#include"conio.h"
#include"clsNode.h"
#include"lk_list.h"
#include"stdlib.h"
struct node
{
int data;//存当前位的数字
int value;//存当前位的权
};
//界面设计
void design(void)
{
int i;
char temp;
cout<<endl<<endl;
for(i=0;i<16;i++) cout<<" ";
cout<<"~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ welcome ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "<<endl;
cout<<endl;
for(i=0;i<18;i++) cout<<" ";
cout<<"this is a program to deal with huge number"<<endl;
cout<<endl;
for(i=0;i<13;i++) cout<<" ";
temp=26;
for(i=0;i<8;i++) cout<<" "<<temp;
cout<<" zhangjie ";
temp=27;
for(i=0;i<8;i++) cout<<temp<<" ";
cout<<endl<<endl<<endl;
}
//判断输入的数字是否正确
bool numTrue(char *a)
{
int dotNum=0,i,length;
for(i=0;i<100;i++)
{
if(a[i]=='\0') break;
else continue;
}
length=i;
for(i=0;i<length;i++)
{
if(a[i]=='.') dotNum++;
if(dotNum==2) return false;
if((a[i]!='.')&&( a[i]<48 || a[i]>57)) return false;
}
for(i=0;i<100;i++)
{
if(a[i]=='\0') break;
else continue;
}
length=i;
if(a[0]=='.' || a[length]=='.') return false;
if(a[0]=='0' && a[1]<=57 && a[1]>=48) return false;
return true;
}
int charConvert(char a)
{
switch(a)
{
case'0':return 0;
case'1':return 1;
case'2':return 2;
case'3':return 3;
case'4':return 4;
case'5':return 5;
case'6':return 6;
case'7':return 7;
case'8':return 8;
case'9':return 9;
default:return 0;
}
}
//将一个字符数组中的元素转到链表中
//123.45存为54.321
//返回小数点所在的位置
void convert(char *a,lk_list<node> &b)
{
node temp;
int dotPos,i;
int d,v;
for(i=0;i<100;i++)
{
if(a[i]=='.' || a[i]=='\0') break;
else continue;
}
dotPos=i;
for(i=0;i<100;i++)
{
if(a[i]=='\0') break;
if(i<dotPos)
{
v=dotPos-i-1;
d=charConvert(a[i]);
temp.data=d;
temp.value=v;
b.InsertNode(1, temp);
}
else if(i>dotPos)
{
v=dotPos-i;
d=charConvert(a[i]);
temp.data=d;
temp.value=v;
b.InsertNode(1, temp);
}
else continue;
}
}
//加法运算
void add(lk_list<node> a,lk_list<node> b,lk_list<node> &c)
{
int ap=1,bp=1,cp=1,toNext=0;
node tempa,tempb,tempc;
while(true)
{
while(ap<=a.ListLength() && bp<=b.ListLength())
{
a.GetValue(ap,tempa);
b.GetValue(bp,tempb);
if(tempa.value<tempb.value)
{
c.InsertEnd(tempa);
ap++;
}
else if(tempa.value>tempb.value)
{
c.InsertEnd(tempb);
bp++;
}
else
{
tempc.data=tempa.data+tempb.data;
tempc.value=tempa.value;
ap++; bp++;
c.InsertEnd(tempc);
}
}
while(ap==a.ListLength()+1 && bp<=b.ListLength())
{
b.GetValue(bp,tempb);
c.InsertEnd(tempb);
bp++;
}
while(ap<=a.ListLength() && bp==b.ListLength()+1)
{
a.GetValue(ap,tempa);
c.InsertEnd(tempa);
ap++;
}
break;
}
for(cp=1;cp<=c.ListLength();cp++)
{
c.GetValue(cp,tempc);
tempc.data=tempc.data+toNext;
if(tempc.data>=10)
{
toNext=1;
tempc.data=tempc.data-10;
c.locate_delete(cp);
c.InsertNode(cp,tempc);
}
else
{
toNext=0;
c.locate_delete(cp);
c.InsertNode(cp,tempc);
}
}
if(toNext==1)
{
c.GetValue(c.ListLength(),tempc);
tempc.data=1;
tempc.value++;
c.InsertEnd(tempc);
}
}
//对链表进行整理,删除小数点前首部的零,删除小数点后尾部的零
void to_correct(lk_list<node> &a)
{
node temp;
int i,p;
p=0;
for(i=a.ListLength();i>=1;i--)//找出第一位小数所在位置
{
a.GetValue(i,temp);
if(temp.value<0) {p=i; break;}
}
if(p!=a.ListLength()) //删除小数点前首部的0
{
for(i=a.ListLength();i>p;i--)
{
a.GetValue(i,temp);
if(temp.data==0) a.locate_delete(i);
else break;
}
}
if(p!=0) //删除小数点后尾部的0
{
for(i=1;i<=p;i++)
{
a.GetValue(1,temp);
if(temp.data==0) a.locate_delete(1);
else break;
}
}
}
//链表中可能有断层,将其补上
void add_zero(lk_list<node> & a)
{
int p1,p2,i,length;
node temp1,temp2,temp;
length=a.ListLength();
if(length==0) return;
a.GetValue(1,temp1);
a.GetValue(length,temp2);
p1=temp1.value;//最低位
p2=temp2.value;//最高位
if(p1>=0 && p2 >0)//最高位大于零,最低位大于等于零
{
if(p1!=0)
{
for(i=p1-1;i>=0;i--)//将p1后的零补上
{
temp.data=0;
temp.value=i;
a.InsertNode(1,temp);
}
}
for(i=p1;i<p2;i++)//将p1,p2间有可能出现的断层补上
{
a.GetValue(i+1,temp1);
a.NextElem(i+1,temp2);
if(temp2.value!=temp1.value+1)//相邻的两位权值相差应为一
{
temp.data=0;
temp.value=temp1.value+1;
a.InsertNode(i+2,temp);
}
}
}
else if(p1<0 && p2>=0)//最高位大于等于零,最低位小于零
{
for(i=1;i<=p2-p1;i++)//从p1到p2
{
a.GetValue(i,temp1);
a.NextElem(i,temp2);
if(temp2.value!=temp1.value+1)
{
temp.data=0;
temp.value=temp1.value+1;
a.InsertNode(i+1,temp);
}
}
}
else if(p1<0 && p2<0)
{
if(p2!=-1)
{
for(i=p2+1;i<=-1;i++)
{
temp.data=0;
temp.value=i;
a.InsertEnd(temp);
}
}
for(i=1;i<=p2-p1;i++)
{
a.GetValue(i,temp1);
a.NextElem(i,temp2);
if(temp2.value!=temp1.value+1)
{
temp.data=0;
temp.value=temp1.value+1;
a.InsertNode(i+1,temp);
}
}
}
else return;
}
//比较两个数的大小
int compare(lk_list<node> &a,lk_list<node> &b)
{
node tempa,tempb;
int ap,bp;
ap=a.ListLength();
bp=b.ListLength();
to_correct(a);
to_correct(b);
add_zero(a);
add_zero(b);
ap=a.ListLength();
bp=b.ListLength();
while(true)
{
a.GetValue(ap,tempa);
b.GetValue(bp,tempb);
if(tempa.value>tempb.value) return 1;
else if(tempa.value<tempb.value) return -1;
else
{
if(tempa.data>tempb.data) return 1;
else if(tempa.data<tempb.data) return -1;
else ap--;bp--;
if(ap==0 && bp>0) return -1;
else if(bp==0 && ap>0) return 1;
else if(ap==0 && bp==0) return 0;
else continue;
}
}
}
//作减法运算的程序
void subtration(lk_list<node> a,lk_list<node> b,lk_list<node> &c)
{
int ap=1,bp=1,cp=1;
bool isNeed=false;
node tempa,tempb,tempc;
while(true)
{
while(ap<=a.ListLength() && bp<=b.ListLength())
{
a.GetValue(ap,tempa);
b.GetValue(bp,tempb);
if(tempa.value<tempb.value)
{
c.InsertEnd(tempa);
ap++;
}
else if(tempa.value>tempb.value)
{
tempb.data=0-tempb.data;
c.InsertEnd(tempb);
bp++;
}
else
{
tempc.data=tempa.data-tempb.data;
tempc.value=tempa.value;
ap++; bp++;
c.InsertEnd(tempc);
}
}
while(ap==a.ListLength()+1 && bp<=b.ListLength())
{
b.GetValue(bp,tempb);
tempb.data=0-tempb.data;
c.InsertEnd(tempb);
bp++;
}
while(ap<=a.ListLength() && bp==b.ListLength()+1)
{
a.GetValue(ap,tempa);
c.InsertEnd(tempa);
ap++;
}
break;
}
for(cp=1;cp<=c.ListLength();cp++)
{
c.GetValue(cp,tempc);
if(isNeed==true) tempc.data--;
if(tempc.data<0)
{
isNeed=true;
tempc.data=10+tempc.data;
c.locate_delete(cp);
c.InsertNode(cp,tempc);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -