关于二十四节气的实现的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);    ////销毁顺序表  
}

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

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);    ////销毁顺序表  
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

#include <stdbool.h>//增加bool的库,可以不用
#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[15]; //诗句作者 原来的空间太小
}; 
struct LNode{
    ElementType Data[MAXSIZE];
    int Last;
};
//初始化空的顺序表 
List MakeEmpty(){
    List p = (List)malloc(sizeof(struct 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 
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++){
        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);    ////销毁顺序表  
}