📄 sy5.cpp
字号:
// sy5.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "iostream.h"
#include "string.h"
#define MAX 5
int length;//当前长度
typedef struct person
{
int id;
char name[20];
char tel_no[20];
char email[30];
}Person;
Person pers[MAX];
/*void del()
{
char name[20];
char *p;
int i,j;
cout<<"请输入你想要删除的人的姓名";
cin>>name;
for(i=0;i<MAX;i++)
{
if(strcm(pers[i].name,name)==0)
{
p=&pers[i];
for(++p;j<MAX-i;++p)
{
*(p-1)=*p;
}
}
if(i==MAX-1)
{
cout<<endl<<"表中不存在你想要删除的人!请确认后重新输入删除."<<endl;
}
}
}
*/
void insert()
{
Person add;
cout<<endl<<"现在开始添加一条通讯录记录!记录将置于通讯录的末尾."<<endl;
cout<<endl<<"请输入你想要添加的人的编号:";
cin>>add.id;
cout<<endl<<"请输入你想要添加的人的姓名:";
cin>>add.name;
cout<<endl<<"请输入你想要添加的人的电话号码:";
cin>>add.tel_no;
cout<<endl<<"请输入你想要添加的人的email:";
cin>>add.email;
if(length<MAX)
{
pers[length]=add;
length=length+1;
}
else
{
cout<<endl<<"存储空间已满!"<<endl;
}
}
void sort(Person pers[],int low,int high)
{
int i,j;
Person key;
i=low;
j=high;
key=pers[low]; //枢轴记录关键字
while(i<j)
{
while(i<j&&pers[j].id>=key.id)
{
--j;
}
if(i<j)
{
pers[i]=pers[j];
i++;
}
while(i<j&&pers[i].id<=key.id)
{
++i;
}
if(i<j)
{
pers[j]=pers[i];
j--;
}
}
pers[i]=key;
if(low<i)
sort(pers,low,i-1);
if(i<high)
sort(pers,j+1,high);
cout<<"sort success!";
}
void input()
{
int i,x;
do
{
cout<<endl<<"请输入你通讯录中包含的人数:";
cin>>length;
if(length<MAX)
{
x=0;
for(i=0;i<length;i++)
{
cout<<endl<<"现在开始初始化通讯录!"<<endl;
cout<<endl<<"请输入第"<<i+1<<"个的人的编号:";
cin>>pers[i].id;
cout<<endl<<"请输入第"<<i+1<<"个的人的姓名:";
cin>>pers[i].name;
cout<<endl<<"请输入第"<<i+1<<"个的人的电话号码:";
cin>>pers[i].tel_no;
cout<<endl<<"请输入第"<<i+1<<"个的人的email:";
cin>>pers[i].email;
}
}
else
{
x=1;
cout<<endl<<"超出存储空间!请重新输入.";
}
}while(x=1);
}
int main(int argc, char* argv[])
{
int i;
int high=length-1,low=0;
input();
insert();
for(i=0;i<length+1;i++)
{
cout<<endl<<pers[i].id<<pers[i].name<<pers[i].tel_no<<pers[i].email<<endl;
}
sort(pers,low,high);
for(i=0;i<length+1;i++)
{
cout<<endl<<pers[i].id<<pers[i].name<<pers[i].tel_no<<pers[i].email<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -