数据结构课程设计题目
『壹』 大二数据结构课设,要求用C语言!拜托啦!必采纳,题目在下面
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#definelen(x)((int)log10(x)+1)
/*Nodeofthehuffmantree*/
structnode{
intvalue;
charletter;
structnode*left,*right;
};
typedefstructnodeNode;
/*81=8.1%,128=12.8%andsoon.The27thfrequencyisthespace.SourceisWikipedia*/
intenglishLetterFrequencies[27]={81,15,28,43,128,23,20,61,71,2,1,40,24,69,76,20,1,61,64,91,28,10,24,1,20,1,130};
/*findsandreturnsthesmallsub-treeintheforrest*/
intfindSmaller(Node*array[],intdifferentFrom){
intsmaller;
inti=0;
while(array[i]->value==-1)
i++;
smaller=i;
if(i==differentFrom){
i++;
while(array[i]->value==-1)
i++;
smaller=i;
}
for(i=1;i<27;i++){
if(array[i]->value==-1)
continue;
if(i==differentFrom)
continue;
if(array[i]->value<array[smaller]->value)
smaller=i;
}
returnsmaller;
}
/**/
voidbuildHuffmanTree(Node**tree){
Node*temp;
Node*array[27];
inti,subTrees=27;
intsmallOne,smallTwo;
for(i=0;i<27;i++){
array[i]=malloc(sizeof(Node));
array[i]->value=englishLetterFrequencies[i];
array[i]->letter=i;
array[i]->left=NULL;
array[i]->right=NULL;
}
while(subTrees>1){
smallOne=findSmaller(array,-1);
smallTwo=findSmaller(array,smallOne);
temp=array[smallOne];
array[smallOne]=malloc(sizeof(Node));
array[smallOne]->value=temp->value+array[smallTwo]->value;
array[smallOne]->letter=127;
array[smallOne]->left=array[smallTwo];
array[smallOne]->right=temp;
array[smallTwo]->value=-1;
subTrees--;
}
*tree=array[smallOne];
return;
}
/*.(usedtofacilitatearithmetic)*/
voidfillTable(intcodeTable[],Node*tree,intCode){
if(tree->letter<27)
codeTable[(int)tree->letter]=Code;
else{
fillTable(codeTable,tree->left,Code*10+1);
fillTable(codeTable,tree->right,Code*10+2);
}
return;
}
/*functiontocompresstheinput*/
voidcompressFile(FILE*input,FILE*output,intcodeTable[]){
charbit,c,x=0;
intn,length,bitsLeft=8;
intoriginalBits=0,compressedBits=0;
while((c=fgetc(input))!=10){
originalBits++;
if(c==32){
length=len(codeTable[26]);
n=codeTable[26];
}
else{
length=len(codeTable[c-97]);
n=codeTable[c-97];
}
while(length>0){
compressedBits++;
bit=n%10-1;
n/=10;
x=x|bit;
bitsLeft--;
length--;
if(bitsLeft==0){
fputc(x,output);
x=0;
bitsLeft=8;
}
x=x<<1;
}
}
if(bitsLeft!=8){
x=x<<(bitsLeft-1);
fputc(x,output);
}
/**/
fprintf(stderr,"Originalbits=%dn",originalBits*8);
fprintf(stderr,"Compressedbits=%dn",compressedBits);
fprintf(stderr,"Saved%.2f%%ofmemoryn",((float)compressedBits/(originalBits*8))*100);
return;
}
/*functiontodecompresstheinput*/
voiddecompressFile(FILE*input,FILE*output,Node*tree){
Node*current=tree;
charc,bit;
charmask=1<<7;
inti;
while((c=fgetc(input))!=EOF){
for(i=0;i<8;i++){
bit=c&mask;
c=c<<1;
if(bit==0){
current=current->left;
if(current->letter!=127){
if(current->letter==26)
fputc(32,output);
else
fputc(current->letter+97,output);
current=tree;
}
}
else{
current=current->right;
if(current->letter!=127){
if(current->letter==26)
fputc(32,output);
else
fputc(current->letter+97,output);
current=tree;
}
}
}
}
return;
}
/*function*/
voidinvertCodes(intcodeTable[],intcodeTable2[]){
inti,n,;
for(i=0;i<27;i++){
n=codeTable[i];
=0;
while(n>0){
=*10+n%10;
n/=10;
}
codeTable2[i]=;
}
return;
}
intmain(){
Node*tree;
intcodeTable[27],codeTable2[27];
intcompress;
charfilename[20];
FILE*input,*output;
buildHuffmanTree(&tree);
fillTable(codeTable,tree,0);
invertCodes(codeTable,codeTable2);
/*getinputdetailsfromuser*/
printf("Typethenameofthefiletoprocess:n");
scanf("%s",filename);
printf(":n");
scanf("%d",&compress);
input=fopen(filename,"r");
output=fopen("output.txt","w");
if(compress==1)
compressFile(input,output,codeTable2);
else
decompressFile(input,output,tree);
return0;
}
『贰』 数据结构课程设计
#include <stdio.h>
#include <malloc.h>
#include <windows.h>
#include <time.h>
#define MAXSIZE 100
typedef struct Ttime{
int t_hour;
int t_min;
int t_sec;
}Ttime;
typedef struct Mcar{
int number;
Ttime t[2];
}Mcar;
typedef struct{
Mcar elements[MAXSIZE];
int top;
}CarStop;
typedef struct node{
Mcar date;
struct node *next;
}LinkQueue;
typedef struct{
LinkQueue *front;
LinkQueue *rear;
}Queue;
void InitQueue(Queue *Q){
Q -> front = (LinkQueue *) malloc(sizeof(LinkQueue));
Q -> front -> next = NULL;
Q -> rear = Q -> front;
}
int IsEmptyH(Queue Q){
if(Q.front == Q.rear)
return 1;
else
return 0;
}
void EeQueue(Queue *Q, Mcar x){
Q -> rear -> next = (LinkQueue *)malloc(sizeof(LinkQueue));
Q -> rear = Q -> rear -> next;
Q -> rear -> date = x;
Q -> rear -> next = NULL;
}
Mcar DeQueue(Queue *Q){
LinkQueue *p;
Mcar y;
if(!(*Q)){
p = Q -> front -> next;
Q -> front -> next = p -> next;
if(p -> next == NULL)
Q -> rear = Q -> front;
y = p -> date;
free(p);
return y;
}
}
int IsEmpty(CarStop S){
if(S.top == 0)
return 1;
else
return 0;
}
int IsFull(CarStop S){
if(S.top == MAXSIZE)
return 1;
else
return 0;
}
int Init(CarStop *S){
S -> top = 0;
}
void PutIn(CarStop *S, Mcar D){
if(!IsFull(*S)){
S -> elements[S -> top] = D;
S -> top++;
}
else
printf("IS FULL");
}
Mcar Pop(CarStop *S){
if(!IsEmpty(*S)){
S -> top--;
return S -> elements[S -> top];
}
else
printf("IS NULL");
}
Mcar TopNum(CarStop S){
if(!IsEmpty(S)){
return S.elements[S.top - 1];
}
}
int serachOneCar(CarStop S, Mcar num){
int i = 0;
if(!IsEmpty(S)){
while(i <= S.top){
if(num.number == S.elements[i].number)
return 1;
i++;
}
}
else
return 0;
}
void getInOneCar(CarStop *CarStack, Queue *Q, Mcar car){
time_t secnow;
struct tm * tmptr;
time(&secnow);
tmptr = localtime(&secnow);
car.t[0].t_hour= tmptr -> tm_hour;
car.t[0].t_min = tmptr -> tm_min;
car.t[0].t_sec = tmptr -> tm_sec;
printf("please put in you car number:\n");
scanf("%d", &car.number);
if(!IsFull(*CarStack))
PutIn(CarStack, car);
else{
printf("car stop is full, please stay in bian\n");
EeQueue(Q, car);
}
printf("you car's intime is :[%d]:[%d]:[%d]\n\n\n", car.t[0].t_hour, car.t[0].t_min, car.t[0].t_sec);
}
void getOutOneCar(CarStop *CarStack, CarStop *CarStackL, Queue *Q, Mcar car){
Mcar lin;
time_t secnow;
struct tm * tmptr;
time(&secnow);
tmptr = localtime(&secnow);
car.t[1].t_hour= tmptr -> tm_hour;
car.t[1].t_min = tmptr -> tm_min;
car.t[1].t_sec = tmptr -> tm_sec;
printf("please put in you car number:\n");
scanf("%d", &car.number);
if(IsEmpty(*CarStack) || !serachOneCar(*CarStack, car))
printf("soory, there is no the car\n");
else{
while(1){
lin = Pop(CarStack);
if(lin.number == car.number)
break;
else
PutIn(CarStackL, lin);
}
while(!IsEmpty(*CarStackL)){
PutIn(CarStack, Pop(CarStackL));
}
if(!IsEmptyH(*Q))
PutIn(CarStack, DeQueue(Q));
printf("\n\nyou car's intime is :[%d]:[%d]:[%d]\n", lin.t[0].t_hour, lin.t[0].t_min, lin.t[0].t_sec);
printf("you car's out time is :[%d]:[%d]:[%d]\n", car.t[1].t_hour, car.t[1].t_min, car.t[1].t_sec);
int alltime = (car.t[1].t_hour - lin.t[0].t_hour) * 3600 + (car.t[1].t_min - lin.t[0].t_min) * 60 + (car.t[1].t_sec - lin.t[0].t_sec);
printf("you car's all time is second:[%d]\n", alltime);
}
}
int main(){
CarStop CarStack, CarStackL;
Queue Q;
LinkQueue *pp;
InitQueue(&Q);
Init(&CarStack);
Init(&CarStackL);
Mcar car;
//String caozuo;
int caozuo, i, j;
while(1){
printf("\t**************the car stop menu*************\n");
printf("\t\t***** 1. get in a car\n\n");
printf("\t\t***** 2. get out a car\n\n");
printf("\t\t***** 0. out the car stop\n\n");
printf("\t\t*****put in the else you can see all the car in the carstop\n\n");
printf("\t*****************************************\n");
printf("please put in you caozuo: \n");
scanf("%d", &caozuo);
switch(caozuo){
case 1: getInOneCar(&CarStack, &Q, car);
break;
case 2: getOutOneCar(&CarStack, &CarStackL, &Q, car);
break;
case 0: return 0; break;
default : break;
}
if(!IsEmpty(CarStack)){
i = 0;
printf("the all car is:\n");
while(i < CarStack.top){
printf("the [%d] is %d\n",i + 1, CarStack.elements[i].number);
i++;
}
}
else
printf("the car stop IS NULL\n");
printf("****************************\n");
if(!IsEmptyH(Q)){
j = 1;
pp=Q.front;
printf("the bian's car is:\n");
while(pp!=Q.rear){
printf("the [%d] wait car is %d\n", j, pp -> next -> date.number);
pp = pp -> next;
j++;
}
}
else
printf("the bian is NULL\n");
printf("****************************\n");
system("pause");
system("cls");
}
}
C语言编译通过
『叁』 数据结构课程设计
这个是程序:
#include "string.h"
#define NULL 0
unsigned int key;
unsigned int key2;
int *p;
struct node
{
char name[20],address[20];
char num[11];
struct node *next;
}
**phone;
**nam;
**address;
typedef struct node *pnode;
typedef struct node *mingzi;
void hash(char num[11])
{
int i = 3;
key=(int)num[2];
while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
key=key%17;
}
void hash2(char name[8])
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2+=(int)name[i];
i++;
}
Key2=key2%17;
}
struct node* input()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp = new node;
temp->next=NULL;
printf("qing shu ru xing ming(no more then 8 chars) :\n");
scanf("%s",&temp->name);
printf"qing shu ru dian hua hao ma(no more then 11 numbers):\n");
scanf("%s",&temp->num);
printf("qing shu ru di (no more then 20 chars):\n");
scanf("%s",&temp->address);
return temp;
}
int add()
{
struct node *newphone;
struct node *newname;
newphone=input();
printf("ni de ji lu shi");
if(find(newphone->num))
{
printf("hao ma yi jing cun zai bu nene zai tian jia.\n");
return 0;
}
if(find2(newphone->name))
{
printf("xing ming yi jing cun zai bu neng zai tian jia.\n");
return 0;
}
printf("%s_%s_%s\n",newphone->name,newphone->address,newphone->num);
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
hash(newphone->num);
hash2(newname->name);
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}
int find(char num[11])
{
int isfind=0;
struct node *q;
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num) ==0)
{
isfind=1;
break;
}
q=q->next;
}
if(isfind)
printf("%s_%s_%s\n",q->name,q->address,q->num);
return isfind;
}
int find2(char name[8])
{
int isfind=0;
hash2(name);
struct node *q=nam[key2]->next;
while(q!= NULL)
{
if(strcmp(name,q->name)==0)
{
isfind=1;
break;
}
q=q->next;
}
if(isfind)
printf("%s_%s_%s\n",q->name,q->address,q->num);
return isfind;
}
void create()
{
int i;
phone = (pnode*)malloc(13*sizeof(pnode));
for(i=0;i<13;i++)
{
phone[i]=(struct node*)malloc(sizeof(struct node));
phone[i]->next=NULL;
}
}
void create2()
{
int i;
nam = (mingzi*)malloc(13*sizeof(mingzi));
for(i=0;i<13;i++)
{
nam[i]=(struct node*)malloc(sizeof(struct node));
nam[i]->next=NULL;
}
}
void list()
{
int i;
struct node *p;
for(i=0;i<13;i++)
{
p=phone[i]->next;
while(p)
{
printf("%s_%s_%s\n",p->name,p->address,p->num);
p=p->next;
}
}
}
void list2()
{
int i;
struct node *p;
for(i=0;i<13;i++)
{
p=nam[i]->next;
while(p!=NULL)
{
printf("%s_%s_%s\n",p->name,p->address,p->num);
p=p->next;
}
}
}
void menu()
{
printf("0.tian jia ji lv.\n");
printf("2.cha zhao ji lu.\n");
printf("3.xing ming lie biao.\n");
printf("4.hao ma lie biao.\n");
printf("5.qing chu suo you ji lu.\n");
printf("6.tui chu xi tong.\n");
}
int main()
{
char num[11];
char name[8];
int sel;
create();
create2();
while(1)
{
menu();
scanf("%d",&sel);
if(sel==3)
{
char b;
printf("Input 9 to search with number,8 to name.\n");
b=getch();
if(b=='9')
{
printf("Please input the number:\n");
scanf("%s",&num);
if(!find(num))
printf("Record not exist.\n");
}
else if(b=='8')
{
printf("Please input the name:\n");
scanf("%s",&name);
if(!find(name))
printf("Record not exist.\n");
}
else
{
printf("Wrong input!Please input again!\n");
continue;
}
}
else if(sel==2)
{
printf("All record with name:\n");
list2();
}
else if(sel==0)
{
printf("Please input the information:\n");
add();
}
else if(sel==4)
{
printf("All record with number:\n");
list();
}
else if(sel==5)
{
create();
create2();
printf("All records have been removed.\n");
}
else if(sel==6)
{printf("7");
break;
}
}
return 0;
}
『肆』 数据结构课程设计题目:猴子选大王问题
我使用了另一种方法,不需要构建环形链表也可以,只需要用一个数组标记已离开的猴子即可,相对比较简单。这个程序输出猴子离开的顺序,最后输出的即为大王。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main()
{
int m,n;
int *not_in;
int pos;
int i;
int j;
printf("m n = ");
scanf("%d%d", &m, &n);
assert(m > 0);
assert(n > 0);
not_in = (int *)malloc(sizeof(int)*(m + 1));
memset(not_in, 0, sizeof(int)*(m + 1));
pos = m;
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
do
{
pos++;
if (pos == m + 1)
pos = 1;
}
while (not_in[pos]);
}
printf("%d ", pos);
not_in[pos] = 1;
}
printf("\n");
return 0;
}
『伍』 有没有谁做过数据结构课程设计,题目为:
题目1 马踏棋盘问题
#include "stdio.h"
#define M 70
#define FALSE 0
#define TRUE 1
typedef struct node
{
int vec,x,y;
struct node *link;
}listnode;
#define MAXSIZE 70
typedef struct
{
int stack[MAXSIZE];
int top;
}seqstack;
seqstack *s;
void setnull(seqstack *s)
{
s->top=-1;
}
int push(seqstack *s,int x)
{
if(s->top>=MAXSIZE-1)
{
printf("stack overflow!\n");
return FALSE;
}
else
{
s->stack[++s->top]=x;/*栈顶指针上移,数据元素入栈*/
return TRUE;
}
}
int pop(seqstack *s)/*出当前栈s的栈顶元素*/
{ int p;
if(s->top<0)
{
printf("stack empty!\n");/* 栈空,返回空值*/
return NULL;
}
else
{
s->top--;
return(s->stack[s->top+1]);
}/*由于return语句的特点,必须先使 top减1,然后再执行return语句。而此时栈顶元素的表示应该为s->top+1*/
}
void creatlist(listnode ag[])
{
listnode *p;
int i,j,x,y,x1,x2,y1,y2,k;
k=1;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
ag[k].x=i;
ag[k].y=j;
ag[k].vec=8*ag[k].x+ag[k].y-8;
ag[k].link=NULL;
printf("%d(%d,%d)",ag[k].vec,ag[k].x,ag[k].y);
k++;
}
printf("\n");
}
for(i=1;i<=64;i++)
{
/*printf("*%d*",i); */
setnull(s);
x1=ag[i].x;
y1=ag[i].y;
if((x1+1>0)&&(y1+2>0)&&(x1+1<=8)&&(y1+2<=8))
{
x2=x1+1;
y2=y1+2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1+1>0)&&(y1-2>0)&&(x1+1<=8)&&(y1-2<=8))
{
x2=x1+1;
y2=y1-2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-1>0)&&(y1+2>0)&&(x1-1<=8)&&(y1+2<=8))
{
x2=x1-1;
y2=y1+2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-1>0)&&(y1-2>0)&&(x1-1<=8)&&(y1-2<=8))
{
x2=x1-1;
y2=y1-2;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1+2>0)&&(y1-1>0)&&(x1+2<=8)&&(y1-1<=8))
{
x2=x1+2;
y2=y1-1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1+2>0)&&(y1+1>0)&&(x1+2<=8)&&(y1+1<=8))
{
x2=x1+2;
y2=y1+1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-2>0)&&(y1+1>0)&&(x1-2<=8)&&(y1+1<=8))
{
x2=x1-2;
y2=y1+1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
if((x1-2>0)&&(y1-1>0)&&(x1-2<=8)&&(y1-1<=8))
{
x2=x1-2;
y2=y1-1;
j=8*x2+y2-8;
/*printf("%d ",j);*/
push(s,j);
}
do
{
j=pop(s);
x2=ag[j].x;
y2=ag[j].y;
p=(listnode *)malloc(sizeof(listnode));
p->vec=j;
p->x=x2;
p->y=y2;
p->link=ag[i].link;
ag[i].link=p;
/*printf(" %d(%d,%d) ",p->vec,p->x,p->y);*/
p=(listnode *)malloc(sizeof(listnode));
p->vec=i;
p->x=x1;
p->y=y1;
p->link=ag[j].link;
ag[j].link=p;
/*printf(" %d(%d,%d) ",p->vec,p->x,p->y);*/
} while(s->top>=0);
}
}
void dfs(listnode ag[],int v,int flag[])
{
listnode *p;
int i;
flag[v]=1;
printf("(%d,%d)",ag[v].x,ag[v].y);
p=ag[v].link;
while(p!=NULL)
{
i=p->vec;
if(flag[i]==0)
dfs(ag,i,flag);
p=p->link;
}
}
/*void blt(listnode ag[M],int n)
{
int i;
int flag[M];
for(i=1;i<=n;i++)
flag[i]=0;
for(i=1;i<=n;i++)
if(flag[i]==0)
dfs(ag,i,flag);
}*/
void main()
{
listnode ag[M];
int flag[M];
creatlist(ag);
dfs(ag,28,flag);
}
『陆』 数据结构课程设计
#include "stdio.h"
#include "time.h"
#define MAXNUM 5 //停车场车位数
#define PRICE 2.0 //每小时收费
typedef struct car //定义车的结构体
{
char num[10]; //车牌号(最多10个字节)
struct tm intime; //进入时间
struct tm outtime; //离开时间
int expense; //费用
int length; //停车时长
int position; //停车位
}CAR;
typedef struct //栈结构
{
CAR car[MAXNUM]; //车辆信息
int top; //栈顶指针
} SeqStack;
void StackInit(SeqStack * s) //初始化栈
{
s->top = -1;
}
int StackIsEmpty(SeqStack * s) //判断栈是否为空
{
if (s->top == -1)
return 1;
else
return 0;
}
int StackIsFull(SeqStack *s) //判断栈是否为满
{
if (s->top == MAXNUM - 1)
return 1;
else
return 0;
}
void StackPush(SeqStack *s, CAR car) // 进栈
{
if (!StackIsFull(s)) //若栈未满
{
s->top++; //修改栈顶指针
s->car[s->top] = car; //将车辆信息入栈
}
}
CAR StackPop(SeqStack *s) //出栈
{
CAR car;
if (s->top != -1) //栈不为空
{
car = s->car[s->top];
s->top--;
return car;
}
}
CAR StackGetTop(SeqStack * s) //取栈顶元素
{
CAR car;
if (s->top != -1)
{
car = s->car[s->top];
return car;
}
}
//队列链表
typedef struct carnode //定义链队列的节点
{
CAR data;
struct carnode *next;
}CarNodeType;
typedef struct //链队列
{
CarNodeType *head; //头指针
CarNodeType *rear; //尾指针
}CarChainQueue;
void ChainQueueInit(CarChainQueue * q) //初始化链队列接点性质
{
if(!(q->head = (CarNodeType *) malloc(sizeof(CarNodeType))))
{
printf("内存分配失败!\n");
exit(0);
}
q->rear = q->head; //头尾指针相同
q->head->next = NULL; //头尾指针后下一个节点为空
q->rear->next = NULL;
}
void ChainQueueIn(CarChainQueue * q, CAR car) //入队列
{
CarNodeType *p;
if(!(p = (CarNodeType *) malloc(sizeof(CarNodeType))))
{
printf("内存分配失败!\n");
exit(0);
}
p->data = car;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
CAR ChainQueueOut(CarChainQueue * q) //出队列
{
CarNodeType *p;
CAR car;
if (q->head != q->rear)
{
p = q->head->next;
if (p->next == NULL)
{
car = p->data;
q->rear = q->head;
free(p);
} else
{
q->head->next = p->next;
car = p->data;
free(p);
}
return car;
}
}
int ChainQueueIsEmpty(CarChainQueue * q) //判断链队列是否为空
{
if (q->rear == q->head) //若队首等于列尾
return 1; //返回空
else
return 0; //返回非空
}
void separator(int n,char ch,char newline) //输出多个字符
{
int i;
for(i=0;i<n;i++)
printf("%c",ch);
if(newline==1)
printf("\n");
}
void PrintDate(struct tm gm_date)
{
printf("%d/%d %02d:%02d:%02d\n", gm_date.tm_mon,gm_date.tm_mday,gm_date.tm_hour+8, gm_date.tm_min, gm_date.tm_sec);
}
void ShowPark(SeqStack *s) //查看车位状态
{
int i;
struct tm gm_date;
printf("\n车位使用情况\n");
separator(40,'_',1);
if (StackIsEmpty(s)) //若栈是空
printf("停车场内已没有车辆!\n");
else
{
printf("位置\t车牌号\t进站时间\n");
for (i = 0; i <= s->top; i++)
{
printf("%d\t", s->car[i].position);
printf("%s\t", s->car[i].num);
PrintDate(s->car[i].intime); //输出日期
}
printf("\t\t\t共%d辆", s->top + 1);
if (s->top == MAXNUM - 1)
printf("(已满)\n");
else
printf("(还可停放放%d辆)\n", MAXNUM - 1 - s->top);
printf("\n");
}
separator(40,'_',1);
}
void ShowAisle(CarChainQueue * q)//显示过道上车辆的情况
{
if (!ChainQueueIsEmpty(q)) //若队列不为空
{
CarNodeType *p;
p = q->head->next; //队列中的第1辆车
printf("\n\n过道使用情况\n");
separator(30,'_',1);
printf("车牌\t进入时间\n");
while (p!= NULL) //队列不为空
{
printf("%s\t", p->data.num);
PrintDate(p->data.intime);; //显示该辆车的信息
p = p->next; //下一辆
}
} else
printf("\n过道上没有车在等待\n");
separator(30,'_',1);
printf("\n\n");
}
void ShowAll(SeqStack *s, CarChainQueue *q) //查看车位和过道使用情况
{
ShowPark(s); //显示车位使用情况
ShowAisle(q); //显示过道使用情况
}
void InPark(SeqStack * s, CarChainQueue * q) //车辆进入停车场
{
CAR car;
struct tm *gm_date;
time_t seconds;
time(&seconds);
gm_date = gmtime(&seconds);;
printf("\n车牌号:");
scanf("%s",&car.num);
car.intime=*gm_date; //进入停车场的时间
if (!StackIsFull(s) && ChainQueueIsEmpty(q)) //如果车位未占完,且过道上没有车
{
car.position = (s->top) + 2; //车位号
StackPush(s, car); //车辆直接进入车位
ShowPark(s); //输出现在停车场的情况
}
else if (StackIsFull(s) || !ChainQueueIsEmpty(q)) //如果车位已满,过道上还有车,则必须放在过道上
{
printf("提示:车位满,只有先停放在过道中。\n");
car.position = MAXNUM;
ChainQueueIn(q, car); //停放在过道
ShowPark(s); //显示车位的情况
ShowAisle(q); //输出过道上的情况
}
}
void PrintRate(CAR *car) //离开时输出费用等情况
{
printf("\n\n 账单\n" );
separator(30,'_',1);
printf("车牌:%s\n", car->num);
printf("停车位置:%d\n", car->position);
printf("进入时间:");
PrintDate(car->intime);
printf("离开时间:");
PrintDate(car->outtime);
printf("停车时间(秒):%d\n", car->length);
printf("费用(元):%d\n", car->expense);
separator(30,'_',1);
printf("\n\n");
}
void OutPark(SeqStack *s, CarChainQueue *q) //车出站出当时过道上的情况放在过道上直接进入车站
{
struct tm *gm_date;
time_t seconds;
SeqStack p; //申请临时放车的地方
StackInit(&p);
char nowtime[10];
int i, pos;
CAR car;
if (StackIsEmpty(s)) //如果车位中没有车辆停放
{
printf("所有车位都为空,没有车辆需要离开!\n");
}
else
{
printf("现在车位使用情况是:\n");
ShowPark(s); //输出车位使用情况
printf("哪个车位的车辆要离开:");
scanf("%d", &pos);
if (pos > 0 && pos <= s->top + 1) //输入车位号正确
{
for (i = s->top + 1; i > pos; i--) //在将pos车位之后停的车放入临时栈,以使pos车位的车出来
{
car = StackPop(s); //出栈
car.position = car.position - 1;//修改车位号
StackPush(&p, car); //将车辆放入临时栈
}
car = StackPop(s); //将位于pos车位的车辆出栈
time(&seconds);
gm_date = gmtime(&seconds); //获取当前时间
car.outtime=*gm_date;//离开时间
car.length=mktime(&car.outtime)-mktime(&car.intime); //停车场中停放时间
car.expense=(car.length/3600+1)*PRICE; //费用
PrintRate(&car); //输出车出站时的情况---进入时间,出站时间,原来位置,花的费用等
while (!StackIsEmpty(&p)) //将临时栈中的车重新进入车位
{
car = StackPop(&p); //从临时栈中取出一辆车
StackPush(s, car); //将车进入车位
}
while(!StackIsFull(s) && !ChainQueueIsEmpty(q)) //如果车位未满, 且过道上还有车
{
car = ChainQueueOut(q); //将最先停在过道中的车辆进入车位
time(&seconds);
gm_date = gmtime(&seconds); //获取当前时间
car.intime = *gm_date;//保存进入车位的时间
StackPush(s, car); //将车辆进入车位
}
}
else //车位号输入错误
{
printf("车位号输入错误,或该车位没有车辆!\n");
}
}
}
int main()
{
SeqStack Park; //停车场栈
CarChainQueue Aisle; //过道链表
StackInit(&Park);
ChainQueueInit(&Aisle);
char choice;
do{
printf("\n\n");
separator(10,' ',0);
printf("停车场管理\n");
separator(30,'_',1);
printf("1.车辆进入\n");
printf("2.车辆离开\n");
printf("3.查看停车场情况\n");
printf("0.退出系统\n");
separator(56,'_',1);
printf("提示:本停车场有%d个车位,车位停满后的车辆将停放在过道上。\n",MAXNUM);
printf("本停车场时计费,收费标准:%.2f元/小时,过道上不收费。\n\n",PRICE);
printf("\n选择操作(0~3):");
fflush(stdin);
choice=getchar();
switch (choice)
{
case '1': //车辆进入
InPark(&Park,&Aisle);
break;
case '2': //车辆离开
OutPark(&Park,&Aisle);
break;
case '3':
ShowAll(&Park,&Aisle);
break;
}
}while(choice!='0');
return 0;
}
『柒』 数据结构课程设计题目,图的建立以及遍历。
//图的遍历算法程序
//图的遍历是指按某条搜索路径访问图中每个结点,使得每个结点均被访问一次,而且仅被访问一次。图的遍历有深度遍历算法和广度遍历算法,程序如下:
#include <iostream>
//#include <malloc.h>
#define INFINITY 32767
#define MAX_VEX 20 //最大顶点个数
#define QUEUE_SIZE (MAX_VEX+1) //队列长度
using namespace std;
bool *visited; //访问标志数组
//图的邻接矩阵存储结构
typedef struct{
char *vexs; //顶点向量
int arcs[MAX_VEX][MAX_VEX]; //邻接矩阵
int vexnum,arcnum; //图的当前顶点数和弧数
}Graph;
//队列类
class Queue{
public:
void InitQueue(){
base=(int *)malloc(QUEUE_SIZE*sizeof(int));
front=rear=0;
}
void EnQueue(int e){
base[rear]=e;
rear=(rear+1)%QUEUE_SIZE;
}
void DeQueue(int &e){
e=base[front];
front=(front+1)%QUEUE_SIZE;
}
public:
int *base;
int front;
int rear;
};
//图G中查找元素c的位置
int Locate(Graph G,char c){
for(int i=0;i<G.vexnum;i++)
if(G.vexs[i]==c) return i;
return -1;
}
//创建无向网
void CreateUDN(Graph &G){
int i,j,w,s1,s2;
char a,b,temp;
printf("输入顶点数和弧数:");
scanf("%d%d",&G.vexnum,&G.arcnum);
temp=getchar(); //接收回车
G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配顶点数目
printf("输入%d个顶点.\n",G.vexnum);
for(i=0;i<G.vexnum;i++){ //初始化顶点
printf("输入顶点%d:",i);
scanf("%c",&G.vexs[i]);
temp=getchar(); //接收回车
}
for(i=0;i<G.vexnum;i++) //初始化邻接矩阵
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=INFINITY;
printf("输入%d条弧.\n",G.arcnum);
for(i=0;i<G.arcnum;i++){ //初始化弧
printf("输入弧%d:",i);
scanf("%c %c %d",&a,&b,&w); //输入一条边依附的顶点和权值
temp=getchar(); //接收回车
s1=Locate(G,a);
s2=Locate(G,b);
G.arcs[s1][s2]=G.arcs[s2][s1]=w;
}
}
//图G中顶点k的第一个邻接顶点
int FirstVex(Graph G,int k){
if(k>=0 && k<G.vexnum){ //k合理
for(int i=0;i<G.vexnum;i++)
if(G.arcs[k][i]!=INFINITY) return i;
}
return -1;
}
//图G中顶点i的第j个邻接顶点的下一个邻接顶点
int NextVex(Graph G,int i,int j){
if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum){ //i,j合理
for(int k=j+1;k<G.vexnum;k++)
if(G.arcs[i][k]!=INFINITY) return k;
}
return -1;
}
//深度优先遍历
void DFS(Graph G,int k){
int i;
if(k==-1){ //第一次执行DFS时,k为-1
for(i=0;i<G.vexnum;i++)
if(!visited[i]) DFS(G,i); //对尚未访问的顶点调用DFS
}
else{
visited[k]=true;
printf("%c ",G.vexs[k]); //访问第k个顶点
for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i))
if(!visited[i]) DFS(G,i); //对k的尚未访问的邻接顶点i递归调用DFS
}
}
//广度优先遍历
void BFS(Graph G){
int k;
Queue Q; //辅助队列Q
Q.InitQueue();
for(int i=0;i<G.vexnum;i++)
if(!visited[i]){ //i尚未访问
visited[i]=true;
printf("%c ",G.vexs[i]);
Q.EnQueue(i); //i入列
while(Q.front!=Q.rear){
Q.DeQueue(k); //队头元素出列并置为k
for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w))
if(!visited[w]){ //w为k的尚未访问的邻接顶点
visited[w]=true;
printf("%c ",G.vexs[w]);
Q.EnQueue(w);
}
}
}
}
//主函数
void main(){
int i;
Graph G;
CreateUDN(G);
visited=(bool *)malloc(G.vexnum*sizeof(bool));
printf("\n广度优先遍历: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
DFS(G,-1);
printf("\n深度优先遍历: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
BFS(G);
printf("\n程序结束.\n");
}
输出结果为(红色为键盘输入的数据,权值都置为1):
输入顶点数和弧数:8 9
输入8个顶点.
输入顶点0:a
输入顶点1:b
输入顶点2:c
输入顶点3:d
输入顶点4:e
输入顶点5:f
输入顶点6:g
输入顶点7:h
输入9条弧.
输入弧0:a b 1
输入弧1:b d 1
输入弧2:b e 1
输入弧3:d h 1
输入弧4:e h 1
输入弧5:a c 1
输入弧6:c f 1
输入弧7:c g 1
输入弧8:f g 1
广度优先遍历: a b d h e c f g
深度优先遍历: a b c d e f g h
程序结束.