C_Primer_Plus(第五版)全书源代码_第1页
C_Primer_Plus(第五版)全书源代码_第2页
C_Primer_Plus(第五版)全书源代码_第3页
C_Primer_Plus(第五版)全书源代码_第4页
C_Primer_Plus(第五版)全书源代码_第5页
已阅读5页,还剩55页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/ chapter 01 /* #include int main(void) int dogs; printf(How many dogs do you have?n); scanf(%d, printf(So you have %d dog(s)!n, dogs); return 0; */ / / / chapter 02 / fathm_ft.c - converts 2 fathoms to feet /* #include int main(void) int feet, fathoms; fathoms = 2; feet = 6 * fathoms; printf(There

2、are %d feet in %d fathoms!n, feet, fathoms); printf(Yes, I said %d feet!n, 6 * fathoms); return 0; */ / /* #include int main(void) / a simple program int num;/ define a variable called num num = 1;/ assign a value to num printf(I am a simple ); / use the printf() function printf(computer.n); printf(

3、My favorite number is %d because it is first.n,num); return 0; */ / / two_func.c - a program using two functions in one file /* #include void butler(void);/ ISO/ANSI C function prototyping int main(void) printf(I will summon the butler function.n); butler(); printf(Yes. Bring me some tea and writeab

4、le CD-ROMS.n); return 0; void butler(void)/ start of function definition printf(You rang, sir?n); */ / / / chapter 03 数据和 C /* / altnames.c - portable names for integer types #include the #include / supports portable types the system doesnt contain header file int main(void) int16_t me16;/ me16 a 16

5、-bit signed variable me16 = 4593; printf(First, assume int16_t is short: ); printf(me16 = %hdn, me16); printf(Next, lets not make any assumptions.n); printf(Instead, use a macro from inttypes.h: ); printf(me16 = % PRId16 n, me16); return 0; */ / /* / bases.c-prints 100 in decimal, octal, and hex #in

6、clude int main(void) int x = 100; printf(dec = %d; octal = %o; hex = %xn, x, x, x); printf(dec = %d; octal = %#o; hex = %#xn, x, x, x); /%# 十六进制前显示 Ox / 八进制数前显示 o return 0; */ / /* / charcode.c-displays code number for a character #include int main(void) char ch; printf(Please enter a character.n);

7、scanf(%c, printf(The code for %c is %d.n, ch, ch); return 0; */ / /* /print1.c-displays some properties of printf() #include int main(void) int ten = 10; int two = 2; printf(Doing it right: ); printf(%d minus %d is %dn, ten, 2, ten - two ); printf(Doing it wrong: ); printf(%d minus %d is %dn, ten );

8、/ forgot 2 arguments return 0; */ / /* print2.c-more printf() properties */ /* #include int main(void) unsigned int un = 00; / system with 32-bit int short end = 200;/ and 16-bit short long big = 65537; long verybig = 908642; /C 也可以使用前缀 h 来表示 short 类型。 / 因此 %hd 显示一个十进制的 short 整型。 %ho 为八进制形式。 printf(

9、un = %u and not %dn, un, un); printf(end = %hd and %dn, end, end); printf(big = %ld and not %hdn, big, big); printf(verybig= %lld and not %ldn, verybig, verybig); return 0; */ / /* showf_pt.c - displays float value in two ways */ /* #include int main(void) float aboat = 32000.0; double abet = 2.14e9

10、; long double dip = 5.32e-5; printf(%f can be written %en, aboat, aboat); printf(%f can be written %en, abet, abet); printf(%f can be written %en, dip, dip); return 0; */ / /* toobig.c-exceeds maximum int size on our system */ /* #include int main(void) int i = 47; unsigned int j = 95; printf(%d %d

11、%dn, i, i+1, i+2); printf(%u %u %un, j, j+1, j+2); return 0; */ / /* typesize.c - prints out type sizes */ /* #include int main(void) / c99 provides a %zd specifier for sizes printf(Type int has a size of %u bytes.n, sizeof(int); /4 bytesprintf(Type char has a size of %u bytes.n, sizeof(char);/ 1 by

12、tes printf(Type long has a size of %u bytes.n, sizeof(long);/4 bytes printf(Type double has a size of %u bytes.n, sizeof(double);/8 bytes return 0; */ / / / this is / chapter04 字符串的格式化输入 / 输出 / defines.c - uses defined constants from limit.h and float. useful ,you can know some limits of the system

13、/* #include #include / integer limits #include / floating-point limits int main(void) printf(Some number limits for this system:n); printf(Biggest int: %dn, INT_MAX); printf(Smallest long long: %lldn, LONG_MIN); printf(One byte = %d bits on this system.n, CHAR_BIT); printf(Largest double: %en, DBL_M

14、AX); printf(Smallest normal float: %en, FLT_MIN); printf(float precision = %d digitsn, FLT_DIG); printf(float epsilon = %en, FLT_EPSILON); return 0; */ / /* flags.c - illustrates some formatting flags */ /* #include int main(void) printf(%x %X %#xn, 31, 31, 31); printf(*%d*% d*% d*n, 42, 42, -42); p

15、rintf(*%5d*%5.3d*%05d*%05.3d*n, 6, 6, 6, 6); return 0; */ / /* floatcnv.c - mismatched floating-point conversions */ /* #include int main(void) float n1 = 3.0; double n2 = 3.0; long n3 = 00; long n4 = 90; printf(%.1e %.1e %.1e %.1en, n1, n2, n3, n4); printf(%ld %ldn, n3, n4); printf(%ld %ld %ld %ldn

16、, n1, n2, n3, n4); return 0; */ / / floats.c - some floating-point combinations /* #include int main(void) const double RENT = 3852.99;/ const-style constant printf(*%f*n, RENT); printf(*%e*n, RENT); printf(*%4.2f*n, RENT); printf(*%3.1f*n, RENT); /take care here printf(*%10.3f*n, RENT); printf(*%10

17、.3e*n, RENT); printf(*%+4.2f*n, RENT); printf(*%010.2f*n, RENT); return 0; */ / /* intconv.c - some mismatched integer conversions */ /* #include #define PAGES 336 #define WORDS 65618 int main(void) short num = PAGES; short mnum = -PAGES; printf(num as short and unsigned short:%hd %hun, num, num); p

18、rintf(-num as short and unsigned short: %hd %hun, mnum, mnum); printf(num as int and char: %d %cn, num, num); printf(WORDS as int, short, and char: %d %hd %cn, WORDS, WORDS, WORDS); return 0; */ / /* longstrg.c - printing long strings */ /* #include int main(void) printf(Heres one way to print a );

19、printf(long string.n); printf(Heres another way to print a long string.n); printf(Heres the newest way to print a long string.n);/ ANSI C return 0; */ / /* praise1.c - uses an assortment of strings */ /* #include #define PRAISE What a super marvelous name! int main(void) char name40; printf(Whats yo

20、ur name?n); scanf(%s, name); printf(Hello, %s. %sn, name, PRAISE); printf(Hello, %s. %sn, name, very good); return 0; */ / /* praise2.c */ /* #include #include / provides strlen() prototype /take care of this program /take care of here #define PRAISE What a super marvelous name! int main(void) char

21、name40; printf(Whats your name?n); scanf(%s, name); printf(Hello, %s. %sn, name, PRAISE); printf(Your name of %d letters occupies %d memory cells.n, strlen(name), sizeof name); printf(The phrase of praise has %d letters , strlen(PRAISE); printf(and occupies %d memory cells.n, sizeof PRAISE); return

22、0; */ / /* printout.c - uses conversion specifiers */ /* #include #define PI 3.141593 int main(void) int number = 5; float espresso = 13.5; int cost = 3100; printf(The %d CEOs drank %f cups of espresso.n, number, espresso); printf(The value of pi is %f.n, PI); printf(Farewell! thou art too dear for

23、my possessing,n); printf(%c%dn, $, 2 * cost); return 0; */ / /* prntval.c - finding printf()s return value */ /* #include int main(void) int bph2o = 212; int rv; rv = printf(%d F is waters boiling point.n, bph2o); / finding printf(The printf() function printed %d characters.n, printf()s return value

24、= how many characters printed rv); return 0; */ / /* skip2.c - skips over first two integers of input */ /* #include int main(void) int n; printf(Please enter three integers:n); scanf(%*d %*d %d, skip printf(The last integer was %dn, n); return 0; */ / /* strings.c - string formatting */ /* #include

25、 #define BLURB Authentic imitation! int main(void) printf(/%2s/n, BLURB); understand clearly printf(/%24s/n, BLURB); printf(/%24.5s/n, BLURB); printf(/%-24.5s/n, BLURB); return 0; */ / /* varwid.c - uses variable-width output field */ /* #include int main(void) unsigned width, precision; int number

26、= 256; double weight = 242.5; printf(What field width?n); scanf(%d, printf(The number is :%*d:n, width, number); printf(Now enter a width and a precision:n); /* means / still dont / still dont understand clearly scanf(%d %d, /%*.*f %24.5s printf(Weight = %*.*fn, width, precision, weight); what does

27、is mean? printf(Done!n); return 0; */ / /* width.c - field widths */ /* #include #define PAGES 931 int main(void) / still will print 931 / still will print 931 printf(*%d*n, PAGES); printf(*%2d*n, PAGES); printf(*%10d*n, PAGES); printf(*%-10d*n, PAGES); return 0; */ / / / chapter 05 运算符、表达式和语句 /* ad

28、d_one.c - incrementing: prefix and postfix */ /* #include int main(void) int ultra = 0, super = 0; while (super 5) super+; +ultra; printf(super = %d, ultra = %d n, super, ultra); return 0; */ / /* addemup.c - four kinds of statements */ /* #include int main(void)/ finds sum of first 20 integers int

29、count, sum; int num=0; count = 0; sum = 0; while (count+ 20)/ you should take care of num+; sum = sum + count; printf(count= %d sum = %dn,count, sum); printf( the time of loop is %dn,num); return 0; */ / /* bottles.c - counting down */ /* #include #define MAX 10 int main(void) int count = MAX + 1; w

30、hile (-count 0) printf(%d bottles of spring water on the wall, %d bottles of spring water!n, count, count); printf(Take one down and pass it around,n); printf(%d bottles of spring water!nn, count - 1); return 0; */ / /* shoes1.c - converts a shoe size to inches */ /* #include #define ADJUST 7.64 #de

31、fine SCALE 0.325 int main(void) double shoe, foot; shoe = 9.0; foot = SCALE * shoe + ADJUST; printf(Shoe size (mens) foot lengthn); printf(%10.1f %15.2f inchesn, shoe, foot); return 0; */ / /* shoes2.c - calculates foot lengths for several sizes */ /* #include #define ADJUST 7.64 #define SCALE 0.325

32、 int main(void) double shoe, foot; printf(Shoe size (mens) foot lengthn); shoe = 3.0; while (shoe 18.5) foot = SCALE*shoe + ADJUST; printf(%10.1f %15.2f inchesn, shoe, foot); shoe = shoe + 1.0; printf(If the shoe fits, wear it.n); return 0; */ / /* golf.c - golf tournament scorecard */ /* #include i

33、nt main(void) int jane, tarzan, cheeta; cheeta = tarzan = jane = 68; printf( cheeta tarzan janen); printf(First round score %4d %8d %8dn,cheeta,tarzan,jane); return 0; */ / /* squares.c - produces a table of first 20 squares */ /*/ have fun ,you can learn much #include int main(void) int num = 1; wh

34、ile (num 21) printf(%4d %6dn, num, num * num); num = num + 1; return 0; */ / /* wheat.c - exponential growth */ /* / 棋盘赠大米,国王傻眼啦 #include #define SQUARES 64 #define CROP 1E15 / squares on a checkerboard / US wheat crop in grains int main(void) double current, total; int count = 1; printf(square grai

35、ns total ); printf(fraction of n); printf( added grains ); printf(US totaln); total = current = 1.0; / start with one grain printf(%4d %13.2e %12.2e %12.2en, count, current, total, total/CROP); while (count SQUARES) count = count + 1; current = 2.0 * current; / double grains on next square total = t

36、otal + current; / update total printf(%4d %13.2e %12.2e %12.2en, count, current, total, total/CROP); printf(Thats all.n); return 0; */ / /* divide.c - divisions we have known */ /* #include int main(void) printf(integer division: 5/4 is %d n, 5/4); printf(integer division: 6/3 is %d n, 6/3); printf(

37、integer division: 7/4 is %d n, 7/4); printf(integer division: 7/-4 is %d n, 7/-4);/print -1 printf(integer division: -7/4 is %d n, -7/4);/print -1 printf(floating division: 7./4. is %1.2f n, 7./4.); printf(mixed division: 7./4 is %1.2f n, 7./4); printf(mixed division: -7./4 is %1.2f n, -7./4); / pri

38、nt -1.75 printf(mixed division: 7./-4 is %1.2f n, 7./-4); /print -1.7 printf(mixed division:-7./-4 is %1.2f n, -7./-4); return 0; */ / / sizeof.c - uses sizeof operator /%z dont work / uses C99 %z modifier - try %u or %lu if you lack %zd /* #include int main(void) int n = 0; size_t intsize; intsize

39、= sizeof (int); printf(n = %d, n has %u bytes; all ints have %u bytes.n, n, sizeof n, intsize ); return 0; */ / /* post_pre.c - postfix vs prefix */ /* #include int main(void) int a = 1, b = 1; int aplus, plusb; aplus = a+; plusb = +b; printf(a aplus b plusb n); printf(%1d %5d %5d %5dn, a, aplus, b,

40、 plusb);/ 2 1 return 0; */ / /* convert.c - automatic type conversions */ /* #include int main(void) char ch; int i; float fl; fl = i = ch = C; printf(ch = %c, i = %d, fl = %2.2fn, ch, i, fl); ch = ch + 1; i = fl + 2 * ch; fl = 2.0 * ch + i; printf(ch = %c, i = %d, fl = %2.2fn, ch, i, fl); ch = 5212

41、205.17; confused printf(Now ch = %cn, ch); return 0; */ / /* pound.c - defines a function with an argument /* #include void pound(int n); int main(void) int times = 5; char ch = !;/ ASCII code is 33 float f = 6.0; pound(times); pound(ch);/ char automatically - int pound(int) f);/ cast forces f - int

42、 return 0; void pound(int n) while (n- 0) printf(#); printf(n); */ */ / / print - still / / chapter 06 C 控制语句:循环 /* summing.c - sums integers entered interactively */ /* #include int main(void) long num; long sum = 0L; int status; printf(Please enter an integer to be summed ); printf(q to quit): );

43、status = scanf(%ld, while (status = 1) / = means is equal to sum = sum + num; printf(Please enter next integer (q to quit): ); status = scanf(%ld, printf(Those integers sum to %ld.n, sum); return 0; */ / /* t_and_f.c - true and false values in C */ /* #include int main(void) int true_val, false_val;

44、 true_val = (10 2); false_val = (10 = 2); printf(true = %d; false = %d n, true_val, false_val); return 0; */ / / boolean.c - using a _Bool variable /* #include int main(void) long num; long sum = 0L; int input_is_good; printf(Please enter an integer to be summed ); printf(q to quit): ); input_is_goo

45、d = (scanf(%ld, while (input_is_good) sum = sum + num; printf(Please enter next integer (q to quit): ); input_is_good = (scanf(%ld, printf(Those integers sum to %ld.n, sum); return 0; */ / / sweetie1.c - a counting loop /* #include int main(void) const int NUMBER = 22; / initialization / test / acti

46、on int count = 1; while (count = NUMBER) printf(Be my Valentine!n); count+; printf( the time of loop is %dn,-count); return 0; */ / / sweetie2.c - a counting loop using for /* #include int main(void) const int NUMBER = 22; int count; for (count = 1; count = NUMBER; count+) printf(Be my Valentine!n);

47、 printf( the time of loop is %dn,-count); return 0; */ / /* rows1.c - uses nested loops */ /* #include #define ROWS 6 #define CHARS 10 int main(void) int row; char ch; for (row = 0; row ROWS; row+) for (ch = A; ch (A + CHARS); ch+) printf(%c, ch); printf(n); return 0; */ / / rows2.c - using dependen

48、t nested loops /* #include int main(void) const int ROWS = 6; const int CHARS = 6; int row; char ch; for (row = 0; row ROWS; row+) for (ch = (A + row);ch (A + CHARS); ch+) printf(%c, ch); printf(n); return 0; */ / / scores_in.c - uses loops for array processing /* #include #define SIZE 10 #define PA

49、R 72 int main(void) int index, scoreSIZE; int sum = 0; float average; printf(Enter %d golf scores:n, SIZE); for (index = 0; index SIZE; index+) scanf(%d, / read in the ten scores printf(The scores read in are as follows:n); for (index = 0; index SIZE; index+) printf(%5d, scoreindex); / verify input

50、printf(n); for (index = 0; index SIZE; index+) sum += scoreindex;/ add them up average = (float) sum / SIZE;/ time-honored method printf(Sum of scores = %d, average = %.2fn, sum, average); printf(Thats a handicap of %.0f.n, average - PAR); return 0; */ / / an answer to the question 16 at page 151 /*

51、 #include void main() int year=0; double f; for(f=100;f0;f=f-10) printf(at the year %d , the money you have is %g n,year,f); f=f+f*0.08; year+; printf(at the year %dn,year); printf(you have no money now!n); */ / / / chapter 07 C 控制语句:分支与跳转 / colddays.c - finds percentage of days below freezing /* #i

52、nclude int main(void) const int FREEZING = 0; float temperature; int cold_days = 0; int all_days = 0; printf(Enter the list of daily low temperatures.n); printf(Use Celsius, and enter q to quit.n); while (scanf(%f, if (temperature FREEZING) cold_days+; if (all_days != 0) printf(%d days total: %.1f%

53、were below freezing.n, all_days, 100.0 * (float) cold_days / all_days); if (all_days = 0) printf(No data entered!n); return 0; */ / /* cypher1.c - alters input, preserving spaces */ /* / thats quote-space-quote #include #define SPACE int main(void) char ch; ch = getchar(); while (ch != n) if (ch = S

54、PACE) putchar(ch); else putchar(ch + 1); ch = getchar(); /* while not end of line /* leave the space /* character unchanged /* change other characters putchar(ch); return 0; */ / / cypher2.c - alters input, preserving non-letters /* #include #include / for isalpha() int main(void) char ch; while (ch

55、 = getchar() != n) if (isalpha(ch) putchar(ch + 1); else putchar(ch); putchar(ch); return 0; / if a letter, / change it / otherwise, / print as is / print the newline */ / / chcount.c- use the logical AND operator /* #include #define PERIOD . int main(void) int ch; int charcount = 0; while (ch = get

56、char() != PERIOD) if (ch != printf(There are %d non-quote characters.n, charcount); return 0; */ / / wordcnt.c - counts characters, words, lines /* #include #include / for isspace() #define STOP | #define false 0 #define true 1 int main(void) char c; char prev; long n_chars = 0L; int n_lines = 0; in

57、t n_words = 0; int p_lines = 0; int inword = false; / read in character / previous character read / number of characters / number of lines / number of words / number of partial lines / = true if c is in a word printf(Enter text to be analyzed (| to terminate):n); prev = n;/ used to identify complete

58、 lines while (c = getchar() != STOP) n_chars+;/ count characters if (c = n) n_lines+; / count lines if (!isspace(c) / starting a new word n_words+; / count word if (isspace(c) / reached end of word prev = c; / save character value if (prev != n) p_lines = 1; printf(characters = %ld, words = %d, line

59、s = %d, , n_chars, n_words, n_lines); printf(partial lines = %dn, p_lines); return 0; */ / /* paint.c - uses conditional operator */ /* #include / square feet per paint can #define COVERAGE 200 int main(void) int sq_feet; int cans; printf(Enter number of square feet to be painted:n); while (scanf(%d

60、, cans += (sq_feet % COVERAGE = 0) ? 0 : 1; printf(You need %d %s of paint.n, cans, cans = 1 ? can : cans); printf(Enter next value (q to quit):n); return 0; */ / / / chapter 08 字符输入 / 输出和输入确认 /* echo.c - repeats input */ /* #include int main(void) char ch; while (ch = getchar() != #) putchar(ch); p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论