语言:C语言
环境:vs2019社区版
安装包:Windows安装包-通讯录
话不多说,源代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n = 0;
struct Person {
char name[10];
char phone[20];
char address[20];
char email[20];
}stu[10], temp;
void set() {
FILE* fp;
fp = fopen("C:\\Users\\Administrator\\Desktop\\联系人.txt", "a+");
fclose(fp);
}
void read() {
FILE* fp;
int i = 0;
fp = fopen("C:\\Users\\Administrator\\Desktop\\联系人.txt", "rb");
fread(&n, sizeof(int), 1, fp);
for (i = 0; i < n; i++) {
fread(&stu[i], sizeof(struct Person), 1, fp);
}
fclose(fp);
}
void save() {
int i;
FILE* fp;
fp = fopen("C:\\Users\\Administrator\\Desktop\\联系人.txt", "wb");
fwrite(&n, sizeof(int), 1, fp);
for (i = 0; i < n; i++) {
fwrite(&stu[i], sizeof(struct Person), 1, fp);
}
fclose(fp);
}
void paixu1() {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (strcmp(stu[j].name, stu[j+1].name) > 0) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void paixu2() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(stu[j].name, stu[j + 1].name) < 0) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void paixu3() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(stu[j].phone, stu[j + 1].phone) > 0) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void paixu4() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(stu[j].phone, stu[j + 1].phone) < 0) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void new(){
int a = 1;
do {
system("CLS");
printf("------------------------------\n 新建\n------------------------------\n请输入姓名:");
scanf("%s", stu[n].name);
printf("请输入电话:");
scanf("%s", stu[n].phone);
printf("请输入地址:");
scanf("%s", stu[n].address);
printf("请输入email:");
scanf("%s", stu[n].email);
printf("\n请输入(1.新建 0.完成):");
scanf("%d", &a);
n++;
} while (a != 0);
save();
}
void output() {
system("CLS");
printf("------------------------------\n 联系人\n------------------------------\n序号 姓名 电话 地址 email\n");
for (int i = 0; i < n; i++) {
printf("%-6d%-10s%-20s%-20s%-20s\n", i + 1, stu[i].name, stu[i].phone, stu[i].address, stu[i].email);
}
getch();
}
void delete() {
output();
int a;
printf("------------------------------\n 删除\n------------------------------\n请输入要删除的序号(0全部删除):");
scanf("%d", &a);
if (a == 0) {
for (int i = 0; i < n; i++) {
stu[i] = stu[n];
n--;
}
}
for (a; a < n; a++) {
stu[a - 1] = stu[a];
}
n--;
save();
printf("\n删除成功\n");
getch();
}
void alter() {
output();
int a, b;
printf("------------------------------\n 修改\n------------------------------\n");
while (1) {
printf("请输入序号(0返回菜单):");
scanf("%d", &a);
if (a == 0) {
break;
}
printf("修改(1.姓名 2.电话 3.地址 4.email):");
scanf("%d", &b);
if (b == 1) {
printf("请输入新的姓名:");
scanf("%s", stu[a - 1].name);
}
if (b == 2) {
printf("请输入新的电话:");
scanf("%s", stu[a - 1].phone);
}
if (b == 3) {
printf("请输入新的地址:");
scanf("%s", stu[a - 1].address);
}
if (b == 4) {
printf("请输入新的email:");
scanf("%s", stu[a - 1].email);
}
printf("\n修改成功\n\n");
}
save();
system("CLS");
}
void find() {
int a;
system("CLS");
printf("------------------------------\n 查询\n------------------------------\n1.直接查询\n2.姓名由小到大查询\n3.姓名由大到小查询\n4.电话由小到大查询\n5.电话由大到小查询\n请输入1-5:");
scanf("%d", &a);
switch (a) {
case 1:break;
case 2:paixu1(); break;
case 3:paixu2(); break;
case 4:paixu3(); break;
case 5:paixu4(); break;
}
system("CLS");
printf("------------------------------\n 联系人\n------------------------------\n序号 姓名 电话 地址 email\n");
for (int i = 0; i < n; i++) {
printf("%-6d%-10s%-20s%-20s%-20s\n", i + 1, stu[i].name, stu[i].phone, stu[i].address, stu[i].email);
}
getch();
}
void main() {
int a;
set();
read();
printf("------------------------------\n 通讯录v2.0功能\n\n1.开放联系人邮箱,地址等信息存储功能\n2.查询新增排序功能,按姓名或电话由小到大、由大到小排序输出\n3.优化界面,修复部分bug\n4.新增删除全部联系人功能\n\n\n\n\n\n\n\n开发者:DWH\n版本:2.0\n\n------------------------------\n");
getch();
system("CLS");
do {
system("CLS");
printf("------------------------------\n 通讯录\n------------------------------\n 1.新建联系人\n 2.删除联系人\n 3.修改联系人\n 4.查询联系人\n 0.退出\n------------------------------\n请输入0-4:");
scanf("%d", &a);
switch (a) {
case 1:new(); break;
case 2:delete(); break;
case 3:alter(); break;
case 4:find(); break;
}
} while (a != 0);
system("CLS");
printf("\n程序已退出\n");
}
|