机软实验报告.docx
《机软实验报告.docx》由会员分享,可在线阅读,更多相关《机软实验报告.docx(15页珍藏版)》请在冰豆网上搜索。
机软实验报告
机软实验报告
2014级自动化201刘强学号14020114
线性表基本操作的实现:
//shiyan11.c
#include
#include
#defineNull0
#defineMaxSize1024
typedefintDataType;
typedefstructnode{
DataTypedata[MaxSize];
intlast;
}SequenList;
voidPrintOut(SequenList*L);
intInsert(SequenList*L,DataTypex,inti);
intDelete(SequenList*L,inti);
voidCreateList(SequenList*L);
intmain(){
SequenListMyList,*L;
charcmd;
inti,t,x;
L=&MyList;
L->last=-1;
do{
do{
clrscr();
printf("\n\tc,C......CreateList");
printf("\n\ti,I.....Insert");
printf("\n\td,D......Delete");
printf("\n\tq,Q......Quit\n\tYourchoice:
");
cmd=getchar();
}while((cmd!
='d')&&(cmd!
='D')&&(cmd!
='q')&&(cmd!
='Q')&&(cmd!
='i')&&(cmd!
='I')&&(cmd!
='c')&&(cmd!
='C'));
/*或:
while((toupper(cmd)!
=’D’)&&(toupper(cmd)!
=’Q’)&&(toupper(cmd)!
=’I’)&&(toupper(cmd)!
=’C’));*/
switch(cmd){
case'c':
case'C':
CreateList(L);/创建一个链表
break;
case'i':
case'I':
printf("\nInputthedatatobeinserted:
");
scanf("%d",&x);
printf("\nInputthepoistiontobeinserted");
printf("(1--%d):
",(L->last+2));
scanf("%d",&i);
Insert(L,x,i);
PrintOut(L);
printf("\nPressanykeytocontinue\n");
getch();
break;
case'd':
case'D':
printf("\nInputtheindex_Noofdatatobedeleted");
printf("(1---%d):
",(L->last+1));
scanf("%d",&i);
Delete(L,i);
PrintOut(L);
printf("\nPressanykeytocontinue\n");
getch();
break;
default:
break;
}
}while((cmd!
='q')&&(cmd!
='Q'));
/*或:
while((toupper(cmd)!
='Q'));*/
return0;
}
intInsert(SequenList*L,DataTypex,inti){
intj;
SequenList*p;
p=L;/*可以删去p,本函数中所有的p均改为L.*/
if(p->last>=(MaxSize-1)){
printf("\nOverflow!
");
returnNull;
}
elseif((i<1)||(i>(p->last+2))){
printf("rangeerror");
returnNull;
}
else{
for(j=L->last;j>=i-1;j--)L->data[j+1]=L->data[j];/添加
L->data[i-1]=x;
L->last=L->last+1;
}
return
(1);
}
voidPrintOut(SequenList*L){
inti;
for(i=0;i<=L->last;i++){
printf("data[%d]=",i+1);
printf("%d\t",L->data[i]);
if((i!
=0)&&((i%4)==0))printf("\n");
}
}/*PrintOut*/
intDelete(SequenList*L,inti){
intj;
if((i<1)||(i>L->last+1)){
printf("rangeerror");
returnNull;
}
else{
for(j=i;jlast+1;j++)L->data[j-1]=L->data[j];/删除一项,后面补位
L->last--;
}
return
(1);
}
voidCreateList(SequenList*L){
intn,i;
intmydata;
printf("\nPleaseinputtotalnumberofdataitem\n");
scanf("%d",&n);
for(i=0;iprintf("data[%d]=",i+1);
scanf("%d",&mydata);
L->data[i]=mydata;
}
L->last=n-1;
printf("\nPressanykeytocontinue\n");
getch();
}/*CreateList*/
一元多项式的简单计算:
//shiyan12.c
#include"stdafx.h"
#include
#include
#include
#defineNull0
#defineTrue1
#defineFalse0
typedefstructpolyterm{
intcoef;
intexp;
structpolyterm*next;
}TERM;
TERM*reverse(TERM*q);
voidpolyout(TERM*head);
TERM*creatpoly(){
TERM*head,*r,*s;
intm,n;
head=(TERM*)malloc(sizeof(TERM));
printf("\ninputcoefandexp(1,2):
\n");
scanf("%d,%d",&n,&m);
r=head;
while(n){
s=(TERM*)malloc(sizeof(TERM));
s->coef=n;s->exp=m;
r->next=s;r=s;
printf("\nInputcoefandexp:
\n");
scanf("%d,%d",&n,&m);
}
r->next=Null;r=head;
head=head->next;free(r);
return(head);
}/*creatpoly*/
TERM*polyadd(TERM*ha,TERM*hb){
TERM*hc,*p,*q,*s,*r;
intx;
p=ha;q=hb;
hc=(TERM*)malloc(sizeof(TERM));
s=hc;
while((p!
=Null)&&(q!
=Null)){
if(p->exp==q->exp){/*coeficients*/
x=p->coef+q->coef;
if(x!
=0){
r=(TERM*)malloc(sizeof(TERM));
r->exp=p->exp;r->coef=x;
s->next=r;s=r;
}
p=p->next;q=q->next;
}
elseif(p->expexp){
r=(TERM*)malloc(sizeof(TERM));
r->coef=q->coef;r->exp=q->exp;
s->next=r;s=r;
q=q->next;
}
else{/*p->exp>q->exp*/
r=(TERM*)malloc(sizeof(TERM));
r->exp=p->exp;r->coef=p->coef;
s->next=r;s=r;
p=p->next;
}
}
while(p!
=Null){
r=(TERM*)malloc(sizeof(TERM));
r->exp=p->exp;r->coef=p->coef;
s->next=r;s=r;
p=p->next;
}
while(q!
=Null){
r=(TERM*)malloc(sizeof(TERM));
r->exp=q->exp;r->coef=q->coef;
s->next=r;s=r;
q=q->next;
}
s->next=Null;
r=hc;
hc=hc->next;
free(r);
return(hc);
}/*polyadd*/
TERM*polymulti(TERM*f,TERM*g){/*关于一元多项式相乘的程序*/
TERM*fp,*gp,*hp,*q,*h;
intmaxp,p,r,x;
maxp=f->exp+g->exp;
h=(TERM*)malloc(sizeof(TERM));
hp=h;
g=reverse(g);
for(r=maxp;r>=0;r--){
x=0;fp=f;
gp=g;
while((fp!
=Null)&&(gp!
=Null)){
p=fp->exp+gp->exp;
if(p>r)fp=fp->next;
elseif(pnext;
else{
x+=fp->coef*gp->coef;
fp=fp->next;
gp=gp->next;
}
}/*endofwhile*/
if(x!
=0){
q=(TERM*)malloc(sizeof(TERM));
q->exp=r;q->coef=x;
q->next=Null;
hp->next=q;hp=q;
}
}/*endoffor*/
hp=h;h=h->next;
free(hp);
return(h);
}
TERM*reverse(TERM*q){
TERM*p1,*p2;
if(q!
=Null){
p1=q->next;q->next=Null;
while(p1!
=Null){
p2=p1->next;p1->next=q;
q=p1;p1=p2;
}/*endofwhile*/
}
return(q);
}
voidpolyout(TERM*head){
TERM*p,*q;
p=head;
/*p=head->next;*/
while(p!
=Null){
printf("%d,%d",p->coef,p->exp);
p=p->next;
}
printf("\n");
}
intmain(){
TERM*ha,*hb,*hc,*p,*q,*h;
printf("\nInputthe1stpolynomial");
ha=creatpoly();
printf("\nInputthe2ndpolynomial");
hb=creatpoly();
printf("\nthe1stpolynomialis:
");
polyout(ha);
printf("\nthe2ndpolynomialis:
");
polyout(hb);
hc=polyadd(ha,hb);
printf("\ntheadditionofthetwopolynomialis:
");
polyout(hc);
h=polymulti(ha,hb);
printf("\nthemultiplicationofthetwopolynomialis:
");
polyout(h);
return0;
}
先输入的是系数,然后加入x的次方,依次进行运算
约瑟夫环游戏的两种实现方法对比:
A.链式结构实现
//shiyan13.c
#include"stdafx.h"
#include
#include
#include
#defineNull0
typedefstructtagnode{
intnum;
structtagnode*next;
}LinkList;
LinkList*creat(intn);
LinkList*select(LinkList*head,intm);
LinkList*head=Null,*last;
intmain(){
intn,m;
printf("\nInputthetotalnumberofpeople:
");
scanf("%d",&n);
printf("\ninputthenumberofpersonyouaretocall:
");
scanf("%d",&m);
head=creat(n);
last=select(head,m);
n=last->num;
printf("thelastone:
%d\n",n);
free(last);
getch();
return0;
}
LinkList*select(LinkList*head,intm){
LinkList*p,*q;
inti,t,flag=0;
p=head;
t=1;
q=p;/*q--前趋指针,p--当前指针*/
do{
p=q->next;
t=t+1;
if(t%m==0){/*报数到*/
printf("%4d\t",p->num);
if(q->next==q){flag=1;break;}
q->next=p->next;
free(p);
p=q;
}
elseq=p;
}while((q==p)||(flag==0));
head=p;
return(head);
}/*Select*/
LinkList*creat(intn){/使n个人围成一圈,并给每个人标识号数
LinkList*head,*s,*p;
inti;
s=(LinkList*)malloc(sizeof(LinkList));
head=s;
s->num=1;
p=s;
for(i=2;i<=n;i++){
s=(LinkList*)malloc(sizeof(LinkList));
s->num=i;
p->next=s;
p=s;
}
p->next=head;
return(head);
}/*creat*/
B.顺序结构实现
//shiyan14.c
#include"stdafx.h"
#include
#include
#include
voidJosephus(intn,intm,ints);
intmain(){
intn,m,s;
printf("Johephasproblem\n");
printf("inputnumberofpeople,numbertocall,andstartnumber:
\n");
scanf("%d,%d,%d",&n,&m,&s);
Josephus(n,m,s);
getch();
return0;
}/*main*/
voidJosephus(intn,intm,ints){
int*R,i,j;
R=(int*)malloc(n*sizeof(int));
for(i=1;i<=n;i++)R[i]=i;
for(i=n;i>=2;i--){
s=(s+m-1)%i;
if(s==0)s=i;
printf("%2d",R[s]);
for(j=s+1;j<=i;j++)R[j-1]=R[j];
}
printf("%2d\n",R[1]);
}/*Johephus*/
经过测试结果均正确
思考题
1、可结合1中链表,给每个人进行编号,然后以数字进行游戏运转
2、实现两多项式相减,和加法一样,同系数进行运算,把加改为减