C语言课程部分代码

来源:互联网 发布:悟 知乎 编辑:程序博客网 时间:2024/03/29 22:22

实验8584代码

#include
#include
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct
{
   QElemType *base; //初始化的动态分配存储空间
   int front; //头指针,若队列不空,指向队列头元素
   int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置
 }SqQueue;

Status InitQueue(SqQueue &Q)
{
   Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q.base)return ERROR;
   Q.front=Q.rear=0;
    returnOK;
}

Status EnQueue(SqQueue &Q,QElemType e)
{
 if((Q.rear+1)%MAXQSIZE==Q.front) returnERROR;
 Q.base[Q.rear]=e;
 Q.rear=(Q.rear+1)%MAXQSIZE;
 return OK;
}

Status DeQueue(SqQueue &Q,QElemType &e)
{
   if(Q.front==Q.rear) return ERROR;
   e=Q.base[Q.front];
   Q.front=(Q.front+1)%MAXQSIZE;
    returnOK;
}

Status GetHead(SqQueue Q, QElemType &e)
{
   if(Q.front==Q.rear) return ERROR;
   e=Q.base[Q.front];
    returnOK;
}

int QueueLength(SqQueue Q)
{
    return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

Status QueueTraverse(SqQueue Q)
{
// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
 int i;
 i=Q.front;
 if(Q.front==Q.rear) printf("The Queue isEmpty!");  //请填空
 else{
  printf("The Queue is: ");
  while(i!=Q.rear)    //请填空
  {
   printf("%d",Q.base[i]);   //请填空
   i=i+1;  //请填空
  }
 }
 printf("\n");
 return OK;
}

int main()
{
 int a;
    SqQueueS;
 QElemType x,e;
 if(InitQueue(S)==OK)   // 判断顺序表是否创建成功,请填空
 {
  printf("A Queue HasCreated.\n");
 }
 while(1)
 {
 printf("1:Enter \n2:Delete \n3:Get the Front\n4:Return the Length of the Queue\n5:Load theQueue\n0:Exit\nPlease choose:\n");
  scanf("%d",&a);
  switch(a)
  {
   case 1:scanf("%d",&x);
     if(EnQueue(S,x)==ERROR) printf("Enter Error!\n"); //判断入队是否合法,请填空
     else printf("The Element %d is Successfully Entered!\n", x);
     break;
   case 2:if(DeQueue(S,e)==ERROR) printf("Delete Error!\n"); //判断出队是否合法,请填空
     else printf("The Element %d is Successfully Deleted!\n", e);
     break;
   case 3:if(GetHead(S,e)==ERROR) printf("Get Head Error!\n"); // 判断GetHead是否合法,请填空
     else printf("The Head of the Queue is %d!\n", e);
     break;
   case 4:printf("The Length of the Queue is%d!\n",QueueLength(S));  //请填空
     break;
   case 5:QueueTraverse(S); //请填空
     break;
   case 0:return 1;
  }
 }
}


实验8585代码
#include"stdio.h"

void Eight(int n)
{
   if(n>7)
   Eight(n/8);
   printf("%d",n%8);
}

int main()
{
    intnum;
   scanf("%d",&num);
   Eight(num);
    return0;
}

实验8583代码

#include
#include
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10

typedef int SElemType; typedef int Status;

struct SqStack
{
    SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
    SElemType *top; 
    int stacksize;
};

Status InitStack(SqStack &S)
{
   S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)returnERROR;         S.top=S.base;
   S.stacksize=STACK_INIT_SIZE;
    returnOK;
}

Status Push(SqStack &S,SElemType e)
{
   if(S.top-S.base>=S.stacksize)
    {
   S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
    if(!S.base)returnERROR;       S.top=S.base+S.stacksize;
   S.stacksize=S.stacksize+STACKINCREMENT;
    }
   *S.top++=e;
    returnOK;
}

Status Pop(SqStack &S,SElemType &e)
{
   if(S.top==S.base) returnERROR;        e=*--S.top;
    returnOK;
}

Status GetTop(SqStack S,SElemType &e)
{
   if(S.top==S.base) returnERROR;    e=*(S.top-1);
    returnOK;
}

int StackLength(SqStack S)
{
    inti=0;
   while(S.top!=S.base)
    {
       i++;
       S.top--;
    }
    returni;
}

Status StackTraverse(SqStack S)
{
 SElemType *p = (SElemType*)malloc(sizeof(SElemType));
 p=S.top;
 if(S.top==S.base) printf("The Stack isEmpty!");
 else
 {
  printf("The Stack is: ");
  p--;
  S.base--;
  while(p!=S.base)
  {
   printf("%d ",*p);
   p--;
  }
 }
 printf("\n");
 return OK;
}

int main()
{
    int a;
    SqStack S;
    SElemType x, e;
    if(InitStack(S)==OK){    printf("AStack Has Created.\n");
    }
while(1)
 {
   printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of theStack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
 scanf("%d",&a);
  switch(a)
  {
   case 1:scanf("%d", &x);
       if(Push(S,x)==ERROR) printf("PushError!\n");        else printf("The Element %d is Successfully Pushed!\n", x);
       break;
  case 2: if(Pop(S,e)==ERROR)printf("PopError!\n");     else printf("The Element %d is Successfully Poped!\n", e);
      break;
  case 3: if(GetTop(S,e)==ERROR)printf("GetTopError!\n");     else printf("The Top Element is %d!\n", e);
       break;
   case 4:printf("The Length of the Stack is %d!\n",StackLength(S));
     break;
   case5:StackTraverse(S);
     break;
   case 0:return 1;
  }
 }
}



0 0
原创粉丝点击