《單片機C語言作業及上機習題及答案》由會員分享,可在線閱讀,更多相關《單片機C語言作業及上機習題及答案(37頁珍藏版)》請在人人文庫網上搜索。
1、第一次課熟悉winTC編譯環境、熟悉C語言程序結構1.使用C 語言編譯環境,輸入下面的源程序。將你的程序命名為hello.c,然后編譯運行它。/* program writes the words Hello, world to the screen*File : Hello.c* By : NJCIT* Date : 07-03-09*/#include main()printf(Hello, worldn);return(0);2.main() /*求兩數之和*/int a,b,sum; /* 這是變量定義*/a=123;b=456;sum=a+b;printf(“sum is %dn”,。
2、sum); 回答下列問題:1. C語言中的標識由 字母 、 數字 和 下劃線 組成,以 字母 和 下劃線 開頭,不可使用 關鍵字 。2. C語言源程序從 main 開始執行?每個C語言程序必須有一個且只能有一個主函數,主函數的名字為 main 。 C程序的函數由 函數頭 和 函數體 兩部分構成,函數頭包括 函數屬性 、 函數類型 、 函數名 和 形參表 ;函數體由一系列的語句組成,語句由 分 號結束,函數休包含在一對 花 括號中。程序中的注釋內容是用符號 /* */ 界定。2. 計算機上實現C語言程序要經過 編輯 (產生*. C 文件)、 編譯 (產生*. OBJ 文件)、 鏈接 (產生*. 。
3、EXE 文件)和 調試 四個階段。第二次課熟悉printf()函數、常見轉義字符及各種數據類型的輸出格式1.#include main()printf(n12345678901234567890);printf(nnnn a few new lines );printf(nttand nsome ntabs);printf(nand a beep just to be heard.an);printf(nthi);printf(s wor);printf(ks toon);return (0);(1) printf()函數的功能是什么?按指定的格式在屏幕上顯示指定的內容(2) 在 printf。
4、()函數中n的起什么作用?回車換行(3) 在 printf()函數中t的起什么作用?水平跳格(4) 在 printf()函數中a的起什么作用?蜂嗚器響2. main() int a=5,b=7,c=-1;float x=67.8564,y=-789.124;char c=A;long n=1234567;unsigned u=65535;printf(“%d%dn”,a,b);/*57*/printf(“%3d%3dn”,a,b); /* 5 7*/printf(“%f,%fn”,x,y); /*67.856400,-789.124000*/printf(“%-10f%-10fn”,x,y);。
5、 /*67.856400 ,-789.124000*/printf(“%8.2f,%8.2f,%.4f,%.4f,%3f,%3fn”,x,y,x,y,x,y);/* 67.85, -789.12,67.8564,-789.1240,67.856400,-789.124000*/printf(“%e,%10.2en”,x,y);/*6.78564e+01, -7.9e+02*/printf(“%c,%d,%o,%xn”,c,c,c,c);/*A,65,101,41*/printf(“%ld,%lo,%xn”,n,n,n);/*1234567,4553207,d687*/printf(“%u,%o。
6、,%x,%dn”,u,u,u,u);/*65535,177777,ffff,-1*/printf(“%s,%5.3sn”,”COMPUTER”,”COMPUTER”);/*COMPUTER, COM*/3. 假設下面的例子都是完整程序的一部分,那么他們每一個將會輸出什么?a. printf(Baa Baa Black Sheep.);b. printf(Have you any wool?n);c. printf(Begone!nO creature of lard!);d. printf(What?nNolnBonzo?n);e. int num;num = 2;f. printf(%d +。
7、 %d = %d, num, num, num + num);4.加載,編譯并運行下面的程序。顯示輸出界面然后回答下面的問題。#include main()char ch;int x;float y;double z;ch = A;printf(ch = %c and its ascii value is %d. What is ASCII I wonder?n,ch, ch);x = 10;printf(x = %dn, x);y = 3.1415926;printf(y = %fn, y);z = 4.75E5;printf(z = %lfn, z);return(0);1. 字符的A的A。
8、SCII碼是多少?652. “%f”默認情況下小數點后面有幾位數字?6位3. 程序中的字符被指定的值為A,為什么不是“A”?A為字常量,A為字符串常量4. 改變程序使它以10個字符位寬度和2位小數的形式輸出浮點型數據。%f改為%10.2f5. 改變程序使它以6個字符位寬度輸出整型數,左對齊。%d改為-6d%3.問答題(1)C語言的基本數據類型有幾種,分別是什么,并指出各種數據類型的關鍵字?(2)描述C語言中標識符的組成(3)常量和變量的區別是什么?(4)在程序中如何使用變量?(5)從下面列出標識符中選出哪些可以用作合法的C用戶定義標識符,哪些不能使用。為什么?(1) a3_b3 (2)void。
9、 (3) _123 (4)123_ (5) IF (6) INT (7) For (8) printf (9) WORD (10) define (11) _abc (12) sizeof (13) answer (14) to (15)signed (16) Case (17)_if (18) extern (19) putchar (20) _double(6)請選出正確的數值和字符常量,說明類型;對于不正確的數,說明原因(1) 0.0 (2) 5L (3) o13 (4) 0Xff (5) oxaa (6) 018 (7) 9861 (8) 011 (9) 3.987E-2 (10) .。
10、987 (11) 0xab (12) 50. (13) 8.9e1.2 (14) 1e1 (15)0xFF00 (16) 0.825e2 (17)473 (18) OX4 (19) “c” (20)t (21) ” (22)0 (23)0 (24) A4選擇題(1)合法的字符常量是 。A) t B) “A” C) a D)x32(2) 合法的字符常量是 。A) 084 B) 84 C) ab D)x43(3)是C語言提供的合法的數據類型關鍵字。A) Float B) signed C) integer D)Char(4)在以下各組標識符中,合法的標識符是。A) A)B01 B)table_1 。
11、C) 0_t D) k%B) A)Fast_ B) void C)pbl D)C) A)xy_ B)longdouble C)*p D)CHARD) A) sj B)Int C)_xy D)w_y23(5)屬于合法的C語言長整型常量的是 。A)5876273 B)0L C)2E10 D)(long)5876273(6)下面選項中,不是合法整型常量的是A)160 B)0xcdg C)01 D)0x48a第三次課熟悉scanf()函數的使用:1. 用下面的scanf()函數輸入數據,使a=3,b=7,x=8.5,y=71.82,c1=A,c2=a;main()int a,b;float x,y;c。
12、har c1,c2;scanf(“a=%d b=%d”,&a,&b);scanf(“ x=%f y=%e”,&x,&y);scanf(“ c1=%c c2=%c”,&c1,&c2);printf(“a=%d b=%d”,a,b);printf(“ x=%f y=%e”,x,y);printf(“ c1=%c c2=%c”,c1,c2);2.加載,編譯并運行下面的程序然后回答下面的問題。#include main()int user_age;char user_name51;/* Get the users name */printf(Enter your name :);scanf(%s, u。
13、ser_name);/* Get the users age */printf(Enter your age in years :);scanf(%d, &user_age);/* Print out their name and age in days */printf(Gday %s, you are %d days oldn, user_name, user_age*365);return(0);1. 存儲用戶名的變量名是什么?2. 改寫這個程序使他可以用一個單獨的變量以天的形式存儲用戶的年齡。3.使用 scanf() 讀取多行輸入#include main()int user_age;。
14、char user_name51;/* Get the users name and age*/printf(Enter your name followed by your age in years (eg fred 23) :);scanf(%s %d, user_name, &user_age);/* Print out their name and age in days */printf(Gday %s, you are %d days oldn, user_name, user_age*365);return(0);1. 當你在回答問題時顛倒了年齡和姓名會出現什么情況?2. 當你在。
15、代碼中省去了&時會出現什么情況?4編程題(1) 已知a,b均是整型變量,寫出將a,b兩個變量中的值互換的程序來。(2)若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=a,c2=b。想得到以下的輸出格式和結果,請寫出程序(包括定義變量類型和設計輸出)。a= 3 b= 4 c= 5x=1.200000,y=2.400000,z=-3.600000x+y= 3.60 y+z=-1.20 z+x=-2.40u= 51274 n= 128765c1=a or 97(ASCII)c2=b or 98(ASCII)(3)輸入一個華氏溫度,要求輸出攝氏溫。
16、度,公式為:,取兩位小數。5.請判斷以下表達式是否正確,若正確,寫出表達式的值;若不正確,寫出出錯原因。各變量的類型說明如下:int i=8, j=3, k, a, b;unsigned long w=5, u;double x=1.42 , y=5.2 , t ;(1) k=i+ 8 (2) (int)x+0.4 1.4 (3) w+=-2 3 (4) y+=x+ ? (5) i/=j+12 0(6) k=-i 7 (7) f=3/2*(t=30.0-10.0) 30.0 (8) k=(a=2,b=3,a+b) 5(9) a+=a-=(b=4)*(a=3) -18 (10) a=2*a=3?。
17、(11) u=65535, j=-1,u=u+j (12) +(i+j) ? (13) 2%(-3) 2 (14) -2%(-3) 26. 求以下表達式的值,假設所有變量都為整型。(1) (a=b=4,a+1,b+1,a+b)(2) (a=2,b=5,ab?a+:b+,a+b)(3) (x=8,x%=x+5)(4) 30 % 6 / 27.寫出下面各邏輯表達式的值。設:a=3,b=4,c=5.(1)a+bc&b=c(2)a |b+c&b-c(3)!(ab) & !c | 1 (4)!(x=a)&(y=b)&0(5)!(a+b) + c 1 & b + c/2 第四次課1. if語句應用(1)加。
18、載,編譯并運行下面的程序,先使用一個正整數,然后再使用一個負數。有什么不同?#include main()int x;printf(Enter a number between -10 and +10 : );scanf(%d, &x);if (x 0) printf(nYour number is positiven);return(0);(2)使用 if語句避免除0#include main()float x, y;printf(Enter a number to be inverted : );scanf(%f, &x);y = 1/x;printf(The inverse of %f 。
19、equals %fn, x, y);return(0);1. 當你輸入數字0時結果為多少?2. 增加一個if語句使它只有當if x不等于0時進行計算。3. 現在你輸入0會出現什么情況?4. 改進程序使之可以當輸入數字0時打印出警告信息。(3)編寫 if語句寫一個程序用來計算電阻上的功率。功率大小等于電阻上的電壓值乘以流過電阻的電流值。完成計算后,使用一個if 語句判斷功率是否低于0.5 瓦特。如果低于0.5 瓦特則輸出“Okey to use half watt resistor”,否則輸出“haff watt registor willnot be okey”。下面是程序的開始部分,請添加完。
20、成if語句。#include main()float power, voltage, current;printf(Enter the voltage : );scanf(%f, &voltage);printf(Enter the current : );scanf(%f, ¤t);/* Calculation here */* if check here */* else here */return(0);如果功率小于0.5 瓦特,屏幕輸出應該如下所示:Enter the voltage : 5Enter the current : 0.002okay to use half 。
21、watt resistor.如果功率大于0.5瓦特:Enter the voltage : 5Enter the current : 200half watt resisitor will not be okay.2. 編程練習:(1) 有三個整數a,b,c,由鍵盤輸入,輸出其中最大的數。(2) 編程輸入整數a和b,若 大于100,則輸出 百位以上的數字,否則輸出兩數之和。(3) 有一函數:寫一個程序,輸入x的值,輸出y值。(4)寫一段程序計算兩個并聯電阻的阻值,使你的程序檢查是有短路的(0歐姆)將導致0作為被除數,使用一個if語句或者三目運算避免這種情況(使用三目運算計算及檢查是否除0)。提。
22、示: Rt = 1 / (1/R1 + 1/R2);你的輸出應該如下所示:Enter the value of resistor 1 : 45Enter the value of resistor 2 : 5645.0 ohms in parallel with 56.0 ohms gives 24.95 ohms假如有一個電阻阻值為0的話,結果將如下所示:Enter the value of resistor 1 : 0Enter the value of resistor 2 : 450.0 ohms in parallel with 45.0 ohms gives 0.00 ohms第5。
23、次課1. switch語句應用(1)加載,編譯并運行下面的程序,并回答問題。#include main()int num1, num2, ans;char arithOp;ans=0;printf(Enter number 1 );scanf(%d, &num1);printf(Enter number 2 );scanf(%d, &num2);printf(Enter an operator (+, - or *) );fflush(stdin);scanf(%c, &arithOp);switch (arithOp) case +:ans = num1 + num2; break;case。
24、 -:ans = num1 - num2; break;case *:ans = num1 * num2; break;printf(n%d %c %d = %d, num1, arithOp, num2, ans);fflush(stdin);getchar();return(0);1. 嘗試輸入一個不匹配case語句的操作符,會發生什么?2. 將程序保存為switch-02.c并為“ans”添加乘(*)和除(/)的算法。3. 添加一個default語句來處理輸入的運算符不符合case情況。4. 添加程序處理除數為零的情況。2. 加載,編譯并運行下面的程序,將文件保存為switch-01.c。
25、(2)加載,編譯并運行下面的程序。#include main()int choice;printf(How many stars (1 to 10) do you want? );scanf(%d, &choice);switch (choice) case 10:printf(*);case 9:printf(*);break;case 8:printf(*);case 7:printf(*);case 6:printf(*);case 5:printf(*);break;case 4:printf(*);case 3:printf(*);case 2:printf(*);case 1:pr。
26、intf(*);break;default:printf(The number you asked for is out of rangen);return(0);1. 打印 9顆星和8顆星是不同的。描述這兩種方法。2. 編輯這段程序,使之工作在沒有break語句的情況下。(3)任務1:完成下面的程序,使用if/else語句,判斷輸入的數是正數還是負數任務2:完成下面的程序,使用switch語句,判斷輸入的數是正數還是負數,并且要處理輸入的數越界的情況。#include main()int num;printf(Program indicates whether number is +ve o。
27、r -ven);printf(Enter a number between -5 and +5 :);scanf(%d, &num);/* 添加語句 */return(0);(4)本程序是從鍵盤輸入一個10進制數,根據用戶要求輸出這個數對應的16進制、8進制或者10進制數。例:若用戶輸入“H”,則輸出這個數的16進制數。任務1:添加if語句完成程序功能任務2:添加switch語句完成程序功能,注意對輸入無效數制的處理。比如輸入:X時,作何處理。#include main()int num;char choice;printf(Program prints octal or hex equiva。
28、lent of numbersenteredn);printf(Enter an integer : );scanf(%d, &num);printf(Do you want Decimal, Octal or Hex (H, D, O) : );fflush(stdin);choice = getchar();/* 添加程序 */return(0);(5) 給出一百分制成績,要求輸出成績等級A,B,C,D,E。90分以上為A,8089分為B,7079分為C,6069分為D,60分以下為E。(6) 從鍵盤輸入三個數,判斷這三個數是否能構成三角形,如果是,輸出“the three numbers。
29、 could be the sides of a triangle”,如果不是輸出“the three numbers couldt the sides of a triangle”;并判斷這個三角形是不是直角三角形,如果是,輸出“the numbers could be the sides of a right angle triangle”,如果不是,輸出“the numbers couldt the sides of a right angle triangle”。第6次課 循環1. 寫一個程序讓用戶輸入一個112之間的數,程序將會輸出這個數的乘法表。使用一個循環進行計算并輸出一行,屏幕。
30、輸出應該如下所示:Enter a number between 1 and 12: 55 times multiplication table1 x 5 = 52 x 5 = 103 x 5 = 154 x 5 = 205 x 5 = 256 x 5 = 307 x 5 = 358 x 5 = 409 x 5 = 4510 x 5 = 5011 x 5 = 5512 x 5 = 602. 添加循環語句,計算并輸出120之間的偶數的和。#include main()int i, result;/* 添加語句 */return(0);3. Break 語句的使用為程序添加語句,使得輸入的數據在11。
31、0之間時,退出循環,否則輸出“the number is out of range”#include main()int num;while (1) printf(Enter a number between 1 and 10 :);scanf(%d, &num);/* if ( main()int i, num;char isPrime;printf(Program checks to see if a number is primen );printf(Enter the integer : ); /* A */scanf(%d, &num);isPrime = 1; /* B */for。
32、 (i = num - 1; i 1; i-) if (num % i = 0)isPrime = 0;printf(Number %d is divisible by %d and is not primen, num,i);break;if (isPrime)printf(Number %d is PRIMEn, num); /* C */return(0);5.求愛因斯坦數學題。有一條長階,若每步跨2階,則最后剩余1階;若每步跨3階,則最后剩2階;若每步跨5階,則最后剩4階;若每步跨6階,則最后剩5階;若每步跨7步,最后正好一階不剩。6.計算斐波那契分數序列前n項之和(n是某個常數).(。
33、 2/1,3/2,5/3,8/5,13/8,21/13,. 前一項的分子作為后一項的分母。前一項的分子、分母之和作為后一項的分子。)。7.問答題(1) 下面循環錯在哪里?for (i = 1; i 5; i+) x /=2;(2)下面循環錯在哪里?for (i = 10; i 2; i+) x *=2;(3) 這個for循環的輸出結果是多少?for (i = 3; i -2; i-) printf(%d , i);(4) 這個while循環的輸出結果是多少?i = 0;while (i ; i-) printf(My name is );綜合練習一:打印ASC碼表在這個項目中,我們想去打印所有。
34、的ASCII碼字符及相關聯的十進制和二進制值。關于ASCIIASCII表示美國信息交換標準碼,正如它的名字所暗示的, 它是由美國的一個代碼指定的數字每個信息存儲或傳輸計算機來完成。這里是關于ASCII碼的重點:l 每個鍵盤字符都可以映射到數字從32127;l 數字從0 到31用于特殊字符,如制表符、鈴、換行符等;l 擴展ASCII范圍從128 至255,含有專門128 個字符,如邊框線等;l 擴展的ASCII字符是非標準化,可能會從一個操作系統類型到另一個(如PC和MAC與Linux);l 標準的 ASCII是標準化的,同樣都可以跨越不同的操作系統類型。第一步寫出程序的基本框架任何C程序都具有。
35、以下的基本框架/* Title : Print Standard and Extended ASCII chart* Source : print_ascii.c* Author : Some Student* Version : 1.0*/#include main()第二步添加一個具有一個變量的循環語句我們想去打印所有的標準和擴展ASCII碼的值,這就意味著循環從0至255/* Title : Print Standard and Extended ASCII chart* Source : print_ascii.c* Author : NJCIT* Version : 1.0*/#in。
36、clude main()int i;for (i=1; imain()int i;for (i=1; imain()int i;printf(DEC HEX ASCIIn);for (i=1; imain()int i;printf(| DEC HEX ASC | DEC HEX ASC | DEC HEX ASC );printf(| DEC HEX ASC | DEC HEX ASC |n);for (i=1; imain()int i, a;for (a=0; avoid pretty_line()int a;for (a=0; a/* This function prints a li。
37、ne accros the screen.* The line can be one of the following types:* line_type = 0 - The top line* line_type = 1 - The middle line* line_type = 2 - the bottom line*/void pretty_line(int line_type)char left_char, right_char, mid_char;char ext_char;int a;/* Set left, right and mid characters depending 。
38、on line type */switch (line_type) case 0: /* Top line */left_char = 213;right_char = 184;mid_char = 209;break;case 1: /* Middle line */left_char = 198;right_char = 181;mid_char = 216;break;case 2: /* Bottom line */left_char = 212;right_char = 190;mid_char = 207;break;default: /* Invalid argument pas。
39、sed to us. */left_char = -;right_char = -;mid_char = +;for (a=0; a71; a+) ext_char = (a=0) ? left_char : (a=70) ? right_char : (a % 14= 0) ? mid_char : 205;printf(%c, ext_char);printf(n);left_char = 198;right_char = 181;mid_char = 216; break;case 2: /* Bottom line */left_char = 212;right_char = 190;。
40、mid_char = 207; break;default: /* Invalid argument passed to us. */left_char = -;right_char = -;mid_char = +;for (a=0; a71; a+) ext_char= (a=0)? left_char:(a=70)?right_char:(a % 14= 0) ? mid_char : 205;printf(%c, ext_char);printf(n);return;main()int i;/* Print the top border line */pretty_line(0);/*。
41、 Print the heading */printf(| DEC HEX ASC | DEC HEX ASC | DEC HEX ASC );printf(| DEC HEX ASC | DEC HEX ASC |n);/* Print the middle border line */pretty_line(1);/* Print the ASCII table */for (i=1; i=51; i+) if (i 32)printf(| %3d %3x %3s , i, i, n/a);elseprintf(| %3d %3x %3c , i, i, i);printf(| %3d %。
42、3x %3c | %3d %3x %3c , i+51, i+51, i+51, i+102, i+102, i+102);printf(| %3d %3x %3c | %3d %3x %3c |n, i+153, i+153, i+153, i+204, i+204, i+204);/* Print the bottom border line */pretty_line(2);return(0);最終結果第8次課1.編程題(1)將輸入數組an中的n個元素,再輸入另一個數x,查看a中是否有值為x的元素,若有,則輸出其下標,若沒有,則輸出-1。#include Stdio.h#define N 10int main。