ADD file via upload

main
pphrg4z7q 2 years ago
parent 4ecee6a992
commit 128a999bab

@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义宠物结构体
struct Pet {
char name[20];
int age;
char species[20];
char price[10];
};
// 定义全局变量
int petCount = 0;
struct Pet pets[100];
// 添加宠物函数
void addPet() {
struct Pet newPet;
printf("请输入宠物名称:");
scanf("%s", newPet.name);
printf("请输入宠物年龄:");
scanf("%d", &newPet.age);
printf("请输入宠物品种:");
scanf("%s", newPet.species);
printf("请输入宠物价格:");
scanf("%s", newPet.price);
pets[petCount++] = newPet;
printf("宠物添加成功!\n");
}
// 查看所有宠物函数
void viewPets() {
printf("所有宠物列表:\n");
for (int i = 0; i < petCount; i++) {
printf("%d. 名称:%s年龄%d品种%s价格%s\n", i+1, pets[i].name, pets[i].age, pets[i].species, pets[i].price);
}
}
// 搜索宠物函数
void searchPets() {
char keyword[20];
printf("请输入要搜索的关键词:");
scanf("%s", keyword);
printf("搜索结果:\n");
for (int i = 0; i < petCount; i++) {
if (strstr(pets[i].name, keyword) != NULL || strstr(pets[i].species, keyword) != NULL) {
printf("%d. 名称:%s年龄%d品种%s价格%s\n", i+1, pets[i].name, pets[i].age, pets[i].species, pets[i].price);
}
}
}
// 购买宠物函数
void buyPet() {
char name[20];
printf("请输入要购买的宠物名称:");
scanf("%s", name);
for (int i = 0; i < petCount; i++) {
if (strcmp(pets[i].name, name) == 0) {
printf("购买成功!\n");
return;
}
}
printf("找不到该宠物,请重新输入。\n");
}
// 出售宠物函数
void sellPet() {
char name[20];
printf("请输入要出售的宠物名称:");
scanf("%s", name);
for (int i = 0; i < petCount; i++) {
if (strcmp(pets[i].name, name) == 0) {
printf("出售成功!\n");
return;
}
}
printf("找不到该宠物,请重新输入。\n");
}
int main() {
int choice;
while (1) {
printf("\n宠物交易系统\n");
printf("1. 添加宠物\n");
printf("2. 查看所有宠物\n");
printf("3. 搜索宠物\n");
printf("4. 购买宠物\n");
printf("5. 出售宠物\n");
printf("6. 退出系统\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addPet();
break;
case 2:
viewPets();
break;
case 3:
searchPets();
break;
case 4: buyPet(); break;
case 5: sellPet(); break;
default: printf ("无效的选择,请重新输入。\n"); break;
}
return 0;}
return 0;}
Loading…
Cancel
Save