C程式語言 練習 第一章

2021-07-04 02:10:01 字數 5000 閱讀 1542

/*練習1-3 修改溫度轉行程式,使之能在轉換表的頂部列印乙個標題。*/

#include /* 當fahr= 0,20,...,300時,分別

列印華氏溫度和攝氏溫度對照表*/

main()

getch();

}

/*練習1-4 編寫乙個程式列印攝氏溫度轉換為相應華氏溫度的轉換表。*/

#include main()

getch();

}

/*練習1-5 修改溫度轉換程式,要求以逆序(即按照300度到0度的順序)列印溫度轉換表*/

#include main()

/*練習1-6 驗證表示式getchar() != eof的值是0還是1。 */

#include main()

/*練習1-7 編寫乙個列印eof值得程式。*/

#include main()

/*練習1-8 編寫乙個統計空格、製表符與換行符個數的程式。*/

#include main()

printf("%d %d %d\n", n1, n2, n3);

}

/*練習1-9 編寫乙個將輸入複製到輸出的程式,並將其中連續的多個空格用乙個空格代替。*/

#include #define nonblank 'a'

main()

}

/*練習1-10 編寫乙個將輸入複製到輸出的程式,並將其中的製表符替換為\t,把回退符替換為\b,把反斜槓替換為\\。這樣可以將製表符和回退符以可見的方式顯示出來。*/

#include main()

}

/*練習1-12 編寫乙個程式,以每行乙個單詞的形式列印其輸入。*/

#include #define in 1

#define out 0

main()

} else if (state == out)

else

putchar(c);

}}

/*練習1-13 編寫乙個程式,列印輸入中單詞長度的直方圖。水平方向的直方圖比較容易繪製,垂直方向的直方圖則要困難些。*/

#include #define maxhist 15

#define maxword 11

#define in 1

#define out 0

main()

else if (state == out)

else

++nc;

} maxvalue = 0;

for (i = 1; i < maxword; ++i)

if (wl[i] > maxvalue)

maxvalue = wl[i];

for (i = 1; i < maxword; ++i)

else

len = 0;

while (len > 0)

putchar('\n');

} if (ovflow > 0)

printf("there are %d words >= %d\n", ovflow, maxword);

}

/*習題1-13 垂直方向的直方圖*/

#include #define maxhist 15

#define maxword 11

#define in 1

#define out 0

main()

else if (state == out) else

++nc;

} maxvalue = 0;

for (i = 1; i < maxword; ++i)

if (wl[i] > maxvalue)

maxvalue = wl[i];

for (i = maxhist; i > 0; --i)

for (i = 1; i < maxword; ++i)

printf("%4d", i);

putchar('\n');

for (i = 1; i < maxword; ++i)

printf("%4d", wl[i]);

putchar('\n');

if (ovflow > 0)

printf("there are %d words >= %d\n", ovflow, maxword);

}

/*練習1-14 編寫乙個程式,列印輸入中各個字元出現頻度的直方圖*/

#include #include #define maxhist 15

#define maxchar 128

main()

else

len = 0;

while (len > 0)

putchar('\n');

}}

/*練習1-15 重新編寫1.2節中的溫度轉換程式,使用函式實現溫度轉換計算。*/

#include float tempe(float fahr);

main()

}float tempe(float fahr)

/*練習1-16 修改列印最長文字行的程式的主程式main,使之可以列印任意長度的輸入行的長度,並盡可能多地列印文字。*/

#include #define maxline 1000

int getline(char line, int maxline);

void copy(char to, char from);

main()

} if(max > 0)

printf("%s",longest);

return 0;

}int getline(char s, int lim)

if (c == '\n')

s[j] = '\0';

return i;

}void copy(char to, char from)

/*編寫乙個程式,列印長度大於80個字元的所有輸入行。*/

#include #define maxline 1000

int getline(char line, int maxline);

main()

int getline(char s, int lim)

if(c == '\n')

s[j] = '\0';

return i;

}

/*練習1-18 編寫乙個程式,刪除每個輸入行末尾的空格及製表符,並刪除完全是空格的行。*/

#include #define maxline 1000

int getline(char line, int maxline);

int removec(char s);

main()

int removec(char s)

return i;

}int getline(char s, int lim)

if(c == '\n')

s[j] = '\0';

return i;

}

/*練習1-19 編寫函式reverse(s),將字串s中的字元順序顛倒過來。使用該函式編寫乙個程式,每次顛倒乙個輸入行中的字元順序。*/

#include #define maxline 1000

int getline(char line, int maxline);

void reverse(char s);

main()

}int getline(char s, int lim)

if (c == '\n')

s[j] = '\0';

return i;

}void reverse(char s)

}

/*練習1-20 編寫程式detab,將輸入中的製表符替換成適當數目的空格,使空格充滿到下乙個製表符終止位的地方。假設製表符終止位的位置是固定的,比如每隔n列就會出現乙個製表符終止位。n應該作為變數還是符合常量呢?*/

#include #define tabinc 8

main()

}else if(c == '\n')else

}}

/*練習1-22 編寫乙個程式,把較長的輸入行「折」成短一些的兩行或者多行,折行的位置在輸入行的第n列之前的最後乙個非空格之後。要保證程式能夠智慧型地處理輸入行很長以及在指定的列前沒有空格或者製表符時的情況。*/

#include #define maxcol 10

#define tabinc 8

char line[maxcol]; /*輸入行*/

int exptab(int pos);

int findblnk(int pos);

int newpos(int pos);

void printl(int pos);

main()

else if (++pos >= maxcol) }}

void printl(int pos)

int exptab(int pos)

}int findblnk(int pos)

int newpos(int pos)

return i;

}}

C 程式語言(第一章)

一.全書結構 1,1 3章,c 程式設計風格,以及有關c 標準庫的綜述 2,4 9章,c 內部型別 3,10 15,c 物件導向和通用型程式設計 4,16 22,c 標準庫 5,23 25,設計和開發軟體 6,附錄 二.給c程式設計師得建議 1,c 裡幾乎不需要用巨集,用const或enum定義明顯...

第一章練習

1.用student使用者登陸系統圖形介面 2.開啟乙個bash 3.修改student的密碼,把密碼更新成 t3st1ngtlme 主機字母和數字 4.顯示當前系統時間 root localhost desktop date 5.顯示當前系統時間,顯示格式為 小時 分鐘 秒 am pm am pm...

第一章 程式設計和C語言

toc 第1章 程式設計和c語言 你好!這是你第一次使用markdown編輯器所展示的歡迎頁。如果你想學習如何使用markdown編輯器,可以仔細閱讀這篇文章,了解一下markdown的基本語法知識。全新的介面設計,將會帶來全新的寫作體驗 在創作中心設定你喜愛的 高亮樣式,markdown將 片顯示...