使用C语言,使用vscode软件,使用g++编译器,使用simplified chinese(gbk)编码环境,文件夹WJ下有三个文件:
main.c文件内容为:
#include "recipe.h"
#include <windows.h>
// 程序入口
int main()
{
SetConsoleOutputCP(936); // 设置控制台输出为 GBK
SetConsoleCP(936); // 设置控制台输入为 GBK
main_menu();
return 0;
}
recipe.c文件内容为:
#include "recipe.h"
// 全局变量定义
Recipe recipes[MAX_RECIPES];
int recipe_count = 0;
// 主菜单函数定义
void main_menu()
{
int choice;
while (1)
{
printf("\n--- 菜谱管理系统 ---\n");
printf("1. 添加菜谱\n");
printf("2. 修改菜谱\n");
printf("3. 删除菜谱\n");
printf("4. 查询菜谱\n");
printf("5. 查看全部菜谱\n");
printf("6. 退出系统");
printf("\n-------------------\n");
printf("请选择操作: ");
scanf("%d", &choice);
getchar();
switch (choice)
{
case 1:
add_recipe();
break;
case 2:
edit_recipe();
break;
case 3:
delete_recipe();
break;
case 4:
search_recipes();
break;
case 5:
view_all_recipes();
break;
case 6:
exit(0);
default:
printf("无效选择,请重新输入。\n");
}
}
}
// 添加菜谱
void add_recipe()
{
if (recipe_count >= MAX_RECIPES)
{
printf("菜谱已满,无法添加更多。\n");
return;
}
Recipe *r = &recipes[recipe_count++];
printf("请输入菜名: ");
fgets(r->name, NAME_LEN, stdin);
r->name[strcspn(r->name, "\n")] = '\0';
printf("请输入分类: ");
fgets(r->category, CATEGORY_LEN, stdin);
r->category[strcspn(r->category, "\n")] = '\0';
printf("请输入食材: ");
fgets(r->ingredients, INGREDIENTS_LEN, stdin);
r->ingredients[strcspn(r->ingredients, "\n")] = '\0';
printf("请输入制作步骤: ");
fgets(r->steps, STEPS_LEN, stdin);
r->steps[strcspn(r->steps, "\n")] = '\0';
printf("请输入谁喜欢吃: ");
fgets(r->liked_by, LIKED_BY_LEN, stdin);
r->liked_by[strcspn(r->liked_by, "\n")] = '\0';
printf("菜谱添加成功!\n");
}
// 修改菜谱
void edit_recipe()
{
char name[NAME_LEN];
printf("请输入要修改的菜名: ");
fgets(name, NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0';
for (int i = 0; i < recipe_count; i++)
{
if (strcmp(recipes[i].name, name) == 0)
{
int field;
printf("请选择要修改的字段:\n");
printf("1. 分类\n2. 食材\n3. 制作步骤\n4. 谁喜欢吃\n");
scanf("%d", &field);
getchar();
switch (field)
{
case 1:
printf("请输入新分类: ");
fgets(recipes[i].category, CATEGORY_LEN, stdin);
recipes[i].category[strcspn(recipes[i].category, "\n")] = '\0';
break;
case 2:
printf("请输入新食材: ");
fgets(recipes[i].ingredients, INGREDIENTS_LEN, stdin);
recipes[i].ingredients[strcspn(recipes[i].ingredients, "\n")] = '\0';
break;
case 3:
printf("请输入新制作步骤: ");
fgets(recipes[i].steps, STEPS_LEN, stdin);
recipes[i].steps[strcspn(recipes[i].steps, "\n")] = '\0';
break;
case 4:
printf("请输入新的喜好者: ");
fgets(recipes[i].liked_by, LIKED_BY_LEN, stdin);
recipes[i].liked_by[strcspn(recipes[i].liked_by, "\n")] = '\0';
break;
default:
printf("无效选择。\n");
}
printf("菜谱信息已更新。\n");
return;
}
}
printf("未找到该菜名的菜谱。\n");
}
// 删除菜谱
void delete_recipe()
{
char name[NAME_LEN];
printf("请输入要删除的菜名: ");
fgets(name, NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0';
for (int i = 0; i < recipe_count; i++)
{
if (strcmp(recipes[i].name, name) == 0)
{
for (int j = i; j < recipe_count - 1; j++)
{
recipes[j] = recipes[j + 1];
}
recipe_count--;
printf("菜谱删除成功。\n");
return;
}
}
printf("未找到该菜名的菜谱。\n");
}
// 查询菜谱
void search_recipes()
{
int choice;
printf("请选择查询方式:\n");
printf("1. 按菜名\n2. 按分类\n3. 按食材\n4. 按谁喜欢吃\n");
scanf("%d", &choice);
getchar();
char keyword[NAME_LEN];
printf("请输入菜名: ");
fgets(keyword, NAME_LEN, stdin);
keyword[strcspn(keyword, "\n")] = '\0';
int found = 0;
for (int i = 0; i < recipe_count; i++)
{
switch (choice)
{
case 1:
if (strstr(recipes[i].name, keyword))
{
printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n",
recipes[i].name, recipes[i].category,
recipes[i].ingredients, recipes[i].steps,
recipes[i].liked_by);
found = 1;
}
break;
case 2:
if (strstr(recipes[i].category, keyword))
{
printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n",
recipes[i].name, recipes[i].category,
recipes[i].ingredients, recipes[i].steps,
recipes[i].liked_by);
found = 1;
}
break;
case 3:
if (strstr(recipes[i].ingredients, keyword))
{
printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n",
recipes[i].name, recipes[i].category,
recipes[i].ingredients, recipes[i].steps,
recipes[i].liked_by);
found = 1;
}
break;
case 4:
if (strstr(recipes[i].liked_by, keyword))
{
printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n",
recipes[i].name, recipes[i].category,
recipes[i].ingredients, recipes[i].steps,
recipes[i].liked_by);
found = 1;
}
break;
}
}
if (!found)
printf("未找到匹配的菜谱。\n");
}
// 查看全部菜谱
void view_all_recipes()
{
if (recipe_count == 0)
{
printf("当前没有菜谱。\n");
return;
}
for (int i = 0; i < recipe_count; i++)
{
printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n",
recipes[i].name, recipes[i].category,
recipes[i].ingredients, recipes[i].steps,
recipes[i].liked_by);
}
}
recipe.h文件内容为:
#ifndef RECIPE_MANAGER_H
#define RECIPE_MANAGER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define MAX_RECIPES 100
#define NAME_LEN 100
#define CATEGORY_LEN 50
#define INGREDIENTS_LEN 200
#define STEPS_LEN 500
#define LIKED_BY_LEN 100
// 定义菜谱结构体
typedef struct
{
char name[NAME_LEN];
char category[CATEGORY_LEN];
char ingredients[INGREDIENTS_LEN];
char steps[STEPS_LEN];
char liked_by[LIKED_BY_LEN];
} Recipe;
// 全局变量声明
extern Recipe recipes[MAX_RECIPES];
extern int recipe_count;
// 函数声明
void add_recipe();
void edit_recipe();
void delete_recipe();
void search_recipes();
void view_all_recipes();
void main_menu();
#endif
将WJ文件在集成终端打开:
PS C:\Users\34171\Desktop\code\WJ> g++ -finput-charset=GBK -o WJ1 main.c recipe.c
PS C:\Users\34171\Desktop\code\WJ> ./WJ
运行后显示这些乱码
--- 鑿滆氨绠$悊绯荤粺 ---
1. 娣诲姞鑿滆氨
2. 淇敼鑿滆氨
3. 鍒犻櫎鑿滆氨
3. 鍒犻櫎鑿滆氨
4. 鏌ヨ鑿滆氨
5. 鏌ョ湅鍏ㄩ儴鑿滆氨
4. 鏌ヨ鑿滆氨
5. 鏌ョ湅鍏ㄩ儴鑿滆氨
5. 鏌ョ湅鍏ㄩ儴鑿滆氨
6. 閫€鍑虹郴缁
-------------------
璇烽€夋嫨鎿嶄綔:
应该怎么修改