m0_73643597 2023-03-17 11:49 采纳率: 0%
浏览 144
已结题

c语言个人知识库系统,帮我看看,能改一下最好了

#求求帮我看一下哪里写错了(要人工,不要AI回答),最好能给我改一下,纯C语言的

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

img

//我写的代码如下

#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;   
        }

运行结果:

img

要求里的修改问题没有实现,其他还有些细节没有优化。

  • 写回答

3条回答 默认 最新

  • 码诅 2023-03-17 12:01
    关注

    可是,你25找不到人工帮你处理

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月18日
  • 修改了问题 3月18日
  • 创建了问题 3月17日

悬赏问题

  • ¥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后的因变量没有被发现识别啊