#求求帮我看一下哪里写错了(要人工,不要AI回答),最好能给我改一下,纯C语言的
#要求如下:个人知识库程序
设计并编写一个个人知识库系统 PKB ( Personal Knowledge Base )本系统用户可以使用本系统将遇到的问题及解决方案进行记录,实现永久存储,并提供查询功能,以备不时之需。
//我写的代码如下
#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;
}
运行结果:
要求里的修改问题没有实现,其他还有些细节没有优化。
可是,你25找不到人工帮你处理
这个我建议你可以找一个现成的代码修改一下。
你提供的问题中信息比较多,一下不好理解,你也已经完成了代码,请把代码具体的报错信息帖出来,大家才好帮你解答。鉴于你目前的代码,给你提几个小建议:
1、main函数中的代码太多了,建议有些代码移除来放在函数中处理,这样逻辑更加清晰,一到出错了,更好的定位问题,不然,看着代码很多,容易出错
2、代码的格式要调整好,该对齐的对齐,看着舒服,其次不容易写错,尤其是多一个括号,少一个括号的问题。
3、最好把代码运行的错误贴出来,让大家帮你解答,如果需要修改代码,也要说明具体实现那部分的功能,否则拿到你的代码,看着这么混乱,无法下手的。