?? 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 + -