??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include<iostream>
#include<stdlib.h>//产生随机数组?br />#include<time.h> //同上
#include<list>
{
public:
int m_beginVex;
int m_endVex;
int m_weight;
MyArc(int beginVex,int endVex,int weight);
MyArc(){}
bool operator < (const MyArc& arc)
{
return m_weight<arc.m_weight;
}
bool operator == (const MyArc& arc)
{
return m_weight==arc.m_weight;
}
bool operator > (const MyArc& arc)
{
return m_weight>arc.m_weight;
}
} ;
{}
//2) 表示囄L矩阵cGraph:
class Graph
{
public:
int m_vexnum;
int m_arcnum;
int *m_pmatrix;
public:
~Graph();
Graph(int vexnum);
Graph(int vexnum,int *pmatrix);
void insert(MyArc arc);//按权值大排序插?br /> bool bound(int x); //判断点x是否已与其它点q?br />};
//构造函?br />Graph::Graph(int vexnum)
{
m_pmatrix=new int[vexnum*vexnum];
m_vexnum=vexnum;m_arcnum=0;
for(int i=0;i<vexnum*vexnum;++i)
m_pmatrix[i]=0;
{
m_vexnum=vexnum;
// m_arcnum=arcnum;
m_pmatrix=new int[m_vexnum*m_vexnum];
for(int i=0;i<m_vexnum*m_vexnum;++i)
m_pmatrix[i]=pmatrix[i];
}
{
for(int i=0;i<m_vexnum;++i) if(m_pmatrix[x+i*m_vexnum]!=0) return true;
return false;
}
void Graph::insert(MyArc arc)
{
m_pmatrix[arc.m_beginVex*m_vexnum+arc.m_endVex]=arc.m_weight;
m_pmatrix[arc.m_endVex*m_vexnum+arc.m_beginVex]=arc.m_weight;
++m_arcnum;
}
//析构
Graph::~Graph()
{
delete[] m_pmatrix;
}
//3) 按权存储边的有序队列cMyQueues:
class MyQueues
{
public:
list<MyArc> m_list;
MyQueues(){}
void insert(const MyArc& arc);//Ҏ权值插入队列中合适位|?
void InsertGraph(const Graph &graph);//图的连通分量插入队?br /> MyArc pop();
};
//边出?br />MyArc MyQueues::pop()
{
MyArc arc=m_list.front();
m_list.pop_front();
return arc;
}
//Ҏ权值插入队列中合适位|?
void MyQueues::insert(const MyArc& arc)
{
list<MyArc>::iterator pos;
pos=m_list.begin();
while(pos!=m_list.end())
{
if(*pos>arc) break;
else ++pos;
}
m_list.insert(pos,arc);
}
//图的连通分量插入队?br />void MyQueues::InsertGraph(const Graph &graph)
{
for(int i=0;i<graph.m_vexnum;++i)
{
for(int j=i+1;j<graph.m_vexnum;++j)
if(graph.m_pmatrix[i*graph.m_vexnum+j]) insert(MyArc(i,j,graph.m_pmatrix[i*graph.m_vexnum+j]));
}
}
{
list<int> my_list;
my_list.push_back(arc.m_beginVex);
int *ps=new int[graph.m_vexnum];
for(int i=0;i<graph.m_vexnum;++i)
ps[i]=0;
while(!my_list.empty())
{
int x=my_list.front();
ps[x]=1;
my_list.pop_front();
for(int i=0;i<graph.m_vexnum;++i)
{
if(graph.m_pmatrix[i+x*graph.m_vexnum]!=0)
{
if(i==arc.m_endVex) return true;
if(ps[i]!=1) my_list.push_back(i);
}
}
}
delete[] ps;
return false;
}
//4) kruskal法:
void kruskal(const Graph& graph,Graph& smtree)
{
MyQueues arcqueues;//保存从小到大排列的边
arcqueues.InsertGraph(graph);
MyArc myarc;//Arc表示边的cd
int arcnum=0; //边的个数
while(arcnum<graph.m_vexnum-1)
{
myarc=arcqueues.pop();
if(!IsCycle(smtree,myarc))
{
smtree.insert(myarc);
++arcnum;
}
}
}
void SetMatrix(int vexnum,int *pmatrix)
{
srand((unsigned)time(NULL));
for(int i=0;i<vexnum;++i)//产生随机权值矩?br /> {
for(int j=i;j<vexnum;++j)
{
if(j==i)
{
pmatrix[i*vexnum+j]=0;
continue;
}
int rnum=rand();rnum%=99;rnum++;//产生1~99的随机整C的权?br /> pmatrix[i*vexnum+j]=rnum;
pmatrix[j*vexnum+i]=rnum;
}
}
cout<<"***随机产生的各Ҏ值矩?[点Cؓ "<<vexnum<<"] ****\n";
for(int i=0;i<vexnum;++i)//输出随机权值矩?br /> {
for(int j=0;j<vexnum;++j)
{
cout<<pmatrix[i*vexnum+j]<<"\t";
}
cout<<endl;
}
}
void SmallestTreeOutput(const Graph& smtree)
{
cout<<"最生成树:"<<endl;
for(int i=0;i<smtree.m_vexnum;++i)//输出最树
for(int j=i+1;j<smtree.m_vexnum;++j)
if(smtree.m_pmatrix[i*smtree.m_vexnum+j])
cout<<'('<<i<<','<<j<<','<<smtree.m_pmatrix[i*smtree.m_vexnum+j]<<')'<<endl;
}
void main()
{
char i;
cout<<"误入顶Ҏ?";
cin>>i;
int vex=i-'0';
int *matrix=new int[vex*vex];
cout<<endl;
SetMatrix(vex,matrix);
Graph graph(vex,matrix),smtree(vex);
kruskal(graph,smtree);
SmallestTreeOutput(smtree);
delete []matrix;
}
l果输出Q?br />误入顶Ҏ?6
***随机产生的各Ҏ值矩?[点Cؓ 6] ****
0 64 7 15 22 43
64 0 72 86 53 40
7 72 0 53 37 22
15 86 53 0 87 82
22 53 37 87 0 9
43 40 22 82 9 0
最生成树:
(0,2,7)
(0,3,15)
(0,4,22)
(1,5,40)
(4,5,9)
#include<stdio.h>
#define MAX 50
int cycle(char *s)
{
char *h,*t;
for(h=s,t=s+strlen(s)-1;t>h;h++,t--)
{
if(*h!=*t)
{
printf("%c",h);
break;
}
}
return t<=h;
}
main()
{
char s[MAX];
while(1)
{
puts("Please input the string you want to judge(input ^ to quit):");
scanf("%s",s);
if(s[0]=='^')
break;
if(cycle(s))
printf("%s is a cycle string.\n",s);
else
printf("%s is not a cycle string.\n",s);
}
puts("\nThank you for you using ,bye bye!\n");
}
输出l果Q?br />
Please input the string you want to judge(input ^ to quit):
abcabc
abcabc is not a cycle string.
Please input the string you want to judge(input ^ to quit):
abccba
abccba is a cycle string.
Please input the string you want to judge(input ^ to quit):
printf("Enter another character and tow integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr,int lines,int width)
{
int row ,col;
for(row =1;row<=lines;row++)
{
for(col=1;col<=width;col++)
{
putchar(cr);
}
putchar('\n');
}
}
l果输出Q?br />
Enter a character and tow integers:
d 3 2
dd
dd
dd
Enter another character and tow integers;
Enter a newline to quit.
Bye.
Press any key to continue...
可以看见紧跟?后面的那个换行符Qscanf()函数该换行W留在了输入队列中.而getchar()q不跌换行W,所以在循环?br />下一周期Q在你有Z输入其他内容之前Q这个换行符由getchar()dQ然后将其赋值给chQ而ch为换行符正是l止循环的条Ӟ
要解册个问题,必须跌一个输入周期中键入的最后一个数字与下一行开始处键入的字W之间的所有换行符或空|
改进如下Q?strong>char2.cpp
/*C语言一个I/O问题E序修改版本*/
#include<stdio.h>
void display(char cr,int lines,int width);
int main(void)
{
int ch;
int rows,cols;
printf("Enter a character and tow integers:\n");
while((ch=getchar())!='\n')
{
if( scanf("%d %d",&rows,&cols)!=2)
break;
display(ch,rows,cols);
while(getchar()!='\n') /*剔除掉scanf()输入后的所有字W.*/
{
printf("1"); /*后面有多字W就输出多少?*/
continue;
}
printf("Enter another character and tow integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr,int lines,int width)
{
int row ,col;
for(row =1;row<=lines;row++)
{
for(col=1;col<=width;col++)
{
putchar(cr);
}
putchar('\n');
}
}
输出l果Q?br />Enter a character and tow integers:
d 3 4
dddd
dddd
dddd
Enter another character and tow integers;
Enter a newline to quit.
d 3 4
dddd
dddd
dddd
11Enter another character and tow integers;
Enter a newline to quit.
首先Q?/span>
main()
使用参数
1
调用了函?/span>
up_and_down()
Q于?/span>
up_and_down()
中Ş式参?/span>
n
的值是
1,
故打印语?/span>
#1
输出?/span>
Level1
?br />然后Q由?/span>
n
的数值小?/span>
4
Q所?/span>
up_and_down()
Q第
1
U)使用参数
n+1
x?/span>
2
调用?/span>
up_and_down()(
W?/span>
2
U?/span>
).
使得
n
在第
2
U调用中被赋?/span>
2,
打印语句
#1
输出的是
Level2
。与之类|下面的两ơ调用分别打印出
Level3
?/span>
Level4
?br />
当开始执行第
4
U调用时Q?/span>
n
的值是
4
Q因?/span>
if
语句的条件不满。这时候不再l调?/span>
up_and_down()
函数。第
4
U调用接
着执行打印语句
#2
Q即输出
Level4
Q因?/span>
n
的值是
4
。现在函数需要执?/span>
return
语句Q此时第
4
U调用结束,把控制权q回l该
函数的调用函敎ͼ也就是第
3
U调用函数。第
3
U调用函C前一个执行过的语句是?/span>
if
语句中进行第
4
U调用。因此,它
l执行其后代码Q即执行打印语句
#2
Q这会输出
Level3
Q当W?/span>
3
U调用结束后Q第
2
U调用函数开始l执行,卌?br />
Level2
Q依ơ类推.
注意Q每一U的递归都用它自己的私有的变量
n
Q可以查看地址的值来证明Q?br />
递归的基本原理:
Q 每一ơ函数调用都会有一ơ返回.当程序流执行到某一U递归的结֤Ӟ它会转移到前一U递归l箋执行Q?/span>
Q 递归函数中,位于递归调用前的语句和各U被调函数具有相同的序Q如打印语句 #1 位于递归调用语句前,它按照?br /> 归调用的序被执行了 4 ơ.
Q 每一U的函数调用都有自己的私有变量.
Q 递归函数中,位于递归调用语句后的语句的执行顺序和各个被调用函数的序相反Q?/span>
Q 虽然每一U递归有自q变量Q但是函C码ƈ不会得到复制Q?/span>
Q 递归函数中必d含可以终止递归调用的语句.
再看一个具体的递归函数调用的例子:以二q制形式输出整数
/*输入一个整敎ͼ输出二进制Ş?/
#include<stdio.h>
void to_binary(unsigned long n);
int main(void)
{
unsigned long number;
printf("Enter an integer(q to quit):\n");
while(scanf("%ul",&number)==1)
{
printf("Binary equivalent :");
to_binary(number);
putchar('\n');
printf("Enter an integer(q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_binary(unsigned long n) /*递归函数*/
{
int r;
r=n%2; /*在递归调用之前计算n%2的数|然后在递归调用语句之后q行输出Q这?br /> 计算出的W一个数值反而是在最后一个输?/
if(n>=2)
to_binary(n/2);
putchar('0'+r);/*如果r?Q表辑ּ'0'+r是字符'0'Q如果r?Q则表达式的gؓ
'1'Q注意前提是字符'1'的数值编码比字符'0'的数值编码大1Q?br /> ASCII和EBCDICq两U编码都满q个条gQ?/
return;
}
typedef struct
{
char ch[MaxSize];
int len;
}SqString;
int Index(SqString s,SqString t)/* 单匹配方?/
{
int i=0,j=0,k;
while(i<s.len&&j<t.len)
{
if(s.ch[i]==t.ch[j])/*l箋匚w下一个字W?/
{
i++;
j++;
}
else/*子串、主串的指针回溯重新开始下一ơ匹?/
{
i=i-j+1;
j=0;
}
}
if(j>=t.len)/*匚w成功Q返回匹配的W一个字W下?/
k=i-t.len;
else/*匚w不成?/
k=-1;
return k;
}
void GetNext(SqString t,int next[])/*由模式串t求出next?/
{
int j,k;
j=0;k=-1;next[0]=-1;
while(j<t.len-1)
{
if(k==-1||t.ch[j]==t.ch[k])
{
j++;k++;
next[j]=k;
}
else k=next[k];
}
}
void GetNextval(SqString t,int nextval[])/*由模式串t求出nextval?/
{
int j=0,k=-1;
nextval[0]=-1;
while(j<t.len)
{
if(k==-1||t.ch[j]==t.ch[k])
{
j++;k++;
if(t.ch[j]!=t.ch[k])
nextval[j]=k;
else
nextval[j]=nextval[k];
}
else k=nextval[k];
}
}
int KMPIndex(SqString s,SqString t)/*KMP法*/
{
int next[MaxSize];
int i=0,j=0;
int v;
GetNext(t,next);
while(i<s.len && j<t.len)
{
if(j==-1||s.ch[i]==t.ch[j])
{
i++;
j++;
}
else j=next[j];
}
if(j>=t.len)
v=i-t.len;
else
v=-1;
return v;
}
int KMPIndex1(SqString s,SqString t)/*改进的KMP法*/
{
int nextval[MaxSize],next[MaxSize],i=0,j=0,v;
GetNextval(t,next);
GetNextval(t,nextval);
while(i<s.len&&j<t.len)
{
if(i==-1||s.ch[i]==t.ch[j])
{
i++;
j++;
}
else j=nextval[j];
}
if(j>=t.len)
v=i-t.len;/*q回匚w模式串的首字W下?/
else
v=-1;
return v;
}
void StrAssign(SqString &str,char cstr[])/*׃帔Rcstr创徏串str */
{
int i;
for(i=0;cstr[i]!='\0';i++)
str.ch[i]=cstr[i];
str.len=i;
}
void DispStr(SqString s)/*输出串s的所有元?/
{
int i;
if(s.len>0)
{
for(i=0;i<s.len;i++)
printf("%c",s.ch[i]);
printf("\n");
}
}
void main()
{
int j;
int next[MaxSize],nextval[MaxSize];
SqString s,t;
StrAssign(s,"abcabcdabcdeabcdefabcdefg");
StrAssign(t,"abcdeabcdefab");
printf("串s:");DispStr(s);
printf("串t:");DispStr(t);
printf("单匹配P法:\n");
printf("t在s中的位置:%d\n",Index(s,t));
GetNext(t,next);
GetNextval(t,nextval);
printf(" j ");
for(j=0;j<t.len;j++)
{
printf("%4d",j);
}
printf("\n");
printf("t[j] ");
for(j=0;j<t.len;j++)
printf("%4c",t.ch[j]);
printf("\n");
printf("next ");
for(j=0;j<t.len;j++)
printf("%4d",next[j]);
printf("\n");
printf("nextval");
for(j=0;j<t.len;j++)
printf("%4d",nextval[j]);
printf("\n");
printf("KMP法:\n");
printf("t在s中的位置:%d\n",KMPIndex(s,t));
printf("改进的KMP法:\n");
printf("t在s中的位置:%d\n",KMPIndex1(s,t));
}
l果如下Q?/strong>
串s:abcabcdabcdeabcdefabcdefg
串t:abcdeabcdefab
单匹配P法:
t在s中的位置:7
j 0 1 2 3 4 5 6 7 8 9 10 11 12
t[j] a b c d e a b c d e f a b
next -1 0 0 0 0 0 1 2 3 4 5 0 1
nextval -1 0 0 0 0 -1 0 0 0 0 5 -1 0
KMP法:
t在s中的位置:7
改进的KMP法:
t在s中的位置:7
大学里实行学分制。每门课都有一定的?/span>
?/span>
。每个学生均需要修满规定数量的评才能毕业。其中有些课E可以直接修读,有些评需要一定的基础知识Q必d选了其他一些课E的基础上才能修诅R例如,《数据结构》必d选修了《高U语aE序设计》之后才能选修。我们称《高U语aE序设计》是《数据结构》的“先修课”。在我们的大学里Q假?font color="#000000" size="2">每门评直接先修?span style="COLOR: red">臛_只有一门,两门译֏能存在相同的先修?/font>。例如:
译֏ |
先修译֏ |
学分 |
1 |
0 |
1 |
2 |
1 |
1 |
3 |
2 |
3 |
4 |
0 |
3 |
5 |
2 |
4 |
上例中,1?span lang="EN-US">2的先修课Q即如果要选修2Q则1必定被选?span lang="EN-US">
学生不可能学完大学里开讄所有课E,因此每个学生必须在入学时选定自己要学的课E。每个学生可选课E的L目是l定的。现在请你们组~写一个“学生选课pȝ”,Ll定一U课E体p(总课E数Q课E之间的修读先后制约关系Q学生毕业要求修的课E数目)Q该pȝ能帮助学生找?span style="COLOR: red">一U?/font>选课ҎQ得他能得到的学分最?/font>Qƈ且必L?span style="COLOR: red">先修评优先的原?br />
具体实现如下Q?/span>
1. ?b style="mso-bidi-font-weight: normal">评体系存放E体pL?/font> CourseHierarchy.txt Q?/span>
2. ?b style="mso-bidi-font-weight: normal">评体系文g CourseHierarchy.txt 转换左孩子右兄弟二叉树表C?/font>Q?/span>
3. 在此基础上对二叉树进行先序、中序、后序遍历?/font>
4. l出最多学分选课Ҏ?/font>
课E体pL件{换ؓ二叉?/span>
思想Q从
CourseHierarchy.txt
头部开始扫描,W一个先修课?/span>
0
Q即没有先修课)的课Eؓ整棵二叉树的根节?/span>
i
。从
CourseHierarchy.txt
头部开始扫描,所?/span>
i
的后l课E作?/span>
i
的左子树Q课E体pMQ其他先修课?/span>
0
的节点,及其后箋评构成
i
的右子树。第一个先修课?/span>
i
的节点作?/span>
i
的左子树的根节点
步骤
1
Q从文g
CourseHierarchy.txt
生成评链表
Course_Slist
Q参数表Q?/span>
步骤
2
Q将指针
CousreSystem_root
指引?/span>
评链表递归转换E体pM叉树
实现代码
tree_binary.cpp
// tree_binary.cpp : 定义控制台应用程序的入口炏V?br />//
#pragma once
#include <iostream>
#include <tchar.h>
//#include<fstream>
//#include<iostream>
//#include<cstdlib>
#include"tree_binaryHF.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char file_name[20]="CourseHierarchy.txt";
LinkList L;//链表的头指针
LinkList T;//树的?br /> cout<<"\t本程序的作用是从文g(文g内容可按需求改?"<<endl
<<" 中读取信息,攑ֈ链表中,最后生成二叉树Q?<<endl
<<" 然后先序中序Q后l遍历二叉树."<<endl;
InitList(L);
cout<<"d的文件内?"<<endl;
OpenFile(L,file_name);
cout<<"生成链表后的l果:"<<endl;
PrintLinkLIst(L);
CreateBiTree(L,T,0);//生成二叉?br /> cout<<"先序遍历二叉树的l果:";
PreOrder(T);
cout<<endl<<"中序遍历二叉树的l果:";
InOrder(T);
cout<<endl<<"后序遍历二叉树的l果:";
BackOrder(T);
cout<<endl;
return 0;
}
tree_binaryHF.h
#include"cao.hpp"
//*************************************//
//打开文gQƈd里面的所有内?br />Status OpenFile(LinkList&L,char file_name[])//׃q里需要把数据插入链表?br />{
int CNum,CScore,PreScore;
ifstream in_stream;
in_stream.open(file_name);
if(in_stream.fail())//if the file is not opened successfully
{
cout<<"can't open the file of "<<file_name<<endl;
exit(0);
}
//i
cout<<"CNum"<<"\t"<<"PreScore"<<"\t"<<"CScore"<<endl;
while(in_stream>>CNum>>PreScore>>CScore)//d三个数据(评~码,学分,先修?
{
cout<<CNum<<"\t"<<PreScore<<"\t"<<"\t"<<CScore<<endl; //攑ֈ节点?br /> LinkInsert(L,CNum,PreScore,CScore);//insert the node to the linklist L
}
in_stream.close();
return OK;
}
Status InitList(LinkList &L)
{
L= (LinkList)malloc(sizeof(LNode));
if(!L)
return ERROR;
L->lchild=NULL;
L->rchild=NULL;
L->CNumber=0;
L->CScore=0;
L->PreCourse=0;
return OK;
}
Status LinkInsert(LinkList &L,Status CNum,Status PreCourse,Status CScore)//把新得到的结Ҏ入链表,从头l点处插?br />{
LinkList p;
p=(LinkList)malloc(sizeof(LNode));
if(!p)
return ERROR;
p->CNumber=CNum;
p->CScore=CScore;
p->PreCourse=PreCourse;
p->lchild=NULL;
p->rchild=L->rchild;
L->rchild=p;
return OK;
}
Status PrintLinkLIst(LinkList &L)//打印整个链表Q右指针指向后l点Q左指针I?br />{
LinkList p;
p=L->rchild;
while(p)
{
cout<<p->CNumber<<"\t"<<p->PreCourse<<"\t"<<"\t"<<p->CScore<<endl;
p=p->rchild;
}
return OK;
}
Status CreateBiTree(LinkList &L,LinkList&T,Status c)
{
LinkList p;
//while(!EmptyLinkList(L))
//{
if(!(p=Serach(L,c)))
T=NULL;
else
{
T=p;//生成根节?br /> delet(L,c);//删除pl点
CreateBiTree(L,T->lchild,T->CNumber);//构造左子树
CreateBiTree(L,T->rchild,T->PreCourse);//构造右子树
}
//}
return OK;
}
LinkList Serach(LinkList &L,Status c)
{
LinkList head,next;
head=L;
next=head->rchild;
while(next&&(next->PreCourse!=c))
{
head=next;
next=next->rchild;
}
if(next==NULL)
return NULL;//没有扑ֈ
else//扑ֈ?br /> return next;
}
void delet(LinkList &L,Status c)
{
LinkList head,next;
head=L;
next=head->rchild;
while(next&&(next->PreCourse!=c))
{
head=next;
next=next->rchild;
}
head->rchild=next->rchild;
//free(next);
}
Status EmptyLinkList(LinkList L)
{
if(L->rchild=NULL)
return OK;
else
return ERROR;
}
//先序遍历的递归
void PreOrder(LinkList T)
{
if(T)
{
//讉Kl点
cout<<T->CNumber<<" ";
PreOrder(T->lchild); //遍历左子?br /> PreOrder(T->rchild); //遍历叛_?br /> //cout<<endl;
}
}
//中序遍历的递归
void InOrder(LinkList T)
{
if(T)
{
InOrder(T->lchild); //遍历左子?br /> //讉Kl点
cout<<T->CNumber<<" ";
InOrder(T->rchild); //遍历叛_?br /> }
}
//后序遍历的递归
void BackOrder(LinkList T)
{
if(T)
{
BackOrder(T->lchild); //遍历左子?br /> BackOrder(T->rchild); //遍历叛_?br /> cout<<T->CNumber<<" ";
}
}
cao.hpp
#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;
typedef int Status;
#define OK 1
#define ERROR 0
typedef struct LNode
{
struct LNode *lchild;
struct LNode *rchild;
Status CNumber;
Status CScore;
Status PreCourse;
}LNode,*LinkList;
Status OpenFile(LinkList&L,char file_name[]);
//打开文gQƈ且插入节?br />//*************************//
LinkInsert(LinkList &L,Status CNum,Status CScore,Status PreScore);
//insert the node to the linklist L
//*************************//
Status InitList(LinkList &L);//初始化链?br />//****************************************//
Status PrintLinkList(LinkList &L);//输出链表
//********************************//
LinkList Serach(LinkList &L,Status c);//查找节点
//*********************************************//
void delet(LinkList&L,Status c);//删除节点
//*************************************//
Status EmptyLinkList(LinkList L);//查链表是否ؓI(没用刎ͼ
//***********************************//
//先序遍历二叉?br />void PreOrder(LinkList T);
//***********************************//
void InOrder(LinkList T);//中序遍历二叉?br />//*****************************//
void BackOrder(LinkList T);//后箋遍历二叉?br />
CourceSource.txt
1 2 2
2 0 1
3 0 4
4 2 1
5 7 1
6 7 6
7 2 2
设计q实现魔王语a的解释器Q具体要求如下:大写字母表示王语言的词汇;写字母表示人的词汇语言Q魔王语a中可包含括号?/font>
如:我们有魔王语a的解释规则:BQ?gt;tAdAQA->saeQ则王语言 B(ehnxgz)B解释成tsaedsaeezegexenehetsaedsae?br />
实现代码如下Q?br />
#include<stdlib.h>
#include<stdio.h>
#define STACK_INIT_SIZE 100 //存储I间初始分配?br />#define STACK_INCREMENT 10 //存储I间分配增量
#define OVERFLOW 1
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef char SElemType;
typedef char QElemType;
typedef int Status;
typedef struct{
SElemType *base; //栈基址
SElemType *top; //栈顶地址
int stacksize;
}SqStack;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front; //队头指针
QueuePtr rear; //队尾指针
}LinkQueue;
Status InitStack(SqStack &S)
//构造一个空?br />{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base)
exit (OVERFLOW);//存储单元分配p|
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
//插入元素e栈顶单元
{
if(S.top-S.base>=S.stacksize)
{//栈满Q追加存储空?br /> S.base=(SElemType *)realloc(S.base,(S.stacksize+STACK_INCREMENT)*sizeof(char));
if(!S.base)
exit (OVERFLOW);//存储单元分配p|
S.top=S.base+S.stacksize;
S.stacksize+=STACK_INCREMENT;
}
*(S.top)=e;
S.top++;
return OK;
}
Status Pop(SqStack &S,SElemType& e)
//若栈不ؓI,则删除S的栈单元,用eq回其?br />{
if(S.base==S.top)
return ERROR;
S.top--;
e=*(S.top);
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.base==S.top)
return 0;
else
return 1;
}
Status InitQueue(LinkQueue &Q)
//构造一个空队列Q
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof (QNode));
if(!Q.front)
exit (OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status EnQueue (LinkQueue&Q,QElemType e)
//插入元素e为Q的新的队օ?br />{
QueuePtr p;
p=(QueuePtr)malloc(sizeof (QNode));
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue (LinkQueue &Q,QElemType &e)
//若队列不I,则删除Q的队头元素,用eq回其|q返回OK;
//否则q回ERROR
{
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(p==Q.rear)
Q.rear=Q.front;
free(p);
return OK;
}
Status QueueEmpty(LinkQueue Q)
//若队列Q为空队列,则返回TRUEQ否则返回FALSE
{
if(Q.rear==Q.front)
return FALSE;
else
return TRUE;
}
void InStack(char fiend[],SqStack &S)
{
int m,i=0;
for(;fiend[i]!='\0';i++);//计算fiend中有多少
for(m=i-1;m>=0;m--)
Push(S,fiend[m]);
}
void main()
{
char e,c,d;
SqStack S,zhan;
LinkQueue Q;
InitQueue(Q);
char mowang[]="B(ehnxgz)B";
printf("你想要解释的王语言为:%s\n",mowang);
char B[]="tAdA";
InitStack(S);
InitStack(zhan);
InStack(mowang,S);//全部压进栈中
while(StackEmpty(S))//在栈不ؓI的情况?br /> {
Pop(S,e);
if(e=='B')
InStack(B,zhan);
else
if(e=='(')//如果为右括号Q则输出括号中所有内?br /> {
while(Pop(S,e)&&e!=')')//当ؓ左括h停止
{
if(e!=')')
EnQueue (Q,e);
}
DeQueue (Q,c);//d队列中第一个元?br /> while(QueueEmpty(Q))
{
DeQueue (Q,d);//取出元素
Push(zhan,c);
Push(zhan,d);
}
Push(zhan,c);//再次压入W一个元?br /> // Pop(S,e);//L左括?br /> }
}
printf("\n解释的结果ؓ: ");
while(StackEmpty(zhan))//在栈不ؓI的情况?br /> {
Pop(zhan,c);
if(c=='A')
printf("sae");
else
printf("%c",c);
}
printf("\n");
}
void DispPoly(PolyNode *L) //输出多项?br />{
PolyNode *p=L->next;
while (p!=NULL)
{
printf("%g,%d;",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
void CreateListR(PolyNode * &L,PolyArray a,int n) //插入法
{
PolyNode *s,*r;int i;
L=(PolyNode *)malloc(sizeof(PolyNode)); //创徏头结?br /> L->next=NULL;
// L->exp=n;
// printf("%d\n",L->exp);
r=L; //r始终指向l端l点Q开始时指向头结?br /> for(i=0;i<n;i++)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //创徏新结?br /> s->coef=a[i].coef;
s->exp=a[i].exp;
r->next=s; //?s插入*r之后
r=s;
}
r->next=NULL; //终端结点next域置为NULL
}
void Sort(PolyNode * &head) //按exp域的值递减排序
{
PolyNode *p=head->next,*q,*r;
if(p!=NULL) //当原单链表不为空?br /> {
r=p->next; //r保存*pl点后l点的指?br /> p->next=NULL; //构造只含一个数据结点的有序?br /> p=r;
while(p!=NULL)
{
r=p->next; //r保存*pl点后l点的指?br /> q=head;
while(q->next!=NULL && q->next->exp>p->exp)
q=q->next; //在有序表中找插入*p的前q?q
p->next=q->next; //?p插入?q之后
q->next=p;
p=r;
}
}
}
void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc)//求两个有序表的ƈ
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode)); //创徏头结?br /> tc=hc;
while(pa!=NULL && pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pb->exp;s->coef=pb->coef;
tc->next=s;tc=s;
pb=pb->next;
}
else //pa->exp=pb->exp?br /> {
c=pa->coef+pb->coef;
if(c!=0) //pL之和不ؓ0时创建新l点
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=c;
tc->next=s;tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL) //复制余下l点
pa=pb;
while(pa!=NULL)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void Subs(PolyNode *ha,PolyNode *hb,PolyNode *&hc) //求两个有序表的差
{
PolyNode *pa=ha->next,*pb=hb->next,*s,*tc;
float c;
hc=(PolyNode *)malloc(sizeof(PolyNode)); //创徏头结?br /> tc=hc;
while(pa!=NULL && pb!=NULL)
{
if(pa->exp>pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
else if(pa->exp<pb->exp)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pb->exp;s->coef=-pb->coef; //如果前个多项式中的指数小于后个多式指数加上负号
tc->next=s;tc=s;
pb=pb->next;
}
else //pa->exp=pb->exp?br /> {
c=pa->coef-pb->coef;
if(c!=0) //pL之差不ؓ0时创建新l点
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=c;
tc->next=s;tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL) //复制余下l点
pa=pb;
while(pa!=NULL)
{
s=(PolyNode *)malloc(sizeof(PolyNode)); //复制l点
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void main()
{
PolyNode *ha,*hb,*hc;
//PolyArray a={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}};
//PolyArray b={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}};
int m=4,n=5;
PolyArray a,b;
for(int i=0;i<m;i++)
{
printf("误入A多项式中W?d的pL和指?,i);
scanf("%f%d",&a[i].coef,&a[i].exp);
}
for(int j=0;j<n;j++)
{
printf("误入B多项式中W?d的pL和指?,j);
scanf("%f%d",&b[j].coef,&b[j].exp);
}
CreateListR(ha,a,4);
CreateListR(hb,b,5);
printf("原来A:\n");DispPoly(ha);
printf("原来B:\n");DispPoly(hb);
Sort(ha);
Sort(hb);
printf("排序后A:\n");DispPoly(ha);
printf("排序后B:\n");DispPoly(hb);
Add(ha,hb,hc);
printf("相加?\n");DispPoly(hc);
Subs(ha,hb,hc);
printf("相减?\n");DispPoly(hc);
}
void CreatComplex(Complex& c,float a,float b);
void AddComplex(Complex& sum,Complex c1,Complex c2 );
void Subtract_C(Complex& Sub,Complex c1,Complex c2 );
void Multiple_C(Complex& product,Complex c1,Complex c2 );
void Print_C(Complex c);
void CreatComplex(Complex& c,float a,float b)
{
c.real = a;
c.image = b;
}
void AddComplex(Complex& sum,Complex c1,Complex c2)
{
sum.real = c1.real + c2.real ;
sum.image = c1.image + c2.image ;
}
void Subtract_C(Complex& Sub,Complex c1,Complex c2 )
{
Sub.real = c1.real -c2.real ;
Sub.image = c1.image - c2.image ;
}
void Multiple_C(Complex& product,Complex c1,Complex c2 )
{
product.real = c1.real * c2.real - c1.image * c2.image ;
product.image = c1.real * c2.image + c1.image * c2.real ;
}
void Print_C(Complex c)
{
if (c.image == 0.0)
printf("%5.2f\n",c.real );
else
printf("%5.2f+%5.2fi\n",c.real ,c.image );
if(c.real==0.0)
printf("%5.2fi\n",c.image);
}
complex.cpp代码Q?br />#include "complex.h"
#include <stdio.h>
void main()
{
float a,b,c,d;
Complex c1,c2,sum,Sub,Prod;
scanf("%f%f%f%f",&a,&b,&c,&d);
CreatComplex(c1,a,b);
Print_C(c1);
CreatComplex(c2,c,d);
Print_C(c2);
AddComplex(sum,c1,c2);
Print_C(sum);
Subtract_C(Sub,c1,c2);
Print_C(Sub);
Multiple_C(Prod,c1,c2);
Print_C(Prod);
}