ArLo182317 2022-02-25 15:27 采纳率: 86.7%
浏览 250
已结题

关于二十四节气的实现的C语言数据结构问题,怎么写代码


#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define MAXSIZE 15
#define ERROR -1
typedef struct SolarTerm ElementType;
typedef struct LNode * List;
//节气信息结构 
struct SolarTerm{
    char name[20];//节气名称 
    char poem[30];//节气诗句 
    char author[10]; //诗句作者 
}; 
struct LNode{
    ElementType Data[MAXSIZE];
    int Last;
};
//初始化空的顺序表 
List MakeEmpty(){
} 
//查找  返回元素所对应的下标 
int Find(List L,ElementType X){
}  
//插入  位序i处插入元素X,注意对应的下标是i-1 ,1<=i<=L->Last+2 
bool Insert(List L,ElementType X,int i){
    //1.判定是否满;2.判定位序是否合法;3.移动;4.插入;5.表长加1     
}
//删除 位序为i的元素 ,下标是i-1位置的元素 ,合法的位序为1<=i<=L->Last+1 
bool Delete(List L,int i){
    //1.判定是否为空;2.判定位序是否合法;3.通过移动数据直接删除;4.表长减去1
}
//销毁 
void Destroy(List L){
    if(L)
        free(L);
}
//显示 
void Display(List L){
    for(int i=0;i<=L->Last;i++){
        printf("%s %s %s\n",L->Data[i].name,L->Data[i].poem,L->Data[i].author);//显示节气名称和对应的诗句 及作者 printf("%d ",L->Data[i]);
    }
    printf("\n");
}
int main(){
    //1.创建空的单链表
    //2.位置1插入立春节气信息,位置1插入雨水节气信息,位置1插入春分节气信息 
    //3.删除位置2的元素
    //4.查找春分节气 在表中的位序 
    //5.销毁顺序表 
    List L=MakeEmpty();
    ElementType Data;
    strcpy(Data.name, "立春");
    strcpy(Data.poem,"春冬移律吕,天地换星霜 ");
    strcpy(Data.author,"唐 元稹 ");
    Insert(L,Data,1);
    Display(L);
    strcpy(Data.name, "雨水");
    strcpy(Data.poem,"随风潜入夜,润物细无声 ");
    strcpy(Data.author,"唐 杜甫");
    
    Insert(L,Data,1);
    Display(L);
    
    strcpy(Data.name, "春分");
    strcpy(Data.poem,"春风如贵客,一到便繁华");
    strcpy(Data.author,"清 袁枚");
    Insert(L,Data,1);
    Display(L);
    ElementType NewData;
    strcpy( NewData.name, "立春"); 
    printf("locate=%d\n",Find(L,NewData));  //查找春分节气 在表中的位序 ,注意返回的是下标 
    Delete(L,2);//删除第二个元素 
    Display(L);
    Destroy(L);    ////销毁顺序表  
}
  • 写回答

3条回答 默认 最新

  • 关注

    你想达到什么效果呢?还是补全代码就可以了。补全的代码如下:

    img

    
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    #define MAXSIZE 15
    #define ERROR -1
    typedef struct SolarTerm ElementType;
    typedef struct LNode * List;
    //节气信息结构 
    struct SolarTerm{
        char name[20];//节气名称 
        char poem[30];//节气诗句 
        char author[10]; //诗句作者 
    }; 
    struct LNode{
        ElementType Data[MAXSIZE];
        int Last;
    };
    //初始化空的顺序表 
    List MakeEmpty(){
        List p = (List)malloc(sizeof(LNode));
        p->Last = 0;
        return p;
    } 
    //查找  返回元素所对应的下标 
    int Find(List L,ElementType X){
        int i;
        for(i=0;i<L->Last;i++){
            if(strcmp(L->Data[i].name,X.name)==0)
                return i;
        }
        return -1;
    }  
    //插入  位序i处插入元素X,注意对应的下标是i-1 ,1<=i<=L->Last+2 (这里错了吧,不能等于L->Last+2,只能到L->Last+1)
    bool Insert(List L,ElementType X,int i){
        //1.判定是否满;2.判定位序是否合法;3.移动;4.插入;5.表长加1   
        int j;
        if(L->Last == MAXSIZE)
            return false;
        if(i<1 || i>=L->Last+2) 
            return false;
        if(i== MAXSIZE+1)
            L->Data[i-1] = X;
        else
        {
            for(j = L->Last;j>=i;j--)
                L->Data[j] = L->Data[j-1];
            L->Data[i-1] = X;
        }
        L->Last += 1;
        return true;
    }
    //删除 位序为i的元素 ,下标是i-1位置的元素 ,合法的位序为1<=i<=L->Last+1 
    bool Delete(List L,int i){
        //1.判定是否为空;2.判定位序是否合法;3.通过移动数据直接删除;4.表长减去1
        int j;
        if(L->Last == 0)
            return false;
        if(i<1 || i >= L->Last+1)
            return false;
        for(j=i-1;j<L->Last-1;j++)
            L->Data[j] = L->Data[j+1];
        L->Last -=1;
        return true;
    }
    //销毁 
    void Destroy(List L){
        if(L)
            free(L);
    }
    //显示 
    void Display(List L){
        for(int i=0;i<L->Last;i++){//这里i不能等于L->Last
            printf("%s %s %s\n",L->Data[i].name,L->Data[i].poem,L->Data[i].author);//显示节气名称和对应的诗句 及作者 printf("%d ",L->Data[i]);
        }
        printf("\n");
    }
    int main(){
        //1.创建空的单链表
        //2.位置1插入立春节气信息,位置1插入雨水节气信息,位置1插入春分节气信息 
        //3.删除位置2的元素
        //4.查找春分节气 在表中的位序 
        //5.销毁顺序表 
        List L=MakeEmpty();
        ElementType Data;
        strcpy(Data.name, "立春");
        strcpy(Data.poem,"春冬移律吕,天地换星霜 ");
        strcpy(Data.author,"唐 元稹 ");
        Insert(L,Data,1);
        Display(L);
        strcpy(Data.name, "雨水");
        strcpy(Data.poem,"随风潜入夜,润物细无声 ");
        strcpy(Data.author,"唐 杜甫");
    
        Insert(L,Data,1);
        Display(L);
    
        strcpy(Data.name, "春分");
        strcpy(Data.poem,"春风如贵客,一到便繁华");
        strcpy(Data.author,"清 袁枚");
        Insert(L,Data,1);
        Display(L);
        ElementType NewData;
        strcpy( NewData.name, "立春"); 
        printf("locate=%d\n",Find(L,NewData));  //查找春分节气 在表中的位序 ,注意返回的是下标 
        Delete(L,2);//删除第二个元素 
        Display(L);
        Destroy(L);    ////销毁顺序表  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月26日
  • 已采纳回答 2月25日
  • 创建了问题 2月25日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加