#include
#include
#include
#define M 3
FILE *fp;
char filename[50] = {""};
typedef struct message {
char name[16];
char telephone[16];
char email[24];
char address[24];
int postcode;
struct message *next;
} *LinkList,*pNode;
void ReadData(pNode p) {
printf("\t姓 名 : ");
scanf("%s",p->name);
printf("\t电话号码 : ");
scanf("%s",p->telephone);
printf("\t邮箱号码 : ");
scanf("%s",p->email);
printf("\t地 址 : ");
scanf("%s",p->address);
printf("\t邮政编码 : ");
scanf("%d",&p->postcode);
}
void ShowLine(int n) {
int i;
for(i = 0; i < n; ++i)
printf("*");
printf("\n");
}
void ShowData(pNode p) {
printf("%-16s",p->name);
printf("%-16s",p->telephone);
printf("%-24s",p->email);
printf("%-24s",p->address);
printf("%-8d\n",p->postcode);
}
void ShowAll(LinkList head) {
pNode p;
ShowLine(88);
for(p = head->next; p; p = p->next)
ShowData(p);
ShowLine(88);
}
LinkList GetEmptyList() {
LinkList head = (pNode)malloc(sizeof(struct message));
head->next = NULL;
head->name[0] = 0;
head->address[0] = 0;
head->postcode = 0;
head->telephone[0] = 0;
head->email[0] = 0;
return head;
}
void Addmember(LinkList head) { // 按telephone增序添加,可用于创建新链表
pNode q,p = (pNode)malloc(sizeof(struct message));
ReadData(p);
q = head;
while(q->next) {
if(strcmp(q->next->telephone,p->telephone) == 0) {
ShowData(p);
printf("\t***** 这个电话号码已经存在。\n");
return;
}
if(strcmp(q->next->telephone,p->telephone) > 0) {
p->next = q->next;
q->next = p;
return;
}
}
q->next = p;
p->next = NULL;
}
pNode SearchName(LinkList head,char name[]) {
pNode p;
if(head == NULL) return NULL;
for(p = head->next; p; p = p->next) {
if(strcmp(p->name,name) == 0) {
ShowData(p);
return p;
}
}
return NULL;
}
pNode SearchTelephone(LinkList head,char telephone[]) {
pNode p;
if(head == NULL) return NULL;
for(p = head->next; p; p = p->next) {
if(strcmp(p->telephone,telephone) == 0) {
ShowData(p);
return p;
}
}
return NULL;
}
void ModifyListName(LinkList head) {
pNode p;
char name[24];
printf("\t被修改人的姓名 : ");
scanf("%s",name);
p = SearchName(head,name);
if(p == NULL) printf("\tXXXXX 没有找到姓名为%s的记录!\n",name);
else {
ShowData(p);
ReadData(p);
}
}
void ModifyListTelephone(LinkList head) {
pNode p;
char telephone[16];
printf("\t被修改的电话号码 : ");
scanf("%s",telephone);
p = SearchTelephone(head,telephone);
if(p == NULL) printf("\t***** 没有找到电话号码为%s的记录!\n",telephone);
else {
ShowData(p);
ReadData(p);
}
}
void ModifyList(LinkList head) {
int select;
do {
printf("\t1、按姓名修改,2、按电话号码修改,0、返回\n");
printf("\t请选择:");
scanf("%d",&select);
switch(select) {
case 1 : ModifyListName(head);
case 2 : ModifyListTelephone(head);
}
}while(select);
}
int EraseTelephone(LinkList head,char telephone[]) { // 删除电话号码为telephone的结点
int an;
pNode p,q;
if(head->next == NULL) {
printf("\t号码本为空。\n");
return 0;
}
for(p = head; p->next; p = p->next) {
if(strcmp(p->next->telephone,telephone) == 0) {
ShowData(p);
printf("\t1.删除, 0.不删除 : ");
scanf("%d",&an);
if(an == 1) {
q = p->next;
p->next = q->next;
free(q);
return 1;
}
else return 0;
}
}
printf("\t***** 没有找到电话号码为%s的记录!\n",telephone);
return 0;
}
void LoadData(LinkList head) { // 传送文件的内容到内存
pNode p,tmp;
int an,flag;
if(filename[0] == '\0') {
printf("\t要传送的数据文件名 : ");
scanf("%s",filename);
}
else {
printf("\t加载文件%s? 1.是,0不是 :",filename);
scanf("%d",&an);
if(an == 1) {
fp = fopen(filename,"rb");
if(fp == NULL) {
printf("\tXXXXX 打开文件%s失败。\n",filename);
return;
}
}
else {
printf("\t文件名 : ");
scanf("%s",filename);
fp = fopen(filename,"rb");
if(fp == NULL) {
printf("\tXXXXX 打开文件%s失败。\n",filename);
return;
}
}
}
if(head->next == NULL) {
p = head;
tmp = (pNode)malloc(sizeof(struct message));
while(fread((void *)tmp,sizeof(struct message),1,fp) == 1) {
p->next = tmp;
p = p->next;
tmp = (pNode)malloc(sizeof(struct message));
}
fclose(fp);
p->next = NULL;
free(tmp);
return;
}
printf("\t1.添加,2.覆盖 : ");
scanf("%d",&an);
if(an == 1) {
tmp = (pNode)malloc(sizeof(struct message));
while(fread(tmp,sizeof(struct message),1,fp) == 1) {
flag = 1;
for(p = head; p->next && flag; p = p->next) {
if(strcmp(p->next->telephone,tmp->telephone) == 0) {
printf("\t***** 因电话号码相同,本条信息未添加:\n");
ShowData(tmp);
flag = 0;
break;
}
if(strcmp(p->next->telephone,tmp->telephone) > 0) {
tmp->next = p->next;
p->next = tmp;
flag = 0;
break;
}
}
if(flag) {
p->next = tmp;
tmp->next = NULL;
}
tmp = (pNode)malloc(sizeof(struct message));
}
fclose(fp);
free(tmp);
return;
}
if(an == 2) {
p = head->next;
while(p) {
tmp = p;
p = p->next;
free(tmp);
}
p = head;
tmp = (pNode)malloc(sizeof(struct message));
while(fread(tmp,sizeof(struct message),1,fp) == 1) {
p->next = tmp;
p = p->next;
tmp = (pNode)malloc(sizeof(struct message));
}
free(tmp);
p->next = NULL;
fclose(fp);
}
}
void SaveData(LinkList head) { // 保存内存到文件
int an;
char mode[5];
pNode p;
if(filename[0] == 0) {
printf("\t请输入文件名 : ");
scanf("%s",filename);
fp = fopen(filename,"wb");
if(fp == NULL) {
printf("\tXXXXX 打开文件%s失败。\n",filename);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(struct message),1,fp);
fclose(fp);
return;
}
else {
printf("\t1.覆盖文件%s\n2.追加文件%s\n3.新建\n4.无操作返回\n",filename,filename);
printf("\t请选择 : ");
scanf("%d",&an);
if(an < 1 || an > 3) return;
if(an == 3) {
printf("\t请输入文件名 : ");
scanf("%s",filename);
fp = fopen(filename,"wb");
if(fp == NULL) {
printf("\tXXXXX 打开文件%s失败。\n",filename);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(struct message),1,fp);
fclose(fp);
return;
}
else {
if(an == 1) strcpy(mode,"wb");
else if(an == 2) strcpy(mode,"ab");
fp = fopen(filename,"wb");
if(fp == NULL) {
printf("\tXXXXX 打开文件%s失败。\n",filename);
return;
}
for(p = head->next; p; p = p->next)
fwrite(p,sizeof(struct message),1,fp);
fclose(fp);
return;
}
}
}
void Sort(LinkList head) { // 按姓名增排序
pNode p,q,pt;
if(head == NULL) return;
p = head;
while(p->next) {
q = p->next->next;
while(q->next) {
if(strcmp(p->next->telephone,q->next->telephone) > 0) {
pt = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = pt;
}
else q = q->next;
}
p = p->next;
}
}
void FreeList(LinkList head) {
pNode p = head,q;
while(p) {
q = p;
p = p->next;
free(q);
}
}
void menu(LinkList head) {
int choice;
do {
printf("┌─────────────────────────────────┐\n");
printf("│ │\n");
printf("│ 欢迎使用通讯录管理软件 │\n");
printf("│ │\n");
printf("│ 编写 xoalyg │\n");
printf("│ │\n");
printf("└─────────────────────────────────┙\n");
printf("\n\n\n\n");
printf("\t1、新建\n");
printf("\t2、浏览\n");
printf("\t3、修改\n");
printf("\t4、添加\n");
printf("\t5、排序\n");
printf("\t6、加载\n");
printf("\t7、保存\n");
printf("\t0、退出\n");
printf("\n\t请选择 : ");
scanf("%d",&choice);
switch(choice) {
case 1 : Addmember(head); break;
case 2 : ShowAll(head); break;
case 3 : ModifyList(head); break;
case 4 : Addmember(head); break;
case 5 : Sort(head); break;
case 6 : LoadData(head); break;
case 7 : SaveData(head); break;
}
}while(choice);
}
int main() {
LinkList head = GetEmptyList();
menu(head);
FreeList(head);
return 0;
}
可以struct创建一个结构体
然后sort排序
代码随后就到.......[大概一小时,请等待]
代码来了~~~
------------------------------------
#include
using namespace std;
struct txl
{
string xm="无";//姓名
string xb="无";//性别
string nl="无";//年龄
string dh="无";//电话
} a[51]; //最多50个,下标从1~50
int top=0;
bool cmp(txl x,txl y)
{
if(x.xm=="无")
return 1;
if(y.xm=="无")
return 0;
if(x.dh==y.dh)
return x.xm
void jz()
{
if(top==50)
{
cout<<"通讯录已有50人,请删除\n";
return;
}
top++;
cout<<"请输入姓名:";
cin>>a[top].xm;
cout<<"请输入性别(1男,0女):";
int xb;
cin>>xb;
if(xb==1)
a[top].xb="男";
else
a[top].xb="女";
cout<<"请输入年龄:";
cin>>a[top].nl;
cout<<"请输入电话:";
cin>>a[top].dh;
sort(a+1,a+top+1,cmp);
}
void cz()
{
string name;
cout<<"请输入删除者的姓名:";
cin>>name;
for(int i=1; i<=50; i++)
if(a[i].xm==name)
{
a[i].xm="无";
a[i].xb="无";
a[i].nl="无";
a[i].dh="无";
top--;
}
sort(a+1,a+top+1,cmp);
}
void xg()
{
string name,dh;
cout<<"请输入修改者的姓名:";
cin>>name;
cout<<"请输入修改后的电话:";
cin>>dh;
for(int i=1; i<=50; i++)
if(a[i].xm==name)
a[i].dh=dh;
sort(a+1,a+top+1,cmp);
}
void ck()
{
cout<<" 姓名 性别 年龄 电话\n";
for(int i=1; i<=50; i++)
if(a[i].xm!="无")
printf("%8s%6s%6s%11s\n",a[i].xm.c_str(),a[i].xb.c_str(),a[i].nl.c_str(),a[i].dh.c_str());
cout<<"共计 "<
getchar();
}
void sc()
{
ofstream outf;
outf.open("通讯录.xls");
outf<<"姓名\t性别\t年龄\t电话\n";
for(int i=1; i<=50; i++)
if(a[i].xm!="无")
outf< outf<<"共计 "<
cout<<"生成成功!\n";
getchar();
getchar();
}
int main()
{
while(1)
{
system("cls");
cout<<"通讯录创建系统\n 1.新增人员\n 2.删除人员\n 3.修改数据\n 4.查看数据\n 5.生成文件\n>>";
int a;
cin>>a;
if(a==1)
jz();
if(a==2)
cz();
if(a==3)
xg();
if(a==4)
ck();
if(a==5)
sc();
}
return 0;
}
这是一道C语言的增删改查的链表操作操作的典型题目,同时考察对结构体的理解程度。最简单的就是使用顺序链表做。
你要问什么?
通讯录用结构体储存数据,下面的操作用指针或者干脆构造函数做都可以啊。
看起来好像没难点