功能有增加宠物信息,显示宠物信息,删除宠物信息,修改功能和排序功能,可以选择姓名排序,年龄排序,价格排序。进阶的功能有文件操作,动态内存开辟。。
test.c源文件
#include "Pet.h"void menu()
{printf("------------------------\n");printf("- 欢迎来到宠物商店 -\n");printf("- 1.add -\n");printf("- 2.show -\n");printf("- 3.del -\n");printf("- 4.modify -\n");printf("- 5.sort -\n");printf("- 欢迎来到宠物商店 -\n");printf("------------------------\n");}int main()
{printf("hello world\n");init(&shop);load(&shop);int input = 0;do{menu();scanf("%d", &input);switch (input){case 1:add(&shop);break;case 2:show(&shop);break;case 3:del(&shop);break;case 4:modify(&shop);break;case 5:sort(&shop);break;case 0:Save(&shop);Destroy(&shop);printf("退出程序\n");break;default:printf("你的输入不对,请重新输入\n");break;}} while (input);return 0;
}
Pet.c源文件
#include "Pet.h"int ssearch(struct Shop* pc, char* name);void init(struct Shop* pc)
{pc->data= (struct Pet*)malloc(init_value * (sizeof(struct Pet)));if (pc->data == NULL){perror("malloc");return;}pc->sz = 0;pc->capcity = init_value;
}int check(struct Shop* pc)
{if (pc->sz == pc->capcity){struct Pet* ptr = realloc(pc->data, (init_value + cre_value) * sizeof(struct Pet));if (ptr == NULL){perror("realloc");return 0;}pc->data = ptr;pc->capcity += cre_value;printf("增容成功\n");return 1;}return 1;
}void add(struct Shop* pc)
{int ret = check(pc);if (ret == 0){return;}printf("请输入名字:>\n");char name[20];scanf("%s", name);//scanf("%s", pc->data[pc->sz].name); int pos = ssearch(pc, name);if (pos != -1){printf("该宠物名字已存在,不能重名\n");return;}strcpy(pc->data[pc->sz].name, name);printf("请输入年龄:>\n");scanf("%d", &(pc->data[pc->sz].age));printf("请输入价格:>\n");scanf("%f", &(pc->data[pc->sz].price));printf("请输入性别:>\n");scanf("%s", pc->data[pc->sz].sex);printf("增加宠物成功\n");pc->sz++;
}void show(struct Shop* pc)
{int i = 0;printf("%-10s %-10s %-10s %-10s", "姓名", "年龄", "价格", "性别");printf("\n");for (i = 0; i < pc->sz; i++){printf("%-10s %-10d %-10.2f %-10s\n", pc->data[i].name,pc->data[i].age,pc->data[i].price,pc->data[i].sex);}
}void Save(struct Shop* pc)
{FILE* pf = fopen("test.txt", "wb");if (pf == NULL){perror("Save");return;}int i = 0;for (i = 0; i < pc->sz; i++){fwrite(pc->data+i, sizeof(struct Pet), 1, pf);//}//free(pf); 写错了,怪不得加上这个,文件里啥都没有 ,errfclose(pf);pf = NULL;
}void Destroy(struct Shop* pc)
{free(pc->data);pc->data = NULL;pc->capcity = 0;pc->sz = 0;
}void load(struct Shop* pc)
{FILE* pf = fopen("test.txt", "rb");if (pf == NULL){perror("load");return;}struct Pet tmp = {0};while (fread(&tmp, sizeof(struct Pet), 1, pf)>=1){if (check(pc) == 0){return;}pc->data[pc->sz] = tmp;pc->sz++;}if (feof(pf)){printf("end of file reach successfully\n");}else if (ferror(pf)){printf("i/o error when read\n");}fclose(pf);pf = NULL;
}int ssearch(struct Shop* pc,char* name)
{int i = 0;for (i = 0; i < pc->sz; i++){if (strcmp(name, pc->data[i].name) == 0){return i;}}return -1;
}void del(struct Shop* pc)
{if (pc->sz == 0){printf("没有宠物可删除\n");return;}printf("请输入要删除的名字:>\n");char name[20];scanf("%s", name);int ret = ssearch(pc,name);if (ret == -1){printf("没找到\n");return;}printf("找到了,是%s %d %f %s", pc->data[ret].name, pc->data[ret].age, pc->data[ret].price, pc->data[ret].sex);printf("\n");int i = 0;for (i = ret; i < pc->sz-1; i++)//i等于ret,小于最后一个下标{//pc->data[ret] = pc->data[ret + 1]; err//pc->data[i] = pc->data[i + 1];第一种方法memmove(&(pc->data[i]), &(pc->data[i + 1]), sizeof(struct Pet));//目的地,源头,要复制的字节数} printf("删除成功\n");pc->sz--;
}void modify(struct Shop* pc)
{printf("请输入你要查找修改的名字:>\n");char name[20];scanf("%s", name);int m = ssearch(pc, name);if (m == -1){printf("没找到要修改的名字\n");return;}printf("找到了,是%s %d %f %s", pc->data[m].name, pc->data[m].age, pc->data[m].price, pc->data[m].sex);printf("\n");printf("请输入修改后的名字:>\n");scanf("%s", name);int m1 = ssearch(pc, name);if (m1 != -1){printf("该宠物名字已存在,不能重名\n");return;}strcpy(pc->data[m].name, name);printf("请输入修改后的年龄:>\n");scanf("%d", &(pc->data[m].age));printf("请输入修改后的价格:>\n");scanf("%f", &(pc->data[m].price));printf("请输入修改后的性别:>\n");scanf("%s", pc->data[m].sex);
}int cmp1(const void* p1, const void* p2)//按姓名
{return strcmp(((struct Pet*)p1)->name, ((struct Pet*)p2)->name);
}
int cmp2(const void* p1, const void* p2)//按年龄
{return ((struct Pet*)p1)->age - ((struct Pet*)p2)->age;
}
int cmp3(const void* p1, const void* p2)//按价格
{if (((struct Pet*)p1)->price - ((struct Pet*)p2)->price > 0)return 1;if (((struct Pet*)p1)->price - ((struct Pet*)p2)->price < 0)return -1;return 0;
}void sort(struct Shop* pc)
{int sort1 = 0;do{printf("按姓名排序:>1\n");printf("按年龄排序:>2\n");printf("按价格排序:>3\n");printf("退出:>0\n");scanf("%d", &sort1);if (sort1 == 1)qsort(pc->data, pc->sz, sizeof(struct Pet), cmp1);if (sort1 == 2)qsort(pc->data, pc->sz, sizeof(struct Pet), cmp2);if (sort1 == 3)qsort(pc->data, pc->sz, sizeof(struct Pet), cmp3);if(sort1!=3&&sort1!=2&&sort1!=1&&sort1!=0)printf("选择不对,请重新选择\n");} while (sort1);}
Pet.h头文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define init_value 3
#define cre_value 2
#define NAME 20
#define SEX 5struct Pet
{char name[NAME];int age;float price;char sex[SEX];
};struct Shop
{struct Pet* data;int sz;int capcity;
};struct Shop shop;void init(struct Shop* pc);void add(struct Shop*pc);void show(struct Shop* pc);void Save(struct Shop* pc);void Destroy(struct Shop* pc);void load(struct Shop* pc);void del(struct Shop* pc);void modify(struct Shop* pc);void sort(struct Shop* pc);