题目要求:
说明:此博客仅作为备份记录使用。这是一道本科期末考题,较为简单。同时,时间也比较紧张,代码难免有写得不好的地方。c++读取和保存txt文档的内容到数组部分,有一点的参考意义。
政府某部门需在来话呼入时通过运营商的接口匹配来话用户所在位置和号码所有人信息,在业务电脑的屏幕上将电话号码、号码所有人信息、用户所在位置显示出来。
请用C++语言面向对象的程序设计方式设计运行于该部门业务电脑的程序并简述设计方法和思路。
实现思路:
读取数据库文件,保存到列表中,当有新的电话打进来,需要查询电话用户信息个人信息时,就可以遍历保存好的数据库列表,通过比较号码判断用户是谁,从而提取个人信息。
难点:
读取txt文件并保存到数组中,因为这些数据库信息可以看作是二维数组,c++常用的库中没有可以读取文件报错到二维数组中的函数,所以只能按行读取,再处理数据。
核心代码:
string str[9];
istringstream is(line);
is >> str[0] >> str[1] >> str[2] >> str[3] >> str[4] >> str[5] >> str[6] >> str[7] >> str[8];`
完整代码:
#include <fstream>
#include <string>
#include <iostream>
#include<ctype.h>
#include <sstream>
using namespace std;
class QUERY
{
public:
QUERY(string mobile_sql, string telecom_sql)
{
mobile = mobile_sql;
telecom = telecom_sql;
}; //导入数据库文件
void read_moblie();//移动电话数据库查询,把txt内容转移到传入的参数数组中
void read_telecom();//座机数据库查询
void read_call_in(string call_in_sql);
void printf_moblie(int using_select);
void printf_telecom(int using_select);
void print_caller_information(string test_phone);
~QUERY();
string read_list[20][2];
//封装号码数据库文件,提高安全性能
private:
string mobile;
string telecom;
string mobile_list[20][9];
string telecom_list[20][9];
};
QUERY::~QUERY()
{
}
//一i共9列
void QUERY::read_moblie()
{
ifstream in(mobile);
string filename;
string line;
//string read_list[100][2];//行列
int hang_count = 0, lie_count = 0;
if (in) // 有该文件
{
while (getline(in, line)) // line中不包括每行的换行符
{
/*cout << line<< endl;*/
string str[9];
istringstream is(line);
is >> str[0] >> str[1] >> str[2] >> str[3] >> str[4] >> str[5] >> str[6] >> str[7] >> str[8];
/*cout << str[0] << "," << str[1] << endl;*/
for (int lie = 0; lie < 9; lie++)
{
mobile_list[hang_count][lie] = str[lie];
}
/*
read_list[hang_count][0] = str[0];
read_list[hang_count][1] = str[1];
*/
/*for (int k = 0; k < length; k++)
{
cout << line[k] << " ";
}*/
hang_count++;
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
}
//座机数据库查询
void QUERY::read_telecom()
{
ifstream in(telecom);
string filename;
string line;
//string read_list[100][2];//行列
int hang_count = 0, lie_count = 0;
if (in) // 有该文件
{
while (getline(in, line)) // line中不包括每行的换行符
{
/*cout << line<< endl;*/
string str[9];
istringstream is(line);
is >> str[0] >> str[1] >> str[2] >> str[3] >> str[4] >> str[5];
/*cout << str[0] << "," << str[1] << endl;*/
for (int lie = 0; lie < 6; lie++)
{
telecom_list[hang_count][lie] = str[lie];
}
/*
read_list[hang_count][0] = str[0];
read_list[hang_count][1] = str[1];
*/
/*for (int k = 0; k < length; k++)
{
cout << line[k] << " ";
}*/
hang_count++;
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
}
void QUERY::printf_moblie(int using_select)
{
//string mobile_list[20][9];//行列
////cout << "移动电话来电者用户信息"<<endl;
//read_moblie();
for (int i = 1; i < 9; i++)
{
cout << mobile_list[0][i] << ": " << mobile_list[using_select][i] << " ";
}
cout << endl << endl << endl;
}
//
void QUERY:: printf_telecom(int using_select)
{
//string telecom_list[20][9];//行列
////cout << "座机来电者用户信息"<<endl;
//read_telecom();
for (int i = 1; i < 6; i++)
{
cout << telecom_list[0][i] << ": " << telecom_list[using_select][i] << " ";
}
cout << endl << endl << endl;
}
void QUERY::print_caller_information(string test_phone)
{
read_moblie();
read_telecom();
for (int select = 0; select < 10; select++)
{
//移动电话数据库查询
if (mobile_list[select][1] == test_phone)
{
cout << "移动电话来电,用户信息如下: " << endl;
printf_moblie(select);
}
//座机数据库查询
else if (telecom_list[select][0] == test_phone)
{
cout << "座机来电,用户信息如下:" << endl;
printf_telecom(select);
}
}
}
void QUERY::read_call_in(string call_in_sql)
{
ifstream in(call_in_sql);
string filename;
string line;
//string read_list[100][2];//行列
int hang_count = 0;
if (in) // 有该文件
{
while (getline(in, line)) // line中不包括每行的换行符
{
/*cout << line<< endl;*/
int length = line.length();
string str[2];
istringstream is(line);
is >> str[0] >> str[1];
/*cout << str[0] << "," << str[1] << endl;*/
read_list[hang_count][0] = str[0];
read_list[hang_count][1] = str[1];
/*for (int k = 0; k < length; k++)
{
cout << line[k] << " ";
}*/
hang_count++;
}
}
else // 没有该文件
{
cout << "no such file" << endl;
}
/*for (int i = 0; i < hang_count; i++)
{
cout << read_list[i][0] << " " << read_list[i][1] << endl;
}
*/
//hang_count = 0;
}
int main()
{
string test_phone = "13907730001";
QUERY Q1("mobile.txt", "telecom.txt");//读取数据库文件
Q1.read_call_in("call_in.txt");//读取接到电话的数据库文件
//遍历接到电话的数据库文件,得到号码和时间
for (int i = 1; i < 10; i++)
{
cout << "来电时间"<< Q1.read_list[i][0]<<endl;
test_phone = Q1.read_list[i][1];
Q1.print_caller_information(test_phone);
}
}
三个xtx文件:


本文介绍如何使用C++面向对象设计,读取txt文件中的电话用户信息,存储为二维数组,并在接到来电时快速查询显示号码所有人及位置。重点在于文件读取和数据结构的应用。
360





