#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
int x,num;
int cop[5];
char pin[200];
void length(int x)
{
do{
x=0;
printf("please input your password(8~20):");
while((pin[x] = getchar())!='\n')
{
x=x+1;
}
num = x;
if(num>8&&num<=10)
cop[0]=1;
else if(num>10&&num<16)
cop[0]=2;
else
cop[0]=3;
}while(num<=8);
}
void lower(int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='a'&&pin[i]<='z')
{
cop[1]=1;
break;
}
else
cop[1]=0;
}
num = num;
}
void upper(int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='A'&&pin[i]<='Z')
{
cop[2]=1;
break;
}
else
cop[2]=0;
}
num = num;
}
void digits(int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='0'&&pin[i]<='9')
{
cop[3]=1;
break;
}
else
cop[3]=0;
}
num = num;
}
void symbol(int num)
{
int i;
for(i=0;i<=num;i++)
{
if((pin[i]>='!'&&pin[i]<='/')||(pin[i]>=':'&&pin[i]<='@')||(pin[i]>='['&&pin[i]<='-')||(pin[i]>='{'&&pin[i]<='~'))
{
cop[4]=1;
break;
}
else
cop[4]=0;
}
num = num;
}
void unique(int num)
{
int i;
for(i=0;i<num;i++)
{
int j;
for(j=i+1;j<=num;j++)
{
if(pin[i]==pin[j])
{
cop[5]=0;
break;
}
else
cop[5]=1;
}
if(cop[5]==0)
break;
else{}
}
x = num;
}
int main()
{
x=0;
do{
length(x);
lower(num);
upper(num);
symbol(num);
digits(num);
unique(num);
}while(x<8||x>20);
switch(cop[0])
{
case 1:
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
case 2:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1)
{
printf("\nthe password is middle\nclick Enter to return to the menu");
break;
}
else
{
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
}
case 3:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1)
{
printf("\nthe password is strong\nclick Enter to return to the menu");
break;
}
else
if((cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==0)||(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==0&&cop[5]==1)||(cop[1]==1&&cop[2]==1&&cop[3]==0&&cop[4]==1&&cop[5]==1)||(cop[1]==1&&cop[2]==0&&cop[3]==1&&cop[4]==1&&cop[5]==1)||(cop[1]==0&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1))
{
printf("\nthe password is middle\nclick Enter to return to the menu");
break;
}
else
{
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
}
}
return 0;
}
6、7、8行的全局变量都要删除吗?
那就需要在主函数中定义这些变量,然后作为参数传递给与他们相关的函数。如果需要修改值,则需要使用指针类型
修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
int length(int *cop,char *pin,int x)
{
int num=0;
do{
x=0;
printf("please input your password(8~20):");
while((pin[x] = getchar())!='\n')
{
x=x+1;
}
num = x;
if(num>8&&num<=10)
cop[0]=1;
else if(num>10&&num<16)
cop[0]=2;
else
cop[0]=3;
}while(num<=8);
return num;
}
void lower(int *cop,char *pin,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='a'&&pin[i]<='z')
{
cop[1]=1;
break;
}
else
cop[1]=0;
}
num = num;
}
void upper(int *cop,char *pin,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='A'&&pin[i]<='Z')
{
cop[2]=1;
break;
}
else
cop[2]=0;
}
}
void digits(int *cop,char *pin,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(pin[i]>='0'&&pin[i]<='9')
{
cop[3]=1;
break;
}
else
cop[3]=0;
}
}
void symbol(int *cop,char *pin,int num)
{
int i;
for(i=0;i<=num;i++)
{
if((pin[i]>='!'&&pin[i]<='/')||(pin[i]>=':'&&pin[i]<='@')||(pin[i]>='['&&pin[i]<='-')||(pin[i]>='{'&&pin[i]<='~'))
{
cop[4]=1;
break;
}
else
cop[4]=0;
}
}
int unique(int *cop,char *pin,int num)
{
int i;
for(i=0;i<num;i++)
{
int j;
for(j=i+1;j<=num;j++)
{
if(pin[i]==pin[j])
{
cop[5]=0;
break;
}
else
cop[5]=1;
}
if(cop[5]==0)
break;
else{}
}
return num;
}
int main()
{
int x,num;
int cop[5];
char pin[200];
x=0;
do{
num = length(cop,pin,x);
lower(cop,pin,num);
upper(cop,pin,num);
symbol(cop,pin,num);
digits(cop,pin,num);
x = unique(cop,pin,num);
}while(x<8||x>20);
switch(cop[0])
{
case 1:
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
case 2:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1)
{
printf("\nthe password is middle\nclick Enter to return to the menu");
break;
}
else
{
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
}
case 3:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1)
{
printf("\nthe password is strong\nclick Enter to return to the menu");
break;
}
else
if((cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==0)||(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==0&&cop[5]==1)||(cop[1]==1&&cop[2]==1&&cop[3]==0&&cop[4]==1&&cop[5]==1)||(cop[1]==1&&cop[2]==0&&cop[3]==1&&cop[4]==1&&cop[5]==1)||(cop[1]==0&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1))
{
printf("\nthe password is middle\nclick Enter to return to the menu");
break;
}
else
{
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
}
}
return 0;
}