
判断变量名是否合法(C语言程序)
请问谁知道如何用C语言(不要C++或C#)写一个判断输入的变量名是否合法的程序?要求运行时显示“Pleaseprovideavariablename:”输入变量名按ENT...
请问谁知道如何用C语言(不要C++或C#)写一个判断输入的变量名是否合法的程序?
要求运行时 显示“Please provide a variable name:”
输入变量名按ENTER后 输出“VALID“或者”INVALID” 之后循环回开始的地方
如输入ASCII “0” 时,退出程序。
(只能用#include <stdio.h>)
会的再来回答 胡乱跟帖的请离开。帮忙解决的,可以再加分。
不能使用阵列 而是使用getchar
输入“0”时退出 改为不输入变量名直接按ENTER时退出 展开
要求运行时 显示“Please provide a variable name:”
输入变量名按ENTER后 输出“VALID“或者”INVALID” 之后循环回开始的地方
如输入ASCII “0” 时,退出程序。
(只能用#include <stdio.h>)
会的再来回答 胡乱跟帖的请离开。帮忙解决的,可以再加分。
不能使用阵列 而是使用getchar
输入“0”时退出 改为不输入变量名直接按ENTER时退出 展开
展开全部
#include <stdio.h>
main()
{
char chset[256],s[512];
int i;
for(i=0;i<256;i++)chset[i]=0;
for(i='0';i<='9';i++)chset[i]=1;
for(i='a';i<='z';i++)chset[i]=chset[i-32]=2;
chset['_']=3;
for(;;)
{
printf("Please provide a variable name:") ;
scanf("%s",s) ;
if('0'==*(short*)s) break ;
if( chset[s[i=0]] > 1 ) while(chset[s[++i]]) ;
printf((s[i])?"INVALID\n":"VALID\n") ;
}
}
-------------
问题补充:
不能使用阵列
输入“0”时退出 改为不输入变量名直接按ENTER时退出
-------------
#include <stdio.h>
int isfirst(int x){
return (x!='_') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z')? 0:1;
}
int iselse(int x){
return ( (x!='_') &&
(x<'0' || x>'9') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z') )? 0:1;
}
int main(void) {
char key; /* input character from user */
int match; /* keep track of characters matched */
int count; /* number of substring matches */
while (1) {
count = 0;
match = 0;
printf("Please provide a variable name:");
while ((key = getchar()) != '\n') {
count++;
switch (match) {
case 0: /* starting - not matches yet */
if (isfirst(key)) match = 1;
else match = -1;
break;
default:
if (match > 0)
if (iselse(key)) match++;
else match = - match - 1;
break;
}
}
if (count == 0) return 0;
if (match < 0) match = - match - 1;
printf("%d matches of %d\n", match, count);
}
return 0;
}
main()
{
char chset[256],s[512];
int i;
for(i=0;i<256;i++)chset[i]=0;
for(i='0';i<='9';i++)chset[i]=1;
for(i='a';i<='z';i++)chset[i]=chset[i-32]=2;
chset['_']=3;
for(;;)
{
printf("Please provide a variable name:") ;
scanf("%s",s) ;
if('0'==*(short*)s) break ;
if( chset[s[i=0]] > 1 ) while(chset[s[++i]]) ;
printf((s[i])?"INVALID\n":"VALID\n") ;
}
}
-------------
问题补充:
不能使用阵列
输入“0”时退出 改为不输入变量名直接按ENTER时退出
-------------
#include <stdio.h>
int isfirst(int x){
return (x!='_') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z')? 0:1;
}
int iselse(int x){
return ( (x!='_') &&
(x<'0' || x>'9') &&
(x<'A' || x>'Z') &&
(x<'a' || x>'z') )? 0:1;
}
int main(void) {
char key; /* input character from user */
int match; /* keep track of characters matched */
int count; /* number of substring matches */
while (1) {
count = 0;
match = 0;
printf("Please provide a variable name:");
while ((key = getchar()) != '\n') {
count++;
switch (match) {
case 0: /* starting - not matches yet */
if (isfirst(key)) match = 1;
else match = -1;
break;
default:
if (match > 0)
if (iselse(key)) match++;
else match = - match - 1;
break;
}
}
if (count == 0) return 0;
if (match < 0) match = - match - 1;
printf("%d matches of %d\n", match, count);
}
return 0;
}
展开全部
//1以字母和下划线,且后跟字母、数字、下划线的集合。
//2变量名不能包含出“_”(下划线)以外的任何特殊字符,如%、#、逗号、空格等。
//3变量名不能包含空白字符(换行符。空格和制表符称为空白字符
#include<stdio.h>
#include<string.h>
int main()
{
char s[256];
int i,j=1;
gets(s);
while(strcmp(s,"0")!=0)
{ if(s[0]=='_'||'a'<=s[0]&&s[0]<='z'||'A'<=s[0]&&s[0]<='Z')
{
for(i=1;i<strlen(s);i++)
if(s[i]=='_'||'a'<=s[i]&&s[i]<='z'||'A'<=s[i]&&s[i]<='Z'||'1'<=s[i]&&s[i]<='9')
continue;
else
{
j=0;
break;
}
}
else
j=0;
if(j)
printf("VALID");
else
printf("INVALID");
printf("\n");
gets(s);
}
return 0;
}
//2变量名不能包含出“_”(下划线)以外的任何特殊字符,如%、#、逗号、空格等。
//3变量名不能包含空白字符(换行符。空格和制表符称为空白字符
#include<stdio.h>
#include<string.h>
int main()
{
char s[256];
int i,j=1;
gets(s);
while(strcmp(s,"0")!=0)
{ if(s[0]=='_'||'a'<=s[0]&&s[0]<='z'||'A'<=s[0]&&s[0]<='Z')
{
for(i=1;i<strlen(s);i++)
if(s[i]=='_'||'a'<=s[i]&&s[i]<='z'||'A'<=s[i]&&s[i]<='Z'||'1'<=s[i]&&s[i]<='9')
continue;
else
{
j=0;
break;
}
}
else
j=0;
if(j)
printf("VALID");
else
printf("INVALID");
printf("\n");
gets(s);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你得先总结变量名的命名规则(书上都有),再定义一个字符数组来接收输入的变量名,接收后用变量名的规则去比对,会要求给出对的提示,否则提示不对。
对于要循环到开始的地方,我建议你用Do …… while 循环结构即可,条件为字符数组不为0。
具体的过程……你知道的!^O^
对于要循环到开始的地方,我建议你用Do …… while 循环结构即可,条件为字符数组不为0。
具体的过程……你知道的!^O^
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主有没有学过编译原理,书上即有例子。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |