C++期末大作业-简易通讯录

时间:2021-01-02 16:27:17

作为一个软件专业的学生,本来专业课是 Java,可惜我更喜欢 C++,所以呢,平时蹭一下 C++的课(不蹭不行啊,Java 老师说,如果我不上 Java 就必须上 C++)。

上周三,上了第一节 C++课,老师是我们院的席院长,讲得很好,虽然第一节净跟我们讲故事啦……课间,院长说,今年的大作业是一个简易的通讯录软件,按小组完成,需要实现朋友信息的添加、查询、修改等功能,一个简易不能再简易的需求了,接着,院长又给了评分细则,基础功能是80分,软件设计方案10分,图形化界面又是10分,满分一百分,谁都会算。

仔细想来,想要拿高分,基础功能肯定不能就是院长提出的那几个随便写写就行,应该需要尽量考虑更多实际情况中会需要的功能,于是我就多写了一些功能。至于软件设计方案嘛,我就先不写了,毕竟我是蹭课的,不用交作业,我只是凑到别人小组玩玩而已,所以详细设计方案我给他们大致说一下,剩下的交给他们完善就好啦。剩下的也就是图形化界面了,这个我也想好了,现在没空,QT虽然学了,但是没有实战过,所以真正使用起来,具有强迫症的我肯定问题很多,会耗费我竞赛前大量时间,等我慢慢抽空一点点完成吧,反正这不是目前最着急的事。

从上周四开始,我着手构建控制台版本的人脉关系管理软件(也就是院长说的那个通讯录),凌晨花了俩小时写了三分之一代码,第一次写这种小软件,也是遇见不少的问题,毕竟之前学过,却没有深究测试过,实际使用起来问题也是在所难免的,然后周五凌晨(其实已经是周六了)又写了一半,周末停电两天,今天中午才来电,花了一个半小时将后续完善了一下,并且测试了许久,终于完成了NIM 1.0版本,这个东西暂时不弄了,等我 QT 学得稍微好一些时再想办法弄上图形化界面,争取拿个高分回来……尽管我并没有这门课,但是帮助小组成员拿一个高分也是很棒的事情哦~~~

废话差不多已经说完了,这个代码加起来七百多行,对于一个软件而言,代码量实在是少得不要不要的,但是谁叫功能简单呢?(^__^) 嘻嘻……,下边是代码。

main.cpp

//
// main.cpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#include <iostream>
#include "user.hpp"
#include "connections.hpp"

int main(int argc, const char * argv[])
{
User user;
user.userOperate();

std::cout << ".\n.\n.\n";
std::cout << "程序已退出!\n";
return 0;
}

people.hpp

//
// people.hpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#ifndef people_hpp
#define people_hpp

#include <stdio.h>
#include <string>

class People
{

protected:
int id;
std::string name;
int age;
std::string sex;
long long phone;
int state;
};

#endif /* people_hpp */

user.hpp

//
// user.hpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#ifndef user_hpp
#define user_hpp

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include "people.hpp"
#include "connections.hpp"

#define YES 1
#define NO 0
#define USERDATE "user.dat"
#define MYFRDATE "_MyFrd.dat"
#define TEMPDATE "temp.dat"

class User : public People
{

private:
std::string password;

// 是否是新用户
int judgeUser();

// 注册用户
int registerNewUser();

// 登陆用户
int loginUser();

// 用户界面
void userInf();

// 修改用户信息
void edit(int tag);

public:
User();
void userOperate();
};


#endif /* user_hpp */

user.cpp

//
// user.cpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#include "user.hpp"

User::User()
{
this->id = 0;
this->state = 1;
}

/*
* 用户操作
*/

void User::userOperate()
{
if (!judgeUser())
{
if (registerNewUser())
{
std::cout << "成功创建新用户 - " << this->name << " !\n";
while (!loginUser()) ;
userInf();
}
else
{
std::cout << "创建新用户失败!\n";
}
}
else
{
while (!loginUser()) ;
userInf();
}
}

/*
* 是否是老用户
* 老用户返回 YES,反之 NO
*/

int User::judgeUser()
{
printf("是否是老用户?\n");
printf("请输入 Y/N: ");

std::string s;

while (std::cin >> s)
{
if (s.length() == 1)
{
if (s == "Y")
{
return YES;
}
else if (s == "N")
{
return NO;
}
}
}

return NO;
}

/*
* 注册用户
* 创建新用户于 user.dat
* 创建成功返回 YES,否则 NO
*/

int User::registerNewUser()
{
User user;
std::fstream fp;
fp.open(USERDATE, std::ios::in);

// 判断是否打开成功
while (!fp)
{
fp.open(USERDATE, std::ios::in | std::ios::app);
}

std::cout << "\n---------------------\n";
std::cout << "注册用户\n";
std::cout << "---------------------\n";
std::cout << "请输入用户名: ";
std::cin >> this->name;
while (fp >> user.id >> user.name >> user.password >> user.age >> user.sex >> user.phone >> user.state)
{
if (user.id > this->id)
{
this->id = user.id;
}
if (user.name == this->name && user.state)
{
std::cout << "请重新输入用户名(用户名已存在): ";
std::cin >> this->name;
fp.seekg(std::ios::beg);
}
}

printf("请输入密码: ");
std::cin >> this->password;

printf("请输入年龄: ");
std::cin >> this->age;

printf("请输入性别: ");
std::cin >> this->sex;

printf("请输入手机号: ");
std::cin >> this->phone;

fp.close();

fp.open(USERDATE, std::ios::out | std::ios::app);

if (!fp)
{
return NO;
}

fp << this->id + 1 << ' ' << this->name << ' ' << this->password << ' '
<< this->age << ' ' << this->sex << ' ' << this->phone << ' ' << this->state << '\n';

fp.close();

// 新用户创建关系数据文件
std::ofstream fout(this->name + MYFRDATE);

if (!fout)
{
return NO;
}

fout.close();

return YES;
}

/*
* 登陆用户
* 登陆成功返回 YES,否则 NO
*/

int User::loginUser()
{
std::cout << "\n---------------------\n";
std::cout << "登录用户\n";
std::cout << "---------------------\n";
std::cout << "请输入用户名: ";
std::cin >> this->name;
std::cout << "请输入密码: ";
std::cin >> this->password;

User user;
std::ifstream fin(USERDATE, std::ios::in);

while (!fin)
{
fin.open(USERDATE, std::ios::in | std::ios::app);
}

while (fin >> user.id >> user.name >> user.password >> user.age >> user.sex >> user.phone >> user.state)
{
if (user.name == this->name && user.state && user.password == this->password)
{
std::cout << "登录成功,欢迎您~~~\n";
this->id = user.id;
this->age = user.age;
this->sex = user.sex;
this->phone = user.phone;
this->state = user.state;
return YES;
}
}

std::cout << "对不起,用户或密码出错!!!\n";
std::cout << "请重新登陆!\n";
return NO;
}

/*
* 用户界面
*/

void User::userInf()
{
std::cout << "\n---------------------\n";
std::cout << "个人信息\n";
std::cout << "---------------------\n";
std::cout << "姓名: " << this->name << '\n';
std::cout << "年龄: " << this->age << '\n';
std::cout << "性别: " << this->sex << '\n';
std::cout << "手机号: " << this->phone << '\n';

std::cout << "请选择操作: A.人脉信息 B.修改个人信息 C.删除用户 D.退出程序\n";
char op;
int flag = 1;

while (flag)
{
std::cin >> op;
flag = 0;
Connections C;

switch (op)
{
case 'A':
C.conOperate(this->name);
break;
case 'B':
edit(1);
break;
case 'C':
edit(0);
break;
case 'D':
std::cout << "������\n";
return ;
default:
std::cout << "无用命令,请重新选择: ";
flag = 1;
break;
}
}
}

/*
* 删除/修改个人信息
*/

void User::edit(int tag)
{
std::cout << "\n---------------------\n";
if (tag)
{
std::cout << "修改个人信息\n";
}
else
{
std::cout << "删除用户\n";
}
std::cout << "---------------------\n";

User user;
std::ifstream fin(USERDATE, std::ios::in);
std::ofstream fout(TEMPDATE, std::ios::out | std::ios::app);

while (!fin)
{
fin.open(USERDATE, std::ios::in | std::ios::app);
}
while (!fout)
{
fout.open(TEMPDATE, std::ios::out | std::ios::app);
}

while (fin >> user.id >> user.name >> user.password >> user.age >> user.sex >> user.phone >> user.state)
{
if (user.state)
{
if (user.name == this->name && user.password == this->password)
{
if (tag)
{
printf("请输入密码: ");
std::cin >> this->password;

printf("请输入年龄: ");
std::cin >> this->age;

printf("请输入性别: ");
std::cin >> this->sex;

printf("请输入手机号: ");
std::cin >> this->phone;

fout << this->id << ' ' << this->name << ' ' << this->password << ' '
<< this->age << ' ' << this->sex << ' ' << this->phone << ' ' << this->state << '\n';
}
}
else
{
fout << user.id << ' ' << user.name << ' ' << user.password << ' '
<< user.age << ' ' << user.sex << ' ' << user.phone << ' ' << user.state << '\n';
}
}
}

fin.close();
remove(USERDATE);

fout.close();
rename(TEMPDATE, USERDATE);

if (tag)
{
std::cout << "修改成功!\n";
}
else
{
std::cout << "删除成功!\n";
}

userInf();
}

connections.hpp

//
// connections.hpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#ifndef connections_hpp
#define connections_hpp

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include "people.hpp"
#include "user.hpp"

#define YES 1
#define NO 0
#define USERDATE "user.dat"
#define MYFRDATE "_MyFrd.dat"
#define TEMPDATE "temp.dat"

class Connections : public People
{

private:
int relationship;

// 增加关系
void add(std::string name);

// 查找关系
void seek(std::string name);

// 查看关系
void look(std::string name);

// 删除/修改关系
void edit(std::string name, int tag);

public:
Connections();
void conOperate(std::string name);
};

#endif /* connections_hpp */

connections.cpp

//
// connections.cpp
// NIM
//
// Created by ZYJ on 2017/2/23.
// Copyright © 2017年 ZYJ. All rights reserved.
//

#include "connections.hpp"

Connections::Connections()
{
this->id = 0;
this->state = 1;
this->relationship = 0;
}

/*
* 人脉关系操作
*/

void Connections::conOperate(std::string name)
{
std::cout << "\n---------------------\n";
std::cout << "人脉\n";
std::cout << "---------------------\n";
std::cout << "请选择操作: A.添加人脉 B.删除人脉 C.查找人脉 D.查看人脉 E.修改人脉 F.退出程序\n";
char op;
int flag = 1;

while (flag)
{
std::cin >> op;
flag = 0;

switch (op)
{
case 'A':
add(name);
break;
case 'B':
edit(name, 0);
break;
case 'C':
seek(name);
break;
case 'D':
look(name);
break;
case 'E':
edit(name, 1);
break;
case 'F':
std::cout << "������\n";
return ;
default:
std::cout << "无用命令,请重新选择: ";
flag = 1;
break;
}
}

conOperate(name);
}

/*
* 添加人脉
*/

void Connections::add(std::string name)
{
Connections con;
std::string MFD = name + MYFRDATE;
std::fstream fp;
fp.open(MFD, std::ios::in);

// 判断是否打开成功
while (!fp)
{
fp.open(MFD, std::ios::in | std::ios::app);
}

std::cout << "\n---------------------\n";
std::cout << "添加人脉\n";
std::cout << "---------------------\n";
std::cout << "请输入姓名: ";
std::cin >> this->name;
while (fp >> con.id >> con.name >> con.relationship >> con.age >> con.sex >> con.phone >> con.state)
{
if (con.id > this->id)
{
this->id = con.id;
}
if (con.name == this->name && con.state)
{
std::cout << "请重新输入姓名(该姓名已经存): ";
std::cin >> this->name;
fp.seekg(std::ios::beg);
}
}

std::cout << "请输入关系指数(0~9): ";
std::cin >> this->relationship;
if (this->relationship > 9 || this->relationship < 0)
{
std::cout << "请重新输入关系指数(指数不合法): ";
std::cin >> this->relationship;
}

std::cout << "请输入年龄: ";
std::cin >> this->age;

std::cout << "请输入性别: ";
std::cin >> this->sex;

std::cout << "请输入手机号: ";
std::cin >> this->phone;

fp.close();

fp.open(MFD, std::ios::out | std::ios::app);

while (!fp)
{
fp.open(MFD, std::ios::out | std::ios::app);
}

fp << this->id + 1 << ' ' << this->name << ' ' << this->relationship << ' '
<< this->age << ' ' << this->sex << ' ' << this->phone << ' ' << this->state << '\n';

fp.close();
}

/*
* 删除/修改人脉
*/

void Connections::edit(std::string name, int tag)
{
std::cout << "\n---------------------\n";
if (tag)
{
std::cout << "修改人脉\n";
}
else
{
std::cout << "删除人脉\n";
}
std::cout << "---------------------\n";

std::cout << "请输入姓名: ";
std::cin >> this->name;

Connections con;
std::string MFD = name + MYFRDATE;
std::ifstream fin(MFD, std::ios::in);
std::ofstream fout(TEMPDATE, std::ios::out | std::ios::app);

while (!fin)
{
fin.open(MFD, std::ios::in | std::ios::app);
}
while (!fout)
{
fout.open(TEMPDATE, std::ios::out | std::ios::app);
}

while (fin >> con.id >> con.name >> con.relationship >> con.age >> con.sex >> con.phone >> con.state)
{
if (con.state)
{
if (con.name == this->name)
{
if (tag)
{
std::cout << "请输入关系指数: ";
std::cin >> this->relationship;

std::cout << "请输入年龄: ";
std::cin >> this->age;

std::cout << "请输入性别: ";
std::cin >> this->sex;

std::cout << "请输入手机号: ";
std::cin >> this->phone;

fout << this->id << ' ' << this->name << ' ' << this->relationship << ' '
<< this->age << ' ' << this->sex << ' ' << this->phone << ' ' << this->state << '\n';
}
}
else
{
fout << con.id << ' ' << con.name << ' ' << con.relationship << ' '
<< con.age << ' ' << con.sex << ' ' << con.phone << ' ' << con.state << '\n';
}
}
}

fin.close();
remove(MFD.c_str());

fout.close();
rename(TEMPDATE, MFD.c_str());

if (tag)
{
std::cout << "修改成功!\n";
}
else
{
std::cout << "删除成功!\n";
}
}

/*
* 查找人脉
*/

void Connections::seek(std::string name)
{
std::cout << "\n---------------------\n";
std::cout << "查找人脉\n";
std::cout << "---------------------\n";

std::string sur;
std::cout << "请输入要查找人脉的姓氏: ";
std::cin >> sur;

int cnt = 0;
Connections con;
std::string MFD = name + MYFRDATE;
std::ifstream fin(MFD, std::ios::in);

while (!fin)
{
fin.open(MFD, std::ios::in | std::ios::app);
}

while (fin >> con.id >> con.name >> con.relationship >> con.age >> con.sex >> con.phone >> con.state)
{
int flag = 1;
for (int i = 0; i < sur.length(); i++)
{
if (sur[i] != con.name[i])
{
flag = 0;
break;
}
}
if (flag)
{
cnt++;
std::cout << con.name << '\n';
}
}

if (!cnt)
{
std::cout << "对不起,无匹配信息~~~\n";
}
}

/*
* 查看人脉
*/

void Connections::look(std::string name)
{
std::cout << "\n---------------------\n";
std::cout << "查看人脉\n";
std::cout << "---------------------\n";

std::string sur;
std::cout << "请输入要查找人脉的姓名: ";
std::cin >> sur;

Connections con;
std::string MFD = name + MYFRDATE;
std::ifstream fin(MFD, std::ios::in);

while (!fin)
{
fin.open(MFD, std::ios::in | std::ios::app);
}

int flag = 1;
while (fin >> con.id >> con.name >> con.relationship >> con.age >> con.sex >> con.phone >> con.state)
{
if (sur == con.name)
{
std::cout << "姓名: ";
std::cout << con.name << '\n';

std::cout << "关系指数: ";
std::cout << con.relationship << '\n';

std::cout << "年龄: ";
std::cout << con.age << '\n';

std::cout << "手机号: ";
std::cout << con.phone << '\n';
flag = 0;
break;
}
}

if (flag)
{
std::cout << "对不起,无匹配信息~~~\n";
}
}

代码中有部分在 markdown 的解析下有问题,其实也是无关紧要的东西,真要问是什么,其实是三个再见的手掌小表情。