1. 上機測試題
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("Hello World\r");
printf("Hi,World");
printf("\b\b\b\b\b everyone\n");
system("pause");
return 0;
}
2. 上機測試題
#include<stdio.h>
#include<stdlib.h>
int main(){
printf(" /\\\n");
printf(" / \\\n");
printf(" / \\\n");
printf(" / \\\n");
printf("+---------+\n");
system("pause");
return 0;
}
3. 上機測試題
#include<stdio.h>
#include<stdlib.h>
int main(){
short int si=-1;
int i=2147483646;
printf("%hd\n",si);
printf("%hu\n",si);
printf("%d\n",i+1);
printf("%d\n",i+2);
system("pause");
return 0 ;
}
4. 上機測試題
#include<stdio.h>
#include<stdlib.h>
int main(){
float fnum=123.456;
double dnum=123.456;
printf("%f\n",fnum);
printf("%f\n",dnum);
system("pause");
return 0;
}
5. 觀察 跳脫字元)試寫一程式,利用設定字元變數 ch 為 ASCII 碼的方式
讓電腦發出一個警告音(警告音的 ASCII 碼為 7)
#include <stdio.h>
#include <stdlib.h>
int main (){
char deep = 7;
printf("\x07");
system("pause");
return 0;
}
6. (溢位 overflow)請參閱下面的程式碼,然後回答接續的問題:
a. 試說明執行此程式的結果,為什麼是 14464,而不是 80000 這個數字?
b. 如果想讓本題執行結果恰好為 80000,應如何修改程式碼?
#include<stdio.h>
#include<stdlib.h>
int main(void){
unsigned int num=80000;
printf("%d\n",num);
printf("a.因為溢值所以顯示14464");
printf("\n");
system("pause");
return 0;
}
7. 請參閱下面的程式碼,然後回答接續的問題
a. 試執行此程式碼,您會得到什麼結果?
b. 於數學上,30000.1+0.0004=30000.1004,試說明執行此程式碼後,
為什麼得不到這個結果?
c. 如果想讓本題的執行結果恰好為 30000.1004,應如何改進?試撰寫
一個完整的程式碼來改進之。
#include <stdio.h>
#include <stdlib.h>
int main (void){
float num1=30000.1;
float num2=0.0004;
printf("%f\n",num1+num2);
printf("a.30000.100009");
printf("b.因為30000.1+0.0004的範圍超出float的值,所以無法正確計算");
printf("\n");
system("pause");
return 0;
}
8.假設浮點數變數 num1 與 num2 的值分別為 123.39f 與 3.8e5f,試撰
寫一程式,將這兩個變數值轉換成整數。
#include
#include
int main (){
float num1=123.39f, num2=3.8e5f;
printf ("%d \n%d \n", num1, num2);
system("pause");
return 0;
}
9.請參閱下面的程式碼,然後回答接續的問題
a. 試解釋輸出結果為何是 0?
b. 試修改程式碼,利用型態轉換的方式,使得輸出結果為
0.625000。
#include <stdio.h>
#include <stdlib.h>
int main (void){
float num1=5, num2=8;
printf("%f \n", num1/num2);
printf("a.因為5/8是小數,而%d是輸出整數,所以最後結果顯示0");
printf("\n");
system("pause");
return 0;
}
10.將常數100以十進位、八進位和十六進位輸出
11.若有一short int 的變數si , 其值為32776 , 當以
%hd 印出時結果為何? 試撰寫一程式測試之
#include <stdio.h>
#include <stdlib.h>
int main (){
short int si;
si = 32776;
printf("si=%hd \n",si);
system("pause");
return 0;
}
12.若有一short int 的變數 si, 其值為-6 , 當以%hu
印出時結果為何? 試撰寫一程式測試之。
13.圓的直徑、圓周長和面積)撰寫一個程讀取圓
的半徑(整數),然後印出圓的直徑、圓周長和面積。使用 3.14159
代表圓周率。此外請將所有的計算放到 printf 敍述式裡執行,並
使用轉換指定詞 %f。
#include <stdio.h>
#include <stdlib.h>
int main (){
int radius;
float pi=3.14159;
printf("請輸入圓的半徑:");
scanf("%d",&radius);
printf("直徑=%d\n",2*radius);
printf("周長=%f\n",2*pi*radius);
printf("面積=%f\n",radius*radius*pi);
printf("\n");
system("pause");
return 0;
}
14.字元的整數值)在 C 語言的內部,是使用整數
來表示每個字元的。這一組字元以及電腦用來對應這組字元的整
數,就稱為電腦字元集(character set)。你可以印出代表大寫 A 的
整數。例如,執行以下敍述式 printf(“%d”, ‘A’); 寫一個 C 程式,
印出代表某些大寫字母、小寫字母、數字和特殊符號的整數。你
至少要能印出代表下列字元的整數:A B C a b c 0 1 2 3 * + / 以
及空白字元
#include<stdio.h>
#include<stdlib.h>
int main() {
printf("Hello World ");
printf("哈囉! 您好~");
printf("A's integer equivalent is %d\n",'A');
printf("B's integer equivalent is %d\n",'B');
printf("C's integer equivalent is %d\n",'C');
printf("D's integer equivalent is %d\n",'D');
printf("E's integer equivalent is %d\n",'E');
printf("F's integer equivalent is %d\n",'F');
printf("G's integer equivalent is %d\n",'G');
printf("H's integer equivalent is %d\n",'H');
printf("I's integer equivalent is %d\n",'I');
printf("J's integer equivalent is %d\n",'J');
printf("K's integer equivalent is %d\n",'K');
printf("L's integer equivalent is %d\n",'L');
printf("M's integer equivalent is %d\n",'M');
printf("N's integer equivalent is %d\n",'N');
printf("O's integer equivalent is %d\n",'O');
printf("P's integer equivalent is %d\n",'P');
printf("Q's integer equivalent is %d\n",'Q');
printf("R's integer equivalent is %d\n",'R');
printf("S's integer equivalent is %d\n",'S');
printf("T's integer equivalent is %d\n",'T');
printf("U's integer equivalent is %d\n",'U');
printf("V's integer equivalent is %d\n",'V');
printf("W's integer equivalent is %d\n",'W');
printf("X's integer equivalent is %d\n",'X');
printf("Y's integer equivalent is %d\n",'Y');
printf("Z's integer equivalent is %d\n",'Z');
printf("['s integer equivalent is %d\n",'[');
printf("]'s integer equivalent is %d\n",']');
printf("^'s integer equivalent is %d\n",'^');
printf("_'s integer equivalent is %d\n",'_');
printf("a's integer equivalent is %d\n",'a');
printf("b's integer equivalent is %d\n",'b');
printf("c's integer equivalent is %d\n",'c');
printf("d's integer equivalent is %d\n",'d');
printf("e's integer equivalent is %d\n",'e');
printf("f's integer equivalent is %d\n",'f');
printf("g's integer equivalent is %d\n",'g');
printf("h's integer equivalent is %d\n",'h');
printf("i's integer equivalent is %d\n",'i');
printf("j's integer equivalent is %d\n",'j');
printf("k's integer equivalent is %d\n",'k');
printf("l's integer equivalent is %d\n",'l');
printf("m's integer equivalent is %d\n",'m');
printf("n's integer equivalent is %d\n",'n');
printf("o's integer equivalent is %d\n",'o');
printf("p's integer equivalent is %d\n",'p');
printf("q's integer equivalent is %d\n",'q');
printf("r's integer equivalent is %d\n",'r');
printf("s's integer equivalent is %d\n",'s');
printf("t's integer equivalent is %d\n",'t');
printf("u's integer equivalent is %d\n",'u');
printf("v's integer equivalent is %d\n",'v');
printf("w's integer equivalent is %d\n",'w');
printf("x's integer equivalent is %d\n",'x');
printf("y's integer equivalent is %d\n",'y');
printf("z's integer equivalent is %d\n",'z');
printf("{'s integer equivalent is %d\n",'{');
printf("|'s integer equivalent is %d\n",'|');
printf("}'s integer equivalent is %d\n",'}');
printf("~'s integer equivalent is %d\n",'~');
printf("!'s integer equivalent is %d\n",'!');
printf("#'s integer equivalent is %d\n",'#');
printf("$'s integer equivalent is %d\n",'$');
printf("%'s integer equivalent is %d\n",'%');
printf("&'s integer equivalent is %d\n",'&');
printf("('s integer equivalent is %d\n",'(');
printf(")'s integer equivalent is %d\n",')');
printf("*'s integer equivalent is %d\n",'*');
printf("+'s integer equivalent is %d\n",'+');
printf(",'s integer equivalent is %d\n",',');
printf("-'s integer equivalent is %d\n",'-');
printf(".'s integer equivalent is %d\n",'.');
printf("/'s integer equivalent is %d\n",'/');
printf("0's integer equivalent is %d\n",'0');
printf("1's integer equivalent is %d\n",'1');
printf("2's integer equivalent is %d\n",'2');
printf("3's integer equivalent is %d\n",'3');
printf("4's integer equivalent is %d\n",'4');
printf("5's integer equivalent is %d\n",'5');
printf("6's integer equivalent is %d\n",'6');
printf("7's integer equivalent is %d\n",'7');
printf("8's integer equivalent is %d\n",'8');
printf("9's integer equivalent is %d\n",'9');
printf(":'s integer equivalent is %d\n",':');
printf(";'s integer equivalent is %d\n",';');
printf("<'s integer equivalent is %d\n",'<');
printf(">'s integer equivalent is %d\n",'>');
printf("?'s integer equivalent is %d\n",'?');
printf("@'s integer equivalent is %d\n",'@');
printf("The blank character's intege\nr equivalent is %d",' ');
system("pause"); \n
return 0;
}
15. 分開整數的每個數字)撰寫一個程式,輸入一個五
位數的數字,將這個數字分成個別的數字,然後分別印出每個數
字,數字中間必須相隔 3 個空格。【提示:運用整數除法和模數
除法的組合。】例如,若輸入 45697,則程式必須印出:4 5 6
8 7
#include <stdio.h>
#include <stdlib.h>
int main (){
int num1, num2, num3, num4, num5;
int numin;
printf("輸入一個五位數的數字:");
scanf("%d",&numin);
num1 = numin/10000;
numin = numin%10000;
num2 = numin/1000;
numin = numin%1000;
num3 = numin/100;
numin = numin%100;
num4 = numin/10;
numin = numin%10;
num5 = numin/1;
printf(" %d %d %d %d %d\n",num1,num2,num3,num4,num5);
system("pause");
return 0;
}
16.平方和立方的表格)運用跳脫字元的技術,撰寫一
個能夠計算從 0 到 10 的平方數和立方數的程式,並用定位點(tab)
將這些數字依下列格式印出
#include <stdio.h>
#include <stdlib.h>
#define max 10
int main (){
printf("number\tsquare\tcube \n");
int i;
i = -1;
while (i++ < max)
printf("%d\t%d\t%d\n",i,i*i,i*i*i);
system("pause");
return 0;
}
17.共乘節約計算工具)建立一個應用程式,計算你每
程式設計 I 資訊工程學系 102
日開車的費用,然後估計你可以藉由共乘省下多少錢,同時也減
少碳排放量並紓解交通壅。這個應用程式應該輸入下列資訊,並
印出使用者每天開車上班的花費。
甲、每天行駛里程
乙、每公升汽油的價格
丙、每公升汽油行駛里程數
丁、每天停車費
戊、每天的過路費
沒有留言:
張貼留言