⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp2.cpp

📁 有4个小程序
💻 CPP
字号:
/*   struct   node_city.C   */   
  /*实验目的:用单链表解决实际问题*/   
  /*问题描述:   
              将若干城市的信息存入一个表中。其中城市信息包括:城市名,城市的位置坐标。   
              要求能够利用城市名和位置坐标进行有关查找,插入,更新等操作。   
  */   
  /*基本要求:   
              1   用一个带头结点的单链表存储城市信息   
              2   根据给定的城市名返回其位置坐标   
              3   根据给定的位置坐标P和一个距离D,返回所有与P的距离小于等于D的城市   
  */   
  /*测试数据:   
              创建一个有十个城市的单链表,结点数据为城市信息   
  */   
  #include   <malloc.h>   
  #include   <stdlib.h>   
  #include   "stdio.h"   
  #include   "conio.h"   
    
  #define   SLinkElemType   City   
  #define   DElemType   int   
  #define   CityNameType   char   
  #define   Status   int   
  #define   OK   1   
  #define   ERROR   0   
  #define   OVERFLOW   0   
    
  /*结构定义*/   
  typedef   struct   node_DElem   
  {   
    DElemType   x;   
    DElemType   y;   
  }DElem;   
    
  typedef   struct   node_City   
  {   
    char   cityname;   
    DElem   zuobiao;   
  }City;   
    
  typedef   struct   SNode   
  {   
    SLinkElemType   data;   
    struct   SNode   *next;   
  }SNode,*SLink,*SLinkPtr;   
    
    
  /*调用的函数说明*/   
  Status   InitSNode(SLinkPtr   n_sl);   
  Status   InsertCity(SLinkPtr   sl,char   ctname,int   x,int   y);   
  Status   Deletecity(SLinkPtr   sl,char   ctname);   
  Status   GengXinCity(SLinkPtr   sl,char   ctname,char   newctname,int   x,int   y);   
  Status   SearchCity(SLinkPtr   sl,char   ctname,DElem   *zb);   
  Status   SearchCitys(SLinkPtr   sl,DElem   p,int   d);   
    
  /*主程序*/   
  main()   
  {/*测试数据:创建一个有十个城市的单链表,结点数据为城市信息*/   
    
    /*用一个带头结点的单链表存储城市信息*/   
    int   n;   
    char   name;   
    int   x,y;   
    DElem   *p,zb;   
    SLinkPtr   sl;   
    
    n=0;   
    sl=NULL;   
    p=(DElem   *)malloc(sizeof(DElem));   
    /*sl=(SLinkPtr)malloc(sizeof(SNode));   
    if(sl)exit(OVERFLOW);   
    sl->next=NULL;*/   
    InitSNode(&sl);   
    
    for(;n<10;n++)   
      {   
        printf("\nPlease   input   the   %d   city   name   and   position:",n+1);   
        scanf("\n%s,%d,%d",&name,&x,&y);   
        InsertCity(&sl,name,x,y);   
        }   
    
      /*根据给定的城市名返回其位置坐标*/   
      printf("Please   input   the   city   name   and   position:\n");   
      scanf("\n%s",&name);   
      SearchCity(sl,name,&p);   
      printf("%sThe   city's   position   is:%d,%d",name,x,y);   
    
      /*根据给定的位置坐标P和一个距离D,返回所有与P的距离小于等于D的城市*/   
      /*printf("\nPlease   input   the   position   P   and   distance   D:");   
      printf("\nPlease   input   the   position   P");   
      scanf("%d,%d",&zb.x,zb.y);   
      printf("\nPlease   input   the   distance   D:");   
      scanf("%d",&n);   
      SearchCitys(sl,zb,n);   
      printf("\n");   
      */   
      getch();   
  }   
    
  /*函数部分*/   
  Status   InitSNode(SLinkPtr   sl){   
    SLinkPtr   n_sl;   
    n_sl=(SLinkPtr)malloc(sizeof(SNode));   
    if(n_sl)exit(OVERFLOW);   
    n_sl->next=NULL;   
    sl=n_sl;   
    return   OK;   
  }   
    
  Status   InsertCity(SLinkPtr   sl,char   ctname,int   x,int   y){   
    SLink   n_sl,p,q;   
    p=sl;   
    InitSNode(&n_sl);   
    n_sl->data.cityname=ctname;   
    n_sl->data.zuobiao.x=x;   
    n_sl->data.zuobiao.y=y;   
    q=p->next;   
    n_sl->next=q;   
    p->next=n_sl;   
    sl=p;   
    return   OK;   
  }   
    
  Status   DeleteCity(SLinkPtr   sl,char   ctname){   
    SLink   p,q,t;   
    t=sl;   
    q=p=t->next;   
    for(;p!=NULL&&p->data.cityname!=ctname;q=p,p=p->next);   /*删除p*/   
    if(p!=NULL)   
      {   
        q->next=p->next;   
        free(p);   
        sl=t;   
        return   OK;   
      }   
    else   
      return   ERROR;   
  }   
    
  Status   GengXinCity(SLinkPtr   sl,char   ctname,char   newctname,int   x,int   y){   
    SLink   p,t;   
    t=sl;   
    p=t->next;   
    for(;p!=NULL&&p->data.cityname!=ctname;p=p->next);   /*更新结点p*/   
    if(p!=NULL)   
      {   
        p->data.cityname=newctname;   
        p->data.zuobiao.x=x;   
        p->data.zuobiao.y=y;   
        return   OK;   
      }   
    else   
      return   ERROR;   
  }   
    
  Status   SearchCity(SLinkPtr   sl,char   ctname,DElem   *zb){   
    SLink   p;   
    p=sl->next;   
    for(;p!=NULL&&p->data.cityname!=ctname;p=p->next);     /*查找到结点p*/   
    if(p!=NULL)                                                                                   /*返回坐标*/   
      {   
        zb->x=p->data.zuobiao.x;   
        zb->y=p->data.zuobiao.y;   
        return   OK;   
      }   
    else   
      return   ERROR;   
  }   
    
  Status   SearchCitys(SLinkPtr   sl,DElem   p,int   d){   
    SLink   q;   
    int   jl,m,n;   
    d=d*d;   
    q=sl->next;   
    for(;q!=NULL;q=q->next)                                             /*查找到符合条件的结点p*/   
      {m=((q->data.zuobiao.x)-p.x)*((q->data.zuobiao.x)-p.x);   
        n=((q->data.zuobiao.y)-p.y)*((q->data.zuobiao.y)-p.y);   
        if(d>=m+n);/*打印*/   
        printf("The   city   name   is   %s",q->data.cityname);   
      }   
    return   OK;   
  } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -