加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

三元组基本操作的实现 三例C代码

(2008-10-29 03:58:44)
标签:

三元组

抽象数据类型

c语言编程

turbo

c2.0环境实现

it

分类: 数据结构C语言版编程实例

实验一:抽象数据类型三元组基本操作的实现(2学时)

本次实验的主要目的在于帮助学生熟悉抽象数据类型的表示和实现方法。

(一) 问题描述

设计一个实现三元组基本操作的程序。

(二) 基本要求

1.实现三元组的构造、取值、修改、有序判断、最大和最小值求解和销毁。

每个功能应用一个独立的函数实现。

2.自己设计菜单界面,可用最简单的DOS菜单(黑屏上各种功能各占一行,前有序号,可选)

3.各模块进入前应实现刷屏(用clrscr()函数即可)

(三) 测试数据

自行拟定测试数据。尽量保证测试完全。

(四) 实现提示

注意在C语言中的值调用方式及指针的使用。

 

 

本文代码均在turbo C 2.0的环境下编译通过并成功运行得到正确结果。

 

初步写成的抽象数据类型三元组个基本操作的实现代码:

#define TRUE 1     
#define FALSE 0
#define  OK  1
#define ERROR 0

#define OVERFLOW -2   //函数操作结果状态代码
#include<stdio.h>      
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#define NULL 0
typedef int Status;      
typedef int ElemType;
typedef ElemType *Triplet;
void menu()
{ clrscr();
  printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *\n");
  printf("\n***** Init-1  Des-2  Get-3   Put-4  IsAsc-5   IsDsc-6  Max-7  Min-8 *****\n");
  printf("\n** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
  gotoxy(15,10);printf("Operation:_\n");       
  gotoxy(1,20);      
  printf("\n*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
  printf("\n* Enter a operation code:1,2,3,4,5,6,7,8 *\n");
  printf("\n**** **** **** **** **** **** **** **** **** **** **** **** ****\n");
   }
Status InitTriplet(Triplet *H,ElemType V1,ElemType V2,ElemType V3)   
{
 *H=(ElemType*)malloc(3*sizeof(ElemType));
  if(!*H)exit(OVERFLOW);
 (*H)[0]=V1;(*H)[1]=V2;(*H)[2]=V3;
  return OK;
}

void Print(Triplet *H)               
{
  int i;
  ElemType elem,*p=&elem;
  if(*H==NULL)printf("Triplet NULL!");
  else
     for(i=1;i<=3;i++)
 {if(i>=1&&i<=3)*p=(*H)[i-1];
  printf("No.%d=%d\n",i,elem);
 }
}
Status DestroyTriplet(Triplet *H)         
{
  free(*H);*H=NULL;
  return OK;
}
Status Get(Triplet *H,int i,ElemType *e)     
{
   if(i<1||i>3){printf("not find!\n");return ERROR;}
    *e=(*H)[i-1];
    printf("%d\n",*e);
  return OK;
}
Status Put(Triplet *H,int i,ElemType e) 
{
  if(i<1||i>3)return ERROR;
  (*H)[i-1]=e;
  return OK;
}
Status IsAscending(Triplet H)      
{
  if(H[0]<=H[1]&&H[1]<=H[2]){printf(" The Triplet is isascending!\n"); return OK;}

  else {printf("the Triplet is not isascending!\n");return FALSE;}
}
Status IsDscending(Triplet H)         
{
  if((H[0]>=H[1])&&H[1]>=H[2]){printf("it is isdscending!\n");return OK;}
  else{printf("it is not isdscending!\n");return ERROR;}
}
main()
{ Triplet T; int i;
  ElemType e1,e2,e3,e,*p=&e;
  menu();                     
  printf("input three elem:\n");
  scanf("%d%d%d",&e1,&e2,&e3);               
  InitTriplet(T,e1,e2,e3);  clrscr();            
  Print(T);                                      
  printf("input which one you want to know :\n");     
  scanf("%d",&i);
  Get(T,i,e);                 
  printf("input which one you want to change and the new elem:\n");
  scanf("%d%d",&i,&e);
  Put(T,i,e);              
  Print(T);           
  IsAscending(T);      
  IsDscending(T);     
  DestroyTriplet(T);   
  Print(T);
}

 


修改及美化后的源代码

第二例   顺序实现各功能的:

//实现三元组的基本操作
#include"stdio.h"
#include"stdlib.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
Status InitTriplet(Triplet *t,ElemType v1,ElemType v2,ElemType v3){
//构造三元组T,依次置T的三个元素初值为v1,v2,v3
  *t=(ElemType *)malloc(3 *sizeof(ElemType));
  if(!*t) exit(OVERFLOW);
  (*t)[0]=v1;(*t)[1]=v2;(*t)[2]=v3;
  return OK;
}
Status DestroyTriplet(Triplet *t){
//销毁三元组T

  free(*t);
  *t=NULL;
  return OK;
}
Status Get(Triplet T,int i,ElemType *e){
//1<=i<=3,用e返回T的第i元的值
  if(i<1||i>3) return ERROR;
  *e=T[i-1];
  return OK;
}
Status Put(Triplet *t,int i,ElemType e){
//1<=i<=3,置T的第i元的值为e
  if(i<1||i>3) return ERROR;
  (*t)[i-1]=e;
  return OK;
}
Status IsAscending(Triplet T){
//如果T的三个元素按升序排列,则返回1,否则返回0
  return (T[0]<=T[1])&&(T[1]<=T[2]);
}
Status IsDescending(Triplet T){
//如果T的三个元素按降序排列,则返回1,否则返回0
  return (T[0]>=T[1])&&(T[1]>=T[2]);
}
Status Max(Triplet T,ElemType *e){
//用e返回T的最大元素的值
  *e=(T[0]>=T[1])?((T[0]>=T[2])?T[0]:T[2]):((T[1]>=T[2])?T[1]:T[2]);
  return OK;
}
Status Min(Triplet T,ElemType *e){
//用e返回T的最小元素的值
  *e=(T[0]<=T[1])?((T[0]<=T[2])?T[0]:T[2]):((T[1]<=T[2])?T[1]:T[2]);
  return OK;
}
void Print(Triplet T){
//打印三元组T的各个元素值
  int i;
  ElemType elem,*p=&elem;
  for(i=1;i<=3;i++)
   {Get(T,i,p);
    printf("No.%d=%d\n",i,elem);
   }
}
main()
{
 Triplet T,*t=&T;
 ElemType e1,e2,e3,elem,*p=&elem;
 int i;
 printf("\nPlease input 3 numbers:\n");
 scanf("%d%d%d",&e1,&e2,&e3);
 InitTriplet(t,e1,e2,e3);
 Print(T);     //初始化三元组T并打印
 if(IsAscending(T)) printf("Triplet is IsAscending!\n");
 else if(IsDescending(T)) printf("Triplet is IsDescending!\n");
 else printf("Triplet is not sequence!\n");      //判断三元组是否有序
 Max(T,p);
 printf("the max num is %d\n",elem);       //输出最大值
 Min(T,p);
 printf("the min num is %d\n",elem);       //输出最小值
 ll:printf("Please input which one do you want to change:(1-3)");
 scanf("%d",&i);      //输入要改的元素位序
 if(i>=1&&i<=3)
   {printf("Please input a new number:");
    scanf("%d",&elem);
    Put(t,i,elem);
    Print(T);
   }      //修改对应的值
 else {printf("Your input not correct!");    //若不合法,则重新输入要改的元素位序直至合法为止
       goto ll;} 
 DestroyTriplet(t);
}

第三例   采用菜单界面及可选操作的源代码:

#include<conio.h>
 #include<stdio.h>
 #include<string.h>
 #define OK        1
 #define ERROR     0
 #define OVERFLOW -1
 typedef int Status;
 typedef int* Triplet;
 Triplet T;
void menu()
{
 clrscr();
 printf("     ********************************************************************\n");
 printf("          SetTriplet-1         GetElement-2          PutElement-3    *\n");
 printf("          DestroyTriplet-4     IsAscending-5         IsDescending-6  *\n");
 printf("          MaxElement-7         MinElement-8          Quit-9          *\n");
 printf("     ********************************************************************\n");
 gotoxy(1,21);
 printf("     ********************************************************************\n");
 printf("     Enter a operation code:1,2,3,4,5,6,7,8                          *\n");
 printf("     ********************************************************************\n");
 gotoxy(13,14);printf("Rusult:");
 gotoxy(13,10);printf("Triplet:");
 if(T) printf("%d %d %d",T[0],T[1],T[2]);
 gotoxy(13,12);printf("i number:");
 gotoxy(13,8);printf("Operation:");
 }
 Status SetTriplet(Triplet *T1,int v1,int v2,int v3){
 *T1=T;
 T=(int *)malloc(3*sizeof(int));
 if(!T)exit(OVERFLOW);
 T[0]=v1;  T[1]=v2; T[2]=v3;
 return OK;
 }
 Status Get(Triplet T,int i,int *e){
 if(i<1||i>3) return ERROR;
(*e)=T[i-1];
 return OK;
 }
 Status Put(Triplet *T1,int i,int e){
   *T1=T;
 if(i<1||i>3) return ERROR;
T[i-1]=e;
return OK;
}
Status IsAscending(Triplet T){
return (T[0]<=T[1])&&(T[1]<=T[2]);
}
Status IsDescending(Triplet T){
return (T[0]>=T[1])&&(T[1]>=T[2]);
}
Status Max(Triplet T,int *e){
*e=(T[0]>=T[1])?((T[0]>=T[2])?T[0]:T[2])
       :((T[1]>=T[2])?T[1]:T[2]);
return OK;
}
Status Min(Triplet T,int *e){
*e=(T[0]<=T[1])?((T[0]<=T[2])?T[0]:T[2])
       :((T[1]<=T[2])?T[1]:T[2]);
return OK;
}
Status DestroyTriplet(Triplet* T1){
*T1=T;
free(T); T=NULL;
return OK;
}
void ReadCommand(int cmd)
{
 if(cmd<1||cmd>9){cmd=10;printf("Error");}
 if(!T&&cmd!=1&&cmd!=9){cmd=11;printf("Error");}
 gotoxy(7,22); clreol();
 switch(cmd)
 {
 case 1:printf("  Enter a Triplet: -32768<element<32767   ");break;
 case 2:printf("  Enter a Number:1/2/3                    ");break;
 case 3:printf("  Enter a Number:1/2/3  and Put number    ");break;
 case 4:
 case 5:
 case 6:
 case 7:
 case 8:
 case 9:printf("  Enter a Key: Enter                      ");  break;
 case 10:printf(" Error:Operation code:1,2,3,4,5,6,7,8,9   ");  break;
 case 11:printf(" Error:The Triplet not exit               "); break;
 }
 printf("                        *");
 }
 void Interpet(int cmd)
 { int v1,v2,v3,i,e2,e3;
    Triplet *p; int *e1;
    if((cmd>=2&&cmd<=9)&&(T)||(cmd==1))
    switch(cmd)
     {
     case 1:gotoxy(21,10);
    scanf("%d%d%d",&v1,&v2,&v3);
    gotoxy(21,14);
    if(SetTriplet(p,v1,v2,v3)) printf("Success");
    else printf("Error");
    break;
     case 2:
  gotoxy(22,12);
  do{ scanf("%d",&i);
  if(i<1||i>3) printf("Input agian:");
  }
  while(i>3||i<1);
  Get(T,i,e1);
  gotoxy(21,14);
  printf("%d",*e1);
  break;
     case 3:gotoxy(22,12);
     do{
     scanf("%d",&i);
     gotoxy(24,12);
     if(i<1||i>3)
     printf("Input agian:");
     }
     while(i<1||i>3);
     printf("Put number:");
     scanf("%d",&e2);
     e3=T[i-1];
     Put(p,i,e2);
     gotoxy(21,14);
     printf("%d-> %d",e3,e2);
     break;
     case 4: DestroyTriplet(p);
     case 5: gotoxy(21,14);
     if(IsAscending(T)) printf("Yes"); else printf("No");
     break;
     case 6:gotoxy(21,14);
     if(IsDescending(T)) printf("Yes"); else printf("No");
     break;
     case 7:Max(T,e1);gotoxy(21,14);printf("Max %d",*e1);
     break;
     case 8:Min(T,e1);gotoxy(21,14);printf("Min %d",*e1);
    break;
  }
  getch();
  }
 void main(){
 int cmd;
 while(cmd!=9)
 {
 menu();

 scanf("%d",&cmd);
 ReadCommand(cmd);
 Interpet(cmd);
 }
}

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有