回 帖 发 新 帖 刷新版面

主题:帮忙翻译一段程序,多谢了!

帮忙翻译一段程序,多谢了!
本人学识尚浅  有一些函数还不认识  请高手帮帮忙  周日就急需完成!  多谢了 (格式类似下面第四行)

#include<stdio.h>            
#include<malloc.h>            
#include<conio.h>
#define ERROR 0               “定义ERROR为0”
#define OK 1                  
#define EQUAL 1                
#define OVERFLOW -1            
#define LIST_INIT_SIZE 100      
#define LISTINCREMENT 10        
typedef char ElemType;         

typedef struct 
{
  ElemType *elem;
  int length;
  int listsize;
}sqlist;
int init(sqlist *L)
{
  L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if(!L->elem) exit(OVERFLOW);
  L->length=0;
  L->listsize=LIST_INIT_SIZE;
  return OK;
}
int ListLength(sqlist *L)
{
  return L->length;
}

void GetElem(sqlist L,int i,ElemType *e)
{
  *e=L.elem[i];
}

int EqualList(ElemType *e1,ElemType *e2)
{
  if (e1==e2)
    return 1;
  else
    return 0;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{
  if (e1<e2)
    return 1;
  else
    return 0;
}
int LocateElem(sqlist *La,ElemType e,int type)
{
  int i;
  switch (type)
    {
      case EQUAL:
    for(i=0;i<La->length;i++)
      if(EqualList(&La->elem[i],&e))
        return 1;
    break;
      default:
    break;
    }
  return 0;
}

void MergeList(sqlist *La,sqlist *Lb,sqlist *Lc)
{
  ElemType *pa,*pb,*pc,*pa_last,*pb_last;
  pa=La->elem;pb=Lb->elem;
  Lc->listsize = Lc->length = La->length + Lb->length;
  pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType));
  if(!Lc->elem)   exit(OVERFLOW);
  pa_last = La->elem + La->length - 1;
  pb_last = Lb->elem + Lb->length - 1;
  while(pa<=pa_last && pb<=pb_last)
    {
      if(Less_EqualList(pa,pb))   *pc++=*pa++;
      else *pc++=*pb++;
    }
  while(pa<=pa_last) *pc++=*pa++;
  while(pb<=pb_last) *pc++=*pb++;
}

void UnionList(sqlist *La, sqlist *Lb)
{
  int La_len,Lb_len;
  int i;
  ElemType e;

  La_len=ListLength(La);  Lb_len=ListLength(Lb);
  for(i=0;i<Lb_len;i++)
    {
      GetElem(*Lb,i,&e);
      if(!LocateElem(La,e,EQUAL))
    ListInsert(La,++La_len,e);
    }

}

int ListInsert(sqlist *L,int i,ElemType e)
{
  ElemType *p,*q;
  if (i<1||i>L->length+1) return ERROR;
  q=&(L->elem[i-1]);
  for(p=&L->elem[L->length-1];p>=q;--p)
    *(p+1)=*p;
  *q=e;
  ++L->length;
  return OK;
}/*ListInsert Before i */
int listdelet(sqlist *l,int i,ElemType *e)
{
   ElemType  *p,*q;
    if ((i<1)||(i>l->length))  return 0;
    p=&(l->elem[i-1]);
    *e=*p;
   q=l->elem+l->length-1;
   for(++p;p<=q;++p)
   *(p-1)=*p;
   --l->length;
   return OK;
}
int printlist(sqlist L)
{
  int i;

  for(i=0;i<L.length;i++)
      printf("%c\t",L.elem[i]);
  printf("\n");
}



main()
{
  ElemType e;
  sqlist La,Lb,Lc;

  clrscr();

  printf("\n\n-------------------List Demo is running...----------------\n\n");
  printf("First is InsertList function.\n");
  init(&La);
  e='a';
  ListInsert(&La,1,e);
  e='b';
  ListInsert(&La,2,e);

  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

  e='f';
  ListInsert(&La,3,e);
   e='9';
   ListInsert(&La,4,e);
  printlist(La);
  listdelet(&La,4,&e);
  printf("\n\n%c\n\n",e);
  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

  init(&Lb);

  e='A';
  ListInsert(&Lb,1,e);
  e='C';
  ListInsert(&Lb,2,e);

  e='F';
  ListInsert(&Lb,3,e);

  printlist(Lb);
  printf("List B length now is  %d.\n\n",Lb.length);
  getch();

  MergeList(&La,&Lb,&Lc);
  printlist(Lc);
  getch();

  printf("Second is UnionList function.\n");
  printf("Now union List A and List B.....\n");
  UnionList(&La,&Lb);
  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

}

回复列表 (共2个回复)

沙发

里面却点东西吧?能运行吗?在我这里不行啊。。。。

板凳


程序有点错误,现在可以运行了,主要就是一些顺序表的操作(参看严蔚敏的数据结构),上面有讲解

#include<stdio.h> 
#include<stdlib.h>
#include<malloc.h>            
#include<conio.h>
#define ERROR 0               //定义ERROR为0
#define OK 1                  
#define EQUAL 1                
#define OVERFLOW -1            
#define LIST_INIT_SIZE 100      
#define LISTINCREMENT 10        
typedef char ElemType;         

typedef struct 
{
  ElemType *elem;
  int length;
  int listsize;
}sqlist;
//构造一个空的顺序表
int init(sqlist *L)
{
  L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if(!L->elem) 
      exit(OVERFLOW);
  L->length=0;
  L->listsize=LIST_INIT_SIZE;
  return OK;
}

int ListLength(sqlist *L)
{
  return L->length;
}

void GetElem(sqlist L,int i,ElemType *e)
{
  *e=L.elem[i];
}

int EqualList(ElemType *e1,ElemType *e2)
{
  if (e1==e2)
    return 1;
  else
    return 0;
}

int Less_EqualList(ElemType *e1,ElemType *e2)
{
  if (e1<e2)
    return 1;
  else
    return 0;
}
int LocateElem(sqlist *La,ElemType e,int type)
{
  int i;
  switch (type)
    {
      case EQUAL:
    for(i=0;i<La->length;i++)
      if(EqualList(&La->elem[i],&e))
        return 1;
    break;
      default:
    break;
    }
  return 0;
}

void MergeList(sqlist *La,sqlist *Lb,sqlist *Lc)
{
  ElemType *pa,*pb,*pc,*pa_last,*pb_last;
  pa=La->elem;pb=Lb->elem;
  Lc->listsize = Lc->length = La->length + Lb->length;
  pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType));
  if(!Lc->elem)   exit(OVERFLOW);
  pa_last = La->elem + La->length - 1;
  pb_last = Lb->elem + Lb->length - 1;
  while(pa<=pa_last && pb<=pb_last)
    {
      if(Less_EqualList(pa,pb))   *pc++=*pa++;
      else *pc++=*pb++;
    }
  while(pa<=pa_last) *pc++=*pa++;
  while(pb<=pb_last) *pc++=*pb++;
}

int ListInsert(sqlist *L,int i,ElemType e)
{
    ElemType *p,*q;
    if (i<1||i>L->length+1) 
        return ERROR;
    q=&(L->elem[i-1]);
    
    for(p=&L->elem[L->length-1];p>=q;--p)
        *(p+1)=*p;
    *q=e;
    ++L->length;
    return OK;
}/*ListInsert Before i */

void UnionList(sqlist *La, sqlist *Lb)
{
  int La_len,Lb_len;
  int i;
  ElemType e;

  La_len=ListLength(La);  Lb_len=ListLength(Lb);
  for(i=0;i<Lb_len;i++)
    {
      GetElem(*Lb,i,&e);
      if(!LocateElem(La,e,EQUAL))
    ListInsert(La,++La_len,e);
    }

}

int listdelet(sqlist *l,int i,ElemType *e)
{
   ElemType  *p,*q;
    if ((i<1)||(i>l->length))  return 0;
    p=&(l->elem[i-1]);
    *e=*p;
   q=l->elem+l->length-1;
   for(++p;p<=q;++p)
   *(p-1)=*p;
   --l->length;
   return OK;
}

void printlist(sqlist L)
{
  int i;

  for(i=0;i<L.length;i++)
      printf("%c\t",L.elem[i]);
  printf("\n");

}



void main()
{
  ElemType e;
  sqlist La,Lb,Lc;

  system("cls");
  printf("\n\n-------------------List Demo is running...----------------\n\n");
  printf("First is InsertList function.\n");
  init(&La);
  e='a';
  ListInsert(&La,1,e);
  e='b';
  ListInsert(&La,2,e);

  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

  e='f';
  ListInsert(&La,3,e);
   e='9';
   ListInsert(&La,4,e);
  printlist(La);
  listdelet(&La,4,&e);
  printf("\n\n%c\n\n",e);
  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

  init(&Lb);

  e='A';
  ListInsert(&Lb,1,e);
  e='C';
  ListInsert(&Lb,2,e);

  e='F';
  ListInsert(&Lb,3,e);

  printlist(Lb);
  printf("List B length now is  %d.\n\n",Lb.length);
  getch();

  MergeList(&La,&Lb,&Lc);
  printlist(Lc);
  getch();

  printf("Second is UnionList function.\n");
  printf("Now union List A and List B.....\n");
  UnionList(&La,&Lb);
  printlist(La);
  printf("List A length now is  %d.\n\n",La.length);
  getch();

}

我来回复

您尚未登录,请登录后再回复。点此登录或注册