老师要求要用模板类 我真的一脸懵逼 完全不知道C++怎么搞这玩意儿 希望老司机们讲解并帮忙改成规范符合要求的代码
#include<iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <iomanip>
#include <string>
#define N 20
using namespace std;
struct
{
char name[20]; //人名 20个字符内
char job[20]; //工作单位
char tel[11]; //电话号码 11位
char email[20]; //email地址
}TelInfo;
void menu();
void addrecord()
{
char ch;
ofstream ofile("Result.txt",ios_base::out);
do
{
cout<<"请输入通讯录信息"<<endl;
cout<<"请输入姓名"<<endl;
cin>>TelInfo.name;
cout<<"请输入工作单位"<<endl;
cin>>TelInfo.job;
cout<<"请输入电话号码"<<endl;
cin>>TelInfo.tel;
cout<<"请输入邮件地址"<<endl;
cin>>TelInfo.email;
ofile<<TelInfo.name<<" "<<TelInfo.job<<" "<<TelInfo.tel<<" "<<TelInfo.email<<endl;
cout<<"您还要输入他人的通讯录信息吗(y/n))?"<<endl;
std::cin>>ch;
}while(ch=='y'); //yes情况下循环
ofile.close();
}
void changecord()
{
ifstream da("Result.txt", ios_base::in);
string str;
bool flag = true;
ofstream data("return.txt", ios_base::out);
char a[20];
if (!da)
{
cout<<"找不到文件!!!!"<<endl;
return;
}
cout<<"请输入你要修改人的姓名"<<endl;
cin>>a;
while ( da>>TelInfo.name)
{
getline(da, str);
if (strcmp(TelInfo.name,a)==0)
{
cout<<"你想修改的联系人:" <<endl;
cout<<TelInfo.name<<" "<<str<<endl;
flag=false;
break;
}
data<<TelInfo.name
<<" "<<str<<endl;
}
if(flag)cout<<"没有该人"<<endl;
else
{
while (getline(da, str))
{
data<<str <<endl;
}
da.close();
data.close();
ifstream in("return.txt", ios::in);
ofstream out("Result.txt", ios::out);
if (!out || !in)
{
cout<<endl<<"对不起不能打开文件!!!"<<endl<<endl;
return;
}
while (getline(in, str))
{
out<<str<<endl;
}
in.close();
ofstream change("Result.txt",ios_base::app);
cout<<"请输入正确的姓名"<<endl;
cin>>TelInfo.name;
cout<<"请输入正确的工作单位"<<endl;
cin>>TelInfo.job;
cout<<"请输入正确的电话号码"<<endl;
cin>>TelInfo.tel;
cout<<"请输入正确的邮件地址"<<endl;
cin>>TelInfo.email;
out<<TelInfo.name<<" "<<TelInfo.job<<" "<<TelInfo.tel<<" "<<TelInfo.email<<endl;
cout<<"这个人的信息已经从你的通迅录中修改!!!" <<endl;
out.close();
}
}
void delrecord()
{
ifstream da("Result.txt", ios_base::in);
string str;
bool flag = true;
ofstream data("return.txt", ios_base::out);
char a[20];
if (!da)
{
cout<<"找不到文件!!!!"<<endl;
return;
}
cout<<"请输入你要删除人的姓名"<<endl;
cin>>a;
while ( da>>TelInfo.name)
{
getline(da, str);
if (strcmp(TelInfo.name,a)==0)
{
cout <<endl <<"你想删除的联系人:" <<endl;
cout<<TelInfo.name<<" "<<str<<endl;
flag=false;
break;
}
data<<TelInfo.name
<<" "<<str<<endl;
}
if(flag)cout<<"没有该人"<<endl;
else
{
while (getline(da, str))
{
data<<str <<endl;
}
da.close();
data.close();
ifstream in("return.txt", ios::in);
ofstream out("Result.txt", ios::out);
if (!out || !in)
{
cout<<endl<<"对不起不能打开文件!!!"<<endl<<endl;
return;
}
while (getline(in, str))
{
out<<str<<endl;
}
out.close();
in.close();
cout <<endl <<"这个人的信息已经从你的通迅录中删除!!!" <<endl <<endl;
}
}
void printcord()
{
char ch;
ifstream da("Result.txt");
cout<<"人名 "<<"工作单位 "<<"电话"<<" 邮件"<<endl;
while (da.get(ch))
cout << ch;
da.close();
}
void searchcord()
{
char a[20],b[20];
int n;
ifstream da("Result.txt");
cout<<"请输入要查找方式:";
cout<<"1.姓名";
cout<<"2.电话号码"<<endl;
cin>>n;
if(n==1)
{
cout<<"请输入要查询的姓名"<<endl;
cin>>a;
while(da.eof()==0)
{
da>>TelInfo.name>>TelInfo.job>>TelInfo.tel>>TelInfo.email;
if(strcmp(TelInfo.name,a)==0)
{
cout<<"人名 "<<"工作单位 "<<"电话"<<" 邮件"<<endl;
cout<<TelInfo.name<<" "<<TelInfo.job<<" "<<TelInfo.tel<<" "<<TelInfo.email<<endl;
}
}
cout<<"没有该人"<<endl;
da.close();
}
if(n==2)
{
cout<<"请输入要查询的电话"<<endl;
cin>>b;
while(da.eof()==0)
{
da>>TelInfo.name>>TelInfo.job>>TelInfo.tel>>TelInfo.email;
if(strcmp(TelInfo.tel,b)==0)
{
cout<<"人名 "<<"工作单位 "<<"电话"<<" 地址"<<endl;
cout<<TelInfo.name<<" "<<TelInfo.job<<" "<<TelInfo.tel<<" "<<TelInfo.email<<endl;
}
}
cout<<"没有该人"<<endl;
da.close();
}
}
void main()
{
menu();
}
void menu()
{
int i;
for(;;){
std::cout<<" 欢迎登陆简易版通讯录管理系统 "<<std::endl;
std::cout<<"###################################"<<std::endl;
std::cout<<"###########您好,欢迎光临!##########"<<std::endl;
std::cout<<"############请选择操作:############"<<std::endl;
std::cout<<"# \t1.添加记录 #"<<std::endl;
std::cout<<"# \t2.修改记录 #"<<std::endl;
std::cout<<"# \t3.删除记录 #"<<std::endl;
std::cout<<"# \t4.数据显示 #"<<std::endl;
std::cout<<"# \t5.查询记录 #"<<std::endl;
std::cout<<"# \t6.退出系统 #"<<std::endl;
std::cout<<"###################################"<<std::endl;
std::cin>>i;
if(!std::cin.good()){
std::cout<<"非法输入!"<<std::endl;
std::cin.clear();
}
switch(i){
case 1:
addrecord();
break;
case 2:
changecord();
break;
case 3:
delrecord();
break;
case 4:
printcord();
break;
case 5:
searchcord();
break;
case 6:
return;
default:
std::cout<<"错误的选项!"<<std::endl;
}
}
}
asd8532
相关分类