69,382
社区成员
发帖
与我相关
我的任务
分享
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
//char (*(*x[3])())[5];//x是什么类型的变量?
//
//分析C语言声明,关键是搞清楚这个变量是个什么东西(函数、指针、数组),
//是函数那么剩下的就是他的参数和返回值,
//是指针那剩下部分是说明他指向什么,
//是数组剩下的部分就是说明数组的成员是什么类型。
//解析C语言声明规则:
//从左侧第一个标识符开始,按照优先级进行结合。*表示是..的指针,const表示只读的,volatile表示可变的,[]表示是数组,()表示是函数。
//
//x和[3]结合说明是一个大小为3的数组,该数组指向了一个指针,该指针指向一个函数,该函数的无参数,返回一个指针,该指针指向一个大小为5的char型数组
#include <stdio.h>
#include <typeinfo.h>
char num[5];
char (*x00())[5] {
return #
}
int main() {
char (*x000)[5];//返回值
char (*(x00)())[5];//函数原型,参数为空,返回值为指针
char (*(*x0)())[5];//数组的元素,是个函数指针
char (*(*x[3])())[5];//是个数组,大小为3
x0 = x00;
x[0] = x0;
x[1] = x0;
x[2] = x0;
printf("typeid(x).name() is %s\n",typeid(x).name());
return 0;
}
//typeid(x).name() is char (* (__cdecl**)(void))[5]
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag
{
IDENTIFIER,
QUALIFIER,
TYPE
};
struct token
{
char type;
char string[MAXTOKENLEN];
};
int top = -1;
struct token stack[MAXTOKENS];
struct token thos;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string(void)
/* 推断标识符的类型*/
{
char *s = thos.string;
if( !strcmp(s, "const") )
{
strcpy(s, "ready-only");
return QUALIFIER;
}
if( !strcmp(s, "volatile") ) return QUALIFIER;
if( !strcmp(s, "void") ) return TYPE;
if( !strcmp(s, "char") ) return TYPE;
if( !strcmp(s, "signde") ) return TYPE;
if( !strcmp(s, "unsigned") ) return TYPE;
if( !strcmp(s, "short") ) return TYPE;
if( !strcmp(s, "int") ) return TYPE;
if( !strcmp(s, "long") ) return TYPE;
if( !strcmp(s, "float") ) return TYPE;
if( !strcmp(s, "double") ) return TYPE;
if( !strcmp(s, "struct") ) return TYPE;
if( !strcmp(s, "union") ) return TYPE;
if( !strcmp(s, "enum") ) return TYPE;
return IDENTIFIER;
}
void gettoken(void)
/* 读取下一个标记到“this” */
{
char *p = thos.string;
/* 略过空白字符 */
while( (*p == getchar()) == ' ' );
if( isalnum(*p) )
/* 原型:extern int isalnum(int c); 说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。 */
{
/*读入的字符以A-Z,0-9开头*/
while( isalnum( *++p = getchar() ) );
ungetc(*p, stdin);
*p = '\0';
thos.type = classify_string();
return;
}
if( *p == '*')
{
strcpy(thos.string, "pointer to");
thos.type = '*';
return ;
}
thos.string[1] = '\0';
thos.type = * p;
return;
}
void read_to_first_identifer()
{
gettoken();
while( thos.type != IDENTIFIER )
{
push(thos);
gettoken();
}
printf("%s is ",thos.string);
gettoken();
}
void deal_with_arrays()
{
while( thos.type == '[' )
{
printf("array ");
gettoken(); /* 数字或者 ']' */
if( isdigit(thos.string[0]) )
{
printf("0..%d ",atoi(thos.string)-1);
gettoken();
}
gettoken();
printf("of ");
}
}
void deal_with_function_args()
{
while( thos.type != ')' )
{
gettoken();
}
gettoken();
printf("function returning ");
}
void deal_with_pointers()
{
while( stack[top].type == '*' )
{
printf("%s ", pop.string);
}
}
void deal_with_declarator()
/* 处理标识符以后的可能存在的函数/数组 */
{
switch(thos.type)
{
case '[' :deal_with_arrays(); break;
case '(' :deal_with_function_args();
}
deal_with_pointers();
/* 处理读入到标识符之前压入到栈中的符号 */
while(top >= 0)
{
if(stack[top].type == '(')
{
pop;
gettoken(); //读取')'之后的符号
deal_with_declarator();
}
else
{
printf("%s ",pop.string);
}
}
}
int main(void)
{
/* 将标记压入堆栈中,直到遇见标识符 */
read_to_first_identifer();
deal_with_declarator();
printf("\n");
return 0;
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag
{
IDENTIFIER,
QUALIFIER,
TYPE
};
struct token
{
char type;
char string[MAXTOKENLEN];
};
int top = -1;
struct token stack[MAXTOKENS];
struct token thos;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string(void)
/* 推断标识符的类型*/
{
char *s = thos.string;
if( !strcmp(s, "const") )
{
strcpy(s, "ready-only");
return QUALIFIER;
}
if( !strcmp(s, "volatile") ) return QUALIFIER;
if( !strcmp(s, "void") ) return TYPE;
if( !strcmp(s, "char") ) return TYPE;
if( !strcmp(s, "signde") ) return TYPE;
if( !strcmp(s, "unsigned") ) return TYPE;
if( !strcmp(s, "short") ) return TYPE;
if( !strcmp(s, "int") ) return TYPE;
if( !strcmp(s, "long") ) return TYPE;
if( !strcmp(s, "float") ) return TYPE;
if( !strcmp(s, "double") ) return TYPE;
if( !strcmp(s, "struct") ) return TYPE;
if( !strcmp(s, "union") ) return TYPE;
if( !strcmp(s, "enum") ) return TYPE;
return IDENTIFIER;
}
void gettoken(void)
/* 读取下一个标记到“this” */
{
char *p = thos.string;
/* 略过空白字符 */
while( (*p == getchar()) == ' ' );
if( isalnum(*p) )
/* 原型:extern int isalnum(int c); 说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。 */
{
/*读入的字符以A-Z,0-9开头*/
while( isalnum( *++p = getchar() ) );
ungetc(*p, stdin);
*p = '\0';
thos.type = classify_string();
return;
}
if( *p == '*')
{
strcpy(thos.string, "pointer to");
thos.type = '*';
return ;
}
thos.string[1] = '\0';
thos.type = * p;
return;
}
void read_to_first_identifer()
{
gettoken();
while( thos.type != IDENTIFIER )
{
push(thos);
gettoken();
}
printf("%s is ",thos.string);
gettoken();
}
void deal_with_arrays()
{
while( thos.type == '[' )
{
printf("array ");
gettoken(); /* 数字或者 ']' */
if( isdigit(thos.string[0]) )
{
printf("0..%d ",atoi(thos.string)-1);
gettoken();
}
gettoken();
printf("of ");
}
}
void deal_with_function_args()
{
while( thos.type != ')' )
{
gettoken();
}
gettoken();
printf("function returning ");
}
void deal_with_pointers()
{
while( stack[top].type == '*' )
{
printf("%s ", pop.string);
}
}
void deal_with_declarator()
/* 处理标识符以后的可能存在的函数/数组 */
{
switch(thos.type)
{
case '[' :deal_with_arrays(); break;
case '(' :deal_with_function_args();
}
deal_with_pointers();
/* 处理读入到标识符之前压入到栈中的符号 */
while(top >= 0)
{
if(stack[top].type == '(')
{
pop;
gettoken(); //读取')'之后的符号
deal_with_declarator();
}
else
{
printf("%s ",pop.string);
}
}
}
int main(void)
{
/* 将标记压入堆栈中,直到遇见标识符 */
read_to_first_identifer();
deal_with_declarator();
printf("\n");
return 0;
}