📄 gongneng.cpp
字号:
#include "post.h"
#include "gongneng.h"
#include "iomanip.h"
#include "fstream.h"
#include "string.h"
#include <stdlib.h>
#include <stdio.h>
POST obj[30]; //定义了一个对象数组,用于存储文件数据
int j; //用于确定有多少条数据被读入内存
GONGNENG::GONGNENG()
{
}
GONGNENG::~GONGNENG()
{
}
//将信息读入内存
void GONGNENG::reading() //将数据读入内存,以便以后可以使用
{
int i=0;
ifstream infile;
infile.open("post.txt"); //打开文件
if (!infile)
{
cout<<"打开文件出错!>o<";
return ;
}
for(i=0,j=0;!infile.eof();i++,j++)
{
infile>>obj[i].getAddress()>>obj[i].getPost()>>obj[i].getZip(); //从文件post.txt把值送到address,post,zip变量中
}
infile.close();//每次用完之后要把这个流infile关闭
}
//把文件的信息显示出来
void GONGNENG::show()
{
int i;
for(i=0;i<j;i++)
{
cout<<setw(10)<<obj[i].getAddress()<<setw(10)<<" "<<obj[i].getPost()<<setw(10)<<obj[i].getZip()<<endl;
}
}
//按地区查询
void GONGNENG::getaddr()
{
//由于reading()已经将文件post.txt的内容读入内存中,所以不用再打开文件来匹配数据了
int i;
char addr[10]; //定义地址
cout<<"\n请输入要查询的地区^o^:";
cin>>addr; //键盘输入地址
cout<<endl;
for (i=0;i<j;i++)
{
if (!strcmp(addr,obj[i].getAddress()))//用户输入的地址与文件里的地址进行匹配
{
cout<<"你所查询的信息为^-^:"<<endl;
cout<<setw(25)<<"地区"<<setw(10)<<"邮编"<<setw(10)<<"区号"<<endl;
cout<<setw(25)<<obj[i].getAddress()<<setw(10)<<obj[i].getPost()<<setw(10)<<obj[i].getZip()<<endl;
cout<<endl;
break;
}
}
if (i==j)
{
cout<<"对不起,暂无此记录!>o<"<<endl;
}
}
//按邮编查询
void GONGNENG::getpost()
{
//由于reading()已经将文件post.txt的内容读入内存中,所以不用再打开文件来匹配数据了
int i;
char tpost[10]; //定义邮编
cout<<"\n请输入要查询的邮编^o^:";
cin>>tpost; //键盘输入邮编
cout<<endl;
for (i=0;i<j;i++) //按邮编查询的条件
{
if (!strcmp(tpost,obj[i].getPost())) //如果obj对象数据里的post1与tpost相同,表示找到了正确的数据
{
cout<<"你所查询的信息为^-^:"<<endl;
cout<<setw(25)<<"地区"<<setw(10)<<"邮编"<<setw(10)<<"区号"<<endl;
cout<<setw(25)<<obj[i].getAddress()<<setw(10)<<obj[i].getPost()<<setw(10)<<obj[i].getZip()<<endl;
cout<<endl;
break;
}
}
if (i==j)
{
cout<<"对不起,暂无此记录!>o<"<<endl;
}
}
//按区号查询
void GONGNENG::getzip()
{
//由于reading()已经将文件post.txt的内容读入内存中,所以不用再打开文件来匹配数据了
int i;
char tzip[10]; //定义区号
cout<<"\n请输入要查询的区号^o^:";
cin>>tzip;
cout<<endl;
for (i=0;i<j;i++)
{
if (!strcmp(tzip,obj[i].getZip()))
{
cout<<"你所查询的信息为^-^:"<<endl;
cout<<setw(25)<<"地区"<<setw(10)<<"邮编"<<setw(10)<<"区号"<<endl;
cout<<setw(25)<<obj[i].getAddress()<<setw(10)<<obj[i].getPost()<<setw(10)<<obj[i].getZip()<<endl;
cout<<endl;
break;
}
}
if (i==j)
{
cout<<"对不起,暂无此记录!>o<"<<endl;
}
}
//查询信息
void GONGNENG::Find()
{
char ch;
while(1)
{
system("cls");
cout<<"\t\t"<<"@*******************^-^查询方式^-^*******************@"<<endl;
cout<<"\t\t"<<"* *"<<endl;
cout<<"\t\t"<<"* 1.按地区查询 *"<<endl;
cout<<"\t\t"<<"* 2.按邮编查询 *"<<endl;
cout<<"\t\t"<<"* 3.按区号查询 *"<<endl;
cout<<"\t\t"<<"* *"<<endl;
cout<<"\t\t"<<"@****************************************************@"<<endl;
cout<<endl;
cout<<"请选择查询方式^o^:";
cin>>ch; //输入一个1到3的数
switch(ch)
{
case '1': //按地区查询
getaddr();
break;
case '2': //按邮编查询
getpost();
break;
case '3': //按区号查询
getzip();
break;
default:
break;
}
cout<<"查询完毕,是否继续查询?(Y/N)";
cin>>ch;
if(ch=='N'||ch=='n')
{
break;
}
}
}
//添加信息
void GONGNENG::Add()
{
char temp_addr[10];
char temp_post[10];
char temp_zip[10];
char ch;
ofstream outfile;
outfile.open("post.txt",ios::app);//以追加方式打开文件
if (!outfile)
{
cout<<"打开文件出错!>o<";
return ;
}
cout<<"\n请输入要添加的信息^o^:"<<endl;
cout<<"地区"<<setw(10)<<"邮编"<<setw(12)<<"区号"<<endl;
j++;
cin>>temp_addr>>temp_post>>temp_zip;
obj[j].setAddress(temp_addr);
obj[j].setPost(temp_post);
obj[j].setZip(temp_zip);
outfile<<temp_addr<<"\t"<<temp_post<<"\t"<<temp_zip<<endl;
outfile.flush();
outfile.close();
cout<<"\n恭喜,添加成功!^-^"<<endl;
cout<<endl;
cout<<"请问是否需要查看记录?(Y/N)"<<endl;
cin>>ch;
if (ch=='Y'||ch=='y')
{
system("cls");
reading();
show();
getchar();
}
}
//修改信息
void GONGNENG::Alter()
{
int i=0;
char temp_addr[10];
char temp_post[10];
char temp_zip[10];
char ch;
ofstream outfile;
show();
cout<<"请输入要修改的地址^o^:";
cin>>temp_addr;
for (i=0;i<j;i++)
{
if (!strcmp(temp_addr,obj[i].getAddress()))//用户输入的地址与文件里的地址进行匹配
{
cout<<"\n你要修改的信息为^-^:"<<endl;
cout<<"\t"<<obj[i].getAddress()<<"\t"<<obj[i].getPost()<<"\t"<<obj[i].getZip()<<endl;
break;
}
}
if (i==j)
{
cout<<"对不起,没有此记录,请按任意键继续!^o^"<<endl;
getchar();
return ;
}
cout<<"\n请输入你要修改的信息^o^:"<<endl;
cout<< "地区"<<setw(10)<<"邮编"<<setw(10)<<"区号"<<endl;
cin>>temp_addr>>temp_post>>temp_zip;
obj[i].setAddress(temp_addr); //一下三句功能为为obj赋新的值
obj[i].setPost(temp_post);
obj[i].setZip(temp_zip);
outfile.open("post.txt");//每写一次除了先定义格式为在后面添加,否则将里面的内容全部覆盖
if (!outfile)
{
cout<<"打开文件出错!>o<";
return ;
}
for(i=0;i<j;i++)
{
outfile<<obj[i].getAddress()<<"\t"<<obj[i].getPost()<<"\t"<<obj[i].getZip()<<endl;
}
outfile.flush();
outfile.close();
cout<<"\n恭喜,修改成功!^-^"<<endl;
cout<<"请问是否需要查看记录?(Y/N)"<<endl;
cin>>ch;
if (ch=='Y'||ch=='y')
{
system("cls");
reading();
show();
getchar();
}
}
//删除信息
void GONGNENG::Delete()
{
ofstream outfile;
char temp_addr[10];
char ch;
int i=0;
show();
cout<<"请输入要删除的地址^o^:";
cin>>temp_addr;
for (i=0;i<j;i++)
{
if (!strcmp(temp_addr,obj[i].getAddress()))//用户输入的地址与文件里的地址进行匹配
{
cout<<"你要删除的信息为:"<<endl;
cout<<"\t"<<obj[i].getAddress()<<"\t"<<obj[i].getPost()<<"\t"<<obj[i].getZip()<<endl;
break;
}
}
if (i==j)
{
cout<<"对不起,没有此记录,请按任意键继续!^o^"<<endl;
getchar();
return ;
}
while (i<j)//将从找到的数据的后一条覆盖找到的数据
{
strcpy(obj[i].getAddress(),obj[i+1].getAddress()); //以下三句功能为为obj赋新的值
strcpy(obj[i].getPost(),obj[i+1].getPost());
strcpy(obj[i].getZip(),obj[i+1].getZip());
i++;
}
j--; //在内存里删除最后一条信息
outfile.open("post.txt");//每写一次除了先定义格式为在后面添加,否则将里面的内容全部覆盖
if (!outfile)
{
cout<<"打开文件出错!>o<";
}
for(i=0;i<j;i++)
{
if (i==(j-1))
{
outfile<<obj[i].getAddress()<<setw(10)<<obj[i].getPost()<<setw(10)<<obj[i].getZip();
}
else
outfile<<obj[i].getAddress()<<setw(10)<<obj[i].getPost()<<setw(10)<<obj[i].getZip()<<endl;
}
outfile.flush();
outfile.close();
cout<<"\n恭喜,删除成功!^-^"<<endl;
cout<<endl;
cout<<"请问是否需要查看记录?(Y/N)"<<endl;
cin>>ch;
if (ch=='Y'||ch=='y')
{
system("cls");
reading();
show();
getchar();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -