摘要: 復習題1、將下列十進制數轉換為二進制形式:a. 3b. 13c. 59d. 119答:a. 11b. 1101c. 111011d. 11101112、將下列二進制值轉換為十進制、八進制和十六進制形式:a. 00010101b. 01010101c. 01001100d. 10011101答:a. 21, 025, 0x15b. 85, 0125, 0x55c. 76, 0114, 0x4Cd. ... 閱讀全文
2015年11月21日 #
這是王爽老師的《匯編語言(第3版)》,經知友推薦確實是一本極好的書!
實驗4 [bx]和loop的使用
(1)、(2)assume cs:code
code segment
mov ax,0020h
mov ds,ax
mov bx,0
mov cx,64
s:mov [bx],bl ;這里必須是mov [bx],bl,而不能是mov [bx],bx,否則會出現類型不匹配
inc bl
loop s
mov ax,4c00h
int 21h
code ends
end
code segment
mov ax,0020h
mov ds,ax
mov bx,0
mov cx,64
s:mov [bx],bl ;這里必須是mov [bx],bl,而不能是mov [bx],bx,否則會出現類型不匹配
inc bl
loop s
mov ax,4c00h
int 21h
code ends
end
摘要: 復習題1、以下模板有什么錯誤?Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->structure { char itable; int&nb... 閱讀全文
摘要: 書中的一個例子,我也是想了半天了?。?!有點難度!??!Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/* 把多個文件的內容追加到一個文件中 */#include <stdio.h>#include &... 閱讀全文
摘要: 復習題1、哪一存儲類生成的變量對于包含他們的函數來說是局部變量?答:自動存儲類、寄存器存儲類和靜態空鏈接存儲類2、哪一存儲類的變量在包含它們的程序運行時期內一直存在?答:靜態空鏈接存儲類、靜態內部鏈接存儲類和靜態外部鏈接存儲類3、哪一存儲類的變量可以在多個文件中使用?哪一存儲類的變量只限于在一個文件中使用?答:靜態外部鏈接存儲類和靜態內部鏈接存儲類4、代碼塊作用域變量具有哪種鏈接?答:空鏈接5、關... 閱讀全文
摘要: 今天學到了一個新知識——選擇排序算法核心思想:(查找和放置)選擇剩余最大值的一個辦法就是比較剩余數組的第一和第二個元素。如果第二個元素大,就交換這兩個數據?,F在比較第一個和第三個元素。如果第三個大,就交換這兩個數據。每次交換都把大的元素移到上面。繼續這種方法,直到比較第一個和最后一個元素。完成以后,最大的數就在剩余數組的第一個元素中。此時第一個元素已經排好了序,但是數組中的... 閱讀全文
摘要: 這一章感覺好難啊!!!學習筆記:(關于指針和多維數組)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// 多維數組和指針#include <stdio.h>int main(void){  ... 閱讀全文
問題:編寫一個函數將一個整數轉換成二進制形式?(擴展請移步編程練習9)
1、實際參數和形式參量有何不同?
答:
形式參量(也被稱為形式參數)是一個變量,它在被調函數中進行定義。實際參數是在函數調用中出現的值,它被賦值給形式參量??梢园褜嶋H參數認為是在函數被調用時用來初始化形式參量的值。
2、寫出下面所描述的各個函數的ANSI函數頭。注意:只寫出函數頭即可,不需要實現。
a.donut()接受一個int類型的參數,然后輸出若干個0,輸出0的數目等于參數的值。
b.gear()接受兩個int類型的參數并返回int類型的值。
c.stuff_it()的參數包括一個double類型的值以及一個double類型變量的地址,功能是把第一個數值存放到指定的地址中。
答:
a.n_to_char()接受一個int類型的參數并返回一個char類型的值。
b.digits()接受的參數是一個double類型的數值和一個int類型的數值,返回值類型是int。
c.random()不接受參數,返回int類型的數值。
答:
答:
答:
答:(注意:下面這種寫法是錯誤的!?。。?/span>
有錯誤。num應該在salami()的參數列表中而不是在花括號之后聲明,而且應該是count++而不是num++。
8、編寫一個函數,使其返回3個整數參數中的最大值。
答:
Please choose one of the following:
1)copy files 2)move files
3)remove files 4)quit
Enter the number of your choice:
a.用一個函數實現菜單的顯示,且該菜單有4個用數字編號的選項并要求你選擇其中之一(輸出應該如題中所示)。
b.編寫一個函數,該函數接受兩個int類型的參數:一個上界和一個下界。在函數中,首先從輸入終端讀取一個整數,如果該整數不在上下界規定的范圍內,則函數重新顯示菜單(使用本題目a部分中的函數)以再次提醒用戶輸入新值。如果輸入數值在規定的范圍內,那么函數應該將數值返回給調用函數。
c.使用本題目a和b部分中的函數編寫一個最小的程序。最小的意思是該程序不需要實現菜單中所描述的功能;它只需要顯示這些選項并能獲取正確的響應即可。
答:(參考課后答案)
1、
#include <stdio.h>
void to_binary(unsigned long n);
int main(void)
{
unsigned long number;
printf("Enter an integer (q to quit): \n");
while(scanf("%lu", &number) == 1)
{
printf("Binary equivalent: ");
to_binary(number);
putchar('\n');
printf("Enter an integer (q to quit): \n");
}
printf("Done!\n");
return 0;
}
void to_binary(unsigned long n)
{
int r;
r = n % 2;
if(n >= 2)
to_binary(n / 2);
putchar('0' + r);
return;
}
復習題void to_binary(unsigned long n);
int main(void)
{
unsigned long number;
printf("Enter an integer (q to quit): \n");
while(scanf("%lu", &number) == 1)
{
printf("Binary equivalent: ");
to_binary(number);
putchar('\n');
printf("Enter an integer (q to quit): \n");
}
printf("Done!\n");
return 0;
}
void to_binary(unsigned long n)
{
int r;
r = n % 2;
if(n >= 2)
to_binary(n / 2);
putchar('0' + r);
return;
}
1、實際參數和形式參量有何不同?
答:
形式參量(也被稱為形式參數)是一個變量,它在被調函數中進行定義。實際參數是在函數調用中出現的值,它被賦值給形式參量??梢园褜嶋H參數認為是在函數被調用時用來初始化形式參量的值。
2、寫出下面所描述的各個函數的ANSI函數頭。注意:只寫出函數頭即可,不需要實現。
a.donut()接受一個int類型的參數,然后輸出若干個0,輸出0的數目等于參數的值。
b.gear()接受兩個int類型的參數并返回int類型的值。
c.stuff_it()的參數包括一個double類型的值以及一個double類型變量的地址,功能是把第一個數值存放到指定的地址中。
答:
a.void donut(int n)
b.int gear(int n, int m)
c.void stuff_it(double n, double * d)
3、只寫出下列函數的ANSI C函數頭,不需要實現函數。b.int gear(int n, int m)
c.void stuff_it(double n, double * d)
a.n_to_char()接受一個int類型的參數并返回一個char類型的值。
b.digits()接受的參數是一個double類型的數值和一個int類型的數值,返回值類型是int。
c.random()不接受參數,返回int類型的數值。
答:
a.char n_to_char(int n)
b.int digits(double n, int m)
c.int random(void)
4、設計一個實現兩整數相加并將結果返回的函數。b.int digits(double n, int m)
c.int random(void)
答:
int plus(int n, int m)
{
return n + m;
}
5、假如問題4中的函數實現兩個double類型的數值相加,那么應該如何修改原函數?{
return n + m;
}
答:
double plus(double n, double m)
{
return n + m;
}
6、設計函數alter(),其輸入參數是兩個int類型的變量x和y,功能是分別將這兩個變量的數值改為它們的和以及它們的差。{
return n + m;
}
答:(注意:下面這種寫法是錯誤的!?。。?/span>
void alter(int x, int y)
{
x = x + y;
y = x - y;
}
正確的寫法如下:{
x = x + y;
y = x - y;
}
void alter(int * u, int * v)
{
int temp;
temp = *u;
*u = *u + *v;
*v = temp - *v;
}
7、判斷下面的函數定義是否正確。{
int temp;
temp = *u;
*u = *u + *v;
*v = temp - *v;
}
void salami(num)
{
int num, count;
for(count = 1; count <= num; num++)
printf("O salami mio!\n");
}
答:{
int num, count;
for(count = 1; count <= num; num++)
printf("O salami mio!\n");
}
有錯誤。num應該在salami()的參數列表中而不是在花括號之后聲明,而且應該是count++而不是num++。
8、編寫一個函數,使其返回3個整數參數中的最大值。
答:
int max(int x, int y, int z)
{
int max;
if(x > y)
if(x > z)
max = x;
else
max = z;
else
if(y > z)
max = y;
else
max = z;
return max;
}
or (更簡潔一點){
int max;
if(x > y)
if(x > z)
max = x;
else
max = z;
else
if(y > z)
max = y;
else
max = z;
return max;
}
int max(int x, int y, int z)
{
int max = x;
if(y > max)
max = y;
if(z > max)
max = z;
return max;
}
9、給定下面的輸出:{
int max = x;
if(y > max)
max = y;
if(z > max)
max = z;
return max;
}
Please choose one of the following:
1)copy files 2)move files
3)remove files 4)quit
Enter the number of your choice:
a.用一個函數實現菜單的顯示,且該菜單有4個用數字編號的選項并要求你選擇其中之一(輸出應該如題中所示)。
b.編寫一個函數,該函數接受兩個int類型的參數:一個上界和一個下界。在函數中,首先從輸入終端讀取一個整數,如果該整數不在上下界規定的范圍內,則函數重新顯示菜單(使用本題目a部分中的函數)以再次提醒用戶輸入新值。如果輸入數值在規定的范圍內,那么函數應該將數值返回給調用函數。
c.使用本題目a和b部分中的函數編寫一個最小的程序。最小的意思是該程序不需要實現菜單中所描述的功能;它只需要顯示這些選項并能獲取正確的響應即可。
答:(參考課后答案)
#include <stdio.h>
void menu(void);
int get_input(int, int);
int main(void)
{
int res;
menu();
while((res = get_input(1, 4)) != 4)
printf("I like choice %d.\n", res);
printf("Bye!\n");
return 0;
}
void menu(void)
{
printf("Please choose one of the following: \n");
printf("1)copy files 2)move files\n");
printf("3)remove files 4)quit\n");
printf("Enter the number of your choice: \n");
}
int get_input(int min, int max)
{
int number;
scanf("%d", &number);
while(number < min || number > max)
{
printf("%d is not a valid choice; try again.\n", number);
menu();
scanf("%d", &number);
}
return number;
}
編程練習void menu(void);
int get_input(int, int);
int main(void)
{
int res;
menu();
while((res = get_input(1, 4)) != 4)
printf("I like choice %d.\n", res);
printf("Bye!\n");
return 0;
}
void menu(void)
{
printf("Please choose one of the following: \n");
printf("1)copy files 2)move files\n");
printf("3)remove files 4)quit\n");
printf("Enter the number of your choice: \n");
}
int get_input(int min, int max)
{
int number;
scanf("%d", &number);
while(number < min || number > max)
{
printf("%d is not a valid choice; try again.\n", number);
menu();
scanf("%d", &number);
}
return number;
}
1、
#include <stdio.h>
double min(double, double);
int main(void)
{
printf("One of the smaller of the two numbers is %.2f", min(23.34, 12.11));
return 0;
}
double min(double x, double y)
{
return x < y ? x : y;
}
2、double min(double, double);
int main(void)
{
printf("One of the smaller of the two numbers is %.2f", min(23.34, 12.11));
return 0;
}
double min(double x, double y)
{
return x < y ? x : y;
}
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
chline('$', 3, 5);
return 0;
}
void chline(char ch, int i, int j)
{
int index;
for(index = 1; index < i; index++)
putchar(' ');
for(index = 1; index <= j - i + 1; index++)
putchar(ch);
}
3、void chline(char ch, int i, int j);
int main(void)
{
chline('$', 3, 5);
return 0;
}
void chline(char ch, int i, int j)
{
int index;
for(index = 1; index < i; index++)
putchar(' ');
for(index = 1; index <= j - i + 1; index++)
putchar(ch);
}
#include <stdio.h>
void chline(char ch, int col, int row);
int main(void)
{
chline('$', 3, 5);
return 0;
}
void chline(char ch, int col, int row)
{
int i, j;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
putchar(ch);
putchar('\n');
}
}
4、void chline(char ch, int col, int row);
int main(void)
{
chline('$', 3, 5);
return 0;
}
void chline(char ch, int col, int row)
{
int i, j;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
putchar(ch);
putchar('\n');
}
}
#include <stdio.h>
double computer(double a, double b);
int main(void)
{
printf("%.2f和%.2f的諧均值是:%.3f\n", 0.3, 0.5, computer(0.3, 0.5));
return 0;
}
double computer(double a, double b)
{
double result;
result = 1 / ((1/a + 1/b) / 2);
return result;
}
5、double computer(double a, double b);
int main(void)
{
printf("%.2f和%.2f的諧均值是:%.3f\n", 0.3, 0.5, computer(0.3, 0.5));
return 0;
}
double computer(double a, double b)
{
double result;
result = 1 / ((1/a + 1/b) / 2);
return result;
}
#include <stdio.h>
void larger_of(double *, double *);
int main(void)
{
double x = 23.3;
double y = 34.4;
printf("Originally x = %.1f; y = %.1f\n", x, y);
larger_of(&x, &y);
printf("Now x = %.1f; y = %.1f\n", x, y);
return 0;
}
void larger_of(double * u, double * v)
{
double temp;
temp = *u > *v ? *u : *v;
*u = temp;
*v = temp;
}
6、(第一次碼的程序讀取到換行符的時候也會打印出來,會給人看不明白的感覺,索性按[Enter]鍵的時候就退出循環,不要讀到EOF)void larger_of(double *, double *);
int main(void)
{
double x = 23.3;
double y = 34.4;
printf("Originally x = %.1f; y = %.1f\n", x, y);
larger_of(&x, &y);
printf("Now x = %.1f; y = %.1f\n", x, y);
return 0;
}
void larger_of(double * u, double * v)
{
double temp;
temp = *u > *v ? *u : *v;
*u = temp;
*v = temp;
}
#include <stdio.h>
#include <ctype.h>
void printchar(char ch);
int main(void)
{
char ch;
printf("請輸入要分析的東西:\n");
while((ch = getchar()) != EOF)
{
printchar(ch);
}
return 0;
}
void printchar(char ch)
{
if(isalpha(ch))
{
printf("%c %d\n", ch, toupper(ch) % 'A' + 1);
}
}
修改之后,程序如下:#include <ctype.h>
void printchar(char ch);
int main(void)
{
char ch;
printf("請輸入要分析的東西:\n");
while((ch = getchar()) != EOF)
{
printchar(ch);
}
return 0;
}
void printchar(char ch)
{
if(isalpha(ch))
{
printf("%c %d\n", ch, toupper(ch) % 'A' + 1);
}
}
#include <stdio.h>
#include <ctype.h>
int show_c_location(char ch);
int main(void)
{
char ch;
printf("Please enter some characters: \n");
while((ch = getchar()) != '\n')
printf("%c-%d ", ch, show_c_location(ch));
return 0;
}
int show_c_location(char ch)
{
int result;
if(isalpha(ch))
result = toupper(ch) - 'A' + 1;
else
result = -1;
return result;
}
7、#include <ctype.h>
int show_c_location(char ch);
int main(void)
{
char ch;
printf("Please enter some characters: \n");
while((ch = getchar()) != '\n')
printf("%c-%d ", ch, show_c_location(ch));
return 0;
}
int show_c_location(char ch)
{
int result;
if(isalpha(ch))
result = toupper(ch) - 'A' + 1;
else
result = -1;
return result;
}
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
int i;
double result = 1;
if(n != 0)
{
if(p > 0)
{
for(i = 1; i <= p; i++)
result *= n;
}
else if(p < 0)
{
for(i = 1; i <= -p; i++)
result *= (1 / n);
}
else
result = 1;
}
else
{
if(p == 0)
result = 1;// 0的0次方是一個有爭議的數,本題認為會得到1
else
result = 0;
}
return result;
}
8、double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
int i;
double result = 1;
if(n != 0)
{
if(p > 0)
{
for(i = 1; i <= p; i++)
result *= n;
}
else if(p < 0)
{
for(i = 1; i <= -p; i++)
result *= (1 / n);
}
else
result = 1;
}
else
{
if(p == 0)
result = 1;// 0的0次方是一個有爭議的數,本題認為會得到1
else
result = 0;
}
return result;
}
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double result = 1;
if(n != 0)
{
if(p > 0)
result = n * power(n, p-1);
else if(p < 0)
result = (1/n) * power(n, p+1);
else
result = 1;
}
else
{
if(p == 0)
result = 1;// 0的0次方是一個有爭議的數,本題認為會得到1
else
result = 0;
}
return result;
}
9、double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while(scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double result = 1;
if(n != 0)
{
if(p > 0)
result = n * power(n, p-1);
else if(p < 0)
result = (1/n) * power(n, p+1);
else
result = 1;
}
else
{
if(p == 0)
result = 1;// 0的0次方是一個有爭議的數,本題認為會得到1
else
result = 0;
}
return result;
}
#include <stdio.h>
void to_base_n(unsigned long n, int range);
int main(void)
{
unsigned long number;
int range;
printf("請輸入要轉換的無符號整數和所規定的進制數: \n");
while(scanf("%lu %d", &number, &range) == 2)
{
if(range >= 2 && range <= 10)
{
printf("無符號整數%lu轉換成%d進制數為: ", number, range);
to_base_n(number, range);
putchar('\n');
printf("請輸入要轉換的無符號整數和所規定的進制數: \n");
}
else
printf("所規定的進制數的范圍是2~10,請輸入正確的數字\n");
}
printf("Done!\n");
return 0;
}
void to_base_n(unsigned long n, int range)
{
int r;
r = n % range;
if(n >= range)
to_base_n(n / range, range);
putchar('0' + r);
return;
}
10、(題意理解不清楚,借鑒CSDN——vs9841原作者的做法,腦子太笨,實在想不出來)void to_base_n(unsigned long n, int range);
int main(void)
{
unsigned long number;
int range;
printf("請輸入要轉換的無符號整數和所規定的進制數: \n");
while(scanf("%lu %d", &number, &range) == 2)
{
if(range >= 2 && range <= 10)
{
printf("無符號整數%lu轉換成%d進制數為: ", number, range);
to_base_n(number, range);
putchar('\n');
printf("請輸入要轉換的無符號整數和所規定的進制數: \n");
}
else
printf("所規定的進制數的范圍是2~10,請輸入正確的數字\n");
}
printf("Done!\n");
return 0;
}
void to_base_n(unsigned long n, int range)
{
int r;
r = n % range;
if(n >= range)
to_base_n(n / range, range);
putchar('0' + r);
return;
}
#include <stdio.h>
int Fibonacci(int n);
int main(void)
{
int n = 9;
printf("當n為%d時,斐波納契數值為%d", n, Fibonacci(9));
return 0;
}
int Fibonacci(int n)
{
int a, b, i;
a = 0;
b = 1;
int sum;
if(n == 0)
return 0;
if(n == 1)
return 1;
else
{
for(i = 2; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
}
return sum;
}
}
總結:總體來說編程練習相對以往來說要簡單了,但第10題沒明白什么意思,所以只能借鑒別人的了,真是天下文章一大抄!
int Fibonacci(int n);
int main(void)
{
int n = 9;
printf("當n為%d時,斐波納契數值為%d", n, Fibonacci(9));
return 0;
}
int Fibonacci(int n)
{
int a, b, i;
a = 0;
b = 1;
int sum;
if(n == 0)
return 0;
if(n == 1)
return 1;
else
{
for(i = 2; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
}
return sum;
}
}
復習題
1、putchar(getchar())是一個有效的表達式,它實現什么功能?getchar(putchar())也有效嗎?
答:
語句putchar(getchar())使程序讀取下一個輸入字符并打印它,getchar()的返回值作為putchar()的參數。getchar(putchar())則不是合法的,因為getchar()不需要參數而putchar()需要一個參數。
2、下面的每個語句實現什么功能?
a. 顯示字符H
b.如果系統使用ASCII字符編碼,則發出一聲警報
c.把光標移動到下一行的開始
d.退后一格
3、假設您有一個程序count,該程序對輸入的字符進行統計。用count程序設計一個命令行命令,對文件essay中的字符進行計數并將結果保存在名為essayct的文件中。
答:
count < essay > essayct
4、給定問題3中的程序和文件,下面哪個命令是正確的?
答:
a.essayct <essay
b.count essay
c.essay >count
答:
c是正確的。
5、EOF是什么?
答:
它是由getchar()和scanf()返回的信號(一個特定的值),用來表明已經到達了文件的結尾。
6、對給出的輸入,下面每個程序段的輸出是什么(假定ch是int類型的,并且輸入是緩沖的)?
a. 輸入如下所示:
If you quit, I will.[enter]
程序段如下所示:
while ((ch = getchar()) != 'i')
putchar(ch);
b. 輸入如下所示:
Harhar[enter]
程序段如下所示:
while ((ch = getchar()) != '\n')
{
putchar(ch++);
putchar(++ch);
}
答:
a.If you qu
b.HJacrthjacrt
7、C如何處理具有不同文件和換行約定的不同計算機系統?
答:
C的標準I/O庫把不同的文件形式映射為統一的流,這樣就可以按相同的方式對它們進行處理。
8、在緩沖系統中把數值輸入與字符輸入相混合時,您所面臨的潛在問題是什么?
答:
數字輸入跳過空格和換行符,但是字符輸入并不是這樣。假設您編寫了這樣的代碼:
編程練習
1、
1、putchar(getchar())是一個有效的表達式,它實現什么功能?getchar(putchar())也有效嗎?
答:
語句putchar(getchar())使程序讀取下一個輸入字符并打印它,getchar()的返回值作為putchar()的參數。getchar(putchar())則不是合法的,因為getchar()不需要參數而putchar()需要一個參數。
2、下面的每個語句實現什么功能?
a.putchar('H');
b.putchar('\007');
c.putchar('\n');
d.putchar('\b');
答:b.putchar('\007');
c.putchar('\n');
d.putchar('\b');
a. 顯示字符H
b.如果系統使用ASCII字符編碼,則發出一聲警報
c.把光標移動到下一行的開始
d.退后一格
3、假設您有一個程序count,該程序對輸入的字符進行統計。用count程序設計一個命令行命令,對文件essay中的字符進行計數并將結果保存在名為essayct的文件中。
答:
count < essay > essayct
4、給定問題3中的程序和文件,下面哪個命令是正確的?
答:
a.essayct <essay
b.count essay
c.essay >count
答:
c是正確的。
5、EOF是什么?
答:
它是由getchar()和scanf()返回的信號(一個特定的值),用來表明已經到達了文件的結尾。
6、對給出的輸入,下面每個程序段的輸出是什么(假定ch是int類型的,并且輸入是緩沖的)?
a. 輸入如下所示:
If you quit, I will.[enter]
程序段如下所示:
while ((ch = getchar()) != 'i')
putchar(ch);
b. 輸入如下所示:
Harhar[enter]
程序段如下所示:
while ((ch = getchar()) != '\n')
{
putchar(ch++);
putchar(++ch);
}
答:
a.If you qu
b.HJacrthjacrt
7、C如何處理具有不同文件和換行約定的不同計算機系統?
答:
C的標準I/O庫把不同的文件形式映射為統一的流,這樣就可以按相同的方式對它們進行處理。
8、在緩沖系統中把數值輸入與字符輸入相混合時,您所面臨的潛在問題是什么?
答:
數字輸入跳過空格和換行符,但是字符輸入并不是這樣。假設您編寫了這樣的代碼:
int score;
char grade;
printf("Enter the score.\n");
scanf("%d", &score);
printf("Enter the letter grade.\n");
grade = getchar();
假設您輸入分數98,然后按下回車鍵來把分數發送給程序,您同時也發送了一個換行符,它會成為下一個輸入字符被讀取到grade中作為等級的值。如果在字符輸入之前進行了數字輸入,就應該添加代碼以在獲取字符輸入之前剔除換行字符。char grade;
printf("Enter the score.\n");
scanf("%d", &score);
printf("Enter the letter grade.\n");
grade = getchar();
編程練習
1、
#include <stdio.h>
int main(void)
{
int ch;
int count = 0;
while((ch = getchar()) != EOF) // 包括換行符
count++;
printf("The number of characters is %d\n", count);
return 0;
}
2、(覺得這題超難的?。。】戳艘恍┧藢懙睦樱喼焙f八道?。。〔贿^還是完美解決了)int main(void)
{
int ch;
int count = 0;
while((ch = getchar()) != EOF) // 包括換行符
count++;
printf("The number of characters is %d\n", count);
return 0;
}
#include <stdio.h>
int main(void)
{
int ch;
int i = 0;
while((ch = getchar()) != EOF)
{
if(ch >= 32) // 可打印字符
{
putchar(ch);
printf("/%d ", ch);
i++;
}
else if(ch == '\n') // 打印換行符
{
printf("\\n");
printf("/%d ", ch);
putchar(ch); // 清除輸入緩沖區里面的換行符
i = 0; // i置為0重新開始計數,因為題目要求每次遇到一個換行符時就要開始打印一個新行
}
else if(ch == '\t') // 打印制表符
{
printf("\\t");
printf("/%d ", ch);
i++;
}
else // 打印控制字符
{
putchar('^');
putchar(ch + 64);
printf("/%d ", ch);
}
if(i == 10)
{
putchar('\n');
i = 0;
}
}
return 0;
}
運行結果如下:int main(void)
{
int ch;
int i = 0;
while((ch = getchar()) != EOF)
{
if(ch >= 32) // 可打印字符
{
putchar(ch);
printf("/%d ", ch);
i++;
}
else if(ch == '\n') // 打印換行符
{
printf("\\n");
printf("/%d ", ch);
putchar(ch); // 清除輸入緩沖區里面的換行符
i = 0; // i置為0重新開始計數,因為題目要求每次遇到一個換行符時就要開始打印一個新行
}
else if(ch == '\t') // 打印制表符
{
printf("\\t");
printf("/%d ", ch);
i++;
}
else // 打印控制字符
{
putchar('^');
putchar(ch + 64);
printf("/%d ", ch);
}
if(i == 10)
{
putchar('\n');
i = 0;
}
}
return 0;
}
I love you!
I/73 /32 l/108 o/111 v/118 e/101 /32 y/121 o/111 u/117(每行打印10個值)
!/33 \n/10(每次遇到一個換行符時就開始一個新行)
My hello world^A
M/77 y/121 /32 h/104 e/101 l/108 l/108 o/111 /32 w/119(每行打印10個值)
o/111 r/114 l/108 d/100 ^A/1 \n/10(每次遇到一個換行符時就開始一個新行)
^Z
3、I/73 /32 l/108 o/111 v/118 e/101 /32 y/121 o/111 u/117(每行打印10個值)
!/33 \n/10(每次遇到一個換行符時就開始一個新行)
My hello world^A
M/77 y/121 /32 h/104 e/101 l/108 l/108 o/111 /32 w/119(每行打印10個值)
o/111 r/114 l/108 d/100 ^A/1 \n/10(每次遇到一個換行符時就開始一個新行)
^Z
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int low_count = 0, up_count = 0;
while((ch = getchar()) != EOF)
{
if(islower(ch))
low_count++;
if(isupper(ch))
up_count++;
}
printf("A number of capital letters: %d\n", up_count);
printf("A number of lower case letters: %d\n", low_count);
return 0;
}
4、#include <ctype.h>
int main(void)
{
int ch;
int low_count = 0, up_count = 0;
while((ch = getchar()) != EOF)
{
if(islower(ch))
low_count++;
if(isupper(ch))
up_count++;
}
printf("A number of capital letters: %d\n", up_count);
printf("A number of lower case letters: %d\n", low_count);
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
char ch;
long chars = 0L; // 統計單詞的字符數
int words= 0; // 單詞數
bool inword = false; // 如果ch在一個單詞中,則inword為true
printf("Enter text to be analyzed: \n");
while((ch = getchar()) != EOF)
{
if(!isspace(ch) && !ispunct(ch))
chars++;
if(!isspace(ch) && !inword)
{
inword = true;
words++;
}
if(isspace(ch) && inword)
inword = false;
}
printf("The average number of words per word: %ld\n", chars / words);
return 0;
}
5、(二分搜索算法第一次碰見,搞了大半天了,借鑒的是CSDN-----vs9841作者的做法,不過稍微加了下工)#include <ctype.h>
#include <stdbool.h>
int main(void)
{
char ch;
long chars = 0L; // 統計單詞的字符數
int words= 0; // 單詞數
bool inword = false; // 如果ch在一個單詞中,則inword為true
printf("Enter text to be analyzed: \n");
while((ch = getchar()) != EOF)
{
if(!isspace(ch) && !ispunct(ch))
chars++;
if(!isspace(ch) && !inword)
{
inword = true;
words++;
}
if(isspace(ch) && inword)
inword = false;
}
printf("The average number of words per word: %ld\n", chars / words);
return 0;
}
#include <stdio.h>
char get_choice(void);
char get_first(void);
int main(void)
{
int low = 1, high = 100, guess = 50;
char ch;
printf("Pick an integer from 1 to 100. I will try to guess it\n");
printf("Un
is your number %d?\n", guess);
while((ch = get_choice()) != 'q')
{
if(ch == 'a')
{
printf("I knew I could do it!\n");
break;
}
else if(ch == 'b')
{
printf("It is too small!\n");
low = guess + 1;
}
else if(ch == 'c')
{
printf("It is too big!\n");
high = guess - 1;
}
guess = (low + high) / 2;
printf("Un
is your number %d?\n", guess);
}
printf("Done!\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. right b. too small\n");
printf("c. too big q. quit\n");
ch = get_first();
while((ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while(getchar() != '\n')
continue;
return ch;
}
6、char get_choice(void);
char get_first(void);
int main(void)
{
int low = 1, high = 100, guess = 50;
char ch;
printf("Pick an integer from 1 to 100. I will try to guess it\n");
printf("Un

while((ch = get_choice()) != 'q')
{
if(ch == 'a')
{
printf("I knew I could do it!\n");
break;
}
else if(ch == 'b')
{
printf("It is too small!\n");
low = guess + 1;
}
else if(ch == 'c')
{
printf("It is too big!\n");
high = guess - 1;
}
guess = (low + high) / 2;
printf("Un

}
printf("Done!\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. right b. too small\n");
printf("c. too big q. quit\n");
ch = get_first();
while((ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while(getchar() != '\n')
continue;
return ch;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
7、{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
#include <stdio.h>
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
char get_choice(void);
char get_first(void);
int main(void)
{
int hour, choise;
double total, tax, net_pay;
double base_pay; // 基本工資等級不能用#define來定義了,因為它要隨著程序而改變了,書上真是胡說八道
while((choise = get_choice()) != 'q')
{
switch(choise)
{
case 'a':
base_pay = 8.15;
break; // break只是導致程序脫離switch語句,跳到switch之后的下一條語句?。?!
case 'b':
base_pay = 9.33;
break;
case 'c':
base_pay = 10.00;
break;
case 'd':
base_pay = 11.20;
break;
default:
printf("Program error!\n");
break;
}
printf("Please enter the hour used: ");
scanf("%d", &hour); // 獲取每周工作小時數時沒有像書上那樣判斷,我偷懶了!??!
if(hour <= WORK_OVERTIME)
{
total = hour * base_pay;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
}
printf("Bye!\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired pay rate or action:\n");
printf("a) $8.75/hr\tb) $9.33/hr\n");
printf("c) $10.00/hr\td) $11.20/hr\n");
printf("q) quit\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
ch = get_first();
while((ch < 'a' || ch > 'd') && ch != 'q')
{
printf("Please respond with a, b, c, d, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
8、#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
char get_choice(void);
char get_first(void);
int main(void)
{
int hour, choise;
double total, tax, net_pay;
double base_pay; // 基本工資等級不能用#define來定義了,因為它要隨著程序而改變了,書上真是胡說八道
while((choise = get_choice()) != 'q')
{
switch(choise)
{
case 'a':
base_pay = 8.15;
break; // break只是導致程序脫離switch語句,跳到switch之后的下一條語句?。?!
case 'b':
base_pay = 9.33;
break;
case 'c':
base_pay = 10.00;
break;
case 'd':
base_pay = 11.20;
break;
default:
printf("Program error!\n");
break;
}
printf("Please enter the hour used: ");
scanf("%d", &hour); // 獲取每周工作小時數時沒有像書上那樣判斷,我偷懶了!??!
if(hour <= WORK_OVERTIME)
{
total = hour * base_pay;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
}
printf("Bye!\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired pay rate or action:\n");
printf("a) $8.75/hr\tb) $9.33/hr\n");
printf("c) $10.00/hr\td) $11.20/hr\n");
printf("q) quit\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
ch = get_first();
while((ch < 'a' || ch > 'd') && ch != 'q')
{
printf("Please respond with a, b, c, d, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
#include <stdio.h>
char get_choice(void);
char get_first(void);
float get_float(void);
int main(void)
{
char choise;
float first_number, second_number;
while((choise = get_choice()) != 'q')
{
printf("Enter first number: ");
first_number = get_float();
printf("Enter second number: ");
second_number = get_float();
switch(choise)
{
case 'a':
printf("%.1f + %.1f = %.1f\n", first_number, second_number, first_number + second_number);
break;
case 's':
printf("%.1f - %.1f = %.1f\n", first_number, second_number, first_number - second_number);
break;
case 'm':
printf("%.1f * %.1f = %.1f\n", first_number, second_number, first_number * second_number);
break;
case 'd':
if(second_number == 0)
{
printf("Enter a number other than 0: ");
second_number = get_float();
printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
}
else
printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
break;
default:
printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the operation of your choice: \n");
printf("a. add\ts. subtract\n");
printf("m. multiply\td. divide\n");
printf("q. quit\n");
ch = get_first();
while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
{
printf("Please respond with a, s, m, d, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
float get_float(void)
{
float input;
char ch;
while((scanf("%f", &input)) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\nPlease enter a ");
printf("number, such as 2.5, -1.78E8, or 3: ");
}
return input;
}
char get_choice(void);
char get_first(void);
float get_float(void);
int main(void)
{
char choise;
float first_number, second_number;
while((choise = get_choice()) != 'q')
{
printf("Enter first number: ");
first_number = get_float();
printf("Enter second number: ");
second_number = get_float();
switch(choise)
{
case 'a':
printf("%.1f + %.1f = %.1f\n", first_number, second_number, first_number + second_number);
break;
case 's':
printf("%.1f - %.1f = %.1f\n", first_number, second_number, first_number - second_number);
break;
case 'm':
printf("%.1f * %.1f = %.1f\n", first_number, second_number, first_number * second_number);
break;
case 'd':
if(second_number == 0)
{
printf("Enter a number other than 0: ");
second_number = get_float();
printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
}
else
printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
break;
default:
printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the operation of your choice: \n");
printf("a. add\ts. subtract\n");
printf("m. multiply\td. divide\n");
printf("q. quit\n");
ch = get_first();
while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
{
printf("Please respond with a, s, m, d, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == '\n')
continue;
while(getchar() != '\n')
continue;
return ch;
}
float get_float(void)
{
float input;
char ch;
while((scanf("%f", &input)) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\nPlease enter a ");
printf("number, such as 2.5, -1.78E8, or 3: ");
}
return input;
}