1 Star 0 Fork 0

sc0707/20199273

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
通讯录.cpp 4.07 KB
Copy Edit Raw Blame History
sc0707 authored 2020-04-30 17:33 +08:00 . C语言
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int population=0,FreeSpace=50;
struct AddressList
{
int number;
char name[10];
char phone[20];
}
stu[50],temp;
void interface();
void sort();
void add(); 石超20199273
void cancel();
void modify();
void find();
void error();
int main()
{
int choose;
while(1)
{
int i;
system("cls");
printf("==========通讯录==========\n");
printf("\n");
printf("\n");
printf("==========界面==========\n");
printf("人数:%d人 |剩余空间:%d人\n",population,FreeSpace);
for(i=0;i<population;i++)
{
printf("编号:%10d |姓名:%10s |电话:%20s\n",stu[i].number,stu[i].name,stu[i].phone);
}
printf("\n");
printf("\n");
interface();
scanf("%d",&choose);
printf("\n");
switch (choose)
{
case 1:sort();break;
case 2:add();break;
case 3:cancel();break;
case 4:modify();break;
case 5:find();break;
case 6:return 0;
default:error();
}
}
}
void interface()
{
printf("操作列表:\n");
printf("1)排序 2)添加 3)删除\n");
printf("4)修改 5)查找 6)退出程序\n");
printf("请输入操作:\n");
}
void sort()
{
int i,n,j;
printf("1)编号排序 2)名字排序");
printf("请选择你要排序的方式:");
scanf("%d",&i);
switch (i)
{
case 1:
if (population >= 2)
{
for (n=0;n<population-1;n++)
{
for (j=n;j<population;j++)
{
if(stu[n].number>stu[j].number)
{
temp = stu[n];
stu[n] = stu[j];
stu[j] = temp;
}
}
}
}
break;
case 2:
if (population >=2)
{
for (n=0;n<population-1;n++)
{
for (j=n;j<population;j++)
{
if (strcmp(stu[n].name,stu[j].name)>0)
{
temp = stu[n];
stu[n] = stu[j];
stu[j] = temp;
}
}
}
}
break;
default:error();break;
}
}
void add()
{
int i,flag=0;
if (FreeSpace == 0)
{
printf("通讯录已满\n");
system("pause");
}
else
{
printf("添加操作:\n");
printf("请输入添加位置:");
scanf("%d",&stu[population].number);
if(stu[population].number>50||stu[population].number<=0)
{
printf("处理编号超过阈值\n");
system("pause");
}
else
{
for (i=0;i<population;i++)
{
if (stu[population].number == stu[i].number)
{
printf("此处已有数据\n");
system("pause");
flag = 1;
}
}
if(flag==0)
{
printf("请输入联系人姓名:");
scanf("%s",stu[population].name);
printf("请输入联系人电话号码:");
scanf("%s",stu[population].phone);
population++;
}
}
}
}
void cancel()
{
int i,n,flag=0;
printf("删除操作:\n");
printf("请输入删除位置:");
scanf("%d",&n);
if(n>50||n<=0)
{
printf("处理编号超过阈值\n");
system("pause");
}
else
{
for (i=0;i<population;i++)
{
if (n==stu[i].number)
{
stu[i]=temp;
population--;
FreeSpace++;
flag=1;
}
}
if(flag==0)
{
printf("此处无数据\n");
system("pause");
}
}
}
void modify()
{
int i,n,flag=0;
printf("请输入修改位置:");
scanf("%d",&i);
if (i>50||i<=0)
{
printf("处理编号超过阈值\n");
system("pause");
}
else
{
for (n=0;n<population;n++)
{
if (i==stu[n].number)
{
printf("已擦除原有信息,请重新输入\n");
printf("\n");
printf("请输入联系人姓名:");
scanf("%s",stu[n].name);
printf("请输入联系人电话号码:");
scanf("%s",stu[n].phone);
flag=1;
}
}
if (flag==0)
{
printf("此处无数据\n");
system("pause");
}
}
}
void find()
{
int i,flag=0;
char n[20];
printf("请输入你要查找对象的名字或电话号码:");
scanf("%s",n);
for (i=0;i<population;i++)
{
if (strcmp(stu[i].name,n)==0||strcmp(stu[i].phone,n)==0)
{
printf("编号:%10d| 姓名:%10s| 电话:%20s\n",stu[i].number,stu[i].name,stu[i].phone);
flag=1;
}
}
if (flag==0)
{
printf("查无此人\n");
}
system("pause");
}
void error()
{
printf("Error!!!\n错误操作指令,请重新输入\n");
system("pause");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/sc0707/sc20199273.git
git@gitee.com:sc0707/sc20199273.git
sc0707
sc20199273
20199273
master

Search