c语言通讯录程序设计
⑴ C语言程序设计,通讯录管理系统
/*【用C语言编写具有数据插入、修改、删除、显示和查询功能的电话薄管理程序】
1、数据包括:人名、工作单位、电话号码和E-mail地址。
2、可对记录中的姓名和电话号码进行修改。
3、可增加或删除记录。
4、可显示所有保存的记录。
5、可按人名或电话号码进行查询。
另:采用结构体数组和文件系统实现。结构体包括人名、工作单位、电话号码和E-mail
绝对原创,2010年7月12日晚刚写完测试通过的,纯C语言! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define szSTR 64
#define fileDB "c:\\contact.txt"
struct CONTACT {
char name[szSTR];/*姓名*/
char unit[szSTR];/*工作单位*/
char tele[szSTR];/*电话*/
char mail[szSTR];/*mail*/
};
typedef struct CONTACT CON;
#define BNAME 1
#define BUNIT 2
#define BTELE 4
#define BMAIL 8
#define BFULL 0xFF
int OPER[5] = {BFULL, BNAME, BUNIT, BTELE, BMAIL};
void input(CON * s, int mask)
{
if(mask & BNAME) { printf("请输入学生姓名:"); scanf("%s", s->name); }
if(mask & BUNIT) { printf("请输入工作单位:"); scanf("%s", s->unit); }
if(mask & BTELE) { printf("请输入联系电话:"); scanf("%s", s->tele); }
if(mask & BMAIL) { printf("请输入电子邮件:"); scanf("%s", s->mail); }
}/*end input*/
void show (int id, CON * s)
{
if(!s) return ;
if(id)
printf("%d\t%s\t%s\t%s\t%s\n",
id, s->name , s->unit , s->tele , s->mail );
else
printf("【姓名】%s【工作单位】%s【电话】%s【电邮】%s\n",
s->name , s->unit , s->tele , s->mail );
/*end if*/
}/*end show*/
void regist(void)
{
CON s; FILE * f = 0;
input(&s, BFULL);
f = fopen(fileDB, "ab");
fwrite(&s,sizeof(s),1,f);
fclose(f);
printf("资料已保存!\n");
}/*end regist*/
int browse(char * key, fpos_t * p, CON * s)
{
FILE * f =0; int i = 0, r = 0; CON t;
char * a = 0, * b = 0; fpos_t q;
if(!s) return 0;
f = fopen(fileDB, "rb");
if(!f) {
printf("数据库暂无记录\n");
return 0;
}/*end if*/
printf("编号\t姓名\t单位\t电话\t电邮\n");
printf("------------------------------------------------\n");
while(!feof(f)) {
fgetpos(f, &q);
r = fread(&t, sizeof(t), 1, f);
if(r<1)break;
if(t.name[0]==0) continue;
if(key) {
a = strstr(t.name , key);
b = strstr(t.tele , key);
if(a == 0 && b == 0) continue;
}/*end if*/
if(p) *p = q;
if(s) memcpy(s, &t, sizeof(t));
i++;
show(i, &t);
}/*end while*/
fclose(f);
printf("------------------------------------------------\n");
return i;
}/*end browse*/
fpos_t query(CON * s)
{
char key[szSTR]=""; fpos_t p = 0; int r = 0;
printf("请输入姓名或者电话号码:");
scanf("%s", key);
r = browse(key, &p, s);
return r?p:-1;
}/*end query*/
void clear(void)
{
FILE * f = 0; CON s;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
memset(&s, 0, sizeof(s));
fwrite(&s, sizeof(s),1, f);
fclose(f);
printf("记录已删除!\n");
}/*end clear*/
void modify(void)
{
FILE * f = 0; CON s; int cmd = 0;
fpos_t p = query(&s);
if(p<0) return;
f = fopen(fileDB, "rb+");
fsetpos(f, &p);
for(;;) {
show(0, &s);
printf("请选择:1-姓名;2-工作单位;3-电话;4-电邮;5-保存并退出;6-放弃修改并退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 1:
case 2:
case 3:
case 4:
input(&s, OPER[cmd]);
break;
case 5:
fsetpos(f, &p);
fwrite(&s, sizeof(s), 1, f);
case 6:
fclose(f);
return;
default:
printf("错误的操作,请重新输入。\n");
}/*end case*/
}/*mext*/
}/*end clear*/
int main(void)
{
int cmd = 0; CON s;
for(;;) {
printf("欢迎使用通信簿!请选择:1-添加;2-查询;3-删除;4-修改;5-全部输出;0-退出\n");
scanf("%d", &cmd);
switch(cmd) {
case 0: return 0;
case 1: regist(); break;
case 2: query(&s); break;
case 3: clear(); break;
case 4: modify(); break;
case 5: browse(0, 0, &s); break;
}//end case
}
return 0;
}
⑵ 求C语言程序设计手机通讯录代码,急,
//这个是我教材上的,不知道你要的是不这个
#include<stdio.h>
#include<string.h>
struct friends_list
{
char name[10];
int age;
char telephone[13];
};
int Count=0;
void new_friend(struct friends_list friends[]);
void search_friend(struct friends_list friends[],char *name);
int main(void)
{
int choice;
char name[10];
struct friends_list friends[50];
do
{
printf("通讯录功能选项:1:新建2:查询0:退出\n");
printf("请选择功能:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:new_friend(friends);
break;
case 2:printf("请输入要查找的联系人姓名:");
scanf("%s",name);
search_friend(friends,name);
break;
case 0:break;
}
}
while(choice!=0);
printf("谢谢使用通讯录功能!\n");
return 0;
}
void new_friend(struct friends_list friends[])
{
struct friends_list f;
if(Count==50)
{
printf("通讯录已满!\n");
return;
}
printf("请输入新联系人的姓名:");
scanf("%s",f.name);
printf("请输入新联系人的年龄:");
scanf("%d",&f.age);
printf("请输入新联系人的联系电话:");
scanf("%s",f.telephone);
friends[Count]=f;
Count++;
}
void search_friend(struct friends_list friends[],char *name)
{
int i,flag=0;
if(Count==0)
{
printf("通讯录是空的!\n");
return;
}
for(i=0;i<Count;i++)
if(strcmp(name,friends[i].name)==0)
{
flag=1;
break;
}
if(flag)
{
printf("姓名:%s\t",friends[i].name);
printf("年龄:%d\t",friends[i].age);
printf("电话:%s\n",friends[i].telephone);
}
else
printf("无此联系人!");
}
⑶ C语言的课程设计,做的是全班同学通讯录,然而我并不懂程序是怎样调用的
里面包括文件的调用,各种查找,筛选的函
⑷ C语言程序设计 通讯录系统管理
// 头文件部分
//============================
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
// 初始化数据部分
int Menu();
void Start();
void Write();
void Read();
void Search();
void Detele();
void beauty();
void Rework();
void Lock();
int Judge();
//============================
struct Student{
char name[20];
char Tel[20];
char QQ[20];
int age;
}xy[100];
char wenjian[10000];
char Number[10];
char number=0;
int GOTO;
//
int main()
{
Start();
for(;;)
{
GOTO=Menu();
if(GOTO==1)
{
for(;;)
{
Write();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==2)
{
for(;;)
{
Read();
system("pause");
break;
}
}
if(GOTO==3)
{
for(;;)
{
Search();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==4)
{
for(;;)
{
Rework();
int judge=Judge();
if(judge==2)
{
break;
}
}
}
if(GOTO==5)
{
Detele();
break;
}
if(GOTO==6)
{
beauty();
break;
}
if(GOTO==7)
{
}
if(GOTO==8)
{
printf("");
break;
}
if(GOTO==9)
{
printf("\t\t\t退出系统\n\n");
return 0;
}
if(GOTO==0)
{
printf("");
break;
}
}
// beauty();
//
// Write();
// Read();
// Search();
// Rework();
// Detele();
return 0;
}
void Write()
{
printf("\t\t\t请输入好友名字\n");
scanf("%s",&xy[number].name);
printf("\t\t\t请输入好友电话号码\n");
scanf("%s",&xy[number].Tel);
printf("\t\t\t请输入好友QQ号码\n");
scanf("%s",&xy[number].QQ);
printf("\t\t\t请输入好友年龄\n");
scanf("%d",&xy[number].age);
FILE *file;
if((file=fopen("Friend.txt","rt"))==NULL)
{
file=fopen("Friend.txt","wt");
fprintf(file,"%-20s%-20s%-20s%-20s","姓名","电话","QQ","年龄");
}
file=fopen("Friend.txt","at");
fprintf(file,"%-20s%-20s%-20s%-20d\n",xy[number].name,xy[number].Tel,xy[number].QQ,xy[number].age);
fclose(file);
number++;
Number[0]=number;
FILE * Sta;
if((Sta=fopen("number.txt","r"))=NULL)
{
Sta=fopen("number.txt","wt");
exit(1);
}
Sta=fopen("number.txt","wt");
fprintf(Sta,"%d",Number[0]);
fclose(Sta);
}
void Read()
{
printf("\t\t\t您通讯录中一共有%d位好友\n",Number[0]);
FILE* read;
if((read=fopen("Friend.txt","r"))=NULL)
{
printf("\t\t\t您通讯中当前没有好友,请返回添加\n");
}
read=fopen("Friend.txt","rt");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
for(int i=0;i<number;i++)
{
fscanf(read,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
/* fread(wenjian,10000,1,read);
for(int i=0;i<number*80+80;i++)
{
printf("%c",wenjian[i]);
}
printf("\n");
*/
fclose(read);
}
void beauty()
{
int d;
do{
printf("choose");
scanf("%d",&d);
}while(d<1&&d>5);
if(d==1)
{
system("color 27");
printf("===1===");
}
if(d==2)
{
system("color 37");
printf("===2===");
}
if(d==3)
{
system("color 47");
printf("===3===");
}
if(d==4)
{
system("color 57");
printf("===4===");
}
if(d==5)
{
system("color 67");
printf("===5===");
}
}
void Start()
{
FILE* Start;
if((Start=fopen("number.txt","r"))==NULL)
{
Start=fopen("number.txt","w");
number=0;
Number[0]=number;
fscanf(Start,"%d",&Number[0]);
printf("\t\t\t您的通讯录中还没有任何好友,赶快添加吧.此次操作需要重新运行程序。\n");
exit(1);
}
Start=fopen("number.txt","r");
fscanf(Start,"%d",&Number[0]);
number=Number[0];
fclose(Start);
}
void Search()
{
int change=0;
char Searchname[20];
printf("\t\t\t输入要查找的好友的名字\n\n");
scanf("%s",&Searchname);
fflush(stdin);
printf("\t\t\t您要查找的好友是 %s \n\n",Searchname);
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Searchname)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
}
fclose(Search);
}
if(change==0)
{
printf("\t\t\t您要查找的好友不存在,请返回菜单添加。\n");
}
}
void Rework()
{
char Rework[20];
printf("\t\t\t输入要修改的好友的名字\n\n");
scanf("%s",&Rework);
printf("\t\t\t您要修改 %s 的信息\n\n",Rework);
int Change=0;
FILE *Search;
Search=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(Search,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,Rework)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
Change=1;
int Choose;
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
//---------------------------------------------------------------------
do
{
printf("\t\t\tchoose\t\t\t1-----3");
scanf("%d",&Choose);
}while(Choose>3||Choose<1);
if(Choose==1){
printf("\t\t\t请输入好友电话号码\n");
scanf("%s",&xy[i].Tel);
}
if(Choose==2)
{
printf("\t\t\t请输入好友QQ号码\n");
scanf("%s",&xy[i].QQ);
}
if(Choose==3)
{
printf("\t\t\t请输入好友年龄\n");
scanf("%d",&xy[i].age);
}
Search=fopen("Friend.txt","wt");
for(int sta=0;sta<i;sta++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[sta].name,xy[sta].Tel,xy[sta].QQ,xy[sta].age);
}
if(sta==i)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
i++;
}
for(i;i<=number;i++)
{
fprintf(Search,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
printf("\t\t\t修改后的信息为:\n\n");
Read();
}
fclose(Search);
}
if(Change==0)
{
printf("\t\t\t您要查找的好友不存在,请返回菜单添加。\n");
}
}
void Detele()
{
int change=0;
char delete_friend[20];
printf("\t\t\t输入要删除的好友的名字\n\n");
scanf("%s",&delete_friend);
fflush(stdin);
printf("\t\t\t您要删除的好友是 %s \n\n",delete_friend);
FILE *DeleteF;
DeleteF=fopen("Friend.txt","rt");
for(int i=0;i<=number;i++)
{
fscanf(DeleteF,"%s%s%s%d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,&xy[i].age);
if(strcmp(xy[i].name,delete_friend)==0)
{
printf("\t\t\t下面是该好友的信息:\n\n");
printf("%-20s%-20s%-20s%-20s\n","姓名","电话 ","QQ","年龄");
printf("%-20s%-20s%-20s%-20d\n",&xy[i].name,&xy[i].Tel,&xy[i].QQ,xy[i].age);
change=1;
DeleteF=fopen("Friend.txt","wt");
for(int st=0;st<i;st++)
{
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[st].name,xy[st].Tel,xy[st].QQ,xy[st].age);
}
i++;
for(i;i<=number;i++)
{
printf("%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
fprintf(DeleteF,"%-20s%-20s%-20s%-20d\n",xy[i].name,xy[i].Tel,xy[i].QQ,xy[i].age);
}
}
//--------------------------------------------------------------------------------
if(change==1)
{
printf("\t\t\t删除后的通讯录信息为:\n\n");
number--;
Number[0]=number;
FILE * St;
if((St=fopen("number.txt","r"))=NULL)
{
St=fopen("number.txt","wt");
exit(1);
}
St=fopen("number.txt","wt");
fprintf(St,"%d",Number[0]);
fclose(St);
fclose(DeleteF);
Start();
Read();
}
}
if(change==0)
{
printf("\t\t\t您要删除的好友不存在。\n");
}
}
void Lock()
{
}
int Menu()
{
printf("\t\t*****欢迎进入通讯管理界面*****\n\n");
printf("\t\t\t1.添加我的好友\n");
printf("\t\t\t2.显示所有好友\n");
printf("\t\t\t3.查找我的好友\n");
printf("\t\t\t4.修改好友信息\n");
printf("\t\t\t5.删除我的好友\n");
printf("\t\t\t6.设置背景颜色\n");
printf("\t\t\t7.设置管理密码\n");
printf("\t\t\t8.使用注意事项\n");
printf("\t\t\t9.退出通讯系统\n");
printf("\t\t\t0.作者想说的话\n");
printf("\t\t******************************\n\n");
do{
printf("\t\t\t请输入你的选择:\n\n");
scanf("%d",&GOTO);
}while(GOTO>9||GOTO<1);
return GOTO;
}
int Judge()
{
char JUDGE;
do
{
printf("\t\t\t是否继续当前操作\n\n Y(yes) / N(no)\n");
printf("\t\t\t输入你的选择\n");
JUDGE=getch();
if(JUDGE=='y'||JUDGE=='Y')
{
return 1;
}
if(JUDGE=='n'||JUDGE=='N')
{
return 2;
}
}while(JUDGE!='y'||JUDGE!='n');
}
⑸ 请C语言高手帮忙做一下c程序设计题目
没工夫编了,给你找了个现成的,网络知道里的。
#include <iostream>
#include <string>
#include "address.h"
#include "addressList.h"
#include "addressBook.h"
using namespace std;
int main()
{
new AddressBook; //开启通信录
return 0;
}
ostream& operator<<(ostream& os, const RecordList& c_rl)
{
RecordList::const_iterator it;
RecordList::const_iterator it_end =c_rl.end();
for (it=c_rl.begin();it!=it_end; it++)
{
os << (*it)->name << '\n' << (*it)->tel << '\n';
}
return os;
}
istream& operator>> (istream& is, RecordList& rl)
{
Record* rec;
string name;
while (true)
{
//注意这里使用的是全局的getline()函数,而不是istream的成员函
//数.全局的getling()函数将从istream中取出最后一个分隔符,
//而istream的成员函数getline则不会
getline(is,name);
if (name =="")
break;
rec = new Record;
rec->name=name;
getline(is,rec->tel );
rl.push_back(rec);
}
return is;
}
#include<stdlib.h>
#include<fstream>
#include "addressBook.h"
using namespace std;
AddressBook::AddressBook()
{
isModified = false;
start();
}
AddressBook::~AddressBook()
{
}
//功能:显示欢迎画面,并开始进行用户交互处理
void AddressBook::start()
{
cout<<"\n"
<<"\t******************************\n"
<<"\t* 这是一个 *\n"
<<"\t* 简单的通信录程序 *\n"
<<"\t* 可以对通信录进行简单管理 *\n"
<<"\t* 欢迎使用通信录 *\n"
<<"\t******************************\n"
<<"\n";
handleMenu();
}
//功能:显示通信录菜单,返回用户选择的选项
//返回:用户选择的选项,是1-9之间的一个字符
char AddressBook::menuSelect()
{
cout<<"\n"
<<"1.显示记录\n"
<<"2.查询记录\n"
<<"3.增加记录\n"
<<"4.删除记录\n"
<<"5.保存记录\n"
<<"6.读取记录\n"
<<"7.新建记录\n"
<<"8.结束运行\n"
<<"\n左边数字对应功能选择,请选1-8:";
char choice[2];
while(true)
{
cin.getline(choice,2);
if(choice[0]<'1' || choice[0]>'8')
cout<<"\n输入错误,重选1-8:";
else
break;
}
return choice[0];
}
//功能:根据用户选择的项目调用相应的处理函数
void AddressBook::handleMenu()
{
while(true)
{
switch(menuSelect())
{
case '1':
displayRecords();
break;
case '2':
queryRecord();
break;
.......
//功能:打印用于显示记录信息时的表头
void AddressBook::dispTable()
{
}
//功能:显示通信录中的所有记录
void AddressBook::displayRecords()
{
}
//功能:首先检查通信录是否已保存,然后清空当前通信录中所有记录
//注意:该函数覆盖了基类中的函数
void AddressBook::clear()
{
}
//功能:查询指定记录
void AddressBook::queryRecord()
{
//功能:向当前通信录追加新的记录
//注意:该函数重载了基类中的函数
void AddressBook::addRecord()
{
}
/*
//说明:如果使用string 类,效果更好。下面是实现本功能的程序代码:
while(true)
{
//输入新记录
string name;
cout<<"姓名:";
getline(cin,name);
//如果输入的姓名为“0”,则结束添加记录的操作
if(name=="0")
break;
Record* rec=new Record;
rec->name=name;
cout<<"电话:";
getline(cin,rec->tel);
//将新记录加入链表
AddressList::addRecord(rec);
}
//同理,下面的成员函数removeRecord()中的判断,可以使用如下替代方法:
string str;
getline(cin,str);
while(true)
{
if(str[0]!='1' && str[0]!='2')
{
cout<<"输入错误,重选1-2:";
getline(cin,str);
}
else
break;
}
*/
//功能:从当前通信录移除指定的记录
//注意:该函数重载了基类中的函数
void AddressBook::removeRecord()
{
if(empty())
{
cout<<"通信录已空,退出删除操作!"<<endl;
return;
}
//选择获取需要删除的记录的方式
cout<<"可以通过姓名或电话删除记录\n"
<<"1.通过姓名\n"
<<"2.通过电话\n";
char choice[2];
cin.getline(choice,2);
while(true)
{
if(choice[0]!='1' && choice[0]!='2')
{
cout<<"输入错误,重选1-2:";
cin.getline(choice,2);
}
else
break;
}
int type=static_cast<int>(choice[0])-48;
//输入需要匹配的字符串
if(type==1)
cout<<"请输入姓名:"<<flush;
else
cout<<"请输入电话:"<<flush;
char pattern[20];
cin.getline(pattern,20);
int deletedCount=0;
//找到匹配的记录
Iterator it=findRecord(pattern,type,first());
Iterator it_end=pastEnd();
while(it!=it_end)
{
cout<<"找到一条匹配的记录:\n";
dispTable();
cout<<(*it)->name<<"\t\t"<<(*it)->tel<<"\n";
cout<<"确定要删除这条记录吗?[Y/N]";
cin.getline(choice,2);
//删除这条记录
if(choice[0]!='n' && choice[0]!='N')
{
it=AddressList::removeRecord(it);
deletedCount++;
}
//取下一条匹配的记录
if(it!=it_end)
it=findRecord(pattern,type,++it);
}
cout<<"一共删除了"<<deletedCount<<"条记录\n"
<<"现在还有"<<size()<<"条记录"<<endl;
if(deletedCount!=0)
isModified=false;
}
//功能:将通信录保存至指定文件
//注意:该函数重载了基类中的函数
void AddressBook::saveRecords()
{
if(empty())
{
cout<<"没有记录可存!"<<endl;
return;
}
//因为下面的程序中使用string类的成员函数,所以需要进行一次转换
string fname;
char name[16];
cout<<"请输入要存在的文件名(直接回车选择文件sname):";
cin.getline(name,16);
fname=name;
if(fname.empty())
fname="sname";
ofstream of(fname.c_str(),ios_base::out);
if(!of)
{
cout<<"不能存入文件!"<<endl;
return;
}
AddressList::saveRecords(of);
of.close();
cout<<size()<<"条记录已经存入文件,请继续操作。"<<endl;
isModified=false;
}
//功能:从指定文件读取记录追加到当前通信录末尾
//注意:该函数重载了基类中的函数
void AddressBook::loadRecords()
{
}
void AddressBook::quit()
{
}
#ifndef H_ADDRESSBOOK_H_NNN
#define H_ADDRESSBOOK_H_NNN
#include "addressList.h"
//处理通信录操作的面向用户使用的类,包含所有功能的用户界面及用户交互的实现
class AddressBook :public AddressList
{
protected:
bool isModified; //通信录是否已被修改还未保存
public:
AddressBook();
virtual ~AddressBook();
//开始通信录的用户界面操作
virtual void start();
//菜单处理函数
virtual void handleMenu();
//显示记录
virtual void displayRecords();
//查询记录
virtual void queryRecord();
//增加记录
virtual void addRecord();
//删除记录
virtual void removeRecord();
//保存记录
virtual void saveRecords();
//读取记录
virtual void loadRecords();
//结束程序
virtual void quit();
//清空当前通信录
virtual void clear();
protected:
//菜单选择函数
char menuSelect();
//显示表头
void dispTable();
};
#endif //H_ADDRESSBOOK_HZ-NNN
#include "addressList.h"
AddressList::~AddressList()
{
recList.clear();
}
//功能:向通信录中添加新的记录
//参数:rec,指向新记录的指针
void AddressList::addRecord (Record* rec)
{
if (rec !=NULL)
{
recList.push_back(rec);
}
}
//功能:从通讯录中删除一个记录
//参数:it,指向欲删除记录的迭代器
//注意:it,应是可提领的迭代器,可以通过findRecord()获得
AddressList::Iterator AddressList::removeRecord(AddressList::Iterator it)
{
return recList.erase(it);
}
//功能:从通信录中寻找一个记录
//参数:pattern,欲寻找的记录的指定域与pattern相等
// type,为1表示欲寻找记录的名称(name)与pattern相等;
// 为2表示欲寻找记录的电弧(tel)与(pattern)相等。
// from,从from开始寻找匹配的记录。
//返回:若找到了则返回的迭代器指向第一个匹配记录的迭代器,
// 若找不到则返回的迭代器等于pastEnd()的返回值。
//注意:from应是可提领的迭代器。
// 第一次调用可以用first(),之后使用上一次findRecord()的返回值增1,
// 直到返回pastEnd(),便可以获得所有匹配的记录。
AddressList::Iterator AddressList::findRecord(string pattern,int type,AddressList::Iterator from)
{
Iterator it;
Iterator it_end=recList.end();
for (it=from; it!=it_end; it++)
{
if ((type==1 && (*it)->name==pattern)||(type==2 && (*it)->tel==pattern))
break;
}
return it;
}
//功能:将通信录保存到输出流中
//参数:os.指定的输出流
void AddressList::saveRecords(ostream &os)
{
os << recList;
}
//功能:从输入流中读入数据并追加到当前通信录的末尾
//参数:is,指定的输入流
//返回:读入的记录数
int AddressList::loadRecords(istream &is)
{
int ori =size();
is >> recList;
return size()-ori;
}
#ifndef H_ADDRESSLIST_H_AAA
#define H_ADDRESSLIST_H_AAA
#include <iostream>
#include <string>
#include "address.h"
using namespace std;
//处理通信录操作的底层类,包括增加记录、删
//除记录、查询记录以及保存和读取记录的函数,
//该类不涉及任何关于用户界面的操作
class AddressList
{
protected:
RecordList recList;// 使用对象成员作为数据成员
public:
typedef RecordList::iterator Iterator;
//向通信录中添加新的记录
virtual void addRecord(Record* rec);
//从通信录中删除一个记录
virtual Iterator removeRecord(Iterator it);
//从通讯录中寻找一个记录
virtual Iterator findRecord(string pattern,int type,Iterator from);
//将通信录保存到输出流中
virtual void saveRecords(ostream& os);
//从输入流中读入数据并追加到当前通信录的末尾
virtual int loadRecords(istream& is);
virtual ~AddressList();
//获得通信录中存储的记录数
const int size() const {return (int)recList.size();}
//通信录是否为空
const bool empty() const {return recList.empty();}
//清除通信录
virtual void clear() {recList.clear();}
//获得通信录的第一条记录
Iterator first(){return recList.begin();}
//获得通信录超过最后一个记录之后的迭代器
Iterator pastEnd() {return recList.end();}
};
#endif //H_ADDRESSLIST_H_AAA
⑹ 用C语言帮忙写一个“TCP Client/Server模式的通信程序设计与实现”
CLIENT:
#include <stdio.h>
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define PORT 6666 /* 客户机连接远程主机的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */
int main()
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
char msg[MAXDATASIZE];
char *argv="127.0.0.1";
struct sockaddr_in their_addr; /* 对方的地址端口信息 */
WSADATA ws;WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果侍基建立socket失败,退出程序
printf("socket error\n");
exit(1);
}
//连接对方
their_addr.sin_family = AF_INET; /* 协议类型是INET */
their_addr.sin_port = htons(PORT); /* 连接对方PORT端口悔洞 */
their_addr.sin_addr.s_addr = inet_addr(argv); /* 连接对方老前谨的IP */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
//如果连接失败,退出程序
printf("connet error\n");
closesocket(sockfd);
exit(1);
}
while(1){
scanf("%s",msg);
//发送数据
if (send(sockfd, msg, MAXDATASIZE, 0) == -1)
{
printf("send error");
closesocket(sockfd);
exit(1);
}
//接收数据,并打印出来
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
//接收数据失败,退出程序
printf("recv error\n");
closesocket(sockfd);
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s\n",buf); }
closesocket(sockfd);
return 0;
}
SERVER:
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define MYPORT 6666 /*定义用户连接端口*/
#define BACKLOG 10 /*多少等待连接控制*/ #define MAXDATASIZE 100
int main()
{
int sockfd, new_fd; /*定义套接字*/
struct sockaddr_in my_addr; /*本地地址信息 */
struct sockaddr_in their_addr; /*连接者地址信息*/
int sin_size,numbytes; char msg[10],buf[MAXDATASIZE];
WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll
//建立socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果建立socket失败,退出程序
printf("socket error\n");
exit(1);
}
//bind本机的MYPORT端口
my_addr.sin_family = AF_INET; /* 协议类型是INET */
my_addr.sin_port = htons(MYPORT); /* 绑定MYPORT端口*/
my_addr.sin_addr.s_addr = INADDR_ANY; /* 本机IP*/
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
//bind失败,退出程序
printf("bind error\n");
closesocket(sockfd);
exit(1);
}
//listen,监听端口
if (listen(sockfd, BACKLOG) == -1)
{
//listen失败,退出程序
printf("listen error\n");
closesocket(sockfd);
exit(1);
}
printf("listen...");
//等待客户端连接
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
printf("accept error\n");
closesocket(sockfd);
exit(1);
}
printf("\naccept!\n");
while(1) {
if((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) continue;
if(!strcmp(buf,"bye"))
{
//成功,关闭套接字
closesocket(sockfd);
closesocket(new_fd);
return 0;
}
printf("%s %d",buf,strlen(buf));
sprintf(msg,"%d",strlen(buf));
if (send(new_fd,msg,MAXDATASIZE, 0) == -1)
{
printf("send ERRO");
closesocket(sockfd);
closesocket(new_fd);
return 0;
}
}
}