譚浩強(qiáng)C語(yǔ)言程序設(shè)計(jì)書籍所包含的代碼示例加注釋說(shuō)明如下:
/*筆記記錄者:xy;學(xué)習(xí)教材 譚浩強(qiáng)C語(yǔ)言程序設(shè)計(jì)*/ /*時(shí)間:2022年11月15日14:40:36*/ /*使用本代碼測(cè)試將 #if 0 ------> #if 1 即可*/ /*代碼未必全部理解正確,歡迎萌新大佬對(duì)其修正*/ #include <stdio.h> //輸入輸出頭文件 /*--------------------(第一章 C語(yǔ)言教程)--------------------*/ #if 1 /*例1.1,p14*/ int main(void) { printf("hello world!\n"); return 0; } #endif #if 0 /*例1.2,p15*/ #include <math.h> int main(void) { double x,s; printf("請(qǐng)輸入任意一個(gè)整數(shù):"); scanf("%lf",&x); s = sin(x); printf("%lf",s); return 0; } #endif #if 0 /*比較兩個(gè)數(shù)的大小*/ //#include "math.h" int max(int a,int b);//函數(shù)聲明 int main(void)//主函數(shù) { int x,y,z; //int max(int a,int b);/*可有可無(wú),無(wú)影響*/ printf("Input two number:\n"); scanf("%d%d",&x,&y); z=max(x,y);//----------------調(diào)用比較函數(shù)max--------------- printf("maxnum = %d\n",z); } int max(int a,int b)//自定義比較函數(shù) { if(a>b) return a; else return b; } #endif /*--------------------(第二章 程序的靈魂——算法)--------------------*/ #if 0 /*數(shù)據(jù)結(jié)構(gòu)+算法=程序*/ /* 算法分類: 1)數(shù)值運(yùn)算算法:求解數(shù)值. 2)非數(shù)值運(yùn)算算法:事務(wù)管理領(lǐng)域. */ /*例2.1 求1*2*3*4*5,p32*/ #if 0 int main() { int i,s = 1; for(i = 1;i <= 5;i++){ s = s*i; } printf("s = %d\n",s); } #else /*例2.1.1 求1*3*5*7*9*11*/ int main() { int n,m; printf("輸入相乘的個(gè)數(shù)m:"); scanf("%d",&m); int i = 1,s = 1; for(n = 1;n <= m;n++){ s = s*i; i = i+2; } printf("輸出m(%d)個(gè)數(shù)相乘1*3*5*...*%d*%d的值:",m,i-2,i); printf("s = %d\n",s); } #endif #endif #if 0 /*例2.2,輸出50(或m)個(gè)學(xué)生中,分?jǐn)?shù)在80分以上的,p33*/ int main() { int m,i = 0; int n[50] = {0};//學(xué)號(hào)(輸入的學(xué)號(hào)不能以0開頭如:001,在十進(jìn)制中以0開頭不合法,0無(wú)法輸出出來(lái),第三章會(huì)有介紹) int g[50] = {0};//分?jǐn)?shù) printf("一共需要錄入成績(jī)的人數(shù)有m:"); scanf("%d",&m); for(i=1;i<=m;i++)//循環(huán)輸入50個(gè)人的成績(jī) { printf("請(qǐng)輸入第%d個(gè)學(xué)生的學(xué)號(hào)和成績(jī)\n",i); scanf("%d%d",&n[i-1],&g[i-1]); } printf("\n\n 成績(jī)大于80的學(xué)生的學(xué)號(hào)和成績(jī)?yōu)閈n\n"); for(i=1;i<=m;i++)//有幾人則循環(huán)幾次 { if(g[i-1]>=80) { printf("學(xué)號(hào):%d 成績(jī):%d\n",n[i-1],g[i-1]); } } return 0; } #endif #if 0 /*例2.3,判斷2000——2500年中的閏年,并輸出,p33*/ /*在此基礎(chǔ)上修改判斷任意時(shí)間段中的閏年*/ #if 0 int main(void) { int i; for(i=2000;i<=2500;i++){ if(i%4==0&&i%100!=0){ printf("%d是閏年\n",i); } else if(i%100==0&&i%400==0){ printf("%d是閏年\n",i); } } return 0; } #else int main(void) { int i,m,n; printf("輸入任意兩段年限,判斷是否為閏年:"); scanf("%d%d",&m,&n); for(i=m;i<=n;i++){ if(i%4==0&&i%100!=0){ printf("%d是閏年\n",i); } else if(i%100==0&&i%400==0){ printf("%d是閏年\n",i); } } return 0; } #endif #endif #if 0 /*例2.4,求1-1/2+1/3-1/4+....+1/99-1/100,p34*/ int main(void) { int i; double a = 1.0,s = 1.0; for(i=2;i<=100;i++){ a = (-1) * a; s = s + a/i; } printf("輸出1-1/2+1/3-1/4+....+1/99-1/100的值s = %f",s); return 0; } #endif #if 0 /*例2.5,對(duì)于一個(gè)大于或等于3的正整數(shù),判斷它是不是一個(gè)素?cái)?shù),p34*/ int main(void) { int i;//要判斷的數(shù)字 int m;//用于輔助判斷 int k;//k用作除數(shù) printf("輸入一個(gè)大于或等于3的正整數(shù)i:"); scanf("%d",&i); for(k=2;k<=i;k++){ //k用作除數(shù),用于判斷是否為素?cái)?shù) m=i%k; if(m==0) break; } if(k>=i){ printf("%d是素?cái)?shù)\n",i); }else{ printf("%d不是素?cái)?shù)\n",i); } return 0; } #endif /*--------------------(第三章 數(shù)據(jù)類型、運(yùn)算符與表達(dá)式)--------------------*/ /************************************************************************ 3.1數(shù)據(jù)類型分類: (1)基本數(shù)據(jù)類型: 1)整型: 1.基本型:int(2 byte(字節(jié))) 2.短整型:short int或short(2 byte) 3.長(zhǎng)整型:long int或long(4 byte) 4.無(wú)符號(hào)型:unsigned *無(wú)符號(hào)基本型:unsigned int *無(wú)符號(hào)短整型:unsigned short int *無(wú)符號(hào)長(zhǎng)整型:unsigned long int **無(wú)符號(hào)型數(shù)據(jù),所占空間不變,正數(shù)最大范圍變?yōu)樵瓉?lái)的2倍,但無(wú)法表示負(fù)數(shù)(p48)** 2)字符型:char(1 byte) 3)實(shí)型(浮點(diǎn)型) 1.單精度型:float(4 byte) 2.雙精度型:double(8 byte) 4)枚舉類型 (2)構(gòu)造數(shù)據(jù)類型 1)數(shù)組類型 2)結(jié)構(gòu)體類型 3)共用體(聯(lián)合)類型 (3)指針類型 (4)空類型 ***************************************************************************/ #if 0 /*例3.1,符號(hào)常量的使用,p45*/ #define PRICE 30 //宏定義全局變量 int main(void) { int num,total; num = 10; total = num*PRICE; printf("total = %d\n",total); return 0; } #endif #if 0 /*例3.2,整型變量的定義與使用,p49*/ int main(void) { int a,b,c,d; //a,b,c,d為整型變量 unsigned u; //u為無(wú)符號(hào)整型變量 a = 12;b = -24;u = 10;//對(duì)各個(gè)變量賦值 c = a + u; d = b + u; printf("a+u = %d,b+u = %d\n",c,d); return 0; } #endif #if 0 /*例3.3整型數(shù)據(jù)的溢出,p49*/ #if 0 int main(void) { /*在我的編譯器中定義int被默認(rèn)為了long int(4 byte),正常情況下應(yīng)為基本類型int(2 byte)*/ int a,b; a = 2147483647;//在此處為long int(4 byte) 數(shù)值范圍-2147483648~2147483647,查看p62 b = a + 1; printf("%d,%d\n",a,b); return 0; } #else int main(void) { short int a,b; a = 32767;//此處為short int (2 byte)數(shù)值范圍-32768~32767,查看p62 b = a + 1; printf("%d,%d\n",a,b); return 0; } #endif #endif #if 0 /*例3.4,類型轉(zhuǎn)換,p50*/ int main(void) { long x,y; int a,b,c,d; x = 5,y = 6; a = 7,b = 8; c = x + a; d = y + b; printf("c=x+a= %d,d=y+b= %d\n",c,d); return 0; } #endif #if 0 /*eg3.5,c允許浮點(diǎn)型使用后綴,p51*/ int main(void) { printf("%f\n",356.); printf("%f\n",356); printf("%f\n",356f); return 0; } #endif #if 0 /*eg3.6,實(shí)行數(shù)據(jù)的舍入誤差,p51*/ int main(void) { float a,b; a = 123456.789e5; b = a + 20; printf("%f\n",a); printf("%f\n",b); } #endif #if 0 /*eg3.7,實(shí)行數(shù)據(jù)的舍入誤差,p52*/ int main(void) { float a;//單精度有效位為7位,所以小數(shù)點(diǎn)后2位以后無(wú)效 double b;//雙精度有效位16位,但C規(guī)定小數(shù)后最多保留六位,其余部分四舍五入 a=33333.33333; b=33333.33333333333333; printf("%f\n%f\n",a,b); return 0; } #endif #if 0 /***************************************************************************** 轉(zhuǎn)義字符: 1)\n 回車換行 2)\t 橫向跳到下一制表位置 3)\b 退格 4)\r 回車 5)\f 走紙換頁(yè) 6)\\ 反斜線符"\" 7)\' 單引號(hào)符 8)\" 雙引號(hào)符 9)\a 鳴鈴 10)\add 1~3位八進(jìn)制所代表的字符 11)\xhh 1~2位十六進(jìn)制所代表的字符 ******************************************************************************/ /*eg3.8,轉(zhuǎn)義字符的使用,p53*/ int main(void) { int a,b,c; a = 5;b = 6;c = 7; printf(" ab c\tde\rf\n"); printf("hijk\tL\bM\n"); return 0; } #endif #if 0 /*eg3.9,向字符變量賦以整數(shù),p54*/ int main(void) { char a,b; a = 120; b = 121; printf("%c,%c\n",a,b);//輸出字符型數(shù)據(jù) printf("%d,%d\n",a,b);//輸出整型數(shù)據(jù) return 0; } #endif #if 0 /*eg3.10,字母大小寫轉(zhuǎn)換,p54*/ int main(void) { char a,b; a = 'a'; b = 'b'; a = a - 32;//字母大小寫的ASSCII碼相差32 b = b - 32; printf("%c,%c\n%d,%d\n",a,b,a,b);//輸出轉(zhuǎn)換字符,和相應(yīng)的ASCII碼值 return 0; } #endif #if 0 /*eg3.11,變量賦初值操作,p55*/ int main(void) { int a = 3,b,c = 5; b = a + c; printf("a=%d,b=%d,c=%d\n",a,b,c); return 0; } #endif #if 0 /* 數(shù)據(jù)類型自動(dòng)轉(zhuǎn)換規(guī)則: char,short——>int——>unsigned——>long——>double 將char轉(zhuǎn)為int用atoi()進(jìn)行轉(zhuǎn)換(該函數(shù)包含在stdlib.h頭文件中) */ /*eg3.12,數(shù)據(jù)類型轉(zhuǎn)換,p56*/ #include <stdlib.h> //目的調(diào)用atoi函數(shù)進(jìn)行數(shù)據(jù)轉(zhuǎn)換 int main(void) { char PI[8] = "3.14159"; int s,r = 5; s = r*r*atoi(PI); printf("s=%d\n",s); return 0; } #endif #if 0 /*eg3.13,強(qiáng)制類型轉(zhuǎn)換,p57*/ int main(void) { float f = 5.75; printf("(int)f=%d,f=%f\n",(int)f,f);//將float強(qiáng)制轉(zhuǎn)換為int return 0; } #endif #if 0 /*eg3.14,基本的算術(shù)運(yùn)算符,p58*/ int main(void) { printf("\n\n%d,%d\n",20/7,-20/7); printf("%f,%f\n",20.0/7,-20.0/7); return 0; } #endif #if 0 /*eg3.15,%取余運(yùn)算符的使用,p58*/ int main(void) { /*100除以3取余數(shù)*/ printf("%d\n",100%3);//本例輸出 100 除以 3 所得的余數(shù) 1。 return 0; } #endif #if 0 /*eg3.16,自增自減運(yùn)算符操作,p59*/ int main(void) { int i = 8; printf("%d\n",++i);//9,++i先自加,后運(yùn)算 //printf("%d\n",i);//9 printf("%d\n",--i);//8,--i先自減,后運(yùn)算 //printf("%d\n",i);//8 printf("%d\n",i++);//8,i++先運(yùn)算,后自加 //printf("%d\n",i);//9 printf("%d\n",i--);//9,i--先運(yùn)算,后自減 //printf("%d\n",i);//8 printf("%d\n",-i++);//-8, //printf("%d\n",i);//9 printf("%d\n",-i--);//-9 //printf("%d\n",i);//8 return 0; } #endif #if 0 /*eg3.17,自加運(yùn)算分析,p60*/ /*可運(yùn)行出結(jié)果,會(huì)有報(bào)錯(cuò),解決報(bào)錯(cuò)需將p,q分行寫即可*/ #if 1 int main(void) { /*報(bào)錯(cuò)(Multiple unsequenced modifications to 'i'clang(-Wunsequenced))但可運(yùn)行*/ int i = 5,j = 5,p,q; p = (i++) + (i++) + (i++);//5+6+7 = 18 q = (++j) + (++j) + (++j);//6+7+8 = 21 printf("p=%d,q=%d,i=%d,j=%d\n",p,q,i,j);//18,21,7,8 return 0; } #else int main(void) { /*解決報(bào)錯(cuò)*/ int i = 5,j = 5,p,q; p = i++;//5 p = p + (i++);//5+6 = 11 p = p + (i++);//11+7 =18 q = ++j;//6 q = q + (++j);//6+7 = 13 q = q + (++j);//13+8 = 21 printf("p=%d,q=%d,i=%d,j=%d\n",p,q,i,j);//18,21,7,8 return 0; } #endif #endif #if 0 /*eg3.19,逗號(hào)運(yùn)算符的使用,p62*/ int main(void) { int a = 2,b = 4,c = 6,x,y; y = (x=a+b),c=(b+c); printf("y=%d,x=%d,c=%d",y,x,c);//6,6,10 return 0; } #endif