2024-11-24 07:59:49
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int PHONE_LENGTH = 11;
const char* PREFIX = "139";
typedef struct {
int available;
char number[PHONE_LENGTH+1];
char owner_name[31];
}PhoneNumber;
void phoneNumberGenerator(PhoneNumber*phones,int size, const char *prefix);
void displayPhoneNumber(PhoneNumber*phones,int size);
int main(void)
{
char tname[31];
int id ;
PhoneNumber phones[10];
phoneNumberGenerator(phones,10,PREFIX);
displayPhoneNumber(phones,10);
while(1)
{
puts("请输入您的姓名");
scanf("%s",tname);
puts("请输入您喜欢号码的编号");
scanf("%d",&id);
if(phones[id].available)
{
strcpy (phones[id].owner_name,tname);
phones[id].available = 0;
displayPhoneNumber(phones,10);
}else
{
puts("此号已被占用,请选择其他的号码");
}
}
return 0;
}
void phoneNumberGenerator(PhoneNumber*phones,int size,const char *prefix)
{
int repeat; //重复标志变量
for(int i=0;i<size;++i)
{
strncpy(phones[i].number,prefix,strlen(prefix)); //复制号码前缀
phones[i].available = 1; //初始化号码为可用
strcpy(phones[i].owner_name,"unknow") ; //初始化号码的拥有着为 "unknow"
//开始产生号码
do{
repeat = 0;
/*******************产生一个随机号码******************/
for(int j = strlen(prefix);j<PHONE_LENGTH;++j)
{
(phones[i].number)[j] = rand()%10+'0';
}
(phones[i].number)[PHONE_LENGTH] = '\0';
/************************检测重复性**************************/
for(int k=0;k<i;k++)
{
if(strcmp(phones[i].number,phones[k].number)==0)
{
repeat = 1;
break;
}
}
}while(repeat); //如果当前产生的号码与之前的重复了,则再生成一个,直到不重复为止
}
}
void displayPhoneNumber(PhoneNumber*phones,int size)
{
printf("%-5s%-20s %-10s %s\n","编号","号码","状态","拥有者");
for(int i=0;i<size;++i)
{
printf("%-5d%-20s %-10s %s\n",i,phones[i].number,phones[i].available?"可用":"已占用",phones[i].owner_name);
}
}
2024-11-24 07:59:04