手机短信的程序改改
//信息中的发信息、收信息、已发信息和发信箱储存信息的内容和电话号码。
//一条信息的最大容量为70个字节。容量状态则表示手机储存的短信的条数。
//手机的最大储存量为200条。当超过这数时,显示储存信息已满,请及时删除无用信息。
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#define Max 200
#define Mao 70
//using namespace std;
class News
{
public:
News(){}
void setnews();//写信信
void sentnews();//发信息
void recivenews();//收信息
void asentnews();//已发信息
void sentnewsbox();//发信箱
void savenews();//保存信息
void deletenews();//删除信息
void quit();//退出
private:
string news;//信息
string telenumber;//电话号码
static int number;//计数信息的数量
};
int News::number=0;
void News::setnews()//写信息
{
system("cls");
cout<<"新建信息(以#结束):";
cin>>news;
cout<<"你已建立信息!!!"<<endl;
char choice;
cout<<"保存与否(Y/N):";
cin>>choice;
if(choice=='Y'||choice=='y')
{
savenews();
number++;
}
else
cout<<"你没有保存信息,退出!!!"<<endl;
}
void News::sentnews()//发信息
{
system("cls");
cout<<"新建信息:";
setnews();
cout<<"请输入电话号码(数字):";
cin>>telenumber;
cout<<"\t确定(Y)\t取消(N)"<<endl;
char choice;
cin>>choice;
if(choice=='Y'||choice=='y')
{
system("pause");
cout<<"已发送信息!!!"<<endl;
asentnews();
}
else
cout<<"你已取消发送信息!!!"<<endl;
}
void News::sentnewsbox()//发信箱
{
cout<<"\t你的发信箱的已用空间:"<<number<<endl;
if(number>=195)
{
cout<<"\t你的发信箱的空间即将使用完!!!"<<endl;
cout<<"\t请及时删除无用信息!!!"<<endl;
}
}
void News::asentnews()//已发信息
{
ifstream infile;
infile.open("D://News.txt",ios::out|ios::in|ios::app);
if(!infile)
{
cerr<<"无法打开此文件!!!"<<endl;
exit(0);
}
else
{
while(!infile.eof())
{
int n=1;
cout<<"第"<<n<<"条信息:"<<endl;
cout<<"电话号码(11位):";
infile>>telenumber;
cout<<"信息内容如下:"<<endl;
infile>>news;
cout<<news;
cout<<endl;
}
cout<<"\t\t删除信息与否(Y/N)";
char choice;
cin>>choice;
if(choice=='Y'||choice=='y')
{
//删除信息文件内存中删除
deletenews();
}
else
{
cout<<"你已取消删除信息!退出!!"<<endl;
infile.close();
}
}
}
void News::recivenews()//收信息
{
cout<<"输入你要接受信息的号码:";
string telephone;
cin>>telephone;
cout<<"\t\t已接受信息"<<endl;
savenews();//保存接受的信息
ofstream outfile;
outfile.open("D://News.txt",ios::out);
if(!outfile)
{
cerr<<"无法打开此文件!!!"<<endl;
exit(1);
}
else
{
while(!outfile.eof())
{
int n=1;
cout<<"第"<<n<<"条信息:"<<endl;
cout<<"电话号码:";
outfile<<telenumber;
cout<<"信息内容如下:"<<endl;
outfile<<news;
cout<<news;
cout<<endl;
}
cout<<"\t\t删除信息与否(Y/N)";
char choice;
cin>>choice;
if(choice=='Y'||choice=='y')
{
//删除信息文件内存中删除
deletenews();
}
else
{
cout<<"你已取消删除信息!退出!!"<<endl;
outfile.close();
}
}
}
void News::savenews()//保存信息
{
ifstream infile;
infile.open("D://News.txt",ios::in|ios::app|ios::out);
if(!infile)
{
cerr<<"无法打开此文件!!!"<<endl;
exit(1);
}
else
{
while(infile.eof())
{
//写入文件,保存信息
infile>>news;
infile>>telenumber;
}
infile.close();
}
}
void News::deletenews()//删除信息
{
ofstream outfile;
outfile.open("D://News.txt",ios::out);
if(!outfile)
{
cerr<<"无法打开此文件!!!"<<endl;
exit(1);
}
else
{
int n=1;
while(outfile.eof())
{
outfile<<news;
outfile<<telenumber;
n++;
}
//这里不会匹配删除的信息
number--;
outfile.close();
}
}
void News::quit()
{
for(int i=0;i<40;i++)
cout<<"* ";
cout<<"\t\t\t谢谢使用信息系统"<<endl;
for(i=0;i<40;i++)
cout<<"* ";
cout<<endl;
}
int main()
{
News NEWS;
for(int i=0;i<40;i++)
cout<<"* ";
cout<<"\t\t\t欢迎进入信息系统"<<endl;
for(i=0;i<40;i++)
cout<<"* ";
for(;;)
{
cout<<"\t\t新建信息----------------------(1)"<<endl;
cout<<"\t\t发信息------------------------(2)"<<endl;
cout<<"\t\t收信息------------------------(3)"<<endl;
cout<<"\t\t已发信息----------------------(4)"<<endl;
cout<<"\t\t发信箱------------------------(5)"<<endl;
cout<<"\t\t退出--------------------------(0)"<<endl;
cout<<"\t选择操作项目:";
int choice;
cin>>choice;
switch(choice)
{
case 1:NEWS.setnews();break;
case 2:NEWS.sentnews();break;
case 3:NEWS.recivenews();break;
case 4:NEWS.asentnews();break;
case 5:NEWS.sentnewsbox();break;
case 0:
default:
NEWS.quit();return 0;break;
}
}
}