呃 = = 自学C已有一个月时间之久,进度也完成了50%了 = = (后面的可都是巨头......) = = 在这个月中我感触最深的是 关于 goto 语句,虽然很多淫都说这个很不好 但我在实际的应用中发现,如果应用得当,一样很好!!!
关于 goto
如下面代码所示:
1234567891011121314#include <stdio.h>main(void){ int a; restart: printf("%d ",a); a=a+1; if(40<=a){ goto exit;} else{ goto restart;} //循环打印从0~32767的所有数 exit: return 0;}
= = 呃,就是这样的 它等效于:
12345678910#include <stdio.h>main(void){ int a; for(a=0;a<=39;++a){ printf("%d ",a);} return 0;}
= = 除此之外,goto语句还有很多用法,比如:当程序出现错误的时候,程序就可以使用goto语句来排错或保存日志
比如:
12345678910111213141516171819202122232425262728#include <stdio.h>#define TRUE 12int main(void){ int a; restart: printf("输入正确的答案:\t3+9= "); scanf("%d",&a); if(TRUE==a){ printf(">_<恭喜你!答对了!\n"); printf("按 Enter 键退出"); getch();} else{ printf("Oh,老兄 你输错了= = \n"); printf("重新输入?(0=continue、1=exit)\t"); scanf("%d",&a); if(1==a){ goto exit;} else{ goto restart;} } exit: return 0;}
如果输入错误,程序则会重新要求输入,厉害吧 = =
示例: 要求输入0、1、2、3、4 、5四个数,每个数都用不同的计算方法(如加、减、乘、除、π) 如输入1就执行加法运算
Sorce:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141//呐,这是我自学C一个月的成果= = (C已完成50%了)/*此代码的功能是选择运算方法来计算输入的值 = = *///此代码在Windows中通过(使用XP系统,没有出现任何的提示信息)//但在Linux终端里的GCC和Geany会出现故障(还可运行)//在Geany里的信息如下:/*gcc -Wall -o "csxxoo" "csxxoo.c" (在目录/host/VecHK/10中)csxxoo.c: In function ‘js_jia’:csxxoo.c:9: warning: writing into constant object (argument 2)csxxoo.c:11: warning: writing into constant object (argument 2)csxxoo.c: In function ‘js_jian’:csxxoo.c:19: warning: writing into constant object (argument 2)csxxoo.c:21: warning: writing into constant object (argument 2)csxxoo.c: In function ‘js_cheng’:csxxoo.c:29: warning: writing into constant object (argument 2)csxxoo.c:31: warning: writing into constant object (argument 2)csxxoo.c: In function ‘js_chu’:csxxoo.c:39: warning: writing into constant object (argument 2)csxxoo.c:41: warning: writing into constant object (argument 2)csxxoo.c: In function ‘js_pa’:csxxoo.c:48: warning: writing into constant object (argument 2)csxxoo.c: In function ‘main’:csxxoo.c:59: warning: unused variable ‘fzhi’csxxoo.c:57: warning: unused variable ‘izhi’csxxoo.c:68: warning: ‘chan_1’ may be used uninitialized in this functioncsxxoo.c:68: warning: ‘chan_2’ may be used uninitialized in this functioncsxxoo.c:81: warning: ‘chan1’ may be used uninitialized in this functioncsxxoo.c:81: warning: ‘chan2’ may be used uninitialized in this function编译成功结束。*///呃,如果还有什么问题,那就来找我吧 QQ:498302569 Mail:VecHK@QQ.com#include <stdio.h>#define pa 3.14159int js_jia(const int x,const int y){ //加法函数 int z; printf("输入加数: "); scanf("%d",&x); printf("输入另一个加数: "); scanf("%d",&y); z= x+y; printf("%d + %d= %d\n",x,y,z); return z;} int js_jian(const int x,const int y){ //减法函数 int z; printf("输入被减数: "); scanf("%d",&x); printf("输入减数: "); scanf("%d",&y); z= x-y; printf("%d-%d= %d\n",x,y,z); return z;}int js_cheng(const int x,const int y){ //乘法函数 int z; printf("输入因数:"); scanf("%d",&x); printf("输入另一个因数:"); scanf("%d",&y); z= x*y; printf("%d*%d= %d\n",x,y,z); return z;}float js_chu(const float x,const float y){ //除法函数 float z; printf("输入被除数: "); scanf("%f",&x); printf("输入除数: "); scanf("%f",&y); z= x/y; printf("%f/%f= %f\n",x,y,z); return z;} float js_pa(const float r){ //求pa的面积 = = printf("输入半径: "); scanf("%f",&r); printf("%f^2 * %f= %f\n",r,pa,r*r*pa); return r;} int main(void){ //主函数 int a; int chan_1;int chan_2; int izhi; float chan1;float chan2; float fzhi; printf("请选择: (0=exit,1=加法,2=减法,3=乘法,4=除法,5=pa pa取3.14159)\n");restart: //虽然 goto 语句并不是推荐用,但是在某些情况下很好用!!!>_< scanf("%d",&a); switch (a){ case 0: goto exit; break; case 1: //加载 加法函数 js_jia(chan_1,chan_2); break; case 2: //加载 减法函数 js_jian(chan_1,chan_2); break; case 3: //加载 乘法函数 js_cheng(chan_1,chan_2); break; case 4: //加载 除法函数 js_chu(chan1,chan2); break; case 5: //Pa js_pa(chan1); break; default: //输入错误后重新选择 printf("输入错误的数,请重新输入(0=exit,1=加法,2=减法,3=乘法,4=除法,5=pa pa取3.14159)\n"); goto restart; break;} printf("重新选择计算方法?(1= continue 0= exit)"); scanf("%d",&a); if(1 == a){ //判断是否goto restart printf("请选择: (0=exit,1=加法,2=减法,3=乘法,4=除法,5=pa pa取3.14159)\n"); goto restart;}exit: return 0;}//code by Vec = =