cap_wang 2020-05-27 18:47 采纳率: 100%
浏览 190
已采纳

对于一个结构体,如何能在执行程序后将其存储起来,下次执行程序时可以继续调用(修改)

以下是我写的一个通讯录,但是它的使用仅限于一次程序执行,如何每次执行都可以使用之前的数据
//Book.h
#include
using namespace std;
constexpr auto Max = 500;

//定义联系人的结构体
struct person
{
string name;
double age = 0;
string phone;
string address;
int sex = 0;
};

//定义通讯录数组
struct adds
{
person presonarray[Max];
int size = 0;
};
class interface
{

};

//定义工具类
class tool
{
public:int is_number(string x);
double change(string x);

};

//定义通讯本

class address_book
{
public:
int compare(string name);
void showmenu(); //显示菜单
void add(); //添加联系人
void show_contacts(); //显示联系人
void seek(); //查找联系人
void revise(); //修改联系人
void all_clear(); //清空所有联系人
void delete_person(); //删除联系人
adds a;
};

//Book.cpp
#include"Book.h"

int address_book::compare(string name)
{
for (int i = 0; i < a.size; i++)
{
if (a.presonarray[i].name == name)
{
return i;
}
}
return -1;
}

//显示菜单
void address_book::showmenu()
{
cout << "*****************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、查找联系人 *****" << endl;
cout << "***** 3、显示联系人 *****" << endl;
cout << "***** 4、删除联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "*****************************" << endl;
}

//添加联系人
void address_book::add()
{
tool tool;
cout << "请输入联系人的姓名:" << endl;
string name;
cin >> name;
a.presonarray[a.size].name = name;
string age;
double num = 0;
do
{
cout << "年龄(输入正整数):" << endl;
cin >> age;
int x = tool.is_number(age);
if (x == 1)
{
break;
}
else
{
cout << "输入有误,请输入正整数" << endl;
}
} while (true);
num = tool.change(age);
a.presonarray[a.size].age = num;
while (true)
{
cout << "性别: 1、男 2、女(输入数字)" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2)
{
a.presonarray[a.size].sex = sex;
break;
}
else
{
cout << "输入有误请重新输入" << endl;
}
}
cout << "电话:" << endl;
string phone;
cin >> phone;
a.presonarray[a.size].phone = phone;
cout << "地址" << endl;
string address;
cin >> address;
a.presonarray[a.size].address = address;
a.size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}

//显示联系人
void address_book::show_contacts()
{
if (a.size == 0)
{
cout << "暂无联系人" << endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i < a.size; i++)
{
cout << "姓名:" << a.presonarray[i].name << "\t";
cout << "年龄:" << a.presonarray[i].age << "\t";
cout << "性别:" << (a.presonarray[i].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[i].phone << "\t";
cout << "地址:" << a.presonarray[i].address << endl;
}
system("pause");
system("cls");
}

}

//查找联系人
void address_book::seek() {
string name;
cout << "请输入查找联系人的姓名:" << endl;
cin >> name;
if (compare(name) == -1) {
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else {
cout << "姓名" << a.presonarray[compare(name)].name << "\t";
cout << "年龄:" << a.presonarray[compare(name)].age << "\t";
cout << "性别:" << (a.presonarray[compare(name)].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[compare(name)].phone << "\t";
cout << "地址:" << a.presonarray[compare(name)].address << endl;
system("pause");
system("cls");
}
}

//修改联系人的信息
void address_book::revise()
{
cout << "请输入需要修改联系人的姓名:" << endl;
string name;
cin >> name;
int ret = compare(name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else {
cout << "姓名" << a.presonarray[compare(name)].name << "\t";
cout << "年龄:" << a.presonarray[compare(name)].age << "\t";
cout << "性别:" << (a.presonarray[compare(name)].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[compare(name)].phone << "\t";
cout << "地址:" << a.presonarray[compare(name)].address << endl;
cout << "请输入联系人的姓名:" << endl;
string name;
cin >> name;
a.presonarray[ret].name = name;
cout << "年龄:" << endl;
int age = 0;
cin >> age;
a.presonarray[ret].age = age;
while (true)
{
cout << "性别: 1、男 2、女" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2) {
a.presonarray[ret].sex = sex;
break;
}
else {
cout << "输入有误请重新输入" << endl;
}
}
cout << "电话:" << endl;
string phone;
cin >> phone;
a.presonarray[ret].phone = phone;
cout << "地址" << endl;
string address;
cin >> address;
a.presonarray[ret].address = address;
cout << "修改成功" << endl;
system("pause");
system("cls");

}

}

//清除所有联系人
void address_book::all_clear() {
for (int i = 0; i < a.size; i++) {
a.presonarray[i] = a.presonarray[a.size];
}
a.size = 0;
cout << "清除成功" << endl;
system("pause");
system("cls");
}

//删除选中的联系人
void address_book::delete_person() {
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
if (compare(name) == -1) {
cout << "查无此人" << endl;
}
else {
for (int i = compare(name); i < a.size; i++) {
a.presonarray[i] = a.presonarray[i + 1];
}
cout << "删除成功" << endl;
a.size--;
}
system("pause");
system("cls");

}
//判断字符串是否全部由数字组成
int tool::is_number(string x) {
int n = 0;
n = x.length();
int a = 0;
for (int i = 0; i < n; i++) {
if (x[i] >= '0' && x[i] <= '9') {
a++;
}
}
if (a == n) {
return 1;
}
else {
return 0;
}
}

//将字符串变为整形
double tool::change(string x) {
double n = x.length();
double num = 0;
for (int i = 0; i < n; i++) {
num += ((double)x[i] - 48) * pow(10, n - i - 1);
}
return num;
}

//main.cpp
#include"Book.h"
int main()
{
address_book b;
tool tool;
while (true)
{
b.showmenu();
string select;
cin >> select;
int select1 = (int)tool.change(select);
switch (select1)
{
case 1:
b.add();
break;
case 2:
b.seek();
break;
case 3:
b.show_contacts();
break;
case 4:
b.delete_person();
break;
case 5:
b.revise();
break;
case 6:
b.all_clear();
break;
case 0:
system("cls");
b.showmenu();
cout << "欢迎下次使用" << endl;
return 0;
default:
cout << "请输入0~6之间的整数" << endl;
system("pause");
system("cls");
break;
}
}
return 0;
}



  • 写回答

3条回答 默认 最新

  • 白色一大坨 2020-05-28 14:57
    关注

    给你看看我之前写的管理系统里面的保存类信息吧:

    class Student//学生类
    {
    public:
        string id;//定义见下面的show
        string name;
        string sex;
        string cl;
        string native;
        string birthday;
        string hobby;
        string family;
        string address;
    
        void input(string i, string n, string s, string c, string _native, string b, string h, string f, string _address)//设置类的变量
        {
            id = i;
            name = n;
            sex = s;
            cl = c;
            native = _native;
            birthday = b;
            hobby = h;
            family = f;
            address = _address;
        }
    
        void show()
        {
            cout << "学号:" << id 
                << " 姓名:" << name 
                << " 性别:" << sex 
                << " 班级:" << cl 
                << " 籍贯:" << native 
                << " 生日:" << birthday 
                << " 爱好:" << hobby 
                << " 家庭状况:" << family 
                << " 住址:" << address << endl;
        }
    
        void input(ifstream & is)//从文件读取
        {
            is >> id >> name >> sex >> cl >> native >> birthday >> hobby >> family >> address;
        }
    
        void output(ofstream & os)//写入文件
        {
            os << id
                << " " << name
                << " " << sex 
                << " " << cl 
                << " " << native 
                << " " << birthday 
                << " " << hobby 
                << " " << family 
                << " " << address 
                << endl;
        }
    
    };
    

    之后调用这个类时候,读写信息在这里:

        void save(string filename)//保存文件
        {
            ofstream write(filename.c_str(), ios::out);
            write << m_count << endl;//
            for (int i = 0; i < m_count; i++)
            {
                m_student[i].output(write);
            }
    
            write.close();
        }
    
        void load(string filename)//读取文件
        {
            ifstream read(filename.c_str(), ios::in);
            if (read.peek() == EOF)
            {
                read.close();
                return;
            }
    
            read >> m_count;
            for (int i = 0; i < m_count; i++)
            {
                m_student[i].input(read);
            }
    
            read.close();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?