第一篇:用C语言描述的五角星完整代码
#include
int i1,j1,k1,i2,j2,k2,i3,j3,k3,i4,j4,k4,m4,n4;printf(“五角星:n”);for(i1=1;i1<6;i1++)
// /*这是五角星的上面一个角*/ { for(j1=1;j1<19-i1;j1++)
// /*这是空格*/
printf(“ ”);
for(k1=1;k1<=2*i1-1;k1++)///*这是“*”*/
printf(“*”);
printf(“n”);} for(i2=1;i2<5;i2++)
// /*这是五角星的中间两个角*/ { for(j2=1;j2<3*i2-3;j2++)
printf(“ ”);
for(k2=1;k2<=42-6*i2;k2++)
printf(“*”);
printf(“n”);} for(i3=1;i3<3;i3++)
//
/*这是中间与下部相接的部分*/ { for(j3=1;j3<12-i3;j3++)
printf(“ ”);
for(k3=1;k3<=12+2*i3;k3++)
printf(“*”);
printf(“n”);} for(i4=1;i4<5;i4++)
//
/*这是五角星的下面两个角*/ { for(j4=1;j4<10-i4;j4++)
printf(“ ”);
for(k4=1;k4<=10-2*i4;k4++)
printf(“*”);
for(m4=1;m4<6*i4-3;m4++)
printf(“ ”);
for(n4=1;n4<=10-2*i4;n4++)
printf(“*”);
printf(“n”);} printf(“Congratulations!nWe've successfully drawn a we?n(*^_^*)n”);
return 0;}
beautiful pentagon,haven't
第二篇:C语言课程设计代码
#include “stdio.h” #include
void main(){ int n=0;struct course *head=NULL;void insert(struct course **head,struct course *cou);void Print(struct course **head,int *n);void Modify(struct course **head,int *n);void Require(struct course **head);void Creat(struct course **head,int *n);void Delete(struct course **head,int *n);void Fun(struct course **head,int *n);
Fun(&head,&n);}
void insert(struct course **head,struct course *cou){ struct course *p0,*p1,*p2;p2=p1=*head;p0=cou;if(*head){
while((p0->semester>p1->semester)&&(p1->next))
{
p2=p1;
p1=p1->next;
}
if(p0->semester
semester)
{
if(*head==p1)*head=p0;
else p2->next=p0;
p0->next=p1;}
else
{
if(p0->semester==p1->semester){ while((p0->cID>p1->cID)&&(p1->next)&&(p0->semester==p1->semester))
{
} if(p0->semester!=p1->semester){
} else {
if(p0->cID<=p1->cID){
if(*head==p1)*head=p0;else p2->next=p0;p2=p1;p1=p1->next;p2->next=p0;p0->next=p1;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
}
else
{p1->next=p0;p0->next=NULL;}
} } else
{
*head=p0;
p0->next=NULL;} }
void Print(struct course **head,int *n){ struct course *p;p=*head;if(*head){
if(*n==1)printf(“nThis %d record is:n”,*n);
else printf(“nThese %d records are:n”,*n);
printf(“semester cID
name
creditn”);
do
{ printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit);
p=p->next;
}while(p!=NULL);} else printf(“nList null!n”);}
void Modify(struct course **head,int *n){ struct course *p,*p2;int cID;if(*head){
Print(head,n);while(1){ printf(“nPlease input the cID which you want to modify:”);
scanf(“%d”,&cID);p2=p=*head;while(p->next&&(cID!=p->cID)){
p2=p;
p=p->next;} if(cID==p->cID){
printf(“Please input the new cID(1~60):”);
scanf(“%d”,&p->cID);
while(p->cID<0||p->cID>60)
{
printf(“nError!”);
printf(“nPlease input the new cID(1~60):”);
scanf(“%d”,&p->cID);
}
printf(“Please input the new semester(1~8):”);
scanf(“%d”,&p->semester);while(p->semester<0||p->semester>8)
{
printf(“nError!”);
printf(“nPlease input the new semester(1~8):”);
scanf(“%d”,&p->semester);
}
printf(“Please input the new credit:”);
scanf(“%f”,&p->credit);
printf(“Please input the new name:”);
scanf(“%s”,p->name);
if(p==*head)*head=p->next;
else p2->next=p->next;
insert(head,p);
break;
}
else printf(“%d not been found!n”,cID);
} } else {printf(“nList null!n”);} }
void Require(struct course **head){ struct course *p;float sum=0;int sem,i=0;printf(“nPlease input the semester which is required:”);
scanf(“%d”,&sem);p=*head;while(p){
if(sem==p->semester)
{
i++;if(i==1)printf(“nsemester cID
name
creditn”);printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit);
sum=sum+p->credit;
}
p=p->next;} printf(“The sum of credit in this term is:%.1fn”,sum);}
void Creat(struct course **head,int *n){ struct course *p1;while(1){
p1=(struct course *)malloc(LEN);
printf(“Please input the cID(1~60):”);
scanf(“%d”,&p1->cID);
while(p1->cID<0||p1->cID>60)
{
printf(“nError!”);
printf(“nPlease input the cID(1~60):”);
scanf(“%d”,&p1->cID);
}
if(p1->cID==0)break;
printf(“Please input the semester(1~8):”);
scanf(“%d”,&p1->semester);
while(p1->semester<0||p1->semester>8)
{
printf(“nError!”);
printf(“nPlease input the semester(1~8):”);scanf(“%d”,&p1->semester);
}
} } printf(“Please input the credit:”);scanf(“%f”,&p1->credit);printf(“Please input the name:”);scanf(“%s”,p1->name);insert(head,p1);*n=*n+1;printf(“nYou can continue until the cID is ”0“!n”);Print(head,n);void Delete(struct course **head,int *n){
struct course *p1,*p2;int cID;Print(head,n);if(*head){ printf(“Please input the cID of the course which you want to delete:”);scanf(“%d”,&cID);p1=*head;
while(cID!=p1->cID&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(cID==p1->cID)
{
if(p1==*head)*head=p1->next;
else p2->next=p1->next;
printf(“Have delete cID:%dn”,cID);
*n=*n-1;
}
else printf(“%d not been found!n”,cID);} }
void Fun(struct course **head,int *n){ char num;
while(1)
{
system(“cls”);
puts(“**************** Main Menu ******************”);
puts(“* 1.Add Records
2.Print Records
*”);
puts(“* 3.Delete Records
4.Modify Records *”);
puts(“* 5.Require Records 6.Exit
*”);
printf(“Please input your choice: ”);
scanf(“%d”,&num);
switch(num)
{
case 1:Creat(head,n);break;
case 2:Print(head,n);break;
case 3:Delete(head,n);break;
case 4:Modify(head,n);break;
case 5:Require(head);break;case 6:exit(0);break;
default: break;
}
printf(“nPress ”Enter“ to continue!”);getchar();getchar();
} }
第三篇:c语言迷宫问题代码实现
C语言迷宫问题代码如下:
#include
#include
#define LEN sizeof(SEAT)
#define MAXSIZE 100
#define LENGTH 30
typedef struct
{
int x;//横坐标
int y;//纵坐标
int di;//表示方向,0-3分别表示东南西北。
}SEAT;
struct StackList
{
SEAT stack[MAXSIZE];
int top;
}*Stack;
int EmptyStack(StackList*Stack)//判断是否为空栈
{
if(Stack->top==0)
return 0;
else
return 1;
}
int Move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//分别表示向东、西、南、北需要加上的坐标
int Mase[LENGTH][LENGTH]={0};//初始化为0
int length,width;
void InitMase()//在迷宫的外围添加一层“墙壁”(赋值为1),使得迷宫的任意一点都有四个方向
{
int i,j;
for(i=0;i { for(j=0;j Mase[i][j]=1; } for(i=0;i { for(j=0;j Mase[j][i]=1; } } void InitStack(StackList*Stack)//初始化 { Stack->top=0; return; } int PushStack(StackList*Stack,SEAT CurSeat)//进栈 { if(Stack->top==MAXSIZE-1) return false; else { Stack->stack[Stack->top].x=CurSeat.x; Stack->stack[Stack->top].y=CurSeat.y; Stack->stack[Stack->top].di=CurSeat.di; Stack->top++; return true; } } int PopStack(StackList*Stack)//出栈 { if(Stack->top==0) return false; else { Stack->top--; return true; } } int Pass(SEAT p)//判断当前是否可行 { if(Mase[p.x][p.y]==0) { return true; } else return false; } SEAT NextSeat(SEAT CurSeat)//查找下一个点,并返回 { SEAT temp; temp.x=CurSeat.x+Move[CurSeat.di][0]; temp.y=CurSeat.y+Move[CurSeat.di][1]; return temp; } void Mark(SEAT p)//标记已经走过的点,避免重复 { Mase[p.x][p.y]=-1; } int PathWay(SEAT start,SEAT end)//找路线 { Stack=(struct StackList*)malloc(sizeof(struct StackList)); InitStack(Stack); SEAT CurSeat; CurSeat.x=start.x+1;//由于多加了一层墙壁,因此坐标的值要加1 CurSeat.y=start.y+1;// CurSeat.di=start.di;// do { if(Pass(CurSeat)) { PushStack(Stack,CurSeat); Mark(CurSeat); if(CurSeat.x==end.x+1&&CurSeat.y==end.y+1)//如果找到出口,返回{ return true; } else { int find=0; while(CurSeat.di<3&&find==0)//找下一个结点的方向 { CurSeat.di++; SEAT temp; temp=NextSeat(CurSeat); if(Mase[temp.x][temp.y]==0) { temp.di=-1; CurSeat=temp; find =1; } } } } else { if(EmptyStack(Stack))//当前的点不能走,出栈 PopStack(Stack); if(EmptyStack(Stack))//当前的点变为前一个点 { CurSeat=Stack->stack[Stack->top-1]; } while(CurSeat.di==3&&EmptyStack(Stack))////当前的点找不到下一个点,出栈 { PopStack(Stack); CurSeat=Stack->stack[Stack->top-1];//当前的点变为前一个点} if(EmptyStack(Stack)) { int find=0; while(CurSeat.di<3&&find==0)////找下一个结点的方向 { CurSeat.di++; SEAT temp; temp=NextSeat(CurSeat); if(Mase[temp.x][temp.y]==0) { temp.di=-1; CurSeat=temp; find =1; } } } } }while(EmptyStack(Stack)); return false; } void PrintStack(StackList*Stack)//输出路线 { if(Stack->top==0) printf(“There is no route can be out of the mazen”); else { int i; for(i=0;i { if(i!=0) printf(“->(%d,%d)”,Stack->stack[i].x-1,Stack->stack[i].y-1); else printf(“(%d,%d)”,Stack->stack[i].x-1,Stack->stack[i].y-1); } } } void PrintMase()//输出迷宫 { int i,j; for(i=1;i { for(j=1;j { if(j!=1) printf(“ %d”,Mase[i][j]); else printf(“%d”,Mase[i][j]); } printf(“n”); } } int main() { int n; SEAT start,end; printf(“Please enter the maze of the length and width:n”); scanf(“%d%d”,&length,&width); printf(“Please enter the number of the maze wall unit(0 printf(“Please enter the labyrinth of the coordinates of the wall unit(0<=row,column):n”);while(--n>=0) { int x,y; scanf(“%d%d”,&x,&y); Mase[x+1][y+1]=1; } InitMase(); PrintMase(); printf(“Please enter the coordinates entrance(0<=x<%d,0<=y<%d):n”,length,width); scanf(“%d%d”,&start.x,&start.y); start.di=-1; printf(“Please enter the coordinates exports(0<=x<%d,0<=y<%d):n”,length,width); scanf(“%d%d”,&end.x,&end.y); end.di=0; if(PathWay(start,end)) PrintStack(Stack); else printf(“There is no route can be out of the mazen”);return 0; } of the of the maze maze 《数据结构》课程设计题目(程序实现采用C语言) 题目1:猴子选王(学时:3) 一堆猴子都有编号,编号是1,2,3...m,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第n个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。 要求:m及n要求从键盘输入,存储方式采用向量及链表两种方式实现该问题求解。 //链表 #include typedef struct _RingNode { int pos; struct _RingNode *next;}RingNode, *RingNodePtr; // 创建约瑟夫环,pHead:链表头指针,count:链表元素个数 void CreateRing(RingNodePtr pHead, int count){ RingNodePtr pCurr = NULL, pPrev = NULL; int i = 1; pPrev = pHead; while(--count > 0) { pCurr =(RingNodePtr)malloc(sizeof(RingNode)); i++; pCurr->pos = i; pPrev->next = pCurr; pPrev = pCurr; } pCurr->next = pHead;// 构成环状链表 } void KickFromRing(RingNodePtr pHead, int n){ RingNodePtr pCurr, pPrev; int i = 1; // 计数 pCurr = pPrev = pHead; while(pCurr!= NULL) { if(i == n) { // 踢出环 printf(“n%d”, pCurr->pos); // 显示出圈循序 pPrev->next = pCurr->next; free(pCurr); pCurr = pPrev->next; i = 1; } pPrev = pCurr; pCurr = pCurr->next; if(pPrev == pCurr) { // 最后一个 printf(“nKing is %d”, pCurr->pos); // 显示出圈循序 free(pCurr); break; } i++; } } int main(){ int n = 0, m = 0; RingNodePtr pHead = NULL; printf(“M(person count)= ”); scanf(“%d”, &m); printf(“N(out number)= ”); scanf(“%d”, &n); if(m <= 0 || n <= 0) { printf(“Input Errorn”); return 0; } // 建立链表 pHead =(RingNodePtr)malloc(sizeof(RingNode)); pHead->pos = 1; pHead->next = NULL; CreateRing(pHead, m);// 开始出圈 printf(“nKick Order: ”); KickFromRing(pHead, n); printf(“n”); system(“pause”); return 0;} //数组做: #include void SelectKing(int MonkeyNum, int CallNum); void main(){ int MonkeyNum; int CallNum; /* 输入猴子的个数 */ printf(“Monkey Num = ”); scanf(“%d”, &MonkeyNum); /* 输入M的值 */ printf(“Call Num = ”); scanf(“%d”, &CallNum); SelectKing(MonkeyNum, CallNum); } void SelectKing(int MonkeyNum, int CallNum){ int *Monkeys;// 申请一个数组,表示所有的猴子; int counter = 0;//计数,当计数为猴子个数时表示选到最后一个猴子了; int position = 0;// 位置,数组的下标,轮流遍历数组进行报数; int token = 0;// 令牌,将报数时数到M的猴子砍掉; // 申请猴子个数大小的数组,把桌子摆上。 Monkeys =(int *)malloc(sizeof(int)* MonkeyNum); if(NULL == Monkeys) { printf(“So many monkeys, system error.n”); return; } // 将数组的所有内容初始化为0,被砍掉的猴子设置为1 memset(Monkeys, 0, sizeof(int)*MonkeyNum); // 循环,直到选中大王 while(counter!= MonkeyNum) { // 如果这个位置的猴子之前没有砍掉,那么报数有效 if(Monkeys[position] == 0) { token++;// 成功报数一个,令牌+1,继续报数直到等于M // 如果报数到M,那么将这个猴子砍去 if(token == CallNum) { Monkeys[position] = 1;// 设置为1,表示砍去 counter++;// 计数增加 token = 0;// 设置为0,下次重新报数 // 如果是最后一个猴子,把它的位置打印,这个就是大王了 if(counter == MonkeyNum) { printf(“The king is the %d monkey.n”, position+1); } } } // 下一个猴子报数 position =(position + 1)%MonkeyNum; } // 释放内存,开头为所有猴子创建的桌子 free(Monkeys); return;} 题目2 :字符逆转(学时:3) 从键盘读入一个字符串,把它存入一个链表(每个结点存储1个字符),并按相反的次序将字符串输出到显示屏。 #include struct node { struct node *prev; char c; struct node *next;};struct node *input(struct node *top);int main(void){ struct node T,*top=&T,*bottom=&T,*p=NULL;T.prev=NULL;T.next=NULL;T.c=' ';bottom=input(top);p=bottom->prev;while(p!=NULL){ printf(“%c”,p->c); p=p->prev;} return 0;} struct node *input(struct node *top){ struct node *t;char x;while((x=getchar())!='n'){ top->c=x;t=(struct node *)malloc(sizeof(struct node));top->next=t;t->prev=top;t->next=NULL;t->c=' ';top=top->next; } } return top;题目3 :工资核算(学时:3) 设有一个单位的人员工资有如下信息:name、department、base pay、allowance、total。现从键盘输入一组人员工资数据并将它们存储到名为paydata的文件中;再从paydata取出工资数据并给每个人的base pay增加100元,增加后将工资数据显示于屏幕(每行1人)。 #include char name[100];char department[100]; int basepay; int allowance; int total;}stuff[SIZE];main(){ FILE *fp; int i; printf(“Please enter name department basepay allowance:n”); for(i=0;i scanf(“%s %s %f %f”,&stuff[i].name,&stuff[i].department,&stuff[i].basepay,&stuff[i].allowance); if((fp=fopen(“paydata.dat”,“wb”))==NULL) { printf(“Can't open filen”); return 0;} for(i=0;i if(fwrite(&stuff[i],LENTH,1,fp)!=1) printf(“文件写入出错n”); fclose(fp); if((fp=fopen(“paydata.dat”,“rb”))==NULL) { } printf(“Can't open filen”); printf(“修改过后的结果:n”); for(i=0;i { fread(&stuff[i],LENTH,1,fp); stuff[i].total=stuff[i].basepay+100+stuff[i].allowance; printf(“%-10s%-10s %f %f %fn”,stuff[i].name,stuff[i].department,stuff[i].basepay+100,stuff[i].allowance,stuff[i].total); } fclose(fp);return 0;} 题目4:满足条件的有序表生成(学时:3) 已知三个有序表A、B、C,它们皆由同一类元素构成,现要求对于表A作以下运算而获得有序表D:排出A中所有的既在B中又在C中出现的元素。另外该任务要求具有建立有序表功能以及输出有序表到屏幕的功能。 #include int a[7],b[5],c[6],d[7]; int i,j,k,t,m;printf(“nPlease enter 7 numbers of A:”);for(i=0;i<7;i++)scanf(“%d”,&a[i]);for(j=0;j<6;j++) for(i=0;i<6-j;i++) if(a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} printf(“the sorted numbers:n”);for(i=0;i<7;i++)printf(“%5d”,a[i]); printf(“nPlease enter 5 numbers of B:”); for(i=0;i<5;i++)scanf(“%d”,&b[i]);printf(“n”);for(j=0;j<4;j++)for(i=0;i<4-j;i++) if(b[i]>b[i+1]){t=b[i];b[i]=b[i+1];b[i+1]=t;} printf(“the sorted numbers:n”);for(i=0;i<5;i++)printf(“%5d”,b[i]); printf(“nPlease enter 6 numbers of C:”); for(i=0;i<6;i++)scanf(“%d”,&c[i]); for(j=0;j<5;j++) for(i=0;i<5-j;i++) if(c[i]>c[i+1]){t=c[i];c[i]=c[i+1];c[i+1]=t;} printf(“the sorted numbers:n”);for(i=0;i<6;i++)printf(“%5d”,c[i]);printf(“n”); for(i=0;i<5;i++){ for(j=0;j<6;j++){ if(b[i]==c[j]){ for(k=0;k<7;k++){ if(b[i]==a[k]) } } } } a[k]=m; printf(“n”);k=0;for(i=0;i<7;i++) if(a[i]!=m){ } printf(“生成的有序表d为 ”);for(i=0;i printf(“%4d”,d[i]);d[k]=a[i];k++;printf(“n”);} 题目5:一元 多项式的减法(学时:6) 设有两个一元多项式A(x),B(x),请完成运算A(x)+B(x)、A(x)-B(x),要求多项式采用链表进行存储。另外该任务要求具有建立多项式链表以及输出多项式到屏幕的功能。 #include struct Polynode { int coef;int exp;Polynode *next;}Polynode,*Polylist;void Polycreate(Polylist head){ } void Polyadd(Polylist polya,Polylist polyb){ Polynode *p,*q,*tail,*temp;int sum;p=polya->next;Polynode *rear,*s;int c,e;rear=head;printf(“请输入多项式的系数项和指数项”);scanf(“%d,%d”,&c,&e);while(c!=0){ } rear->next=NULL;s=(Polynode*)malloc(sizeof(Polynode));s->coef=c;s->exp=e;rear->next=s;rear=s;scanf(“%d,%d”,&c,&e); q=polyb->next;tail=polya;while(p!=NULL&&q!=NULL){ if(p->exp sum=p->coef+q->coef;if(sum!=0){ } else { temp=p;p->coef=sum;tail->next=p;tail=p;p=p->next;temp=q;q=q->next;free(temp); } } } } p=p->next;free(temp);q=q->next;free(temp);else { } tail->next=q;tail=q;q=q->next;if(p!=NULL)tail->next=p;else tail->next=q;void Polysubstract(Polylist polya,Polylist polyb){ Polylist h=polyb;Polylist p=pb->next;Polylist pd;while(p){p->coef*=-1;p=p->next;} pd=Polyadd(pa,h);for(p=h->next;p;p=p->next)p->coef*=-1;return pd; } void PrintPolyn(Polyn P) void printfPolyn(Polynode *head){ Polynode *p=head->next;while(p){printf(“%dx^%d”,p->coef,p->exp);if(p->next)printf(“+”);p=p->next;} } int main(){ Polynode *La,Lb;La=Polycreate();Lb=Polycreate();PrintPolyn(La);printf(“/n”);PrintPolyn(Lb); } printf(“/n”);Polyadd(polya,polyb);printPolyn(polya);return 0; 题目6:床位分配(学时:6) 某客店有N个等级的房间,第k级客房有A(k)个,每个房间有B(k)个单人床,以菜单调用方式设计为单身旅客分配床位以及离店时收回床位的程序。要求分配成功时,印出旅客姓名、年龄、性别、到达日期、客房等级、房间号及床位号;分配不成功时,允许更改房间等级,若不更改等级,印出“满客”提示。 #include typedef struct Room { int roomlevel;int roomnum;int bednum;int peoplenum;int bed[N];int sex;char name[10];struct Room *next;}Room;Room *creat(int roomlevel,int room[],int bed[]){ Room *head,*p,*q; int i=1,j,h,num=1; head=(Room *)malloc(sizeof(Room));head->peoplenum=0;q=head;while(i<=roomlevel) { for(j=1;j<=room[i];j++) { p=(Room *)malloc(sizeof(Room)); p->roomlevel=i;p->roomnum=num++; p->sex=-1; for(h=0;h q->next=p; q=p;} i++;} p->next=NULL; p->peoplenum=0; return(head);} void Init(Room *head){ Room *p;int i;p=head;while(p!=NULL){ p->peoplenum=0; p->sex=-1; for(i=0;i p->bed[i]=0; p=p->next;} printf(“nn 操作成功} void Getin(Room *head){ Room *p;n”); int i,s,lev,age;char name[10];int number=0;int bednumber=0;printf(“nn 欢迎使用订房系统 nn”); printf(“请输入性别(1为男,2为女):”); scanf(“%d”,&s);printf(“nn 请输入想入住的房间等级:”);scanf(“%d”,&lev);p=head->next;while(p!=NULL){ if((p->roomlevel==lev)&&((p->sex==s)||(p->sex==-1))){ } for(i=0;i if(p->bed[i]==0){ } if(number!=0)break;number=p->roomnum;bednumber=i+1;p->bed[i]=1;p->sex=s;p->peoplenum++;break; } p=p->next;if(number==0&&bednumber==0) else { head->peoplenum++; printf(“n订单已下,请输入客人信息: n”);printf(“n 满客 n”); printf(“名字:n”);scanf(“%s”,name); printf(“年龄 :n”);scanf(“%d”,&age); printf(“您的订单3:n”); printf(“名字 年龄 性别 到达时间 房间等级 房间号 床位号n”); if(s==1) printf(“%s %d male 11-19 %d %d %dn”,name,age,p->roomlevel,p->roomnum,bednumber); else printf(“%s %d formale 11-19 %d %d %d n”,name,age,p->roomlevel,p->roomnum,bednumber); } printf(“n”); } void Checkout(Room *head){ Room *p;int number,bednumber,i,s;printf(“欢迎使用退房系统:”);printf(“nn 请输入房间号:”);scanf(“%d”,&number);printf(“nn 请输入性别(1为男,0为女):”);scanf(“%d”,&s);printf(“请输入床位号:”);scanf(“%d”,&bednumber);p=head->next;while(p!=NULL){ if(p->roomnum==number) for(i=0;i roomlevel;i++) if(i+1==bednumber){ p->bed[i]=0;p->peoplenum--;if(p->peoplenum<0) p->peoplenum=0; } } } if(p->peoplenum==0) p->sex=-1; printf(“您已退房,欢迎下次光临”);break;p=p->next;void Display(Room *head){ Room *p;int i=0;p=head->next;printf(“nn已订房间查询”);if(head->peoplenum==NULL){ printf(“所有等级房间空房”); return;} while(p->peoplenum!=NULL){ if(p->sex==1) printf(“n 房间号:%d,房间等级:%d,已住人数:%d,住房人性别:男”,p->roomnum,p->roomlevel,p->peoplenum); else printf(“n 房间号:%d,房间等级:%d,已住人数:%d,住房人性别:女”,p->roomnum,p->roomlevel,p->peoplenum); } void main(){ int n,k=1,i,roomlevel,room[10],bed[10],sum=0; Room *head; printf(“请输入房间等级数:n”);printf(“Roomlevel:”);scanf(“%d”,&roomlevel);for(i=1;i<=roomlevel;i++) { printf(“n %d等级房间数:”,i); } printf(“n”);p=p->next; while(i peoplenum) if(p->bed[i]==1) { printf(“,已住床位号:%d”,i+1); i++; } } scanf(“%d”,&room[i]);printf(“n %d房间内床位数:”,i); scanf(“%d”,&bed[i]);sum+=room[i]*bed[i];head=creat(roomlevel,room,bed); while(k==1) { printf(“ n欢迎光临 :n”); printf(“1:订房n2:退房n3:查询n4:退出系统n”); printf(“请输入您的选择:n”); scanf(“%d”,&n); switch(n) { case 1: Getin(head);break; case 2: Checkout(head);break; case 3: Display(head);break; case 4: k=0;break; default : printf(“Error!please input again:”);break; } } } 题目7:文本文件单词的检索及计数(学时:6) 要求编程建立一个文本文件,每个单词不包括空格及跨行,单词由字符序列构成且区分大小写,完成以下功能:统计给定单词在文本文件中出现的总次数、检索输出某单词在文本文件中首次出现的行号及位置。 #include }SW; void CreatTextFile(){ char filename[10],ch; FILE*fp; printf(“请输入所用的文件名:”);scanf(“n%s”,filename);fp=fopen(filename,“w”); printf(“请输入一段文字,以$号结束:n”);scanf(“%s”,&ch); while(ch!='$'){ fwrite(&ch,sizeof(ch),1,fp); } scanf(“%c”,&ch); } fclose(fp);void CountWord(){ FILE *fp;SW S,T;char ch;char filename[10]; int i=0,number=0; printf(“输入文本文件名:”); scanf(“%s”,filename);fp=fopen(filename,“r”); printf(“输入要统计计数的单词:”); scanf(“%s”,T.ch); while(!feof(fp)){ ch= fgetc(fp); if(ch==' ') { if(i!=0){ S.ch[i]=' ';i=0; } } } if(strcmp(S.ch,T.ch)==0)number++;else if(ch=='n'){ if(i!=0) } else { } S.ch[i]=ch;i++;{ S.ch[i]=' '; i=0; } if(strcmp(S.ch,T.ch)==0)number++;if(number==0)printf(“单词不在文本中 n”);else printf(“单词%s在文件%s中共出现了%d次:”,T.ch,filename,number); fclose(fp);} void SearchWord(){ FILE*fp;SW S,T;char filename[10]; int i,i_r,line,flag=0;char ch;printf(“n输入文本文件名:”); scanf(“%s”,filename);fp=fopen(filename,“r”);printf(“输入要统计计数的单词:”); scanf(“%s”,T.ch); i=i_r=0;line=1;while(!feof(fp)){ ch=fgetc(fp); if(ch==' ') { if(i!=0) { i_r++;S.ch[i]=' '; i=0; if(strcmp(T.ch,S.ch)==0){ printf(“%s单词第一次出现是在n”,T.ch,line,i_r); %d 行,%d列 flag=1; break; } } } else if(ch=='n') { if(i!=0) { i_r++;S.ch[i]=' '; if(strcmp(T.ch,S.ch)==0){ printf(“%s单词第一次出现是在n”,T.ch,line,i_r); flag=1; break; } i=0;i_r=0;line++; } else { line++;i_r=0; } } else { %d 行,%d列 S.ch[i]=ch;i++;} } if(flag==0) printf(“%s单词不在文本中n”,T.ch); fclose(fp);} int main(){ } CreatTextFile();CountWord();SearchWord(); 题目8:二叉树的遍历(学时:6) 二叉树以lson-rson链接方式存储,以菜单方式设计并完成功能任务:建立并存储树、输出前序遍历结果、输出中序遍历结果、输出后序遍历结果、交换左右子树、统计高度,其中对于中序、后序的遍历运算要求采用非递归方式。 #include b=NULL;else { b=(BTNode *)malloc(sizeof(BTNode)); b->data=ch; b->lchild=CreatBTree();//递归先序建立左子树 b->rchild=CreatBTree();//递归先序建立右子树 } return b;} void PreOrder(BTNode *b)//递归先序遍历二叉树函数 { if(b!=NULL) { printf(“%c ”,b->data); PreOrder(b->lchild); PreOrder(b->rchild); } } void InOrder(BTNode *b)//非递归中序遍历二叉树函数 { BTNode *stack[M],*p;int top=-1;if(b!=NULL){ p=b; while(top>-1||p!=NULL) { while(p!=NULL)//扫描p的所有左结点并入栈 { top++; stack[top]=p; } p=p->lchild;if(top>-1){ p=stack[top];//出栈访问结点 top--;printf(“%c ”,p->data); p=p->rchild;//扫描p的右结点 } } printf(“n”);} } void PostOrder(BTNode *b)//非递归后序遍历二叉树函数 { BTNode *stack[M],*p;int sign,top=-1;if(b!=NULL){ do { while(b!=NULL)// b所有左结点入栈 { top++; stack[top]=b; b=b->lchild; } p=NULL;// p指向栈顶前一个已访问结点 sign=1; //置b为已访问 while(top!=-1 && sign){ b=stack[top];//取出栈顶结点 if(b->rchild==p)//右孩子不存在或右孩子已访问则访问b { printf(“%c ”,b->data); top--;p=b;//p指向被访问的结点 } else { b=b->rchild;//b指向右孩子结点 sign=0;//置未访问标记 } } }while(top!=-1); printf(“n”);} } void change(BTNode *b) //左右子树交换(递归){ BTNode *r;r=(BTNode *)malloc(sizeof(BTNode));int f1=0,f2=0;if(b==0)return; //树为空时,跳出 if(b->lchild){ change(b->lchild); r->lchild=b->lchild; f1++; //有左子树,符号位不为空 } if(b->rchild){ change(b->rchild); r->rchild=b->rchild; f2++; //有右子树,符号位不为空 } if(f1==0)r->lchild=NULL; //否则,给中间变量赋空值 if(f2==0)r->rchild=NULL;if(f1||f2){ b->rchild=r->lchild; //左右子树交换 b->lchild=r->rchild;} } int max(int m,int n){ } if(m>n)return m;else return n;int count(BTNode *b) //计算树高(递归){ if(b==NULL) return 0;else return(1+max(count(b->lchild),count(b->rchild)));} int main() { BTNode *root = NULL; printf(“-----------------二叉树的遍历-----------------nn”); printf(“请按先序递归顺序创建二叉树(结束符 #):n”); root = CreatBTree(); printf(“n递归先序遍历结果:n”); PreOrder(root); printf(“n非递归中序遍历结果:n”); InOrder(root); printf(“非递归后序遍历结果:n”); PostOrder(root); printf(“n树高: %dn”,count(root));printf(“n左右子树交换位置:”); change(root); printf(“n递归先序遍历结果:n”); PreOrder(root); printf(“n非递归中序遍历结果:n”); InOrder(root); printf(“非递归后序遍历结果:n”); PostOrder(root); return 0; 题目9:创建二叉排序树(学时:3) 二叉排序树以lson-rson链接方式存储,编写能够通过键盘输入建立二叉排序树,并在建立完立即在屏幕显示中序遍历结果的程序。 #include typedef struct node { int data;struct node *lchild ,*rchild;}BSTNode,*BSTTree; //二叉排序树的插入(递归算法)void InsertBST(BSTTree *BST , int key){ } if((*BST)==NULL){ } else if((*BST)->data>key)//如果待插入的元素比根结点元素值小 InsertBST(&((*BST)->lchild),key);//插入在根结点的左子树(*BST)=(BSTNode*)malloc(sizeof(BSTNode));(*BST)->data=key;(*BST)->lchild=NULL;(*BST)->rchild=NULL;else InsertBST(&((*BST)->rchild),key);//插入在根结点的右子树上 //创建一棵二叉排序树 BSTTree CreateBSTTree(){ } //中序遍历 void InOrder(BSTTree BST){ if(BST!=NULL){ } InOrder(BST->lchild);printf(“%5d”,BST->data);InOrder(BST->rchild);BSTTree bst=NULL;int x;while(1){ } return bst; scanf(“%d”,&x);if(x==00)break;InsertBST(&bst,x);} void main(){ BSTTree BST; printf(“建立二叉排序树,请输入序列:n”); BST=CreateBSTTree(); printf(“中序遍历后输出的该序列为:”);InOrder(BST); printf(“n”); } 题目10:霍夫曼编码应用(学时:3) 假设一串由大写字母构成的电文,采用霍夫曼规则对其进行编码,以菜单方式设计并完成功能任务:建立霍夫曼树、霍夫曼编码生成、编码文件译码。 #include char ch; int parent,lchild,rchild;}HTNode; typedef struct{ char ch; char bits[n+1]; }CodeNode; typedef struct { char cha;int number;}COUNTER; int Init(HTNode ht[])//初始化函数,为每一个字母信息赋权值 { int i,s=1;COUNTER counter[27];char ch;printf(“请输入字符:n”);scanf(“%c”,&ch);counter[1].cha='A';counter[2].cha='B';counter[3].cha='C';counter[4].cha='D'; counter[5].cha='E';counter[6].cha='F';counter[7].cha='G';counter[8].cha='H';counter[9].cha='I';counter[10].cha='J';counter[11].cha='K';counter[12].cha='L';counter[13].cha='M';counter[14].cha='N';counter[15].cha='O';counter[16].cha='P';counter[17].cha='Q'; counter[18].cha='R'; counter[19].cha='S';counter[20].cha='T';counter[21].cha='U';counter[22].cha='V';counter[23].cha='W';counter[24].cha='X';counter[25].cha='Y';counter[26].cha='Z';for(i=1;i<=26;i++)counter[i].number=0;while(ch!=' '){ switch(ch){ case 'A':counter[1].number++;break;case 'B':counter[2].number++;break;case 'C':counter[3].number++;break;case 'D':counter[4].number++;break;case 'E':counter[5].number++;break;case 'F':counter[6].number++;break;case 'G':counter[7].number++;break;case 'H':counter[8].number++;break;case 'I':counter[9].number++;break;case 'J':counter[10].number++;break;case 'K':counter[11].number++;break;case 'L':counter[12].number++;break;case 'M':counter[13].number++;break;case 'N':counter[14].number++;break;case 'O':counter[15].number++;break; case 'P':counter[16].number++;break; case 'Q':counter[17].number++;break;case 'R':counter[18].number++;break;case 'S':counter[19].number++;break;case 'T':counter[20].number++;break;case 'U':counter[21].number++;break;case 'V':counter[22].number++;break;case 'W':counter[23].number++;break;case 'X':counter[24].number++;break; } } case 'Y':counter[25].number++;break;case 'Z':counter[26].number++;break;} scanf(“%c”,&ch);for(i=1;i<=26;i++){ } s=s-1;return s;if(counter[i].number!=0){ } ht[s].weight=counter[i].number;ht[s].ch=counter[i].cha;s++; void select(HTNode ht[],int q,int *p1,int *p2)//select函数 { int i,j,x=0,y=0;for(j=1;j<=q;++j){ } if(ht[j].parent==0){ } x=j;break;for(i=j+1;i<=q;++i){ } for(j=1;j<=q;++j){ } for(i=j+1;i<=q;++i){ if(ht[i].weight //选出第二小结点 if(ht[j].parent==0&&x!=j){ } y=j;break;if(ht[i].weight //选出最小结点 } } } if(x>y){ } else { } *p1=x;*p2=y;*p1=y;*p2=x;void huffman_setup(HTNode ht[],int s){ int i,a;int p1,p2;a=2*s-1;for(i=1;i<=a;i++){ if(i<=s){ } else { ht[i].weight=ht[i].weight; } } } ht[i].weight=0;ht[i].parent=ht[i].lchild=ht[i].rchild=0;for(i=s+1;i<=a;++i) //建立赫夫曼树 { } select(ht,i-1,&p1,&p2);ht[p1].parent=i;ht[p2].parent=i;ht[i].lchild=p1;ht[i].rchild=p2;ht[i].weight=ht[p1].weight+ht[p2].weight;void huffman_show(CodeNode hc[],HTNode ht[],int s)//给字符编码 { char q[n];int i,p,c,f;q[s-1]=' ';for(i=1;i<=s;i++) { p=s-1;for(c=i,f=ht[i].parent;f;c=f,f=ht[f].parent){ } } if(ht[f].lchild==c){ } else { } q[--p]='1';q[--p]='0';strcpy(hc[i].bits,&q[p]);} hc[i].ch=ht[i].ch;//-----------------编码 void huffman_code(CodeNode hc[]){ int i=1;char ch,ah;printf(“请输入编码的信息:n”);scanf(“%c”,&ch);printf(“编码是:n”);while(ch!=' '){ ah=hc[i].ch;while(ch!=ah){ } i++;ah=hc[i].ch;printf(“%s”,hc[i].bits);scanf(“%c”,&ch);i=1; } } //-----------------解码 void huffman_read(HTNode ht[],int s)//根据编码来返回字母信息 { int i,j,t;char b;t=2*s-1;i=t;printf(“进行解码n”);fflush(stdin);scanf(“%c”,&b);printf(“解码后的信息是:n”);while(b!=' '){ if(b=='0')i=ht[i].lchild;else i=ht[i].rchild;if(ht[i].lchild==0){ } } } printf(“%c”,ht[i].ch);j=i;i=t;scanf(“%c”,&b);int main(){ int flag=1,choice;int s,i;HTNode ht[m];CodeNode hc[n];printf(“霍夫曼树:n”);s=Init(ht);huffman_setup(ht,s);huffman_show(hc,ht,s);for(i=1;i<=s;i++){ } while(flag){ printf(“%c---> %sn”,hc[i].ch,hc[i].bits); } } printf(“请输入您想要进行的操作:n 1 编码n 2 解码n 3 退出n”);fflush(stdin);scanf(“%d”,&choice);switch(choice){ case 1: huffman_code(hc);printf(“n”);break; case 2: } huffman_read(ht,s);printf(“n”);break; case 3: flag=0;return 0;题目11:关键路径寻找(学时:6) 对于给定的一个工程施工图,该图以边为单位从键盘输入,编写能够找出该图的关键路径的程序。 #include #include int shoudsave = 0;int count1 = 0, count2 = 0, mark = 0, mark1 = 0;structtrain { };structman { };typedefstructnode { structtrain data;structnode * next;char num[10];char name[10];int bookNum;char num[10];char city[10];char takeoffTime[10];char receiveTime[10];int price;int bookNum;}Node, *Link;typedefstructpeople { structman data;structpeople*next;}bookMan, *bookManLink;void printInterface(){ puts(“********************************************************”);puts(“* Welcome to use the system of booking tickets *”);puts(“********************************************************”);puts(“* You can choose the operation: *”);puts(“* 1:Insert a train information *”);puts(“* 2:Inquire a train information *”);puts(“* 3:Book a train ticket *”); } puts(“* 4:Update the train information *”);puts(“* 5:Advice to you about the train *”);puts(“* 6:save information to file *”);puts(“* 7:quit the system *”);puts(“********************************************************”);/*添加一个火车信息*/ void InsertTraininfo(Linklinkhead){ structnode *p, *r, *s;char num[10];r = linkhead;s = linkhead->next;while(r->next!= NULL){ printf(“please input the number of the train(0-return)”);scanf(“%s”, num);if(strcmp(num, “0”)== 0){ } p =(structnode*)malloc(sizeof(structnode));strcpy(p->data.num, num);printf(“Input the city where the train will reach:”);scanf(“%s”, p->data.city);printf(“Input the time which the train take off:”);scanf(“%s”, p->data.takeoffTime);printf(“Input the time which the train receive:”);scanf(“%s”, &p->data.receiveTime);printf(“Input the price of ticket:”);scanf(“%d”, &p->data.price);printf(“Input the number of booked tickets:”);if(strcmp(s->data.num, num)== 0){ } s = s->next;printf(“the train '%s'has been born!n”, num);return;break;while(s)r = r->next;while(1) scanf(“%d”, &p->data.bookNum); p->next = NULL; r->next = p; r = p; shoudsave = 1;} } void printTrainInfo(structnode*p){ puts(“nThe following is the record you want:”);printf(“>>number of train: %sn”, p->data.num);printf(“>>city the train will reach: %sn”, p->data.city);printf(“>>the time the train take off: %snthe time the train reach: %sn”p->data.takeoffTime, p->data.receiveTime);printf(“>>the price of the ticket: %dn”, p->data.price);printf(“>>the number of booked tickets: %dn”, p->data.bookNum);} structnode * Locate1(Linkl, charfindmess[], charnumorcity[]){ Node*r;if(strcmp(numorcity, “num”)== 0){ r = l->next; while(r) { if(strcmp(r->data.num, findmess)== 0) return r; r = r->next; } } elseif(strcmp(numorcity, “city”)== 0){ r = l->next; while(r) { if(strcmp(r->data.city, findmess)== 0) return r; r = r->next; } } , } return 0;void QueryTrain(Linkl){ Node *p;int sel;char str1[5], str2[10];if(!l->next){ } printf(“Choose the way:n>>1:according to the number of train;n>>2:according to the scanf(”%d“, &sel);if(sel == 1){ } elseif(sel == 2){ printf(”Input the city:“);scanf(”%s“, str2);p = Locate1(l, str2, ”city“);if(p){ } printTrainInfo(p);printf(”Input the the number of train:“);scanf(”%s“, str1);p = Locate1(l, str1, ”num“);if(p){ } else { } mark1 = 1;printf(”nthe file can't be found!“);printTrainInfo(p);printf(”There is not any record!“);return;city:n”); } } else { } mark1 = 1;printf(“nthe file can't be found!”);void BookTicket(Linkl, bookManLinkk){ Node*r[10], *p;char ch, dem;bookMan*v, *h;int i = 0, t = 0;char str[10], str1[10], str2[10];v = k;while(v->next!= NULL)v = v->next;printf(“Input the city you want to go: ”);scanf(“%s”, &str);p = l->next;while(p!= NULL){ } printf(“nnthe number of record have %dn”, i);for(t = 0;t { printf(“ndo you want to book it?<1/0>n”);scanf(“%d”, &ch);if(ch == 1){ h =(bookMan*)malloc(sizeof(bookMan));printTrainInfo(r[t]);printf(“ntttSorry!Can't find the train for you!n”);if(i == 0)else if(strcmp(p->data.city, str)== 0){ } p = p->next;r[i] = p;i++; } } } printf(“Input your name: ”);scanf(“%s”, &str1);strcpy(h->data.name, str1);printf(“Input your id: ”);scanf(“%s”, &str2);strcpy(h->data.num, str2);printf(“Input your bookNum: ”);scanf(“%d”, &dem);h->data.bookNum = dem;h->next = NULL;v->next = h;v = h;printf(“nLucky!you have booked a ticket!”);getch();shoudsave = 1;bookMan*Locate2(bookManLinkk, charfindmess[]){ } /*修改火车信息*/ void UpdateInfo(Linkl){ Node*p;char findmess[20], ch;if(!l->next){ printf(“nthere isn't record for you to modify!n”);bookMan*r;r = k->next;while(r){ } return 0;if(strcmp(r->data.num, findmess)== 0){ } r = r->next;mark = 1;return r; } } return;else { } QueryTrain(l);if(mark1 == 0){ } else mark1 = 0;printf(“nDo you want to modify it?n”);getchar();scanf(“%c”, &ch);if(ch == 'y');{ } printf(“nInput the number of the train:”);scanf(“%s”, findmess);p = Locate1(l, findmess, “num”);if(p){ } else printf(“tttcan't find the record!”);printf(“Input new number of train:”);scanf(“%s”, &p->data.num);printf(“Input new city the train will reach:”);scanf(“%s”, &p->data.city);printf(“Input new time the train take off”);scanf(“%s”, &p->data.takeoffTime);printf(“Input new time the train reach:”);scanf(“%s”, &p->data.receiveTime);printf(“Input new price of the ticket::”);scanf(“%d”, &p->data.price);printf(“Input new number of people who have booked ticket:”);scanf(“%d”, &p->data.bookNum);printf(“nmodifying record is sucessful!n”);shoudsave = 1;void AdvicedTrains(Linkl){ } void SaveTrainInfo(Linkl){ FILE*fp;Node*p;int count = 0, flag = 1;fp = fopen(“D: rain.txt”, “wb”);if(fp == NULL){ } p = l->next;while(p){ if(fwrite(p, sizeof(Node), 1, fp)== 1){ } p = p->next;count++;printf(“the file can't be opened!”);return;Node*r;char str[10];int mar = 0;r = l->next;printf(“Iuput the city you want to go: ”);scanf(“%s”, str);while(r){ } if(mar == 0)printf(“ntttyou can't book any ticket now!n”);if(strcmp(r->data.city, str)== 0 && r->data.bookNum < 200){ } r = r->next;mar = 1;printf(“nyou can select the following train!n”);printf(“nnplease select the fourth operation to book the ticket!n”);printTrainInfo(r); } } else { } flag = 0;break;if(flag){ } fclose(fp);printf(“the number of the record which have been saved is %dn”, count);shoudsave = 0;void SaveBookmanInfo(bookManLinkk){ FILE*fp;bookMan*p;int count = 0, flag = 1;fp = fopen(“D:man.txt”, “wb”);if(fp == NULL){ } p = k->next;while(p){ } if(flag){ printf(“the number of the record which have been saved is %dn”, count);if(fwrite(p, sizeof(bookMan), 1, fp)== 1){ } else { } flag = 0;break;p = p->next;count++;printf(“the file can't be opened!”);return; } } shoudsave = 0;fclose(fp);int main(){ FILE*fp1, *fp2;Node*p, *r;char ch1, ch2;Link l;bookManLink k;bookMan*t, *h;int sel;l =(Node*)malloc(sizeof(Node));l->next = NULL;r = l;k =(bookMan*)malloc(sizeof(bookMan));k->next = NULL;h = k;fp1 = fopen(“D: rain.txt”, “ab+”);if((fp1 == NULL)){ } while(!feof(fp1)){ } fclose(fp1);fp2 = fopen(“D:man.txt”, “ab+”);if((fp2 == NULL)){ p =(Node*)malloc(sizeof(Node));if(fread(p, sizeof(Node), 1, fp1)== 1){ } p->next = NULL;r->next = p;r = p;count1++;printf(“can't open the file!”);return 0; } printf(“can't open the file!”);return 0;while(!feof(fp2)){ } fclose(fp2);while(1){ system(“cls”);printInterface();printf(“please choose the operation: ”);scanf(“%d”, &sel);system(“cls”);if(sel == 8){ } switch(sel)if(shoudsave == 1){ } printf(“nThank you!You are welcome toon”);break;getchar();printf(“nthe file have been changed!do you want to save it(y/n)?n”);scanf(“%c”, &ch1);if(ch1 == 'y' || ch1 == 'Y'){ } SaveBookmanInfo(k);SaveTrainInfo(l);t =(bookMan*)malloc(sizeof(bookMan));if(fread(t, sizeof(bookMan), 1, fp2)== 1){ } t->next = NULL;h->next = t;h = t;count2++; } } { case 1: } printf(“nplease press any key to continue.......”);getch();InsertTraininfo(l);break;QueryTrain(l);break;BookTicket(l, k);break;UpdateInfo(l);break;AdvicedTrains(l);break;SaveTrainInfo(l);SaveBookmanInfo(k);break;return 0;case 2: case 3: case 4: case 5: case 6: case 7: return 0;第四篇:数据结构经典题目及c语言代码总结
第五篇:c语言火车票管理系统基本代码