收藏 分销(赏)

简单的例子.doc

上传人:丰**** 文档编号:3676151 上传时间:2024-07-13 格式:DOC 页数:17 大小:77KB 下载积分:8 金币
下载 相关 举报
简单的例子.doc_第1页
第1页 / 共17页
简单的例子.doc_第2页
第2页 / 共17页


点击查看更多>>
资源描述
2.1 簡單的例子 1. 試寫一個程式,可列印出如下的輸出結果: See you tomorrow. Have a good night. /* hw2_1.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i=5; printf("%d+%d=%d\n",i,i,i+i); system("pause"); return 0; } 2.2解析C語言 9. 在編譯下面的程式碼時,編譯器會給您什麼樣的錯誤訊息?請試著了解此一錯誤訊息,並修正錯誤之處。 01 /* hw2_9.c, 有錯誤的程式碼 */ 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main(void) 05 { 06 i=5; 07 printf("i=%d",i); 08 system("pause"); 09 return 0; 10 } 2.2 識別字及關鍵字 12.下面哪些是有效的識別字? _artist #japan ChinaTimes Y2k 2cats pentium3 22456 TOMBO A1234 __two jdk1_3 2_cugii a pencil println news# NO1 AAA ___AMD 2.3 除錯 18.試找出下列程式錯誤之處,並嘗試修正之: 01 /* hw2_18, 請找出此程式何處有誤 */ 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main(void) 05 { 06 int num=2 07 printf(num=%d,num); 08 system(pause); 09 return 0; 10 } 3.2基本資料型態 3.下列何者是錯誤的常數?試指出其錯誤之所在。 甲、 134.45L 乙、 10km24 丙、 a2048 丁、 1.3453F 3.4資料型態的轉換 19.請參閱下面的程式碼,然後回答接續的問題: 01 /* hw3_19, 型態轉換的練習 */ 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main(void) 05 { 06 int num1=5,num2=8; 07 printf("%d\n",num1/num2); 08 09 system("pause"); 10 return 0; 11 } a. 試解釋第7行的輸出結果為何是0? 試修改程式碼,利用型態轉換的方式,使得第7行的輸出結果為0.625000。 /* hw3_19b.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int num1=5,num2=8; printf("%f\n",(float)num1/num2); system("pause"); return 0; } 4.1 4.1函數printf() 2.試撰寫一程式,利用printf() 函數列印出如下的字串(必須包含雙引號): "I love C language best." /* hw4_2.c */ #include <stdio.h> #include <stdlib.h> int main(void) { printf("\"I love C language best.\"\n"); system("pause"); return 0; } 5.試撰寫一程式,利用printf() 函數列印出如下的字串(必須包含單引號): '30% 的學生來自中部地區,42% 的學生來自南部地區。' /* hw4_5.c */ #include <stdio.h> #include <stdlib.h> int main(void) { printf("\'30%%的學生來自中部地區,42%%的學生來自南部地區\'\n "); system("pause"); return 0; } 4.2輸入函數scanf() 9.下面的程式碼是想設計從鍵盤讀入一個整數,並設定給變數num存放。此程式於執行時會發生錯誤,試指出錯誤之所在,並試著修正之,使得程式可以正確的執行。 01 /* hw4_9, 使用scanf()函數的錯誤 */ 02 #include <stdio.h> 03 #include <stdlib.h> 04 int main(void) 05 { 06 int num; 07 scanf("%d",num); 08 printf("num=%d\n",num); 09 10 system("pause"); 11 return 0; 12 } /* hw4_9a.c, 修正hw4_9--使用scanf()函數的錯誤 */ #include <stdio.h> #include <stdlib.h> int main(void) { int num; scanf("%d",&num); // & address printf("num=%d\n",num); system("pause"); return 0; } 10.試撰寫一程式,利用scanf() 函數輸入兩個整數,然後以printf() 函數列印出這兩個整數的乘積。 /* hw4_10.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int a, b; printf("請輸入兩個整數: "); scanf("%d %d", &a, &b); printf("%d*%d=%d\n", a, b, a*b); system("pause"); return 0; } 5.3 運算式 11.試撰寫一程式,可由鍵盤輸入攝氏溫度,程式的輸出為華氏溫度,其轉換公式如下:華氏溫度=(9/5)*攝氏溫度+32 /* hw5_11.c */ #include <stdio.h> #include <stdlib.h> int main(void) { double f, c; printf("請輸入攝氏溫度:"); scanf("%lf", &c); f=(9/5.0)*c+32; printf("攝氏%.2f度=華氏%.2f度\n", c, f); system("pause"); return 0; } #12 /* hw5_12.c */ #include <stdio.h> #include <stdlib.h> int main(void) { double f,c; printf("請輸入華氏溫度:"); scanf("%lf",&f); c=(f-32)*5/9; printf("華氏%.2f度=攝氏%.2f度\n",f,c); system("pause"); return 0; } #13 * hw5_13.c */ #include <stdio.h> #include <stdlib.h> int main(void) { double mile,km; printf("請輸入英哩數:"); scanf("%lf",&mile); km=1.6*mile; printf("%.2f英哩=%.2f公里\n",mile,km); system("pause"); return 0; } 6.2另外的選擇—if-else 敘述 9. 試撰寫一程式,讀入10個學生的成績,成績在0~59分為C,60~75分為B,76~100分為A,最後將得到A、B、C的人數印出。 /* hw6_9.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int score, i; int counta=0,countb=0,countc=0; for(i=1;i<=10;i++) { printf("請輸入成績:"); scanf("%d", &score); if(score>=76 && score<=100) //&& ->and counta++; else if(score>=60 && score<=75) countb++; else if(score>=0 && score<=59) countc++; } printf("\n得到A的有%d人\n", counta); printf("得到B的有%d人\n", countb); printf("得到C的有%d人\n", countc); system("pause"); return 0; } #12 /* hw6_12.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int score; printf("請輸入成績:"); scanf("%d",&score); if(score>=0 && score<60) printf("C級\n"); else if(score<80 && score>=60) printf("B級\n"); else if(score<=100 && score>=80) printf("A級\n"); system("pause"); return 0; } 6.4更好用的多重選擇─switch敘述 20.試由鍵盤輸入數值1~4,並加以判斷輸入值是否在1~4之間,如果超出此範圍,則印出 "輸入錯誤",否則利用switch印出相對應的季節: 1:春天 2:夏天 3:秋天 4:冬天 /* hw6_20.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int season; printf("請輸入數值(1~4):"); scanf("%d", &season); switch(season) { case 1: printf("春天\n"); break; case 2: printf("夏天\n"); break; case 3: printf("秋天\n"); break; case 4: printf("冬天\n"); break; default: printf("輸入錯誤\n"); } system("pause"); return 0; } # 23 /* hw6_23.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int day; printf("請輸入~7之間的整數:"); scanf("%d",&day); switch(day) { case 1: case 2: case 3: case 4: case 5: printf("今天要上班\n"); break; case 6: case 7: printf("今天休息\n"); break; default: printf("輸入錯誤\n"); } system("pause"); return 0; } 7.2 使用for迴圈 3. 試利用for迴圈計算 = sum的總和,其中n為奇數,可由使用者自行輸入。 /* hw7_3.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i, n, sum=0; printf("請輸入n值,n為奇數:"); scanf("%d", &n); for(i=1; i<=n; i+=2) sum+=i; printf("1+3+5+...+%d=%d\n", n, sum); system("pause"); return 0; } #7 /* hw7_7.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i; printf("1~100中,可以被6整除的數有"); for(i=1;i<=100;i++) if(i%6==0) printf("%d ",i); system("pause"); return 0; } 8.試撰寫一程式,利用for迴圈印出從1到100之間,所有可以被7整除,又可以被3整除的數值。 /* hw7_8.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i; printf("1~100中,可以被7整除,又可以被3整除的數有 "); for(i=1; i<=100; i++) { if ( (i%7 = = 0) && (i%3==0)) //% -> MOD, &&-> AND printf("%d ",i); } system("pause"); return 0; } 9. 試撰寫一程式,利用for迴圈計算 的值。 /* hw7_9.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i, j, sum=0; for(i=1, j=2; i<=49; i+=2, j+=2) sum+=i*i - j*j; // sum+=i*i - j*j printf("1^2-2^2+3^2-4^2+...+49^2-50^2=%d\n", sum); system("pause"); return 0; } #10 /* hw7_10.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i,n; double sum=0.0; printf("請輸入一個正整數:"); scanf("%d",&n); for(i=1;i<=n;i++) sum+=1.0/i; printf("1+1/2+1/3+...+1/%d=%f\n",n,sum); system("pause"); return 0; } 7.7巢狀迴圈 26.試利用巢狀迴圈撰寫出一個能產生如下圖結果的程式: 1 12 123 1234 12345 /* hw7_26.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i,j; for(i=1; i<=5; i++) { for (j=1; j<=i; j++) printf("%d", j); printf("\n"); } system("pause"); return 0; } 28. 試撰寫一程式,利用 do while /while/for 迴圈完成九九乘法表。 /* hw7_28.c */ #include <stdio.h> #include <stdlib.h> int main(void) { int i=1,j=1; do { do { printf("%d*%d=%2d ",i,j,i*j); j++; } while(j<=9); printf("\n"); i++; j=1; } while(i<=9); system("pause"); return 0; } /* prog7_10, 巢狀while迴圈求9*9乘法表 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i=1, j=1; /* 設定迴圈控制變數的初值 */ while (i<=9) /* 外層迴圈 */ { while (j<=9) /* 內層迴圈 */ { printf("%d*%d=%2d ", i, j, i*j); j++; } printf("\n"); i++; j=1; } system("pause"); return 0; } /* prog7_9, 巢狀for迴圈印出九九乘法表 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i, j; for (i=1; i<=9; i++) /* 外層迴圈 */ { for (j=1; j<=9; j++) /* 內層迴圈 */ printf("%d*%d=%2d ", i, j, i*j); printf("\n"); } system("pause"); return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服