有一个最大的疑问:为何在函数中定义指针数组需要static?
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define PASSWORD " "
char *s_gets(char *p,int n);
short checkpassword();
long factorial(int n);
long factorials(int n);
long *long_p_num(int n);
char **char_p_num(int n);
short searchnumber(short n,char **pi);
void shownumber(char *p);
int main()
{
/*密码验证*/
if(!checkpassword())
exit(1);
/*定义变量*/
int num=0;
short index=0;
long *ipnum=NULL;
char **cpnum=NULL;
/*显示阶乘结果*/
fputs("Input the number:",stdout);
scanf("%d",&num);//待补充判断循环
while(getchar()!=10);
printf("%d的阶乘是:%ld.\n",num,factorial(num));
printf("%d的递减阶乘和是:%ld.\n",num,factorials(num));
/*将阶乘结果存入数组*/
ipnum=long_p_num(num);
cpnum=char_p_num(num);
fputs("显示整型数组:{",stdout);
for(int i=0;i<num;i++)
printf("%ld ",ipnum[i]);
fputs("\b}\n",stdout);
fputs("显示字符串数组:{",stdout);
for(int i=0;i<num;i++)
printf("%s ",cpnum[i]);
fputs("\b}\n",stdout);
/*显示字符(查找字符)*/
for(;index=searchnumber(num,cpnum);)
shownumber(cpnum[index-1]);
/*清理动态内存*/
free(ipnum);
for(int i=0;i<num;i++)
free(cpnum[i]);
fputs("ProgramOver!\n",stderr);
return 0;}
//
char *s_gets(char *p,int n)//s_gets字符串输入
{
char *gets,*find;
if((*(gets=fgets(p,n,stdin)))!=10&&gets)
{
if(find=strchr(p,10))
*find=0;
else
while(getchar()!=10);
}
else
return 0;
return gets;}
short checkpassword()//密码验证
{
char input[5]={0};
int chance;
for(chance=3,printf("Please input the password,(%d chance left)\nPassword:",chance);
chance>0;
chance--,printf("Please input the password,(%d chance left)\nPassword:",chance))
{
if(s_gets(input,5)&&!strcmp(input,PASSWORD))
{
fputs("Right!!!\n",stdout);
return 1;
}
else
fputs("Wrong!Again!\n",stdout);
if((chance)==1)//是否可以改良掉这段代码?
break;
}
fputs("No chance left!ProgramOver!\n",stdout);
return 0;}
long factorial(int n)//计算阶乘
{
long sum;
for(sum=1;n>0;n--)
sum*=n;
return sum;}
long factorials(int n)//计算递减阶乘和
{
long sums;
for(sums=0;n>0;n--)
sums+=factorial(n);
return sums;}
long *long_p_num(int n)//整数数组存储
{
long *p_num=(long *)calloc(n,sizeof(long));
for(int i=0,i1=n;i<n;i++,i1--)
p_num[i]=factorial(i1);
return p_num;}
char **char_p_num(int n)//字符串数组存储
{
static char *pctemp[]={0};//不加statci就出错
long itemp=0;
for(int i=0,i1=n;i<n;i++,i1--)
{
itemp=factorial(i1);
int lenth=0;
for(int copy=itemp;copy>0;lenth++)
copy/=10;
pctemp[i]=(char *)calloc(lenth+1,sizeof(char));
_itoa(itemp,pctemp[i],10);
}
return pctemp;}
short searchnumber(short n,char **pi)
{
short index=0;
char ctemp[10]={0};
for(fputs("Input the number you wanna show(NothingQuit).\nnumber:",stdout);
!index;
fputs("Couldn't found the number,try another one(NothingQuit).\nnumber:",stdout),index=0)
{
if(!s_gets(ctemp,10))
return 0;
for(;index<n;index++)
if(!strcmp(pi[index],ctemp))
return index+1;
}
return 0;}
void shownumber(char *p)
{
short lenth=strlen(p);
char *p2=(char *)calloc(lenth+1,sizeof(char));
memset(p2,'*',lenth*sizeof(char));
for(short n1=0,n2=lenth-1;n1<=n2;n1++,n2--)
{
Sleep(500);
p2[n1]=p[n1];
p2[n2]=p[n2];
printf("%s",p2);
fputc(10,stderr);
}
free(p2);
/*中间一个值始终无法打印*/
/*
for(short n1=0,n2=lenth;n1<=n2;n1++,n2--)
{
Sleep(500);
for(int i=0;i<n1;i++)
printf("%c",p[i]);
for(int i=0;i<(n2-n1);i++)
fputs("*",stderr);
for(int i=n2;i<lenth;i++)
printf("%c",p[i]);
fputc(10,stderr);
}
*/
}