4. 3-3 5. ----
6. Testing.......1...2..3 7.
8. The output would all appear on the same line since a newline character is not printed until 9. the last printf call. 10. 11.
12.3-5 13.----
14.main (Void) // should be lowercase v 15.( // should be a {
16.INT sum; // should be lowercase int 17./* COMPUTE RESULT // Needs closing */ 18.sum = 25 + 37 = 19 // Needs ; at the end
19./* DISPLAY RESULTS // // Needs */ to close comment 20.printf (\"The answer is %i\\n\" sum); // Missing comma 21.return 0; 22.} 23. 24.
25.Chapter 4 26.------------ 27.
28.4-3 29.----
30.0996 Digits 8 and 9 invalid in octal constant
31.0x10.5 Decimal point not valid in integer constant 32.98.7U Unsigned qualifier only valid for integers 33.1.2Fe-7 Can't use F and e together
34.0X0G1 G is not a valid hexadecimal digit 35.17777s s is not a valid qualifier
36.15,000 Commas not allowed in constants 37. 38. 39.
40.4-5 41.---- 42.d = d 43.
44.
45.4-7 46.----
47.#include 49.int main (void) 50.{ 51.double result; 52. 53.result = (3.31e-8 * 2.01e-7) / (7.16e-6 + 2.01e-8); 54.printf (\"result = %g\\n\55.return 0; 56.} 57. 58. 59. 60.Chapter 5 61.------------ 62. 63.5-3 .---- 65.#include 67.int main (void) 68.{ 69.int n, triangularNumber; 70. 71.printf (\"TABLE OF TRIANGULAR NUMBERS\\n\\n\"); 72.printf (\" n Sum from 1 to n\\n\"); 73.printf (\"--- ---------------\\n\"); 74. 75.for ( n = 5; n <= 50; n += 5) { 76.triangularNumber = n * (n + 1) / 2; 77.printf (\"%2i %i\\n\78.} 79. 80.return 0; 81.} 82. 83. 84. 85.5-5 86.---- 87.#include 88. .int main (void) 90.{ 91.int n, two_to_the_n; 92. 93.printf (\"TABLE OF POWERS OF TWO\\n\\n\"); 94.printf (\" n 2 to the n\\n\"); 95.printf (\"--- ----------\\n\"); 96. 97.two_to_the_n = 1; 98. 99.for ( n = 0; n <= 10; ++n ) { 100. printf (\"%2i %i\\n\101. two_to_the_n *= 2; 102. } 103. 104. return 0; 105. } 106. 107. 108. 109. 5-7 110. ---- 111. The decimal point in the field width causes leading zeroes to be displayed before numbers. 112. This is discussed in more detail in Chapter 16. In this case, it causes a leading zero to 113. appear if the number of cents entered is less than 10 (for example, $29.05). 114. 115. 116. 117. 5-9 118. ---- 119. // Program 5.2 120. 121. /* Program to calculate the 200th triangular number 122. Introduction of the for statement */ 123. 124. #include 126. int main (void) 127. { 128. int n, triangularNumber; 129. 130. triangularNumber = 0; 131. n = 1; 132. 133. while ( n <= 200 ) { 134. triangularNumber = triangularNumber + n; 135. n = n + 1; 136. } 137. 138. printf (\"The 200th triangular number is %i\\nriangularNumber); 139. 140. return 0; 141. } 142. 143. // --------------------------------------------------- 144. // Program 5.3 145. 146. // Program to generate a table of triangular numbers 147. 148. #include 150. int main (void) 151. { 152. int n, triangularNumber; 153. 154. printf (\"TABLE OF TRIANGULAR NUMBERS\\n\\n\"); 155. printf (\" n Sum from 1 to n\\n\"); 156. printf (\"--- ---------------\\n\"); 157. 158. triangularNumber = 0; 159. n = 1; 160. 161. while ( n <= 10 ) { 162. triangularNumber += n; 163. printf (\" %i %i\\n\1. ++n; 165. } 166. 167. return 0; 168. } 169. 170. 171. // --------------------------------------------------- 172. // Program 5.4 173. 174. #include 176. int main (void) 177. { 178. int n, number, triangularNumber; 179. 180. printf (\"What triangular number do you want? \"); 181. scanf (\"%i\182. 183. triangularNumber = 0; 184. n = 1; 185. 186. while ( n <= number ) { 187. triangularNumber += n; 188. ++n; 1. } 190. 191. printf (\"Triangular number %i is %i\\nriangularNumber); 192. 193. return 0; 194. } 195. 196. 197. // --------------------------------------------------- 198. // Program 5.5 199. 200. #include 202. int main (void) 203. { 204. int n, number, triangularNumber, counter; 205. 206. counter = 1; 207. 208. while ( counter <= 5 ) { 209. printf (\"What triangular number do you want? \"); 210. scanf (\"%i\211. 212. triangularNumber = 0; 213. n = 1; 214. 215. while ( n <= number ) { 216. triangularNumber += n; 217. ++n; 218. } 219. 220. printf (\"Triangular number %i is %i\\n\\nriangularNumber); 221. 222. ++counter; 223. } 224. 225. return 0; 226. } 227. 228. 229. 230. 5-11 231. ------ 232. // Program to sum the digits in a number 233. 234. #include 236. int main (void) 237. { 238. int number, right_digit, sum = 0; 239. 240. printf (\"Enter your number: \"); 241. scanf (\"%i\242. 243. while ( number != 0 ) { 244. right_digit = number % 10; 245. sum += right_digit; 246. number /= 10; 247. } 248. 249. printf (\"The sum of the digits is %i\\n\250. 251. return 0; 252. } 253. 254. 255. 256. Chapter 6 257. ------------- 258. 259. 6-3 260. ---- 261. #include 263. int main (void) 2. { 265. int n1, n2; 266. 267. printf (\"Please enter two integers: \"); 268. scanf (\"%i%i\269. 270. if (n2 == 0) 271. printf (\"Division by zero.\\n\"); 272. else 273. printf (\"Result of %i / %i is %.3f\\n\n2); 274. 275. return 0; 276. } 277. 278. 279. 280. 6-5 281. ---- 282. A negative number entered for Program 5.9 causes each digit to print out as a negative number. 283. For example, if -123 is entered, the output would look like this: 284. -3-2-1 285. Here is a corrected version of that program. 286. 287. // Program to reverse the digits of a number (revised) 288. 2. #include 292. int main (void) 293. { 294. int number, right_digit; 295. bool isNegative = false; 296. 297. printf (\"Enter your number.\\n\"); 298. scanf (\"%i\ 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. /* if keyed-in number is negative, make it positive, but remember it was negative */ if ( number < 0 ) { number = -number; isNegative = true; } do { right_digit = number % 10; printf (\"%i\number = number / 10; } while ( number != 0 ); if (isNegative == true) printf (\"-\"); printf (\"\\n\"); return 0; } 6-7 ---- // Program to generate a table of prime numbers (revised) #include int main (void) { int p, d; bool isPrime; // we start off knowing 2 is prime printf (\"2 \"); // now start testing odd numbers from 3 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 3. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382. 383. 384. 385. 386. for ( p = 3; p <= 50; p +=2 ) { isPrime = true; // only test odd divisors for ( d = 3; d < p && isPrime == true; d += 2 ) if ( p % d == 0 ) isPrime = false; if ( isPrime != false ) printf (\"%i \} printf (\"\\n\"); return 0; } Chapter 7 ------------ 7-3 ---- // Modified Program 7.2 -- uses break statement #include int main (void) { int ratingCounters[11], i, response; for ( i = 1; i <= 10; ++i ) ratingCounters[i] = 0; printf (\"Enter your responses\\n\"); printf (\"When you are done, enter 999\\n\"); while (1) { scanf (\"%i\ if (response == 999) 387. break; 388. 3. if ( response < 1 || response > 10 ) 390. printf (\"Bad response: %i\\n\391. else 392. ++ratingCounters[response]; 393. } 394. 395. printf (\"\\n\\nRating Number of Responses\\n\"); 396. printf (\"------ -------------------\\n\"); 397. 398. for ( i = 1; i <= 10; ++i ) 399. printf (\"%4i%14i\\n\400. 401. return 0; 402. } 403. 404. Some people prefer not to use the break statement since it disrupts the normal sequential 405. flow of a program's execution. You can rewrite this program to not use the break statement 406. by replacing the while in the above program with the following equivalent code: 407. 408. do 409. { 410. scanf (\"%i\411. 412. if (response != 999) 413. if ( response < 1 || response > 10 ) 414. printf (\"Bad response: %i\\n\415. else 416. ++rating_counters[response]; 417. } 418. while ( response != 999 ); 419. 420. 421. 7-5 422. ---- 423. Each array element is calculated as the sum of the preceding array elements. This produces 424. the following output: 425. 426. 1 1 2 4 8 16 32 128 256 427. 428. 429. 430. 7-7 431. ---- 432. // Prime numbers generated with Sieve of Erasthothenes 433. 434. int main (void) 435. { 436. int P[151], i, j; 437. int n = 150; 438. 439. for (i = 2; i <= n; ++i) 440. P[i] = 0; 441. 442. i = 2; 443. 444. while (i <= n) { 445. if (P[i] == 0) 446. printf (\"%i \447. 448. j = 1; 449. 450. while (i * j <= n) { 451. P[i * j] = 1; 452. ++j; 453. } 454. 455. ++i; 456. } 457. 458. return 0; 459. } 460. 461. 462. Chapter 8 463. ------------- 4. 465. 466. 8-3 467. ---- 468. Here is a modified square_root function, a sample main routine that calculates the square 469. root of 3 with different values of epsilon, and the output from these calls. 470. 471. // Function to compute the square root of a number 472. 473. #include 475. float squareRoot (float x, float epsilon) 476. { 477. float guess = 1.0; 478. 479. while ( absoluteValue (guess * guess - x) >= epsilon ) 480. guess = ( x / guess + guess ) / 2.0; 481. 482. return guess; 483. } 484. 485. int main (void) 486. { 487. printf (\"%f\\n\488. printf (\"%f\\n\4. printf (\"%f\\n\490. printf (\"%f\\n\491. 492. return 0; 493. } 494. 495. 1.750000 496. 1.732143 497. 1.732143 498. 1.732051 499. 500. 501. 8-5 502. ---- 503. float square_root (float x) 504. { 505. float guess = 1.0; 506. float epsilon = .00001; 507. 508. while ( absoluteValue((guess * guess) / x - 1.0) >= epsilon ) 509. guess = ( x / guess + guess ) / 2.0; 510. 511. return guess; 512. } 513. 514. 515. 516. 517. 518. 8-7 ---- long int x_to_the_n (int x, int n) 519. { 520. long int result = 1; 521. 522. while (n > 0) 523. { 524. result *= x; 525. --n; 526. } 527. 528. return result; 529. } 530. 531. 532. 533. 8-9 534. ---- 535. int lcm (int u, int v) 536. { 537. int (int u, int v); 538. 539. if ( u < 0 || v < 0 ) 540. return 0; 541. else 542. return u * v / (u, v); 543. } 544. 545. 546. 8-11 547. ------ 548. long int array_sum (int values [], int n) 549. { 550. int i; 551. long int sum = 0; 552. 553. for ( i = 0; i < n; ++i ) 554. sum += values[i]; 555. 556. return sum; 557. 558. 559. 560. 561. 562. 563. 5. 565. 566. 567. 568. 569. 570. 571. 572. 573. 574. 575. 576. 577. 578. 579. 580. 581. 582. 583. 584. 585. 586. 587. 588. 5. 590. 591. 592. 593. 594. 595. 596. 597. 598. 599. 600. } 8-13 ------ // Sort an array of integers // descending (order == -1) or ascending (order == 1) sort #include void sort (int a[], int n, int order) { int i, j, temp; for ( i = 0; i < n - 1; ++i ) for ( j = i + 1; j < n; ++j ) if ( (order == -1 && a[i] < a[j]) || (order == 1 && a[i] > a[j]) ) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } int main (void) { int i; int array[16] = { 34, -5, 6, 0, 12, 100, 56, 22, 44, -3, -9, 12, 17, 22, 6, 11 }; void sort (int a[], int n, int order); printf (\"The array before the sort:\\n\"); for ( i = 0; i < 16; ++i ) printf (\"%i \ printf (\"\\n\\nThe array in ascending order:\\n\"); sort (array, 16, 1); for ( i = 0; i < 16; ++i ) printf (\"%i \ 601. 602. printf (\"\\n\\nThe array in descending order:\\n\"); 603. 604. sort (array, 16, -1); 605. 606. for ( i = 0; i < 16; ++i ) 607. printf (\"%i \608. 609. printf (\"\\n\"); 610. return 0; 611. } 612. 613. 614. 615. 8-15 616. ------ 617. Here is a modified getNumberAndBase routine. The rest of the program remains unchanged. 618. 619. void getNumberAndBase (void) 620. { 621. printf (\"Number to be converted? \"); 622. scanf (\"%li\623. 624. do { 625. printf (\"Enter base value between 2 and 16: \"); 626. scanf (\"%i\627. } 628. while ( base < 2 || base > 16 ); 629. } 630. 631. 632. 633. Chapter 9 634. ------------ 635. 636. 637. 9-3 638. ---- 639. #include 1. struct time 2. { 3. int hour; 4. 5. 6. 7. 8. 9. 650. 651. 652. 653. 654. 655. 656. 657. 658. 659. 660. 661. 662. 663. 6. 665. 666. 667. 668. 669. 670. 671. 672. 673. 674. 675. 676. 677. 678. 679. 680. 681. 682. 683. 684. 685. 686. 687. int minutes; int seconds; }; // calculate elapsed time (assume t2 is later than t1) struct time elapsed_time (struct time t1, struct time t2) { struct time result = { 0, 0, 0 }; // first subtract the seconds result.seconds = t2.seconds - t1.seconds; // if seconds < 0, need to borrow one minute if (result.seconds < 0) { result.seconds += 60; --t2.minutes; } // now subtract the minutes result.minutes = t2.minutes - t1.minutes; // if minutes < 0, need to borrow one hour if (result.minutes < 0) { result.minutes += 60; --t2.hour; } // now subtract the hours result.hour = t2.hour - t1.hour; // if hour < 0, need to borrow one day (crossed midnight) if (result.hour < 0) result.hour += 24; return result; 688. 6. 690. 691. 692. 693. 694. 695. 696. 697. 698. 699. 700. 701. 702. 703. 704. 705. 706. 707. 708. 709. 710. 711. 712. 713. 714. 715. 716. 717. 718. 719. 720. 721. 722. 723. 724. 725. 726. 727. 728. 729. 730. 731. } int main (void) { struct time elapsed_time (struct time t1, struct time t2); struct time t1 = { 3, 45, 15 }, t2 = { 9, 44, 03 }, t3 = {22, 50, 59 }, t4 = { 7, 30, 0 }; struct time result; result = elapsed_time (t1, t2); printf (\"Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i \" \"is %.2i:%.2i:%.2i\\n\ t1.hour, t1.minutes, t1.seconds, t2.hour, t2.minutes, t2.seconds, result.hour, result.minutes, result.seconds); result = elapsed_time (t2, t1); printf (\"Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i \" \"is %.2i:%.2i:%.2i\\n\ t2.hour, t2.minutes, t2.seconds, t1.hour, t1.minutes, t1.seconds, result.hour, result.minutes, result.seconds); result = elapsed_time (t3, t4); printf (\"Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i \" \"is %.2i:%.2i:%.2i\\n\ t3.hour, t3.minutes, t3.seconds, t4.hour, t4.minutes, t4.seconds, result.hour, result.minutes, result.seconds); return 0; } Here is some sample output from the program: Time between 03:45:15 and 09:44:03 is 05:58:48 Time between 09:44:03 and 03:45:15 is 18:01:12 Time between 22:50:59 and 07:30:00 is 08:39:01 9-5 ---- struct dateAndTime clockKeeper (struct dateAndTime dt) { struct time timeUpdate (struct time now); struct date dateUpdate (struct date today); 732. 733. 734. 735. 736. 737. 738. 739. 740. 741. 742. 743. 744. 745. 746. 747. 748. 749. 750. 751. 752. 753. 754. 755. 756. 757. 758. 759. 760. 761. 762. 763. 7. 765. 766. 767. 768. 769. 770. 771. 772. 773. 774. 775. dt.stime = timeUpdate (dt.stime); // see if we have to go to the next day if ( dt.stime.hour == 0 && dt.stime.minutes == 0 && dt.stime.seconds == 0 ) dt.sdate = dateUpdate (dt.sdate); return dt; } // Here is a sample main routine and output: int main (void) { struct dateAndTime dt1 = { { 12, 31, 2004 }, { 23, 59, 59 } }; struct dateAndTime dt2 = { { 2, 28, 2008 }, { 23, 59, 58 } }; printf (\"Current date and time is %.2i/%.2i/%.2i \" \"%.2i:%.2i:%.2i\\n\ dt1.sdate.month, dt1.sdate.day, dt1.sdate.year, dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds); dt1 = clockKeeper (dt1); printf (\"Updated date and time is %.2i/%.2i/%.2i \" \"%.2i:%.2i:%.2i\\n\\n\ dt1.sdate.month, dt1.sdate.day, dt1.sdate.year, dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds); printf (\"Current date and time is %.2i/%.2i/%.2i \" \"%.2i:%.2i:%.2i\\n\ dt2.sdate.month, dt2.sdate.day, dt2.sdate.year, dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds); dt2 = clockKeeper (dt2); printf (\"Updated date and time is %.2i/%.2i/%.2i \" \"%.2i:%.2i:%.2i\\n\\n\ dt2.sdate.month, dt2.sdate.day, dt2.sdate.year, dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds); 776. printf (\"Current date and time is %.2i/%.2i/%.2i \" 777. \"%.2i:%.2i:%.2i\\n\ 778. dt2.sdate.month, dt2.sdate.day, dt2.sdate.year, 779. dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds); 780. 781. dt2 = clockKeeper (dt2); 782. 783. printf (\"Updated date and time is %.2i/%.2i/%.2i \" 784. \"%.2i:%.2i:%.2i\\n\\n\ 785. dt2.sdate.month, dt2.sdate.day, dt2.sdate.year, 786. dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds); 787. 788. return 0; 7. } 790. 791. 792. Current date and time is 12/31/2004 23:59:59 793. Updated date and time is 01/01/2005 00:00:00 794. 795. Current date and time is 02/28/2008 23:59:58 796. Updated date and time is 02/28/2008 23:59:59 797. 798. Current date and time is 02/28/2008 23:59:59 799. Updated date and time is 02/29/2008 00:00:00 800. 801. 802. Chapter 10 803. -------------- 804. 805. 806. 10-3 807. ------ 808. There are many different ways to solve this problem. Here, I replaced the alphabetic 809. function with a function called wordchar that considers not only alphabetic characters but 810. also apostrophes as part of a word. We also added a function called numchar that considers 811. digits, decimal points, commas, and dashes as part of a number. These definitions aren't 812. perfect, but they do work reasonably well without making the program overly complex. 813. 814. // Function to determine if a character is part of a word 815. 816. bool wordchar (const char c) 817. { 818. if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '\\'' ) 819. return true; 820. else 821. return false; 822. } 823. 824. // Function to determine if a character is part of a number 825. 826. bool numchar (const char c) 827. { 828. 829. if ( (c >= '0' && c <= '9') || c == '.' || c == ',' || c == '-' ) 830. return true; 831. else 832. return false; 833. } 834. 835. // Function to count the number of words in a string 836. 837. int countWords (const char string[]) 838. { 839. int i, wordCount = 0; 840. bool lookingForWord = true, wordchar (const char c), numchar (const char c); 841. 842. for ( i = 0; string[i] != '\\0'; ++i ) 843. if ( wordchar (string[i]) || numchar (string[i]) ) { 844. if ( lookingForWord ) { 845. ++wordCount; 846. lookingForWord = false; 847. } 848. } 849. else 850. lookingForWord = true; 851. 852. return wordCount; 853. } 854. 855. // Here's a sample main routine and associated output: 856. 857. int main (void) 858. { 859. char text1[] = \"The sum of $552,227 and $-1,204.50 is $551,002.50\"; 860. char text2[] = \"It isn't that I don't understand you.\"; 861. int countWords (const char string[]); 862. 863. printf (\"%s -- words = %i\\n\8. printf (\"%s -- words = %i\\n\865. } 866. 867. The sum of $552,227 and $-1,204.50 is $551,002.50 -- words = 8 868. It isn't that I don't understand you. -- words = 7 869. 870. 871. 872. 10-5 873. ------ 874. // find s1 inside source, return index number if found, -1 if not found 875. 876. int findString (const char source[], const char s[]) 877. { 878. int i, j, foundit = false; 879. 880. // try each character in source 881. 882. for ( i = 0; source[i] != '\\0' && !foundit; ++i ) { 883. foundit = true; 884. 885. // now see if corresponding chars from s match 886. 887. for ( j = 0; s[j] != '\\0' && foundit; ++j ) 888. if ( source[j + i] != s[j] || source[j + i] == '\\0' ) 8. foundit = false; 0. 1. if (foundit) 2. return i; 3. } 4. 5. return -1; 6. } 7. 8. 9. 900. 901. 902. 903. 904. 905. 906. 907. 908. 909. 910. 911. 912. 913. 914. 915. 916. 917. 918. 919. 920. 921. 922. 923. 924. 925. 926. 927. 928. 929. 930. 931. 932. 933. 934. 935. 936. 937. 938. 939. 940. 10-7 ----- /* insert string s into string source starting at i This function uses the stringLength function defined in the chapter. Note: this function assumes source is big enough to store the inserted string (dangerous!) */ void insertString (char source[], char s[], int i) { int j, lenS, lenSource; /* first, find out how big the two strings are */ lenSource = stringLength (source); lenS = stringLength (s); /* sanity check here -- note that i == lenSource effectively concatenates s onto the end of source */ if (i > lenSource) return; /* now we have to move the characters in source down from the insertion point to make room for s. Note that we copy the string starting from the end to avoid overwriting characters in source. We also copy the terminating null (j starts at lenS) as well since the final result must be null-terminated */ for ( j = lenSource; j >= i; --j ) source [lenS + j] = source [j]; /* we've made room, now copy s into source at the insertion point */ for ( j = 0; j < lenS; ++j ) source [j + i] = s[j]; } 941. 942. 943. 944. 945. 946. 947. 948. 949. 950. 951. 952. 953. 954. 955. 956. 957. 958. 959. 960. 961. 962. 963. 9. 965. 966. 967. 968. 969. 970. 971. 972. 973. 974. 975. 976. 977. 978. 979. 980. 981. 982. 983. 984. 10-9 ------ bool replaceString (char source [], char s1[], char s2[]) { int index; // first locate s1 inside the source index = findString (source, s1); if ( index == -1 ) return false; // now delete s1 from the source removeString (source, index, stringLength (s1)); // now insert the new string insertString (source, s2, index); return true; } 10-11 ------- #include int strToInt (const char string[]) { int i = 0, intValue, result = 0; int negative = false; // test for leading minus sign if ( string[0] == '-') { negative = true; i = 1; } 985. while ( string[i] >= '0' && string[i] <= '9' ) { 986. intValue = string[i] - '0'; 987. result = result * 10 + intValue; 988. ++i; 9. } 990. 991. if ( negative ) 992. result = -result; 993. 994. return result; 995. } 996. 997. 998. 999. 10-13 1000. -------- 1001. void uppercase ( char str[] ) 1002. { 1003. int i; 1004. 1005. for ( i = 0; str[i] != '\\0'; ++i ) 1006. if ( str[i] >= 'a' && str[i] <= 'z' ) 1007. str[i] = str[i] - 'a' + 'A'; 1008. } 1009. 1010. 1011. 1012. Chapter 11 1013. -------------- 1014. 1015. 11-3 1016. ------ 1017. You can solve this problem by setting up a \"dummy\" structure variable called listHead, 1018. for example: 1019. struct entry listHead; 1020. 1021. and you can then set it pointing to the head of the list by assigning the next member of 1022. listHead to point to the actual first entry of the list: 1023. 1024. listHead.next = &entry1; 1025. 1026. Now to insert a new entry called newEntry at the front of the list, you can write: 1027. 1028. insertEntry (&new_entry, &list_head); 1029. 1030. 1031. 1032. 11-5 1033. ------ 1034. struct dentry 1035. { 1036. int value; 1037. struct dentry *next; 1038. struct dentry *prev; 1039. }; 1040. 1041. int main (void) 1042. { 1043. struct dentry n1, n2, n3, *lptr; 1044. int i; 1045. 1046. n1.value = 100; 1047. n2.value = 200; 1048. n3.value = 300; 1049. 1050. n1.next = &n2; 1051. n2.next = &n3; 1052. n3.next = (struct dentry *) 0; 1053. 1054. n1.prev = (struct dentry *) 0; 1055. n2.prev = &n1; 1056. n3.prev = &n2; 1057. 1058. // forward search through list 1059. 1060. lptr = &n1; 1061. 1062. while ( lptr != 0 ) { 1063. 10. printf (\"%i \1065. lptr = lptr->next; 1066. } 1067. 1068. printf (\"\\n\"); 1069. 1070. 1071. 1072. 1073. 1074. 1075. 1076. 1077. 1078. 1079. 1080. 1081. 1082. 1083. 1084. 1085. 1086. 1087. 1088. 10. 1090. 1091. 1092. 1093. 1094. 1095. 1096. 1097. 1098. 1099. 1100. 1101. 1102. 1103. 1104. 1105. 1106. 1107. 1108. 1109. 1110. 1111. 1112. 1113. // backward search through list lptr = &n3; while ( lptr != 0 ) { printf (\"%i \lptr = lptr->prev; } printf (\"\\n\"); return 0; } Here's the output from the test program: 100 200 300 300 200 100 11-7 ------ /* Sort an array of integers into ascending order (pointer version ) */ void sort (int *a, int n) { int *aptr1, *aptr2, temp; for ( aptr1 = a; aptr1 < a + n - 1; ++aptr1 ) for ( aptr2 = aptr1 + 1; aptr2 < a + n; ++aptr2 ) if ( *aptr1 > *aptr2 ) { temp = *aptr1; *aptr1 = *aptr2; *aptr2 = temp; } } 11-9 ------ /* Function to read a line of text from the terminal (pointer version) */ 1114. 1115. 1116. 1117. 1118. 1119. 1120. 1121. 1122. 1123. 1124. 1125. 1126. 1127. 1128. 1129. 1130. 1131. 1132. 1133. 1134. 1135. 1136. 1137. 1138. 1139. 1140. 1141. 1142. 1143. 1144. 1145. 1146. 1147. 1148. 1149. 1150. 1151. 1152. 1153. 1154. 1155. 1156. 1157. void readLine (char *buffer) { char character; do { character = getchar (); *buffer++ = character; } while ( character != '\\n' ); *(buffer - 1) = '\\0'; } 11-11 ------- /* Function to calculate tomorrow's date (pointer version) */ void dateUpdate (struct date *today) { int numberOfDays (struct date d); if ( today->day != numberOfDays (*today) ) ++today->day; else if ( today->month == 12 ) { /* end of year */ today->day = 1; today->month = 1; ++today->year; } else { /* end of month */ today->day = 1; ++today->month; } } Chapter 12 -------------- 1158. 1159. 1160. 1161. 1162. 1163. 11. 1165. 1166. 1167. 1168. 1169. 1170. 1171. 1172. 1173. 1174. 1175. 1176. 1177. 1178. 1179. 1180. 1181. 1182. 1183. 1184. 1185. 1186. 1187. 1188. 11. 1190. 1191. 1192. 1193. 1194. 1195. 1196. 1197. 1198. 1199. 1200. 1201. 12-3 ------ int int_size (void) { unsigned int bits; int size = 0; bits = ~0; while ( bits ) { ++size; bits >>= 1; } return size; } 12-5 ------ /* test bit n in word to see if it is on assumes words are 32 bits long */ int bit_test (unsigned int word, int n) { if ( n < 0 || n > 31 ) return 0; if ( (word >> (31 - n)) & 0x1 ) return 1; else return 0; } unsigned int bit_set (unsigned int word, int n) { if ( n < 0 || n > 31 ) return 0; return word | (1 << (31 - n)); } 1202. 1203. 12-7 1204. ------ 1205. // extract count bits from value beginning at bit n 1206. 1207. unsigned int bitpat_get (unsigned int value, int n, int count) 1208. { 1209. int word_size, i; 1210. 1211. word_size = int_size (); // From exercise 3 1212. 1213. if ( n < 0 || n > word_size || count < 0 || count + n > word_size ) 1214. return 0; 1215. 1216. // first shift value to the leftmost part of the word 1217. 1218. value <<= n; 1219. 1220. // now when we shift right, the bits to the left of value will be cleared 1221. 1222. return value >> word_size - count; 1223. } 1224. 1225. 1226. 1227. Chapter 13 1228. -------------- 1229. 1230. 1231. 13-3 1232. ------ 1233. #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 1234. 1235. 1236. 13-5 1237. ------ 1238. #define SHIFT(value, n) (((n) > 0) ? ((value) << (n)) \\ 1239. : ((value) >> -(n))) 1240. 1241. 1242. 1243. 13-7 1244. ------ 1245. #define IS_ALPHABETIC(c) (IS_LOWER_CASE (c) || IS_UPPERCASE (c)) 1246. 1247. 1248. 13-9 1249. ------ 1250. #define ABSOLUTE_VALUE(x) (((x) < 0) ? -(x) : (x)) 1251. 1252. 1253. Chapter 14 1254. -------------- 1255. 1256. 1257. 14-1 1258. ------ 1259. typedef int (*FnPtr) (void); 1260. 1261. 1262. 14-3 1263. ------ 12. Expression Type Value 1265. ------------------------------------ 1266. f + i float 101 1267. l / d double 33.3333 1268. i / l + f float 1.0 1269. l * i long 50000 1270. f / 2 float 0.5 1271. i / (d + f) double 6.25 1272. l / (i * 2.0) double 2.5 1273. l + i / (double) l double 501.0 1274. 1275. 1276. Chapter 16 1277. -------------- 1278. 1279. 16-3 1280. ------- 1281. // Program to copy one file to another 1282. 1283. #include 1285. /* convert lowercase to uppercase character */ 1286. 1287. #define TO_UPPER(c) ((c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c) 1288. 12. int main (void) 1290. { 1291. char inName[], outName[]; 1292. FILE *in, *out; 1293. int c; 1294. 1295. printf (\"Enter name of file to be copied: \"); 1296. scanf (\"%63s\ 1297. printf (\"Enter name of output file: \"); 1298. scanf (\"%63s\1299. 1300. if ( (in = (FILE *) fopen (inName, \"r\")) == NULL ) { 1301. fprintf (stderr, \"Can't open %s for reading.\\n\1302. return 1; 1303. } 1304. else if ( (out = fopen (outName, \"w\")) == NULL ) { 1305. fprintf (stderr, \"Can't open %s for writing.\\n\1306. return 2; 1307. } 1308. 1309. while ( (c = getc (in)) != EOF ) 1310. putc (TO_UPPER (c), out); 1311. 1312. printf (\"File has been copied.\\n\"); 1313. 1314. return 0; 1315. } 1316. 1317. 1318. 1319. 16-5 1320. ------ 1321. /* Program to extract columns from each line of a file 1322. (similar to the UNIX cut command) */ 1323. 1324. #include 1326. int main (void) 1327. { 1328. 1329. char inName[]; 1330. FILE *in; 1331. 1332. 1333. 1334. 1335. 1336. 1337. 1338. 1339. int m, n, curcol, c; printf (\"Enter name of file: \"); scanf (\"%63s\ printf (\"Enter starting and ending column numbers: \"); scanf (\"%i%i\ if ( (in = fopen (inName, \"r\")) == NULL ) { fprintf (stderr, \"Can't open %s for reading.\\n\1340. return 1; 1341. } 1342. else { 1343. curcol = 1; 1344. 1345. while ( (c = getc (in)) != EOF ) { 1346. if ( c == '\\n' ) { 1347. putchar ('\\n'); 1348. curcol = 0; 1349. } 1350. else if ( curcol >= m && curcol <= n ) 1351. putchar (c); 1352. 1353. ++curcol; 1354. } 1355. } 1356. }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 99spj.com 版权所有 湘ICP备2022005869号-5
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务