r=S->next;
r!
=NULL
否
是
r->element==mn[i]
否
是
printf("%s",r->chinese);break;
r=r->next;
i++
结束
运行结果:
六、调试情况,设计技巧及体会
1.在本次课程设计中,虽然可以按照要求完成,但在某些设计上较为繁琐,可以加以修改,让其更加精简完善。
2.此次课程设计,不仅是对所学数据结构知识的加以巩固,更是对自己动手能力的锻炼,将理论与实际真正结合起来,对以后要学习的专业知识也做好了一定的铺垫,对自己有一个较大的提升。
七、参考文献
《C语言程序设计》谭浩强清华大学出版社
《C语言程序设计》王曙燕等科学出版社
《数据结构——使用C语言》陈一华等电子科技大学出版社
《数据结构题集》严蔚敏吴伟民清华大学出版社
八、附录:
源代码
1.多项式相乘
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
typedefstructPolynode
{
intcoef;/*系数*/
intexp;/*指数*/
structPolynode*next;
}Polynode,*Polylist;
/*创建函数*/
voidpolycreate(Polylisthead)
{
Polylistrear,s;
intc,e;
rear=head;/*rear始终指向但链表的尾,便于尾插法进行建表*/
printf("请输入每一项的系数和指数:
\n");
scanf("%d,%d",&c,&e);
while(c!
=0)
{
s=(Polylist)malloc(sizeof(Polynode));
s->next=NULL;
s->coef=c;
s->exp=e;
rear->next=s;/*尾插法进行创建*/
rear=s;
scanf("%d,%d",&c,&e);
}
printf("建立结束!
\n");
}
/*乘法运算函数*/
voidcheng(Polylistpolya,Polylistpolyb,Polylisthead)
{
Polylistp,q,r,L;
L=head;
for(p=polya->next;p!
=NULL;p=p->next)
for(q=polyb->next;q!
=NULL;q=q->next)
{
r=(Polylist)malloc(sizeof(Polynode));
r->coef=p->coef*q->coef;
r->exp=p->exp+q->exp;
L->next=r;L=r;
}
L->next=NULL;
}
/*合并函数*/
voidcombine(PolylistL)
{
Polylistr,p,temp1,temp2;
inta,b;
for(r=L;r->next!
=NULL;r=r->next)
{
a=r->next->exp;p=r->next;/*r指向第一个元素的指数*/
while(p->next!
=NULL)
{
b=p->next->exp;/*指向第二个元素的指数*/
if(a==b)
{
temp1=(Polylist)malloc(sizeof(Polynode));
r->next->coef=r->next->coef+p->next->coef;
temp1=p->next;/*合并后删掉后面一项*/
p->next=temp1->next;free(temp1);/*指针后移*/
}
elsep=p->next;
}
if(r->next->coef==0)/*若系数为0,则删除该结点*/
{
temp2=(Polylist)malloc(sizeof(Polynode));
temp2=r->next;r->next=temp2->next;free(temp2);
}
}
}
/*排序函数*/
voidorder(PolylistL)/*合并后的多项式中指数只存在大于和小于的关系*/
{
Polylistr,p;
inta;floatb2;
r=(Polylist)malloc(sizeof(Polynode));
p=(Polylist)malloc(sizeof(Polynode));
for(r=L->next;r!
=NULL;r=r->next)
for(p=r->next;p!
=NULL;p=p->next)/*按从大到小的顺序排列*/
if((r->exp)<(p->exp))
{
a=r->exp;
r->exp=p->exp;/*交换指数*/
p->exp=a;
b2=p->coef;/*交换系数*/
p->coef=r->coef;
r->coef=b2;
}
}
/*打印函数*/
voidprintf1(PolylistS)
{
Polylistr;
for(r=S->next;r;r=r->next)
{
if(r->coef<0)
printf("%dx^%d",r->coef,r->exp);
else
printf("%dx^%d",r->coef,r->exp);
if(r->next&&r->next->coef>0)printf("+");
}
}
/*主函数*/
voidmain()
{
charflag='a';
Polylisthead,polya,polyb;
head=(Polylist)malloc(sizeof(Polynode));head->next=NULL;
polya=(Polylist)malloc(sizeof(Polynode));polya->next=NULL;
polyb=(Polylist)malloc(sizeof(Polynode));polyb->next=NULL;
while(flag=='a')
{
printf("\n创建第一个多项式\n");
polycreate(polya);
order(polya);
combine(polya);
printf1(polya);
printf("\n创建第二个多项式\n");
polycreate(polyb);
order(polyb);
combine(polyb);
printf1(polyb);
printf("\n");
printf("\n输出多项式相乘\n");
printf("(");printf1(polya);printf(")");
printf("*");printf("(");order(polyb);printf1(polyb);printf(")");
printf("=");cheng(polya,polyb,head);
combine(head);order(head);printf1(head);
flushall();
printf("\n需要继续进行多项式相乘运算吗?
需要请按a键,否则任意其他键退出\n");
scanf("%c",&flag);
}
}
2.魔王语言
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"conio.h"
#defineMAX100
typedefstructnode
{
chardata;
structnode*next;
}LinkStackNode,*LinkStack;
typedefstructNode
{
chardata;
structNode*next;
}LinkQueueNode;
typedefstruct
{
LinkQueueNode*front;
LinkQueueNode*rear;
}LinkQueue;
structpers
{
charelement;
charchinese[50];
structpers*next;
};
voidPush(LinkStacktop,charx)/*头插法*/
{
LinkStacktemp;
temp=(LinkStack)malloc(sizeof(LinkStackNode));
temp->data=x;
temp->next=top->next;
top->next=temp;
}
voidPoP(LinkStacktop,char*x)
{
LinkStacktemp;
temp=top->next;
top->next=temp->next;
*x=temp->data;
free(temp);
}
voidEnterQueue(LinkQueue*Q,charx)/*尾插法*/
{
LinkQueueNode*NewNode;/*队列的结点*/
NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if(NewNode!
=NULL)
{
NewNode->data=x;
NewNode->next=NULL;
Q->rear->next=NewNode;
Q->rear=NewNode;
}
}
voidDeleteQueue(LinkQueue*Q,char*x)
{
LinkQueueNode*p;
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)Q->rear=Q->front;
*x=p->data;
free(p);
}
/*读规则1函数*/
voidread1(structpers*P[26])
{
FILE*fp;
inti;
charx;
if((fp=fopen("H:
\\课程设计\\quying\\rule.txt","rt"))==NULL)
{
printf("\n\n****读规则文件出错,按任意键返回!
****");
getchar();
exit
(1);
}
while(!
feof(fp))
{
for(i=0;i<26;i++)
{
structpers*r,*p;
P[i]=(structpers*)malloc(sizeof(structpers));
p=P[i];
fscanf(fp,"%c",&x);
while(x!
='#')
{
r=(structpers*)malloc(sizeof(structpers));
r->element=x;
p->next=r;
p=r;
fscanf(fp,"%c",&x);
}
p->next=NULL;
}
}
fclose(fp);
printf("\n\n****文件1中信息已正确读出,按任意键继续!
****\n");
getchar();
}
/*读规则2函数*/
voidread2(structpers*S)
{
FILE*fp;
structpers*r,*head;
inti=0;
head=S;
if((fp=fopen("H:
\\课程设计\\quying\\mean.txt","rt"))==NULL)
{
printf("\n\n****读翻译文件出错,按任意键返回****");
getchar();
exit
(1);
}
while(i<26)/*文件未读取完*/
{
r=(structpers*)malloc(sizeof(structpers));
fscanf(fp,"%c",&r->element);
fscanf(fp,"%s",r->chinese);i++;
head->next=r;
head=r;
}
head->next=NULL;
fclose(fp);
printf("\n\n****文件2中信息已正确读出,按任意键继续!
****\n");
getchar();
}
/*写入函数*/
voidsave(charch[MAX],structpers*S)
{
FILE*fp;
structpers*r;
inti=0,l;
if((fp=fopen("H:
\\课程设计\\quying\\results.txt","at+"))==NULL)/*可追加读入*/
{
printf("写文件出错,按任意键返回");
getchar();
exit
(1);
}
l=strlen(ch);
for(i=0;ifprintf(fp,"%c",ch[i]);
for(i=0;i{
r=S->next;
while(r!
=NULL)
if(r->element==ch[i])/*找到该字母的汉语意思,输出*/
{
fprintf(fp,"%s",r->chinese);
break;
}
elser=r->next;
}
printf("恭喜您保存成功,按任意键继续!
\n");
getchar();
fclose(fp);
}
/*去括号函数*/
voidchange(chara[50],LinkStackS1,LinkQueue*Q,intl)
{
inti;
charc,e,k;
for(i=0;iif(a[i]==')')/*遇到右括号出栈*/
{
PoP(S1,&e);
while(S1->next->data!
='(')/*S1有头指针*/
{
EnterQueue(Q,e);
PoP(S1,&e);
}
c=e;/*标记首字母*/
PoP(S1,&k);/*去掉左括号*/
while(Q->rear!
=Q->front)
{
Push(S1,c);
DeleteQueue(Q,&e);
Push(S1,e);
}
Push(S1,c);
}
elsePush(S1,a[i]);
}
/*利用递归进行大小写代换*/
voidexchange(charx,structpers*P[26],LinkStackS3)
{
inti;
structpers*r;
r=(structpers*)malloc(sizeof(structpers));r->next=NULL;
for(i=0;i<26;i++)
if(x==P[i]->next->element)
{
for(r=P[i]->next->next;r!
=NULL;r=r->next)
if(r->element>='a'&&r->element<='z')
Push(S3,r->element);
elseif(r->element>='A'&&r->element<='Z')
exchange(r->element,P,S3);
break;
}
}
/*大小写代换,大写字母调用exchange函数*/
voidtranslate(LinkStackS1,LinkStackS,structpers*P[26])
{
charx,m;
LinkStackS3;
S3=(LinkStack)malloc(sizeof(LinkStackNode));S3->next=NULL;
while(S1->next!
=NULL)