當前位置:首頁 » 編程語言 » c語言11位手機號
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言11位手機號

發布時間: 2023-05-28 04:28:59

c語言 隨機產生10個手機號,不能重復。輸入英文名後綁定手機號,一個名字一個手機號

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

constintPHONE_LENGTH=11;

constchar*PREFIX="139";

typedefstruct{

intavailable;
charnumber[PHONE_LENGTH+1];
charowner_name[31];
}PhoneNumber;


voidphoneNumberGenerator(PhoneNumber*phones,intsize,constchar*prefix);
voiddisplayPhoneNumber(PhoneNumber*phones,intsize);
intmain(void)
{


chartname[31];
intid;
PhoneNumberphones[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("此號已被佔用,請選擇其他的號碼");

}}return0;
}


voidphoneNumberGenerator(PhoneNumber*phones,intsize,constchar*prefix)
{

intrepeat;//重復標志變數

for(inti=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(intj=strlen(prefix);j<PHONE_LENGTH;++j)
{
(phones[i].number)[j]=rand()%10+'0';

}
(phones[i].number)[PHONE_LENGTH]='';

/************************檢測重復性**************************/
for(intk=0;k<i;k++)
{
if(strcmp(phones[i].number,phones[k].number)==0)
{
repeat=1;
break;

}

}
}while(repeat);//如果當前產生的號碼與之前的重復了,則再生成一個,直到不重復為止

}}


voiddisplayPhoneNumber(PhoneNumber*phones,intsize)
{


printf("%-5s%-20s%-10s%s ","編號","號碼","狀態","擁有者");
for(inti=0;i<size;++i)
{


printf("%-5d%-20s%-10s%s ",i,phones[i].number,phones[i].available?"可用":"已佔用",phones[i].owner_name);


}

}

② c語言編程題要求用函數和指針求判斷一個字元串是否為手機號,不 (手機要是11為什麼全為阿拉伯字母)

#include<stdio.h>
#include<string.h>

int istelnum(char *num)
{
int i;

for(i=0;num[i];i++)
{
if(!(num[i]>='0'&&num[i]<='9'))
return 0;
}
return 11==strlen(num);
}

int main()
{
char num[16];

printf("請輸入手機號:");
gets(num);
if(istelnum(num))
puts("是有效的手機號\n");
else
puts("不是有效的手機號\n");
return 0;
}

③ 用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語言關於手機號碼的問題 檢查我的代碼的問題

把整數字元和整數搞混了,比如你的if(st[0]!=1)就應該寫成if(st[0]!='1'),記得加上單引號.

採納哦