大数阶乘数据结构算法课程设计副本Word格式.docx
《大数阶乘数据结构算法课程设计副本Word格式.docx》由会员分享,可在线阅读,更多相关《大数阶乘数据结构算法课程设计副本Word格式.docx(31页珍藏版)》请在冰豆网上搜索。
Chain(){first=0;
~Chain();
boolIsEmpty()const{returnfirst==0;
}
intLength()const;
boolFind(intk,T&
x);
Chain<
&
Insert(intk,constT&
x);
Change(intk,Tx);
Delete(intk,T&
Search(constT&
x)const;
intOutPut();
*first;
/**************析构函数(删除链表的所有节点)********************/
Chain<
:
~Chain()
*next;
while(first)
{
next=first->
link;
deletefirst;
first=next;
}
/**********************确定链表的长度*****************************/
intChain<
Length()const
*current=first;
intlen=0;
while(current)
len++;
current=current->
returnlen;
/*********************在链表中查找第K个元素*************************/
boolChain<
Find(intk,T&
x)
intindex=0;
while(index<
k&
current)
index++;
if(current){x=current->
data;
returntrue;
returnfalse;
/*********************向链表中插入元素*************************/
x)
*p=first;
for(intindex=1;
index<
p;
index++)
p=p->
*y=newChainNode<
y->
data=x;
if(k){
y->
link=p->
p->
link=y;
else{
link=first;
first=y;
return*this;
/********************改变链表第k个元素的值***********************/
Change(intk,Tx)
*p=first;
for(intindex=0;
p&
k;
{p=p->
if(p)
/********************删除链表第k个元素***********************/
Delete(intk,T&
if(k=0)
{first=first->
else
ChainNode<
*p=first;
*q=first;
k-1&
q;
q=q->
p=q->
q-link=p->
x=P->
deletep;
return*this;
}
/************************搜索第k个元素***************************/
Search(constT&
x)const
*current=first;
intindex=1;
while(current&
current->
data!
=x)
current=current->
index++;
if(current)returnindex;
return0;
/***********************倒序输出链表************************/
OutPut()
int*arry=newint[len];
current=first;
{
arry[index]=current->
index=index-1;
cout<
<
arry[index];
for(index;
index>
=0;
index--)
cout.fill('
0'
);
cout.width(3);
cout<
endl;
intmain()
int>
A;
intn,i,j,k;
intl=0;
A.Insert(0,1);
"
pleaseenteranumber:
cin>
>
n;
for(i=1;
i<
=n;
i++)
intm=A.Length();
for(j=0;
j<
m;
j++)
{
A.Find(j,k);
k=i*k;
A.Change(j,k);
if(k>
=1000)
{
if(j<
m-1)
A.Find(j+1,l);
else
{A.Insert(j+1,0);
l=0;
}
l+=k/1000;
A.Change(j+1,l);
k=k%1000;
A.Change(j,k);
}
Length="
A.Length()<
阶乘为:
A.OutPut();
【测试数据】
(1)n=20,n!
=2432902008176640000
(2)n=30,n!
=265252859812191058636308480000000
【运行结果】
实习题目二
算术表达式求值
(1)正确解释表达式;
(2)符合四则运算规则:
先乘除、后加减
从左到右运算
先括号内,后括号外
(3)输出最后的计算结果
【实现关键】
两个栈的使用
两位数以上、负数、小数点?
【实现方式】
基本:
控制台程序
(1)使用两个工作栈:
一个栈:
存放运算符.另一个栈:
存放操作数
(2)运算符之间的优先关系
可用二维数组(算符优先顺序如下:
)
【实现代码】
#include<
classStack
public:
Stack(intMaxStackSize=10);
~Stack(){delete[]stack;
boolIsEmpty()const{returntop==-1;
boolIsFull()const{returntop==MaxTop;
TTop()const;
Stack<
Add(constT&
Delete(T&
private:
inttop;
intMaxTop;
T*stack;
template<
Stack<
Stack(intMaxStackSixe)
MaxTop=MaxStackSixe-1;
stack=newT[MaxStackSixe];
top=-1;
}
TStack<
Top()const
returnstack[top];
Add(constT&
stack[++top]=x;
x=stack[top--];
//此函数用来比较两个符号的优先级
intComp(charleft,charright)
chart[9]={'
+'
'
-'
*'
/'
%'
^'
('
)'
#'
intsmax[9][9]=/*+*/{1,1,2,2,2,2,2,1,1,
/*-*/1,1,2,2,2,2,2,1,1,
/***/1,1,1,1,1,2,2,1,1,
/*/*/1,1,1,1,1,2,2,1,1,
/*%*/1,1,1,1,1,2,2,1,1,
/*^*/1,1,1,1,1,1,2,1,1,
/*(*/2,2,2,2,2,2,2,3,0,
/*)*/1,1,1,1,1,1,0,1,1,
/*#*/2,2,2,2,2,2,2,0,3};
intj,k;
for(inti=0;
9;
if(left==t[i])
j=i;
if(right==t[i])
k=i;
switch(smax[j][k])
case1:
return1;
case2:
return-1;
case3:
return0;
default:
ERROR!
!
return2;
doubleCal(charb,charop,chara)
{
doubled=(int)b-48;
doublee=(int)a-48;
switch(op)
case'
returnd+e;
//计算+
returnd-e;
//计算-
returnd*e;
//计算*
//计算/
if(e==0)
cout<
被除数为0,有错!
!
returnfalse;
elsereturnd/e;
return0;
charx;
Stack<
char>
op;
k;
op.Add('
请输入中缀表达式并以#结尾"
chars[20];
charexpr[20];
cin.getline(s,20);
后缀表达式为:
for(intj=0;
strlen(s);
if(s[j]>
='
s[j]<
9'
s[j];
k.Add(s[j]);
if(s[j]!
if(Comp(op.Top(),s[j])==-1)
op.Add(s[j]);
elseif(Comp(op.Top(),s[j])==1)
cout<
op.Top();
k.Add(op.Top());
op.Delete(x);
if(s[j]=='
)
while(op.Top()!
if(op.Top()=='
op.Delete(x);
inti=0;
chara;
charb;
doublen;
charm;
while(!
k.IsEmpty())
k.Delete(expr[i]);
i++;
for(i=i-1;
i>
i--)
if(expr[i]>
expr[i]<
k.Add(expr[i]);
k.Delete(a);
k.Delete(b);
n=Cal(b,expr[i],a);
m=n+'
k.Add(m);
表达式的值为:
(double)k.Top()-48<
-
实习题目三
二叉树基本算法的实现
【功能要求】
实现Create方法,要求键盘输入二叉树结点序列,创建一棵二叉树(提示:
前序递归)
实现SwapTree方法,以根结点为参数,交换每个结点的左子树和右子树(提示:
增加InorderTree方法,采用非递归方法实现二叉树的中序遍历
你可以选择:
对BinaryTree模板进行功能扩充;
自己定义并实现二叉树类
要求键盘输入二叉树结点序列
结点序列可以是前序,也可以是层次
空结点以#表示
【代码实现】
classBinaryTreeNode;
classBinaryTree
BinaryTree()
root=0;
BinaryTree(constBinaryTree<
&
Tree)
copy(Tree.root,root);
~BinaryTree(){};
boolIsEmpty()const
return((root)?
false:
true);
voidCreat();
boolRoot(T&
voidMakeTree(constT&
element,BinaryTree<
left,BinaryTree<
right);
voidBreakTree(T&
voidPreOrder(void(*Visit)(BinaryTreeNode<
*u))
PreOrder(Visit,root);
voidInOrder(void(*Visit)(BinaryTreeNode<
InOrder(Visit,root);
voidPostOrder(void(*Visit)(BinaryTreeNode<
PostOrder(Visit,root);
voidLevelOrder(void(*Visit)(BinaryTreeNode<
*u));
voidPreOutput()
PreOrder(Output,root);
voidInOutput()
InOrder(Output,root);
voidPostput()
PostOrder(Output,root);
voidLevelOutPut()
LevelOrder(Output);
voidDelete()
PostOrder(Free,root);
intHeight()const
returnHeight(root);
intSize()const
returnSize(root);
BinaryTreeNode<
*iCreat();
boolequal(BinaryTree<
Tree)
returncompare(root,Tree.root);
voidswap()
swap(root);
intleave()
returnleave(root);
intnoleave()
returnnoleave(root);
*root;
voidPreOrder(void(*Visit)(BinaryTreeNode<
*u),BinaryTreeNode<
*t);
voidInOrder(void(*Visit)(BinaryTreeNode<
voidPostOrder(void(*Visit)(BinaryTreeNode<
staticvoidOutput(BinaryTreeNode<
*t)
t->
data<
"
staticvoidFree(BinaryTreeNode<
*t)
deletet;
intHeight(BinaryTreeNode<
*t)const;
intSize(BinaryTreeNode<
boolcompare(BinaryTreeNode<
*t1,BinaryTreeNode<
*t2);
voidcopy(constBinaryTreeNode<
*&
t2);
voidswap(BinaryTr