ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【C语言小项目】通讯录管理系统

2022-07-28 14:00:07  阅读:205  来源: 互联网

标签:count fp addrinfo 管理系统 ++ C语言 通讯录 printf name


 

项目描述:

1.新建一个通讯录系统菜单

2.系统功能包括联系人信息的新建、查询、修改、删除、展示、排序

3.联系人信息包括姓名、职业、电话、邮箱、地址

 

项目地址:

https://www.bilibili.com/video/BV15W411H7wz?p=9&vd_source=c9d2eeb3572d5cf9ab5c79fa78ebf512

源码:

//#include <stdio.h>

//
//int main() {
//    printf("sizeof(char) =%u\n", (unsigned)sizeof(char));//1
//    printf("sizeof(short)=%u\n", (unsigned)sizeof(short));//2
//    printf("sizeof(int)  =%u\n", (unsigned)sizeof(int));//4
//    printf("sizeof(long) =%u\n", (unsigned)sizeof(long));//4
//
//    return 0;
//}


//----------------
//---------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string.h>

//宏定义
#define LEN sizeof(struct addritem)//定义结构体的长度//
#define FORMAT "%-10s%-10s%-15s%-25s%-30s%\n"//这个宏规定了输出的格式//
#define DATA addrinfo[i].name,addrinfo[i].occu,addrinfo[i].tel,addrinfo[i].email,addrinfo[i].address//代表结构体数组元素中的各个成员// 
//定义结构体成员//
struct addritem {
    char name[10];
    char occu[10];
    char tel[15];
    char email[25];
    char address[30];
};
struct addritem addrinfo[100];//定义结构体数组//
//声明程序中要用到的函数//
void input();
void search();
void update();
void del();
void display();
void sort();
void menu();

void menu() { //主菜单函数//
    system("cls");//调用cls函数清空界面//
    printf("\n\n\n\n\n");//控制主功能菜单显示位置//
    printf("\t\t|----------------------------------CONTACT----------------------------------------------------|\n");
    printf("\t\t|0. exit                                                                                      |\n");
    printf("\t\t|1. input  record                                                                             |\n");
    printf("\t\t|2. search  record                                                                            |\n");
    printf("\t\t|3. update  record                                                                            |\n");
    printf("\t\t|4. delete  record                                                                            |\n");
    printf("\t\t|5. sort                                                                                      |\n");
    printf("\t\t|6. display                                                                                   |\n");
    printf("\t\t|---------------------------------------------------------------------------------------------|\n");
    printf("\t\t\tchoose(0-6):");
}

int main() { //主函数//
    int n;
    menu();//调用主菜单//
    scanf("%d", &n); //用户选择功能//
    while (n) { //选择结构进入相应的功能//
        switch (n) {
            case 1:
                input();
                break;
            case 2:
                search();
                break;
            case 3:
                update();
                break;
            case 4:
                del();
                break;
            case 5:
                sort();
                break;
            case 6:
                display();
                break;
            default:
                break;
        }
        getch();
        menu();//再次调用主菜单//
        scanf("%d", &n);
    }
}

void input() {
    int i, count = 0; //定义一个循环变量以及记录通讯录中联系人个数的变量//
    char ch[2];//字符数组用来记录用户的输入//
    FILE *fp;//定义指向文件的指针变量//

    //a+表示附加,如果文件不存在,新创建一个文件;如果存在,内容追加在文件尾部
    if ((fp = fopen("data.txt", "a+")) == NULL) { //调用fopen函数创建一个新的文本来保存联系人信息//
        printf("can not open!\n");
        return;
    }
    while (!feof(fp)) { //判断文件是否结束//
        //fread的第一位表示指针位置,第二位表示 成员变量大小
        //fread的第三位表示每次只读取一条记录
        //fread的第四位表示输入流,即从fp文件中读取
        if (fread(&addrinfo[count], LEN, 1, fp) == 1) //使用fresd函数将文件中的记录逐条读取到结构体数组//
            count++;
    }
    fclose(fp);//关闭文件//
    if (count == 0) //判断当前通讯录是否为空//
        printf("No contact record!\n");
    else {
        system("cls");//清屏//
        display();//显示联系人的所有信息//
    }
    //fopen中的wb表示write binary,只允许写数据
    if ((fp = fopen("data.txt", "wb")) == NULL) { //如果打开文件失败//
        printf("can not open address list!\n");
        return;
    }
    //将数据重写入磁盘//
    for (i = 0; i < count; i++)
        fwrite(&addrinfo[i], LEN, 1, fp);
    printf("please input(y/n)");//提示用户输入//
    scanf("%s", ch);
    while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) { //判断用户是否需要输入//
        printf("name:");
        scanf("%s", &addrinfo[count].name);
        //判断用户输入姓名在通讯录中是否存在//
        for (i = 0; i < count; i++)
            if (strcmp(addrinfo[i].name, addrinfo[count].name) == 0) {
                printf("The name already exists,press any key to continue.");
                getch();
                fclose(fp);
                return;
            }
        //如果通讯录找不到姓名,则输入其他信息
        printf("cooupation:");
        scanf("%s", &addrinfo[count].occu);
        printf("telephone:");
        scanf("%s", &addrinfo[count].tel);
        printf("email:");
        scanf("%s", &addrinfo[count].email);
        printf("address:");
        scanf("%s", &addrinfo[count].address);
        //如果写入失败,输出提示信息
        if (fwrite(&addrinfo[count], LEN, 1, fp) != 1) { //将新建联系人信息保存到磁盘//
            printf("can not save the record!");
            getch();
        } else {
            printf("%s saved!\n", addrinfo[count].name);
            count++;
        }
        printf("continue?(y/n):");
        scanf("%s", ch);
    }
    fclose(fp);
    printf("OK!\n");
}

void search() {
    FILE *fp;
    int i, count = 0;
    char ch[2], name[15];//定义数组接收用户的输入信息//
    //打开磁盘,并将磁盘中的通讯录逐条读取到内存结构体数组//
    //rb 只读二进制文件,只允许读取
    if ((fp = fopen("data.txt", "rb")) == NULL) {
        printf("can not open\n");
        return;
    }
    while (!feof(fp))//判断是否到达文件的尾部//
        if (fread(&addrinfo[count], LEN, 1, fp) == 1)
            count++;//count终值为通讯录条目//
    fclose(fp);
    if (count == 0) {
        printf("no record!\n");
        return;
    }
    printf("please input the name:");
    scanf("%s", name);
    //查找通讯录指定信息,运用字符比较函数//
    for (i = 0; i < count; i++)
        //如果为0,表示两字符串是相同的
        if (strcmp(name, addrinfo[i].name) == 0) {
            printf("find the contact,display?(y/n):");
            scanf("%s", ch);
            if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {
                printf("name      occupation        telephone        email          address  \t\n ");
                printf(FORMAT, DATA);
            }
            break;
        }
    //判断循环结束是因为没有找到联系人还是因为找到了但是没有显示联系人信息//
    if (i == count)
        printf("can not find the contact!\n");
}

void update() {
    FILE *fp;
    int i, j, count = 0;
    char name[15];
    if ((fp = fopen("data.txt", "r+")) == NULL) {
        printf("can not open!\n");
        return;
    }
    while (!feof(fp))
        if (fread(&addrinfo[count], LEN, 1, fp) == 1)
            count++;
    if (count == 0) {
        printf("no record!\n");
        fclose(fp);
        return;
    }
    display();
    //修改联系人信息//
    printf("please input the name of the contact which you want to update!\n");
    printf("updata name:");
    scanf("%s", &name);
    for (i = 0; i < count; i++) {
        if (strcmp(name, addrinfo[i].name) == 0) {
            printf("find the contact!you can updata!\n");
            printf("name:");
            scanf("%s", &addrinfo[i].name);
            printf("occupation:");
            scanf("%s", &addrinfo[i].occu);
            printf("telephone:");
            scanf("%s", &addrinfo[i].tel);
            printf("email:");
            scanf("%s", &addrinfo[i].email);
            printf("address:");
            scanf("%s", &addrinfo[i].address);
            printf("updata succeaafully!");
            //打开磁盘,将修改后的联系人信息写入磁盘//
            if ((fp = fopen("data.txt", "wb")) == NULL) {
                printf("can not open!\n");
                return;
            }
            //如果打开成功,则将文件信息写入磁盘文件中
            for (j = 0; j < count; j++)
                //如果写入不成功,提示无法保存
                if (fwrite(&addrinfo[j], LEN, 1, fp) != 1) {
                    printf("can not save!");
                    getch();
                }
            fclose(fp);
            return;
        }
    }
    printf("not find the contact!\n");//没有找到该条联系人信息//
}

void del() {
    FILE *fp;
    int i, j, count = 0;
    char ch[2];
    char name[15];
    //打开文件,如果没有打开,提示can not open
    if ((fp = fopen("data.txt", "r+")) == NULL) {
        printf("can not open!\n");
        return;
    }
    //打开文件后进行读取
    //读取首先进行判断,读取完毕,关闭文件
    while (!feof(fp))
        if (fread(&addrinfo[count], LEN, 1, fp) == 1)
            count++;
    fclose(fp);
    //
    if (count == 0) {
        printf("no record!\n");
        return;
    }
    //显示当前所有联系人
    display();
    //删除联系人姓名//
    printf("please input the name:");
    scanf("%s", &name);

    for (i = 0; i < count; i++) {
        if (strcmp(name, addrinfo[i].name) == 0) {
            printf("find the contact,del?(y/n)");
            scanf("%s", ch);
            if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0)
                //for循环删除联系人信息,删除完成后count--是record减1
                for (j = i; j < count; j++)
                    addrinfo[j] = addrinfo[j + 1];
            count--;
            //将删除后的通讯录写入对应的磁盘文件//
            if ((fp = fopen("data.txt", "wb")) == NULL) {
                printf("can not open!\n");
                return;
            }
            //删除后,要进行联系人信息重新写入
            for (j = 0; j < count; j++)
                //fwrite逐条写入
                if (fwrite(&addrinfo[j], LEN, 1, fp) != 1) {
                    printf("can not save!\n");
                    getch();
                }
            fclose(fp);
            //没有找到联系人的提示信息
            printf("del succesfully!\n");
            return;
        }
    }
    printf("not find the contact!\n");
}

void sort() {
    FILE *fp;
    struct addritem t;//中间变量保存联系人信息
    int i = 0, j = 0, count = 0;
    if ((fp = fopen("data.txt", "r+")) == NULL) {
        printf("can not open!\n");
        return;
    }
    while (!feof(fp))
        if (fread(&addrinfo[count], LEN, 1, fp) == 1)
            count++;
    fclose(fp);
    if (count == 0) {
        printf("no record!\n");
        return;
    }
    //将前面变量与后面的变量进行比较
    for (i = 0; i < count - 1; i++)
        for (j = i + 1; j < count; j++)
            //strcmp比较的是ASCII值
            //如果第一个字符串大于第二个字符串,返回正数
            if (strcmp(addrinfo[i].name, addrinfo[j].name) > 0) {
                t = addrinfo[i];
                addrinfo[i] = addrinfo[j];
                addrinfo[j] = t;
            }
    if ((fp = fopen("data.txt", "wb")) == NULL) {
        printf("can not open!\n");
        return;
    }
    for (i = 0; i < count; i++)
        if (fwrite(&addrinfo[i], LEN, 1, fp) != 1) {
            printf("can not save!\n");
            getch();
        }
    fclose(fp);
    printf("save successfully!\n");
}

void display() {
    FILE *fp;
    int i, count = 0;
    fp = fopen("data.txt", "rb");//以读写的方式打开文件//
    //读取文件通讯录//
    while (!feof(fp)) {
        if (fread(&addrinfo[count], LEN, 1, fp) == 1)
            count++;
    }
    fclose(fp);//关闭文件//
    printf("name   occupation    telephone    email     address\t\n");
    for (i = 0; i < count; i++)
        printf(FORMAT, DATA);
}

 

标签:count,fp,addrinfo,管理系统,++,C语言,通讯录,printf,name
来源: https://www.cnblogs.com/sdr900/p/16528396.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有