本文目录一览:
c语言设计密码检测程序?
#include stdio.h
#define UC (1U1) // upper case
#define LC (1U2) // lower case
#define NUM (1U3) // 0-9
#define ALL (UC|LC|NUM)
int check(const char pass1[], const char pass2[])
{
const char *p = pass1[0];
unsigned int flag = 0;
if (strlen(pass1) 6 || strlen(pass1) 8)
{
printf("password length is 6 to 8.\n");
return 1;
}
if (strcmp(pass1, pass2))
{
printf("the tow passwords are diffrence.\n");
return 2;
}
while (*p)
{
if (*p = 'a' *p = 'z') flag |= LC;
else if (*p = 'A' *p = 'Z') flag |= UC;
else if (*p = '0' *p = '9') flag |= NUM;
else
{
printf("in valid charactor: %c.\n", *p);
return 3;
}
++p;
}
if (flag == ALL) return 0;
if ((flag UC) == 0)
{
printf("lack of uppercase.\n");
}
if ((flag LC) == 0)
{
printf("lack of lowercase.\n");
}
if ((flag NUM) == 0)
{
printf("lack of number.\n");
}
return -1;
}
int main(int argc, char *argv[])
{
char pass1[100];
char pass2[100];
do {
printf("input password:");
scanf("%s", pass1);
printf("repeat password:");
scanf("%s", pass2);
} while (check(pass1, pass2) != 0);
return 0;
}
C语言问题改正~
#includestdio.h
#includestdlib.h
int ID(char []);
char id[7];
int main()
{ int i;
printf("Please input your four digit ID number: ");
gets(id);
if(ID(id)==0)
printf("You input wrong,please input again");
else if(ID(id)==1)
puts(id);
ID(id);
return 0;
}
int ID(char val[])
{ int i;
int valid; //这里要初始化改为 int valid=1;
if(val[0]=='\0')
valid=0;
if(val[0]'A'||val[0]'Z')
valid=0;
if(val[1]'A'||val[1]'Z')
valid=0;
for(i=2;i7;i++)
{
if(val[i]1||val[i]9) //这里改为if(val[i]'1'||val[i]'9')
valid=0;
}
return valid;
}
测试通过!
C语言中,如何验证输入日期的正确性!~
主要验证三个信息:
a.验证月数是否在1~12,m 0 m = 12
b.验证日数是否超过当月,d 0 d = month[m - 1]
c.对平闰年的判断,if ((y % 4 == 0 y % 100 != 0) || y % 400 == 0)month[1] = 29
例程:
#include stdio.h
static int is_valid_date(int y, int m, int d)
{
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((y % 4 == 0 y % 100 != 0) || y % 400 == 0)
month[1] = 29;
return m 0 m = 12 d 0 d = month[m - 1];
}
int main(void)
{
int y, m, d;
scanf("%4d-%2d-%2d", y, m, d);
if (is_valid_date(y, m, d))
printf("valid date\n");
else
printf("invalid date\n");
return 0;
}
用C语言编程 求助~
代码有点多,慢慢看吧~~~
#include stdio.h
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void deletes();
main()
{
int m;
creat();
while(1)
{
printf("\n\n添加,请按1");
printf("\n查找,请按2");
printf("\n修改,请按3");
printf("\n删除,请按4");
printf("\n输出,请按5");
printf("\nExit,please press 0\n");
scanf("%d",m);
if(m=0m=5)
{
switch(m)
{
case 1:append();
break;
case 2:search();
break;
case 3:modify();
break;
case 4:deletes();
break;
case 5:output();
break;
case 0:exit();
}
printf("\n\n操作完毕,请再次选择");
}
else
printf("\n\n选择错误,请再次选择");
}
}
void creat()
{
struct person one;
long s1;
printf("\n请输入通讯薄名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\n不能建立通讯薄");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
printf("\n请输入姓名、电话号码及住址(以0结束)\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯薄");
exit();
}
printf("\n\n%20s\n","通讯薄");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打开通讯薄");
exit();
}
printf("\n请输入添加的姓名、电话号码及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开通讯簿");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%S\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯簿");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,记录为:");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n请输入新姓名、电话号码及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
}
else
printf("\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}
void deletes()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8],valid[4];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯簿");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\n已查到,记录为");
printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("\n确实要删除,按1;不删除,按0:");
scanf("%d",m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
}
else
printf("'n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}
Debug Assertion Failed! 求教高手!C语言
兄弟,你这个程序 是有问题,不过是个小问题,估计是你粗心吧!
我也看好长时间才发现的;你的程序 我 是用vc6.0运行的。结果就发现了这个问题,我在要修改的地方加了这句话,你看下吧“//*************isValid是个函数,你没有在后面加函数的参数啊,应该改这样例如:if(!isValid(a,b))”
希望能帮到你的忙
#include stdio.h
#include ctype.h
#define NUM_ATOMS 8
#define MAX_FORMULA_LEN 20
int isValid(char c[], char valid_char[]);
int findBracketMatch(char c[], char matchBracket, int start);
int doBracesMatch(char c[]);
void printFormula(char c[]);
int molecularWeight(char c[],char valid_char[],int);
int main()
{
char c[MAX_FORMULA_LEN];
char valid_char[NUM_ATOMS]={'H','S','O','N','C','B','K','P'};
int k = 0;
char d = ' ';
printf("Please enter the formula, put a '.' behind if you want to finish: ");
while(!isValid(c, valid_char))
{
while (d != '.')
{
d = toupper(getchar());
c[k] = d;
k++;
}
}
getchar();
if(!doBracesMatch(c))
{
printf("Error in braces..... ");
}
else
isValid(c, valid_char);
printFormula(c);
printf("The molecular weight for the molecule is:%d\n",molecularWeight(c,valid_char,0));
}
int isValid(char c[], char valid_char[])
{
int i,j,valid=1;
if(isdigit(c[0]))
return 0;
while(!valid c[j]!='.')
{
if(isalpha(c[j]))
{
i=0;
while (c[i] != valid_char[NUM_ATOMS] i NUM_ATOMS)
i++;
valid=(i==NUM_ATOMS) ? 0:1;
}
else
if(c[j]=='(' || c[j]=='[' || c[j]=='{')
valid=isdigit(c[j+1]) ? 0:1;
j++;
}
return (valid);
}
int findBracketMatch (char c[], char matchBracket, int start)
{
while(c!=matchBracket)
{
if(c=='.' || c==')' || c==']' || c=='}')
return 0;
else
if(c=='(')
start=findBracketMatch(c, ')' , start+1);
else
if(c=='[')
start=findBracketMatch(c, ']', start+1);
else
if(c=='{')
start=findBracketMatch(c, '}', start+1);
start++;
}
return (start);
}
int doBracesMatch(char c[])
{
if (findBracketMatch(c, '.' ,0)) // start point: find '.'
return 1;
else
return 0;
}
void printFormula(char c[])
{
int i;
if (!isValid) //*************isValid是个函数,你没有在后面加函数的参数啊,应该改这样例如:if(!isValid(a,b))
printf("the formula is invalid, please enter again.:");
else
{
printf("The formula you have inputed is :\n");
for(i=0;c[i]!='.';i++)
printf("%c",c[i]);
printf("\n");
}
}
int molecularWeight(char c[],char valid_char[], int i)
{
int k,weight=0;
int digit;
int atom[]={1,2,3,4,5,6,7,8};
while(c[i]!='.')
{
if(c[i]=='(' || c[i]=='[' || c[i]=='{')
{
weight+=molecularWeight(c,valid_char,i+1);
break;
}
else
if(isalpha(c[i]))
{
k=0;
while(c[i]!=valid_char[k] k8)
k++;
if(isdigit(c[i+1]))
{
digit=c[i+1]-'0';
weight=atom[k]*digit;
}
else
weight+=atom[k];
}
else
if(c[i]==')' || c[i]==']' || c[i]=='}')
{
if(isdigit(c[i+1]))
{
digit=c[i+1]-'0';
weight*=digit;
}
}
i++;
}
return (weight);
}