m0_73643597 2023-03-16 14:14 采纳率: 0%
浏览 49

希望能有一位很懂C语言的帮我看看我的程序,C语言的个人知识库系统

#求求帮我看一下哪里写错了

#要求如下:个人知识库程序
设计并编写一个个人知识库系统 PKB ( Personal Knowledge Base )本系统用户可以使用本系统将遇到的问题及解决方案进行记录,实现永久存储,并提供查询功能,以备不时之需。

img

系统功能:
1.信息录入:包括问题描述信息的录入、解决方案信息的录入两部分。每一部分的录入以 Ctrl + Z 结束。可以只录入问题的描述、解决方案,也可以在录入问题的描述之后顺次录入解决方案。
2.信息查询,本系统支持两种查询方式
1)系统启动后用菜单选项进行查询:可分别按录入问题的日期( dateofprob )、录入解决方案的日期( dateofans )、问题描述( prob )中包含的字符串(包含用字符串形式表示)、解决方案( ans )中包含的字符串进行查询;
2)系统启动时用命令行选项进行查询:可在系统启动的同时,在命令行输入查询依据。例如,在命令行输入pkb dateofprob=2022.12.06 回车后,直接查询并显示2022年12月06日录入的问题及解决方案。输入pkb dateofans=2022.12.06 and ans=%安装显卡驱动程序的3.7.1版%回车后,则查询并显示解决方案的录入日期为2022年12月06日,且解决方案的文本中包含子串"安装显卡驱动程序的3.7.1版"的问题及解决方案。输入pkb dateofprob =2022.12.06 or ans=%显卡驱动%回车后,则查询并显示问题的录入日期为2022年12月06日或者解决方案中包含了"显卡驱动"字样的问题及解决方案。
系统支持类似的多条件组合查询,此种方式查询、显示信息以后,程序即结束运行。

//我写的代码如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_PROB_LEN 1000
#define MAX_ANS_LEN 1000
#define MAX_DATE_LEN 20

typedef struct {
    char dateofprob[MAX_DATE_LEN];
    char dateofans[MAX_DATE_LEN];
    char prob[MAX_PROB_LEN];
    char ans[MAX_ANS_LEN];
} PKBEntry;

void print_entry(PKBEntry entry) {
    printf("问题录入日期:%s\n", entry.dateofprob);
    printf("解决方案录入日期:%s\n", entry.dateofans);
    printf("问题描述:%s\n", entry.prob);
    printf("解决方案:%s\n", entry.ans);
    printf("\n");
}

void print_entries(PKBEntry *entries, int count) {
    for (int i = 0; i < count; i++) {
        print_entry(entries[i]);
    }
}

int compare_dates(char *date1, char *date2) {
    int year1, month1, day1, year2, month2, day2;
    sscanf(date1, "%d.%d.%d", &year1, &month1, &day1);
    sscanf(date2, "%d.%d.%d", &year2, &month2, &day2);
    if (year1 < year2) {
        return -1;
    } else if (year1 > year2) {
        return 1;
    } else {
        if (month1 < month2) {
            return -1;
        } else if (month1 > month2) {
            return 1;
        } else {
            if (day1 < day2) {
                return -1;
            } else if (day1 > day2) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}

int main(int argc, char *argv[]) {
    PKBEntry *entries = NULL;
    int count = 0;
    char input[MAX_PROB_LEN + MAX_ANS_LEN];
    char *dateofprob = NULL;
    char *dateofans = NULL;
    char *prob = NULL;
    char *ans = NULL;
    char *token = NULL;
    char *query = NULL;
    char *query_field = NULL;
    char *query_value = NULL;
    char *query_op = NULL;
    int query_count = 0;
    int match = 1;

    while (1) {
        printf("请输入问题描述和解决方案(以Ctrl + Z结束):\n");
        if (fgets(input, MAX_PROB_LEN + MAX_ANS_LEN, stdin) == NULL) {
            break;
        }
        if (strlen(input) == MAX_PROB_LEN + MAX_ANS_LEN - 1) {
            printf("输入的问题描述和解决方案太长,请重新输入\n");
            continue;
        }
        token = strtok(input, "\n");
        dateofprob = (char *) malloc(MAX_DATE_LEN);
        dateofans = (char *) malloc(MAX_DATE_LEN);
        prob = (char *) malloc(MAX_PROB_LEN);
        ans = (char *) malloc(MAX_ANS_LEN);
        time_t now = time(NULL);
        struct tm *t = localtime(&now);
        sprintf(dateofprob, "%04d.%02d.%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
        strcpy(prob, token);
        while ((token = strtok(NULL, "\n")) != NULL) {
            if (dateofans[0] == '\0') {
                sprintf(dateofans, "%04d.%02d.%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
            }
            strcat(ans, token);
        }
        PKBEntry entry;
strcpy(entry.dateofprob, dateofprob);
strcpy(entry.dateofans, dateofans);
strcpy(entry.prob, prob);
strcpy(entry.ans, ans);
        entries = (PKBEntry *) realloc(entries, (count + 1) * sizeof(PKBEntry));
        entries[count] = entry;
        count++;
    }

    if (argc > 1) {
        query = argv[1];
        query_count = sscanf(query, "pkb %m[^=]=%m[^ ] %m[^(]%m[^)]", &query_field, &query_value, &query_op, &query_value);
        if (query_count < 2) {
            printf("查询语句格式错误,请重新输入\n");
            return 1;
        }
        if (strcmp(query_field, "dateofprob") != 0 && strcmp(query_field, "dateofans") != 0 && strcmp(query_field, "prob") != 0 && strcmp(query_field, "ans") != 0) {
            printf("查询字段错误,请重新输入\n");
            return 1;
        }
        for (int i = 0; i < count; i++) {
            match = 1;
            if (strcmp(query_field, "dateofprob") == 0) {
                if (compare_dates(entries[i].dateofprob, query_value) != 0) {
                    match = 0;
                }
            } else if (strcmp(query_field, "dateofans") == 0) {
                if (compare_dates(entries[i].dateofans, query_value) != 0) {
                    match = 0;
                }
            } else if (strcmp(query_field, "prob") == 0) {
                if (strstr(entries[i].prob, query_value) == NULL) {
                    match = 0;
                }
            } else if (strcmp(query_field, "ans") == 0) {
                if (strstr(entries[i].ans, query_value) == NULL) {
                    match = 0;
                }
                if (query_count == 4) {
                    if (strcmp(query_op, "or") == 0) {
                        if (strcmp(query_field, "dateofprob") == 0) {
                            if (compare_dates(entries[i].dateofprob, query_value) !=0) 
                            
            {             match = 1;
                                 break;
            }                         } 
            else 
            if (strcmp(query_op, "and") == 0) 
            {                            
             if 
             (strcmp(query_field, "dateofprob") == 0)
              {                                 if (compare_dates(entries[i].dateofprob, query_value) == 0) 
              {                                     
              match = 1; 
                break;  
                    }  
                        } 
                        else if (strcmp(query_field, "dateofans") == 0)
                         {                            
                              if (compare_dates(entries[i].dateofans, query_value) == 0) 
                              {                                    
                               match = 1; 
                                      break; 
                                           
                                              }  
                                    
                                         } 
                                         else if (strcmp(query_field, "prob") == 0) 
                                         {    
                                           if (strstr(entries[i].prob, query_value) != NULL)
                                            {    
                                              match = 1; 
                                                     break;    
                                                             }    
                                                               
                                                                   } else if (strcmp(query_field, "ans") == 0) 
                                                                   {  
                                                                        if (strstr(entries[i].ans, query_value) != NULL)
                                                                         {  
                                                                          match = 1;       
                                                                              break;  
                                                                                      }    
                                                                                          
                                                                                                }      
                                                                                                   
                                                                                                       }   
                                                                                                                 }  
                                                                                                                   
                                                                                                                     }  
                                                                                                                            }  
                                 if (match)
                                  {               
                                               print_entry(entries[i]); 
                                             }  
                                                
                                                    }   
                                                      } else
                                                       {         print_entries(entries, count);
                                                            }  
                                                                for (int i = 0; i < count; i++) 
                                                                {         free(entries[i].dateofprob);  
                                                                       free(entries[i].dateofans);  
                                                                              free(entries[i].prob);  
                                                                                     free(entries[i].ans);  
                                                                                        }   
                                                                                          free(entries);  
                                                                                             if (query_field != NULL)
                                                                                             {         free(query_field);  
                                                                                                } 
                                                                                                 if (query_value != NULL)
                                                                                                  {         free(query_value);
                                                                                                       }   
                                                                                                         if (query_op != NULL) 
                                                                                                         {        \
                                                                                                          free(query_op);
                                                                                                              }    
                                                                                                               return 0;
                                                                                                                }

  • 写回答

3条回答 默认 最新

  • 别,爱℡ 2023-03-16 17:26
    关注

    写的太乱了,把main函数里面的东西用函数封装一下,然后调试

    评论

报告相同问题?

问题事件

  • 修改了问题 3月16日
  • 创建了问题 3月16日

悬赏问题

  • ¥15 rt-thread线程切换的问题
  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊