使用C语言、vscode软件、g++编译器、utf-8编码环境
WJ文件中有三个文件,运行WJ文件
WJ文件的三个子文件有以下内容:
Recipe.h文件:#ifndef RECIPE_H
#define RECIPE_H
#define MAX_NAME_LEN 50
#define MAX_CATEGORY_LEN 30
#define MAX_INGREDIENTS_LEN 200
#define MAX_STEPS_LEN 500
#define MAX_RECIPES 100
#define FILENAME "recipes.dat"
// 菜谱结构体
typedef struct {
int id; // 唯一标识符
char name[MAX_NAME_LEN];
char category[MAX_CATEGORY_LEN];
char ingredients[MAX_INGREDIENTS_LEN];
char steps[MAX_STEPS_LEN];
} Recipe;
// 函数声明
void init_system();
int add_recipe(const char *name, const char *category,
const char *ingredients, const char *steps);
int delete_recipe(int id);
int modify_recipe(int id, const char *name, const char *category,
const char *ingredients, const char *steps);
Recipe* find_recipe_by_id(int id);
Recipe* find_recipe_by_name(const char *name);
void list_all_recipes();
Recipe* get_random_recipe();
void save_to_file();
void load_from_file();
#endif
Recipe.c文件:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Recipe.h"
// 全局变量
Recipe recipes[MAX_RECIPES];
int recipe_count = 0;
int next_id = 1;
// 初始化系统
void init_system() {
load_from_file();
}
// 添加菜谱
int add_recipe(const char *name, const char *category,
const char *ingredients, const char *steps) {
if (recipe_count >= MAX_RECIPES) return 0;
Recipe *r = &recipes[recipe_count];
r->id = next_id++;
strncpy(r->name, name, MAX_NAME_LEN);
strncpy(r->category, category, MAX_CATEGORY_LEN);
strncpy(r->ingredients, ingredients, MAX_INGREDIENTS_LEN);
strncpy(r->steps, steps, MAX_STEPS_LEN);
recipe_count++;
save_to_file();
return r->id;
}
// 删除菜谱
int delete_recipe(int id) {
for (int i = 0; i < recipe_count; i++) {
if (recipes[i].id == id) {
// 移动数组元素
for (int j = i; j < recipe_count - 1; j++) {
recipes[j] = recipes[j + 1];
}
recipe_count--;
save_to_file();
return 1;
}
}
return 0;
}
// 修改菜谱
int modify_recipe(int id, const char *name, const char *category,
const char *ingredients, const char *steps) {
for (int i = 0; i < recipe_count; i++) {
if (recipes[i].id == id) {
if (name) strncpy(recipes[i].name, name, MAX_NAME_LEN);
if (category) strncpy(recipes[i].category, category, MAX_CATEGORY_LEN);
if (ingredients) strncpy(recipes[i].ingredients, ingredients, MAX_INGREDIENTS_LEN);
if (steps) strncpy(recipes[i].steps, steps, MAX_STEPS_LEN);
save_to_file();
return 1;
}
}
return 0;
}
// 通过ID查找
Recipe* find_recipe_by_id(int id) {
for (int i = 0; i < recipe_count; i++) {
if (recipes[i].id == id) {
return &recipes[i];
}
}
return NULL;
}
// 通过名称查找
Recipe* find_recipe_by_name(const char *name) {
for (int i = 0; i < recipe_count; i++) {
if (strstr(recipes[i].name, name) != NULL) {
return &recipes[i];
}
}
return NULL;
}
// 列出所有菜谱
void list_all_recipes() {
printf("\n==== 所有菜谱 (%d) ====\n", recipe_count);
for (int i = 0; i < recipe_count; i++) {
printf("[ID:%d] %s (%s)\n",
recipes[i].id, recipes[i].name, recipes[i].category);
}
printf("=====================\n");
}
// 随机推荐菜谱
Recipe* get_random_recipe() {
if (recipe_count == 0) return NULL;
srand(time(NULL));
return &recipes[rand() % recipe_count];
}
// 保存到文件
void save_to_file() {
FILE *file = fopen(FILENAME, "wb");
if (!file) return;
// 写入数据
fwrite(&recipe_count, sizeof(int), 1, file);
fwrite(&next_id, sizeof(int), 1, file);
fwrite(recipes, sizeof(Recipe), recipe_count, file);
fclose(file);
}
// 从文件加载
void load_from_file() {
FILE *file = fopen(FILENAME, "rb");
if (!file) return;
// 读取数据
fread(&recipe_count, sizeof(int), 1, file);
fread(&next_id, sizeof(int), 1, file);
fread(recipes, sizeof(Recipe), recipe_count, file);
fclose(file);
}
main.c文件:#include <stdio.h>
#include <string.h>
#include "Recipe.h"
#include <locale.h>
#include <wchar.h>
#include <windows.h>
int main()
{
// 设置控制台输出为UTF-8编码
SetConsoleOutputCP(65001);
// 设置本地化环境,用于宽字符处理
setlocale(LC_ALL, "zh_CN.UTF-8");
init_system();
int choice;
do{
printf("\n==== 家庭菜谱管理系统 ====\n");
printf("1. 添加菜谱\n");
printf("2. 删除菜谱\n");
printf("3. 修改菜谱\n");
printf("4. 查找菜谱(ID)\n");
printf("5. 查找菜谱(名称)\n");
printf("6. 列出所有菜谱\n");
printf("7. 随机推荐菜谱\n");
printf("8. 退出系统\n");
printf("请选择操作: ");
canf("%d", &choice);
getchar(); // 清除输入缓冲区
}
while (choice != 8);
return 0;
{
/* code */
}
}
// 显示菜单
// 添加菜谱界面
void add_recipe_ui() {
char name[MAX_NAME_LEN];
char category[MAX_CATEGORY_LEN];
char ingredients[MAX_INGREDIENTS_LEN];
char steps[MAX_STEPS_LEN];
printf("\n--- 添加新菜谱 ---\n");
printf("菜谱名称: ");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = 0; // 移除换行符
printf("菜谱类别: ");
fgets(category, MAX_CATEGORY_LEN, stdin);
category[strcspn(category, "\n")] = 0;
printf("所需食材: ");
fgets(ingredients, MAX_INGREDIENTS_LEN, stdin);
ingredients[strcspn(ingredients, "\n")] = 0;
printf("制作步骤: ");
fgets(steps, MAX_STEPS_LEN, stdin);
steps[strcspn(steps, "\n")] = 0;
int id = add_recipe(name, category, ingredients, steps);
if (id) {
printf("添加成功! 菜谱ID: %d\n", id);
} else {
printf("添加失败! 已达最大菜谱数量\n");
}
}
// 显示菜谱详情
void display_recipe(Recipe *r) {
if (!r) {
printf("未找到菜谱!\n");
return;
}
printf("\n==== 菜谱详情 ====\n");
printf("ID: %d\n", r->id);
printf("名称: %s\n", r->name);
printf("类别: %s\n", r->category);
printf("食材: %s\n", r->ingredients);
printf("步骤: %s\n", r->steps);
printf("=================\n");
}
int main() {
init_system();
int choice;
do {
display_menu();
scanf("%d", &choice);
getchar(); // 清除输入缓冲区
switch (choice) {
case 1: // 添加
add_recipe_ui();
break;
case 2: { // 删除
int id;
wprintf("输入要删除的菜谱ID: ");
scanf("%d", &id);
getchar();
if (delete_recipe(id)) {
wprintf("删除成功!\n");
} else {
wprintf("删除失败! 无效ID\n");
}
break;
}
case 3: { // 修改
int id;
wprintf("输入要修改的菜谱ID: ");
scanf("%d", &id);
getchar();
Recipe *r = find_recipe_by_id(id);
if (!r) {
wprintf("菜谱不存在!\n");
break;
}
char name[MAX_NAME_LEN];
char category[MAX_CATEGORY_LEN];
char ingredients[MAX_INGREDIENTS_LEN];
char steps[MAX_STEPS_LEN];
wprintf("新名称(回车保持原值): ");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = 0;
wprintf("新类别(回车保持原值): ");
fgets(category, MAX_CATEGORY_LEN, stdin);
category[strcspn(category, "\n")] = 0;
wprintf("新食材(回车保持原值): ");
fgets(ingredients, MAX_INGREDIENTS_LEN, stdin);
ingredients[strcspn(ingredients, "\n")] = 0;
wprintf("新步骤(回车保持原值): ");
fgets(steps, MAX_STEPS_LEN, stdin);
steps[strcspn(steps, "\n")] = 0;
modify_recipe(id,
*name ? name : NULL,
*category ? category : NULL,
*ingredients ? ingredients : NULL,
*steps ? steps : NULL);
wprintf("修改成功!\n");
break;
}
case 4: { // 按ID查找
int id;
wprintf("输入菜谱ID: ");
scanf("%d", &id);
getchar();
display_recipe(find_recipe_by_id(id));
break;
}
case 5: { // 按名称查找
char name[MAX_NAME_LEN];
wprintf("输入菜谱名称: ");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = 0;
display_recipe(find_recipe_by_name(name));
break;
}
case 6: // 列出所有
list_all_recipes();
break;
case 7: // 随机推荐
display_recipe(get_random_recipe());
break;
case 8: // 退出
wprintf("感谢使用!\n");
break;
default:
wprintf("无效选择!\n");
}
} while (choice != 8);
return 0;
}
在运行输入:g++ -o WJ Recipe.c main.c
出现以下错误:Microsoft Windows [版本 10.0.26100.4652]
(c) Microsoft Corporation。保留所有权利。
C:\Users\34171\Desktop\code\WJ>g++ -o WJ Recipe.c main.c
main.c: In function 'int main()':
main.c:28:9: error: 'canf' was not declared in this scope; did you mean 'scanf'?
28 | canf("%d", &choice);
| ^~~~
| scanf
main.c: At global scope:
main.c:89:5: error: redefinition of 'int main()'
89 | int main() {
| ^~~~
main.c:8:5: note: 'int main()' previously defined here
8 | int main()
| ^~~~
main.c: In function 'int main()':
main.c:94:9: error: 'display_menu' was not declared in this scope; did you mean 'display_recipe'?
94 | display_menu();
| ^~~~~~~~~~~~
| display_recipe
main.c:105:25: error: cannot convert 'const char*' to 'const wchar_t*'
105 | wprintf("杈D: ");
| ^~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:110:29: error: cannot convert 'const char*' to 'const wchar_t*'
110 | wprintf("錸");
| ^~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:112:29: error: cannot convert 'const char*' to 'const wchar_t*'
112 | wprintf("錎\n");
| ^~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:119:25: error: cannot convert 'const char*' to 'const wchar_t*'
119 | wprintf("杈D: ");
| ^~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:125:33: error: cannot convert 'const char*' to 'const wchar_t*'
125 | wprintf("鑞");
| ^~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:134:25: error: cannot convert 'const char*' to 'const wchar_t*'
134 | wprintf("鎚);
| ^~~~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:138:25: error: cannot convert 'const char*' to 'const wchar_t*'
138 | wprintf("鎚);
| ^~~~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:142:25: error: cannot convert 'const char*' to 'const wchar_t*'
142 | wprintf("鎚);
| ^~~~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:146:25: error: cannot convert 'const char*' to 'const wchar_t*'
146 | wprintf("鎚);
| ^~~~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:156:25: error: cannot convert 'const char*' to 'const wchar_t*'
156 | wprintf("淇n");
| ^~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:162:25: error: cannot convert 'const char*' to 'const wchar_t*'
162 | wprintf("杈D: ");
| ^~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:172:25: error: cannot convert 'const char*' to 'const wchar_t*'
172 | wprintf("杈m);
| ^~~~~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:189:25: error: cannot convert 'const char*' to 'const wchar_t*'
189 | wprintf("鎛");
| ^~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
main.c:193:25: error: cannot convert 'const char*' to 'const wchar_t*'
193 | wprintf("鎛");
| ^~~~~~~~~~~~~
| |
| const char*
In file included from main.c:1:
D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)'
1093 | int wprintf (const wchar_t *__format, ...)
| ~~~~~~~~~~~~~~~^~~~~~~~
C:\Users\34171\Desktop\code\WJ>
最新发布