likey003 / pianzhu

1 stars 0 forks source link

C语言通讯录管理系统 #5

Open likey003 opened 8 months ago

likey003 commented 8 months ago

include

include

include

include

include

include

include

pragma comment(lib,"winmm.lib")

define Max 1000

define B "\033[1m"

define reset "\033[0m"

define red "\033[31m"

define green "\033[32m"

define yellow "\033[33m"

define blue "\033[34m"

define purple "\033[35m"

define qianlan "\033[036m"

using namespace std; struct Person { string m_Name; string m_Id; int m_Sex; string m_Unit; string m_Phone; string m_Addr; };

struct Addressbooks { struct Person personArray[Max]; int m_Size; };

void addPerson(Addressbooks* abs) { if (abs->m_Size == Max) { cout << "通讯录已满,无法添加" << endl; return; } else { string name; cout << "请添加姓名" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name; cout << "请输入性别" << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; int sex = 0; while (1) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } cout << "你输入错了,重新输入 " << endl; } cout << "请输入学号" << endl; string id; cin >> id; abs->personArray[abs->m_Size].m_Id = id; cout << "请输入工作单位" << endl; string unid; cin >> unid; abs->personArray[abs->m_Size].m_Unit = unid; cout << "请输入电话" << endl; string phone; cin >> phone; abs->personArray[abs->m_Size].m_Phone = phone; cout << "请输入E-mail" << endl; string address; cin >> address; abs->personArray[abs->m_Size].m_Addr = address; abs->m_Size++; cout << " 你添加成功了" << endl; system("pause"); system("cls"); } }

void showPerson(Addressbooks* abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; }

else {
    for (int i = 0; i < abs->m_Size; i++) {
        cout << "姓名:" << abs->personArray[i].m_Name << "\t";
        cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
        cout << "学号:" << abs->personArray[i].m_Id << "\t";
        cout << "工作单位:" << abs->personArray[i].m_Unit << "\t";
        cout << "电话:" << abs->personArray[i].m_Phone << "\t";
        cout << "E-mail:" << abs->personArray[i].m_Addr << endl;
    }
}
system("pause");
system("cls");

}

int isExist(Addressbooks* abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Name == name) { return i; } } return -1; }

int isExist2(Addressbooks* abs, string id) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].m_Id == id) { return i; } } return -1; }

void deletePerson(Addressbooks* abs) { cout << "请输入要删除的人" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->m_Size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } abs->m_Size--; cout << "删除成功" << endl; } else { cout << "找不到此人" << endl; } system("pause"); system("cls");

}

void findPerson(Addressbooks* abs) { cout << "请输入你要查找的人的姓名或学号" << endl; cout << "1 --- 姓名" << endl; cout << "2 --- 学号" << endl; string name; string id; int temp = 0; while (1) { cin >> temp; if (temp == 1) { cout << "请输入你要查找的人的姓名" << endl; cin >> name; int ret = isExist(abs, name); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"; cout << "学号: " << abs->personArray[ret].m_Id << "\t"; cout << "工作单位:" << abs->personArray[ret].m_Unit << "\t"; cout << "电话:" << abs->personArray[ret].m_Phone << "\t"; cout << "E-mail:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查无此人" << endl; } break; } if (temp == 2) { cout << "请输入你要查找的人的学号" << endl; cin >> id; int ret = isExist2(abs, id); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"; cout << "学号: " << abs->personArray[ret].m_Id << "\t"; cout << "工作单位:" << abs->personArray[ret].m_Unit << "\t"; cout << "电话:" << abs->personArray[ret].m_Phone << "\t"; cout << "E-mail:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查无此人" << endl; } break; } cout << "你输入错了,重新输入 " << endl; }

system("pause");
system("cls");

}

void modfyPerson(Addressbooks* abs) { cout << "请输入你要修改的联系人" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { string name; cout << "请输入你想修改成的名字" << endl; cin >> name; abs->personArray[ret].m_Name = name; cout << "请输入你想修改成的性别,1->男,2->女" << endl; int sex = 0; cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; } cout << "请输入你想修改成的学号" << endl; string id; cin >> id; abs->personArray[ret].m_Id = id; cout << "请输入你想修改成的工作单位" << endl; string unit; cin >> unit; abs->personArray[ret].m_Unit = unit; cout << "请输入你想修改成的电话" << endl; string phone; cin >> phone; abs->personArray[ret].m_Phone = phone; cout << "请输入你想修改成的E-mail" << endl; string address; cin >> address; abs->personArray[ret].m_Addr = address; cout << "恭喜您,修改成功!" << endl; } else { cout << "查无此人" << endl; } system("pause"); system("cls"); }

void cleanPerson(Addressbooks* abs) { abs->m_Size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); }

void Population(Addressbooks abs) { cout << "人数为:" << abs->m_Size << endl; system("pause"); system("cls"); } void write(Addressbooks abs)//将数据写入文件 { FILE* fp; char filename[30]; if (!abs->personArray) { printf("\t\t该通讯录为空!"); } printf("\t\t写入到文件!\n"); printf("\t\t请输入所写入的文件名:"); scanf("%s", filename); if (!(fp = fopen(filename, "a+"))) { printf("\t\t无法打开文件!\n"); system("pause"); } else { fprintf(fp, "**通讯录列表**\n"); fprintf(fp, "姓名\t\t手机号码\tQQ号码\t\t电子邮箱\n"); fprintf(fp, "----------------------------------------------------------------------\n"); for (int i = 0; i < abs->m_Size; i++) { fprintf(fp, "%s,%d,%s,%s,%s,%s\n", abs->personArray[i].m_Name, abs->personArray[i].m_Sex, abs->personArray[i].m_Id, abs->personArray[i].m_Unit, abs->personArray[i].m_Phone, abs->personArray[i].m_Addr); fprintf(fp, "----------------------------------------------------------------------\n"); } fprintf(fp, "\n**共%d个联系人**\n", abs->m_Size); fclose(fp); //关闭文件
printf("\t\t写入成功!\n"); system("pause"); system("cls"); } }

void read(Addressbooks abs)//读取文件 { char str[100]; char filename[30]; FILE fp; printf("\t\t读取文件!\n"); printf("\t\t请输入所写入的文件名:"); scanf("\t\t%s", filename); if ((fp = fopen(filename, "a+"))&&fp) { printf("\t\t无法打开文件!\n"); system("pause"); } while((fgets(str, 100, fp))&&fp) { printf("\t\t%s", str); } system("pause"); system("cls"); }

void showMenu() { cout << endl; cout << endl; cout << endl; cout << endl; cout << "\t\t\t\t\t\t " <<yellow B " 通讯录管理系统" << endl; cout << reset qianlan B" \t\t\t\t\t\t *****" << endl; cout << " \t\t\t\t\t\t * 1.添加联系人 " << endl; cout << " \t\t\t\t\t\t 2.显示联系人 " << endl; cout << " \t\t\t\t\t\t 3.删除联系人 " << endl; cout << " \t\t\t\t\t\t 4.查找联系人 " << endl; cout << " \t\t\t\t\t\t 5.修改联系人 " << endl; cout << " \t\t\t\t\t\t 6.清空联系人 " << endl; cout << " \t\t\t\t\t\t 7.查找总人数 " << endl; cout << " \t\t\t\t\t\t 8.写入文件 " << endl; cout << " \t\t\t\t\t\t 9.读取文件 " << endl; cout << " \t\t\t\t\t\t 10.退出通讯录 *" << endl; cout << " \t\t\t\t\t\t *****" << endl; }

int main() { mciSendString("open 1.mp3", 0, 0, 0); mciSendString("play 1.mp3", 0, 0, 0); Addressbooks abs;

abs.m_Size = 0;
int select = 0;
while (1) {
    showMenu();
     cin >> select;
    switch (select) {
    case 1:
        addPerson(&abs);
        break;
    case 2:
        showPerson(&abs);
        break;
    case 3:
        deletePerson(&abs);
        break;
    case 4:
        findPerson(&abs);
        break;
    case 5:
        modfyPerson(&abs);
        break;
    case 6:
        cleanPerson(&abs);
        break;
    case 7:
        Population(&abs);
        break;
    case 8:
         write(&abs);
        break;
    case 9:
        read(&abs);
        break;
    case 10:
        cout << "欢迎下次使用" << endl;
        system("pause");
        return 0;
        break;
    default:
        cout << "输入错误,请重新输入" << endl;
        system("pause");
        system("cls");

    }
}
system("pause");
return 0;

}