Way on c & c++ C岁月 Part 1 —— 【篇二】

<!-- [if !mso]> <mce:style><!-- v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} --> <!-- [endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <mce:style><!-- st1/:*{behavior:url(#ieooui) } --> <!-- [endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 3 2 2 2 4; mso-font-charset:0; mso-generic-font-family:script; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]--><!-- [if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1047"/> </xml><![endif]--><!-- [if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> <o:rules v:ext="edit"> <o:r id="V:Rule1" type="connector" idref="#_x0000_s1044"> <o:proxy start="" idref="#_x0000_s1042" connectloc="7"/> <o:proxy end="" idref="#_x0000_s1043" connectloc="0"/> </o:r> </o:rules> </o:shapelayout></xml><![endif]-->

<!-- [if supportFields]><span lang=EN-US><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span style='mso-spacerun:yes'>&nbsp;</span>= 2 /* GB2 <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]--> <!-- [if supportFields]><span lang=EN-US><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->简单的数据类型?感觉不那么简单。

C 语言的数据类型我认为可以划分为 3 类,分别是空类型 void 、基本类型(字符型、整型、浮点型和枚举类型)和派生类型(数组、函数、指针、结构体和共用体)。

标准 C 上规定 int 类型范围为 -32768 ~ 32767 ,然而实际上往往并非如此,这取决于编译器的设计,比如下述代码:

#include <stdio.h>

int main(int argc, char *argv[]){

printf("%d/n" , sizeof (int ));

printf("%d/n" , sizeof (float ));

printf("%d/n" , sizeof (double ));

printf("%d/n" , sizeof (long long ));

return 0;

}

vc2008 中的输出为: 4 4 8 8

这说明了在 vc2008 编译环境下, int 类型占据了 4 个字节的内存空间。而看到 int 类型,很容易让人联想到自加运算( ++ ),关于自加运算不妨看看 a+++b a+++++b a+++(++b) 有何区别。下面是测试的结果:

#include <stdio.h>

int main(int argc, char *argv[]){

int a,b,c;

a = b = c = 0;

//c = a+++b; // 输出 1,0,0

//c = a+++++b; // 无法通过编译,提示 ++ 需要左值

//c = a+++(++b);// 输出 1,1,1

printf("%d,%d,%d/n" , a,b,c);

return 0;

}

插入旁白:关于左值、右值,网上有人说是翻译错误,应理解为可寻址的和可读的,我觉得也颇有几分道理,但这里就引用编译器报错原文。

第一种结果很容易理解,先执行 c=a+b ,之后 a 自加 1 ;第三种结果也好理解,在赋值运算前 ++b 先得到执行,所以 c 1 ,赋值之后 a++ 得到执行。关键在第二种,为什么会编译出错?“ ++ 需要左值”是指哪个“ ++ ”?为什么是那个“ ++ ”?在此,不妨反问一句,如果让自己设计编译器,那么遇到 a+++++b 这样的语句应该怎样处理?先带着这个疑问继续探寻。

编译器会对 a+++++b 这样的表达式进行出错处理,但却接受 a+++b 这样的表达式,从一定程度上说明了编译器“聪明”到可以处理 a++ ,但却还不够“聪明”去处理 ++b 。当然,这里指的 ++b 是特定地、位于 a+++++b 这样表达式当中的限定场合的。为什么呢?这就属于词法分析的问题了。仍然回到上面那个问题,如果让自己设计编译器,遇到 a+++b 要怎么办呢?

为了找出是哪个“ ++ ”需要左值,我们不妨将代码稍加改动为:

#include <stdio.h>

int main(int argc, char *argv[]){

int a,b,c;

a = b = c = 0;

c = ((a++)++)+b;

printf("%d,%d,%d/n" , a,b,c);

return 0;

}

编译器仍然提示同样的错误,于是我们可以推出是第三个加号和第四个加号组成的“ ++ ”运算符需要左值,即 a+++++b 等效于 ((a++)++)+b

想来现在的最新问题是,(a++) 后面为什么不能接自加运算“++ ”?

回到l-valuer-value 上来,不管采用左值、右值一说,或者采用可寻址值和可读值一说,其本质都是对内存的读写操作(我无法确定这里是否有寄存器操作),也就是说源操作数和目的操作数的地址要能确定。这问题个人感觉不是很好回答,因为依旧涉及到了编译器的设计问题,在此仅谈谈个人的理解。

++a 是先对a 执行加1 操作,然后返回对a 的引用,自然就可以确定此时a 的地址,所以 (++a)++ 是可以编译通过的(于visual c++ 2008编译环境,经博友Promi测试,VC6和GCC并不能编译通过 )。而a++ 是新建了一个临时变量并赋予其当前a 的值,再对a 进行增1 运算,最后返回临时变量值,此时就无法再编译的时候获得该临时变量的地址,于是造成了((a++)++)+b 无法编译通过。

由此可知,在++aa++ 不影响逻辑结构的情况下,我们应该倾向于使用++a 。需要强调的是,我使用的是vc2008 编译环境。

接着回到编译器设计考虑的问题。在C 语言编译器的词法分析中有“贪心法”一说,因为存在“++ ”运算,所以遇到“+ ”自然要再往前看是否还有“+ ”。或许应该换一种说法,在我的知识范围内,词法分析都有“贪心法”一说,即识别单词过程应该包含尽可能多的字符。这很好理解,比如 C 语言中要求变量名必须以下划线或者字母开头,之后可以下划线、字母和数字混合,那么当我们遇到 _abc123def 时,我们难道分析道 _abc 就停止了吗?自然不是的,我们要继续往前以识别出一个完整的单词。这是词法分析的知识范畴,那么编译器是如何识别出标识符的呢?关于这个问题,杭电 ACM 网站上有这么一道简单题,可以去测试一下自己的思路对不, http://acm.hdu.edu.cn/showproblem.php?pid=2024 。再深入想想,如果是判断合法的数值类型应该怎么操作呢?这里的数值类型包括整形、浮点型和指数型。

对于上述杭电 2024 题目,我第一次的思路是分为两种情况:第一个字母只能有下划线和字母,其它字母只能有下划线、字母或数字,于是产生了以下代码:

#include <stdio.h>

#include <ctype.h>

int main(int argc, char *argv[]){//hdu2024_1

int n,flag;

char id[100];

scanf("%d" , &n);

getchar();// 由于 scanf 会将 '/n' 放在缓冲区中,所以先处理掉它

while (n--){

flag = 0;

gets(id);

if (!(id[0] == '_' || isalpha(id[0]))) flag = 1;

for (int i=1; id[i] != '/0' ; ++i){

if (!(isalpha(id[i]) || isdigit(id[i]) || id[i] == '_' )){

flag = 1;

break ;

}

}

printf("%s/n" , (flag == 1) ? "no" : "yes" );

}

return 0;

}

这种思路比较简单,适用于识别标识符,但如果是要识别数值呢(含整形、浮点和指数型)?这里给出另外一种识别标识符的思路,可以用来识别数值类型,即编译器设计时使用的状态转换图(或其改进后的自动机):


<!-- [if mso & !supportInlineShapes & supportFields]><span lang=EN-US style='font-size:9.0pt;font-family:"Comic Sans MS"'><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>/* MERGEFORMAT <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]--><!-- [if gte vml 1]><v:group id="_x0000_s1026" editas="canvas" style='width:415.3pt;height:103.7pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' coordorigin="2362,11568" coordsize="7304,1826"> <o:lock v:ext="edit" aspectratio="t"/> <v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_s1027" type="#_x0000_t75" style='position:absolute; left:2362;top:11568;width:7304;height:1826' o:preferrelative="f"> <v:fill o:detectmouseclick="t"/> <v:path o:extrusionok="t" o:connecttype="none"/> <o:lock v:ext="edit" text="t"/> </v:shape><v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"> <v:stroke joinstyle="miter"/> <v:path gradientshapeok="t" o:connecttype="rect"/> </v:shapetype><v:shape id="_x0000_s1028" type="#_x0000_t202" style='position:absolute; left:2837;top:12255;width:633;height:417;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox style="mso-fit-shape-to-text:t" mce_style="mso-fit-shape-to-text:t"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><b><u><span lang=EN-US style='font-size:14.0pt;color:black'>id :</span></u></b><b><span lang=EN-US style='font-size:14.0pt;mso-bidi-font-family:宋体;color:black'><o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:line id="_x0000_s1029" style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" mce_style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" from="3510,12927" to="4866,12927" strokeweight="2pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrow="block" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:line><v:oval id="_x0000_s1030" style='position:absolute;left:4866;top:12613; width:626;height:627;mso-wrap-style:none;mso-position-horizontal:absolute; mso-position-vertical:absolute;v-text-anchor:middle' filled="f" fillcolor="aqua" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:oval><v:line id="_x0000_s1031" style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" mce_style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" from="5492,12927" to="6849,12927" strokeweight="2pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrow="block" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:line><v:shape id="_x0000_s1032" type="#_x0000_t202" style='position:absolute; left:3614;top:12509;width:1044;height:351;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox style="mso-fit-shape-to-text:t" mce_style="mso-fit-shape-to-text:t"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span lang=EN-US style="mso-bidi-font-size: 10.5pt;color:black" mce_style="mso-bidi-font-size: 10.5pt;color:black">start<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1033" type="#_x0000_t202" style='position:absolute; left:5369;top:12529;width:1583;height:412;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span lang=EN-US style="mso-bidi-font-size: 10.5pt;color:black" mce_style="mso-bidi-font-size: 10.5pt;color:black">letter or &lsquo;_&rsquo;<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1034" type="#_x0000_t202" style='position:absolute; left:4971;top:12718;width:417;height:351;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox style="mso-fit-shape-to-text:t" mce_style="mso-fit-shape-to-text:t"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN">0</span></b><b><span style='mso-bidi-font-size:10.5pt;mso-bidi-font-family:宋体;color:black; mso-ansi-language:ZH-CN'><o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:line id="_x0000_s1035" style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" mce_style="position:absolute;mso-wrap-style:none; mso-position-horizontal:absolute;mso-position-vertical:absolute; v-text-anchor:middle" from="7475,12927" to="8623,12927" strokeweight="2pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrow="block" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:line><v:shape id="_x0000_s1036" type="#_x0000_t202" style='position:absolute; left:7475;top:12509;width:1043;height:351;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox style="mso-fit-shape-to-text:t" mce_style="mso-fit-shape-to-text:t"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span lang=EN-US style="mso-bidi-font-size: 10.5pt;color:black" mce_style="mso-bidi-font-size: 10.5pt;color:black">other<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:group id="_x0000_s1037" style='position:absolute;left:8623;top:12613; width:626;height:627' coordorigin="3696,1152" coordsize="288,288"> <v:oval id="_x0000_s1038" style='position:absolute;left:3696;top:1152; width:288;height:288;v-text-anchor:middle' filled="f" fillcolor="aqua" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:oval><v:oval id="_x0000_s1039" style='position:absolute;left:3744;top:1200; width:192;height:192;v-text-anchor:middle' filled="f" fillcolor="aqua" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:oval><v:shape id="_x0000_s1040" type="#_x0000_t202" style='position:absolute; left:3696;top:1200;width:288;height:231' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN">2<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape></v:group><v:group id="_x0000_s1041" style='position:absolute; left:6849;top:12613;width:626;height:627' coordorigin="2880,1152" coordsize="288,288"> <v:oval id="_x0000_s1042" style='position:absolute;left:2880;top:1152; width:288;height:288;v-text-anchor:middle' filled="f" fillcolor="aqua" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:oval><v:shape id="_x0000_s1043" type="#_x0000_t202" style='position:absolute; left:2880;top:1200;width:288;height:231' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:10.5pt; color:black;mso-ansi-language:ZH-CN">1<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shapetype id="_x0000_t38" coordsize="21600,21600" o:spt="38" o:oned="t" path="m,c@0,0@1,5400@1,10800@1,16200@2,21600,21600,21600e" filled="f"> <v:formulas> <v:f eqn="mid #0 0"/> <v:f eqn="val #0"/> <v:f eqn="mid #0 21600"/> </v:formulas> <v:path arrowok="t" fillok="f" o:connecttype="none"/> <v:handles> <v:h position="#0,center"/> </v:handles> <o:lock v:ext="edit" shapetype="t"/> </v:shapetype><v:shape id="_x0000_s1044" type="#_x0000_t38" style='position:absolute; left:3072;top:1146;width:6;height:102;rotation:270;flip:x y;v-text-anchor:middle' o:connectortype="curved" adj="-669601,212188,9698400" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrow="block" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> </v:shape></v:group><v:shape id="_x0000_s1045" type="#_x0000_t202" style='position:absolute; left:6952;top:11980;width:1900;height:412;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><b><span lang=EN-US style='mso-bidi-font-size:10.5pt;color:black'>letter or digit or &lsquo;_&rsquo;<o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1046" type="#_x0000_t202" style='position:absolute; left:9040;top:12509;width:626;height:470;mso-position-horizontal:absolute; mso-position-vertical:absolute' filled="f" fillcolor="aqua" stroked="f" strokeweight="1pt"> <v:stroke startarrowwidth="narrow" startarrowlength="short" endarrowwidth="narrow" endarrowlength="short"/> <v:shadow color="black"/> <v:textbox style="mso-fit-shape-to-text:t" mce_style="mso-fit-shape-to-text:t"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><b><span style="font-size:18.0pt;color:black; mso-ansi-language:ZH-CN" mce_style="font-size:18.0pt;color:black; mso-ansi-language:ZH-CN">*</span></b><b><span style="font-size:18.0pt; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN" mce_style="font-size:18.0pt; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN"><o:p></o:p></span></b></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><w:wrap type="none"/> <w:anchorlock/> </v:group><![endif]--><!-- [if !vml]--> <!-- [endif]--> <!-- [if mso & !supportInlineShapes & supportFields]><span lang=EN-US style='font-size:9.0pt;font-family:"Comic Sans MS"'><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:415.3pt;height:103.7pt'> <v:imagedata croptop="-65520f" cropbottom="65520f"/> </v:shape><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->

上述识别标识符转换图的含义为:开始状态为 0 ,如果遇到字母或者下划线则转向状态 1 ;在状态 1 下,如果遇到字母、数组或者下划线则仍转向状态 1 ,遇到其它则表示识别出一个标识符了。由其可产生如下代码:

#include <stdio.h>

#include <ctype.h>

int main(int argc, char *argv[]){//hdu2024_2

int n, flag, state;

char id[100];

scanf("%d" , &n);

getchar();// 由于 scanf 会将 '/n' 放在缓冲区中,所以先处理掉它

while (n--){

flag = state = 0;

gets(id);

for (int i=0; id[i] != '/0' ; ++i){

switch (state){

case 0:

if (isalpha(id[i]) || id[i] == '_' ) state = 1;

else { flag = 1; break ; }

break ;

case 1:

if (isalpha(id[i]) || isdigit(id[i]) || id[i] == '_' ) state = 1;

else { flag = 1; break ; }

break ;

case 2: // 这里的 case 2 对于 hdu2024 这道题目不是必须的,但在词法分析中却是需要的

flag = 1; break ;

}

}

printf("%s/n" , (flag == 1) ? "no" : "yes" );

}

return 0;

}

数据类型果真不那么简单,上面那么多的“ + ”已经有点眼花缭乱了,但对于数据类型的知识范围而言不过是刚刚开了个头。现在给自己一道饭后甜点, char 类型的单个字符为什么不能表示一个汉字字符呢?

2010-1-26

----------------------------------------cutting line----------------------------------------

你可能感兴趣的:(part)