├── .gitignore ├── README.md ├── c_primer_plus_note.md ├── ch10 ├── examples │ ├── 10_10_sum_arr1.c │ ├── 10_11_sum_arr2.c │ ├── 10_12_order.c │ ├── 10_13_ptr_ops.c │ ├── 10_14_arf.c │ ├── 10_15_zippo1.c │ ├── 10_16_zippo2.c │ ├── 10_17_array2d.c │ ├── 10_18_vararr2d.c │ ├── 10_19_flc.c │ ├── 10_1_day_mon1.c │ ├── 10_2_no_data.c │ ├── 10_3_somedata.c │ ├── 10_4_day_mon2.c │ ├── 10_5_designate.c │ ├── 10_6_bounds.c │ ├── 10_7_rain.c │ ├── 10_8_pnt_add.c │ └── 10_9_day_mon3.c └── exercises │ ├── 10_10_doubled_array.c │ ├── 10_11_rain.c │ ├── 10_12_number_set.c │ ├── 10_1_rain.c │ ├── 10_2_copy_array_pointer.c │ ├── 10_3_int_array_max.c │ ├── 10_4_double_max_index.c │ ├── 10_5_difference_of_max_min.c │ ├── 10_6_copy_two_dim_array.c │ ├── 10_7_special_copy.c │ ├── 10_8_copy_variable_array.c │ └── 10_9_array_add.c ├── ch11 ├── examples │ ├── 11_12_put_put.c │ ├── 11_13_test.c │ ├── 11_14_str_cat.c │ ├── 11_15_join_chk.c │ ├── 11_16_nogo.c │ ├── 11_17_compare.c │ ├── 11_18_compback.c │ ├── 11_19_quit_chk.c │ ├── 11_1_strings.c │ ├── 11_20_starsrch.c │ ├── 11_21_copy1.c │ ├── 11_22_copy2.c │ ├── 11_23_copy3.c │ ├── 11_24_format.c │ ├── 11_25_sort_str.c │ ├── 11_26_mod_str.c │ ├── 11_27_repeat.c │ ├── 11_28_hello.c │ ├── 11_29_strcnvt.c │ ├── 11_2_quotes.c │ ├── 11_3_p_and_s.c │ ├── 11_4_name1.c │ ├── 11_5_name2.c │ ├── 11_6_name3.c │ ├── 11_7_scan_str.c │ ├── 11_8_put_out.c │ └── 11_9_nono.c └── exercises │ ├── 11_10_display.c │ ├── 11_11_statistics.c │ ├── 11_12_reverse_string_by_words.c │ ├── 11_13_power.c │ ├── 11_14_atoi.c │ ├── 11_15_print_input.c │ ├── 11_1_read_n_char.c │ ├── 11_2_read_n_char.c │ ├── 11_3_read_first_word.c │ ├── 11_4_strchr.c │ ├── 11_5_is_within.c │ ├── 11_6_strncpy.c │ ├── 11_7_string_in.c │ ├── 11_8_reverse_string.c │ └── 11_9_delete_blank.c ├── ch12 ├── examples │ ├── 12_10_r_drive1.c │ ├── 12_11_diceroll.c │ ├── 12_12_diceroll.h │ ├── 12_13_manydice.c │ ├── 12_14_dyn_arr.c │ ├── 12_1_hiding.c │ ├── 12_2_forc99.c │ ├── 12_3_loc_stat.c │ ├── 12_4_global.c │ ├── 12_5_parta.c │ ├── 12_6_partb.c │ ├── 12_7_rand0.c │ ├── 12_8_r_drive0.c │ └── 12_9_s_and_r.c └── exercises │ ├── 12_1_global.c │ ├── 12_2_pe │ ├── 12_2_pe_a.c │ ├── 12_2_pe_a.h │ └── 12_2_pe_b.c │ ├── 12_3_pe │ ├── 12_3_pe_a.c │ ├── 12_3_pe_a.h │ └── 12_3_pe_b.c │ ├── 12_4_func_call_times.c │ ├── 12_5_sort_random_numbers.c │ ├── 12_6_count_frequency_of_random_numbers.c │ ├── 12_7_diceroll │ ├── diceroll.c │ ├── diceroll.h │ └── manydice.c │ └── 12_8_array.c ├── ch13 ├── examples │ ├── 13_1_count.c │ ├── 13_2_reducto.c │ ├── 13_3_addaword.c │ ├── 13_4_parrot.c │ ├── 13_5_reverse.c │ ├── 13_6_append.c │ └── 13_7_randbin.c └── exercises │ ├── 13_10_read_from_file.c │ ├── 13_11_search_substr.c │ ├── 13_12_digit_to_img.c │ ├── 13_13_denoising.c │ ├── 13_1_count.c │ ├── 13_2_file_copy.c │ ├── 13_3_file_toupper.c │ ├── 13_4_display_files.c │ ├── 13_5_append.c │ ├── 13_6_reducto.c │ ├── 13_7_cross_print.c │ ├── 13_7_cross_print_with_line_numbers.c │ ├── 13_8_count_character_occurences.c │ └── 13_9_addaword.c ├── ch2 ├── examples │ ├── 2_1_first.c │ ├── 2_2_fathm_ft.c │ └── 2_3_two_func.c └── exercises │ ├── 2_1_print_name.c │ ├── 2_2_print_name_address.c │ ├── 2_3_convert_age_to_day.c │ ├── 2_4_print_prise_statement.c │ ├── 2_5_compute_sum_square.c │ ├── 2_6_print_string_dec.c │ └── 2_7_print_number.c ├── ch3 ├── examples │ ├── 3_10_escape.c │ ├── 3_1_rhodium.c │ ├── 3_2_print1.c │ ├── 3_3_bases.c │ ├── 3_4_print2.c │ ├── 3_4_toobig.c │ ├── 3_5_charcode.c │ ├── 3_6_altnames.c │ ├── 3_7_floaterr.c │ ├── 3_7_showr_pt.c │ ├── 3_8_typesize.c │ └── 3_9_badcount.c └── exercises │ ├── 3_1_test_overflow.c │ ├── 3_2_ascii_to_char.c │ ├── 3_3_alarm_and_print.c │ ├── 3_4_print_float.c │ ├── 3_5_age_to_seconds.c │ ├── 3_6_quarts_to_molecules.c │ └── 3_7_inch_and_centimeter.c ├── ch4 ├── examples │ ├── 4_10_strings.c │ ├── 4_11_intconv.c │ ├── 4_12_floatcnv.c │ ├── 4_13_prntval.c │ ├── 4_14_longstrg.c │ ├── 4_15_input.c │ ├── 4_16_varwid.c │ ├── 4_17_skip2.c │ ├── 4_1_talkback.c │ ├── 4_2_praise1.c │ ├── 4_3_praise2.c │ ├── 4_4_pizza.c │ ├── 4_5_defines.c │ ├── 4_6_printout.c │ ├── 4_7_width.c │ ├── 4_8_floats.c │ └── 4_9_flags.c └── exercises │ ├── 4_1_print_name.c │ ├── 4_2_print_name.c │ ├── 4_3_print_float.c │ ├── 4_4_print_height.c │ ├── 4_5_print_name.c │ ├── 4_6_test_float_precision.c │ └── 4_7_compute_miles_per_gallon.c ├── ch5 ├── examples │ ├── 5_10_add_one.c │ ├── 5_11_post_pre.c │ ├── 5_12_bottles.c │ ├── 5_13_addemup.c │ ├── 5_14_convert.c │ ├── 5_15_pound.c │ ├── 5_16_running.c │ ├── 5_1_shoes1.c │ ├── 5_2_shoes2.c │ ├── 5_3_golf.c │ ├── 5_4_squares.c │ ├── 5_5_wheat.c │ ├── 5_6_divide.c │ ├── 5_7_rules.c │ ├── 5_8_sizeof.c │ └── 5_9_min_sec.c └── exercises │ ├── 5_1_minutes_to_hour.c │ ├── 5_2_print_num.c │ ├── 5_3_days_to_weeks.c │ ├── 5_4_centimeter_to_feet.c │ ├── 5_5_addemup.c │ ├── 5_6_add_square.c │ ├── 5_7_print_cubic.c │ └── 5_8_fahrenheit_to_celsius.c ├── ch6 ├── examples │ ├── 6_10_sweetie1.c │ ├── 6_11_sweetie2.c │ ├── 6_12_for_cube.c │ ├── 6_13_postage.c │ ├── 6_14_zeno.c │ ├── 6_15_do_while.c │ ├── 6_16_entry.c │ ├── 6_17_rows1.c │ ├── 6_18_rows2.c │ ├── 6_19_scores_in.c │ ├── 6_1_summing.c │ ├── 6_20_power.c │ ├── 6_2_when.c │ ├── 6_3_while.c │ ├── 6_5_cmpflt.c │ ├── 6_6_t_and_f.c │ ├── 6_7_truth.c │ ├── 6_8_trouble.c │ └── 6_9_boolean.c └── exercises │ ├── 6_10_print_integer_reversed_order.c │ ├── 6_11_series_sum.c │ ├── 6_12_print_power.c │ ├── 6_13_get_double_sum.c │ ├── 6_14_print_line_string.c │ ├── 6_15_compute_interest.c │ ├── 6_16_how_many_years.c │ ├── 6_1_print_array.c │ ├── 6_2_print_triangle.c │ ├── 6_3_print_letter_triangle.c │ ├── 6_4_print_pyramid.c │ ├── 6_5_print_table.c │ ├── 6_6_print_word.c │ ├── 6_7_compute_float.c │ ├── 6_8_compute_float.c │ └── 6_9_square_sum.c ├── ch7 ├── examples │ ├── 7_10_break.c │ ├── 7_11_animals.c │ ├── 7_12_vowels.c │ ├── 7_1_colddays.c │ ├── 7_2_cypher1.c │ ├── 7_3_cypher2.c │ ├── 7_4_electric.c │ ├── 7_5_divisors.c │ ├── 7_6_chcount.c │ ├── 7_7_wordcnt.c │ ├── 7_8_paint.c │ └── 7_9_skippart.c └── exercises │ ├── 7_10_compute_tax.c │ ├── 7_12_order_grocery.c │ ├── 7_1_word_count.c │ ├── 7_2_print_char_ascii.c │ ├── 7_3_count_odd_even.c │ ├── 7_4_if_else.c │ ├── 7_5_count_odd_even.c │ ├── 7_6_count_ei.c │ ├── 7_7_salary.c │ ├── 7_8_salary.c │ └── 7_9_get_prime.c ├── ch8 ├── examples │ ├── 8_1_echo.c │ ├── 8_2_echo_eof.c │ ├── 8_3_file_eof.c │ ├── 8_4_guess.c │ ├── 8_5_showchar1.c │ ├── 8_7_checking.c │ └── 8_8_menuette.c └── exercises │ ├── 8_1_count_char.c │ ├── 8_2_print_ascii.c │ ├── 8_3_count_lower_upper.c │ ├── 8_4_word_letters_count.c │ ├── 8_5_guess_by_binary_search.c │ ├── 8_6_get_first.c │ ├── 8_7_salary.c │ └── 8_8_compute.c └── ch9 ├── examples ├── 9_12_loccheck.c ├── 9_13_swap1.c ├── 9_14_swap2.c ├── 9_15_swap3.c ├── 9_1_lethead1.c ├── 9_2_lethead2.c ├── 9_3_lesser.c ├── 9_5_proto.c ├── 9_6_recur.c ├── 9_7_factor.c ├── 9_8_binary.c └── 9_9_usehotel │ ├── hotel.c │ ├── hotel.h │ └── usehotel.c └── exercises ├── 9_10_fibonacci.c ├── 9_1_double_min.c ├── 9_2_chline.c ├── 9_3_print_char.c ├── 9_4_harmonic_mean.c ├── 9_5_larger_of.c ├── 9_6_check_alphabet.c ├── 9_7_power.c ├── 9_8_power.c └── 9_9_base_converter.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | C_Primer 2 | ======== 3 | 4 | C Primer Plus, Fifth Edition 5 | 6 | [C Primer Plus Note](c_primer_plus_note.md) 7 | -------------------------------------------------------------------------------- /ch10/examples/10_10_sum_arr1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_10_sum_arr1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 10:36:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 10 11 | 12 | int sum(int arr[], int n); 13 | 14 | int main(void) 15 | { 16 | int marbles[SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20}; 17 | long answer; 18 | 19 | answer = sum(marbles, SIZE); 20 | printf("The total number of marbles is %ld.\n", answer); 21 | printf("The size of marbles is %zd bytes.\n", sizeof(marbles)); 22 | 23 | return 0; 24 | } 25 | 26 | int sum(int arr[], int n) 27 | { 28 | int i; 29 | int total = 0; 30 | 31 | for (i = 0; i < n; i++) 32 | { 33 | total += arr[i]; 34 | } 35 | printf("The size of arr is %zd bytes.\n", sizeof(arr)); 36 | 37 | return total; 38 | } 39 | -------------------------------------------------------------------------------- /ch10/examples/10_11_sum_arr2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_11_sum_arr2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 10:41:39 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 10 11 | 12 | int sump(int * start, int * end); 13 | 14 | int main(void) 15 | { 16 | int marbles[SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20}; 17 | long answer; 18 | 19 | answer = sump(marbles, marbles + SIZE); 20 | printf("The total number of marbles is %ld.\n", answer); 21 | 22 | return 0; 23 | } 24 | 25 | int sump(int * start, int * end) 26 | { 27 | int total = 0; 28 | 29 | while (start < end) 30 | { 31 | total += *start; 32 | start++; 33 | } 34 | 35 | return total; 36 | } 37 | -------------------------------------------------------------------------------- /ch10/examples/10_12_order.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_12_order.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 10:44:23 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int data[2] = {100, 200}; 11 | int moredata[2] = {300, 400}; 12 | 13 | int main(void) 14 | { 15 | int * p1, * p2, * p3; 16 | 17 | p1 = p2 = data; 18 | p3 = moredata; 19 | printf("*p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3); 20 | printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n", *p1++, *++p2, (*p3)++); 21 | printf("*p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch10/examples/10_14_arf.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_14_arf.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 11:02:44 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 5 11 | 12 | void show_array(const double arr[], int n); 13 | void mult_array(double arr[], int n, double mult); 14 | 15 | int main(void) 16 | { 17 | double dip[SIZE] = {20.0, 17.66, 8.2, 15.3, 22.22}; 18 | 19 | printf("The original dip array:\n"); 20 | show_array(dip, SIZE); 21 | mult_array(dip, SIZE, 2.5); 22 | printf("The dip array after calling mult_array():\n"); 23 | show_array(dip, SIZE); 24 | 25 | return 0; 26 | } 27 | 28 | void show_array(const double arr[], int n) 29 | { 30 | int i; 31 | 32 | for (i = 0;i < n; i++) 33 | { 34 | printf("%8.3f ", arr[i]); 35 | } 36 | putchar('\n'); 37 | } 38 | 39 | void mult_array(double arr[], int n, double mult) 40 | { 41 | int i; 42 | 43 | for (i = 0;i < n; i++) 44 | { 45 | arr[i] *= mult; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /ch10/examples/10_15_zippo1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_15_zippo1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 11:08:05 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int zippo[4][2] = { {2, 4}, {6, 8}, {1, 3}, {5, 7} }; 13 | 14 | printf("zippo = %p, zippo + 1 = %p\n", zippo, zippo + 1); 15 | printf("zippo[0] = %p, zippo[0] + 1 = %p\n", zippo[0], zippo[0] + 1); 16 | printf("*zippo = %p, *zippo + 1 = %p\n", *zippo, *zippo + 1); 17 | printf("zippo[0][0] = %d\n", zippo[0][0]); 18 | printf("**zippo = %d\n", *zippo[0]); 19 | printf("**zippo = %d\n", **zippo); 20 | printf("zippo[2][1] = %d\n", zippo[2][1]); 21 | printf("*(*(zippo + 2) + 1) = %d\n", *(*(zippo + 2) + 1)); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch10/examples/10_16_zippo2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_16_zippo2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 11:13:33 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int zippo[4][2] = { {2, 4}, {6, 8}, {1, 3}, {5, 7} }; 13 | int (*pz)[2]; 14 | pz = zippo; 15 | 16 | printf("pz = %p, pz + 1 = %p\n", pz, pz + 1); 17 | printf("pz[0] = %p, pz[0] + 1 = %p\n", pz[0], pz[0] + 1); 18 | printf("*pz = %p, *pz + 1 = %p\n", *pz, *pz + 1); 19 | printf("pz[0][0] = %d\n", pz[0][0]); 20 | printf("**pz = %d\n", *pz[0]); 21 | printf("**pz = %d\n", **pz); 22 | printf("pz[2][1] = %d\n", pz[2][1]); 23 | printf("*(*(pz + 2) + 1) = %d\n", *(*(pz + 2) + 1)); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch10/examples/10_1_day_mon1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_1_day_mon1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:22:06 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MONTHS 12 11 | 12 | int main(void) 13 | { 14 | int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 15 | int index; 16 | 17 | for (index = 0; index < MONTHS; index++) 18 | { 19 | printf("Month %d has %2d days.\n", index + 1, days[index]); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch10/examples/10_2_no_data.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_2_no_data.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:28:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 4 11 | 12 | int main(void) 13 | { 14 | int no_data[SIZE]; 15 | int i; 16 | 17 | printf("%2s%14s\n", "i", "no_data[i] "); 18 | for (i = 0; i < SIZE; i++) 19 | { 20 | printf("%2d%14d\n", i, no_data[i]); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch10/examples/10_3_somedata.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_3_somedata.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:30:28 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 4 11 | 12 | int main(void) 13 | { 14 | int some_data[SIZE] = {1492, 1066}; 15 | int i; 16 | printf("%2s%14s\n", "i", "some_data[i]"); 17 | for (i = 0; i < SIZE; i++) 18 | { 19 | printf("%2d%14d\n", i, some_data[i]); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch10/examples/10_4_day_mon2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_4_day_mon2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:34:36 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31}; 13 | int index; 14 | 15 | for (index = 0; index < sizeof(days) / sizeof(days[0]); index++) 16 | { 17 | printf("Month %2d has %d days.\n", index + 1, days[index]); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch10/examples/10_5_designate.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_5_designate.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:38:01 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MONTHS 12 11 | 12 | int main(void) 13 | { 14 | int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29}; 15 | int i; 16 | 17 | for (i = 0; i < MONTHS; i++) 18 | { 19 | printf("%2d %d\n", i + 1, days[i]); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch10/examples/10_6_bounds.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_6_bounds.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 09:40:03 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 4 11 | 12 | int main(void) 13 | { 14 | int value1 = 44; 15 | int arr[SIZE]; 16 | int value2 = 88; 17 | int i; 18 | 19 | printf("value1 = %d, value2 = %d\n", value1, value2); 20 | for (i = -1; i <= SIZE; i++) 21 | { 22 | arr[i] = 2 * i + 1; 23 | } 24 | for (i = -1; i < 7; i++) 25 | { 26 | printf("%2d %d\n", i, arr[i]); 27 | } 28 | printf("value1 = %d, value2 = %d\n", value1, value2); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /ch10/examples/10_8_pnt_add.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_8_pnt_add.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 10:15:00 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 4 11 | 12 | int main(void) 13 | { 14 | short dates[SIZE]; 15 | short * pti; 16 | short index; 17 | double bills[SIZE]; 18 | double * ptf; 19 | 20 | pti = dates; 21 | ptf = bills; 22 | printf("%23s %10s\n", "short", "double"); 23 | for (index = 0; index < SIZE; index++) 24 | { 25 | printf("pointers + %d: %10p %10p\n", index, pti + index, ptf + index); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch10/examples/10_9_day_mon3.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_9_day_mon3.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 23 Jun 2014 10:18:39 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MONTHS 12 11 | 12 | int main(void) 13 | { 14 | int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 15 | int index; 16 | 17 | for (index = 0; index < MONTHS; index++) 18 | { 19 | printf("Month %2d has %d days.\n", index + 1, *(days + index)); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch10/exercises/10_3_int_array_max.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_3_int_array_max.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 24 Jun 2014 10:24:15 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 5 11 | 12 | int max(const int [], const int); 13 | 14 | int main(void) 15 | { 16 | const int arr[SIZE] = {1, 4, 2, 7, 3}; 17 | 18 | printf("Max: %d\n", max(arr, SIZE)); 19 | 20 | return 0; 21 | } 22 | 23 | int max(const int arr[], const int size) 24 | { 25 | int i = 0; 26 | int max_elem = 0; 27 | 28 | for (i = 0, max_elem = arr[0]; i < size; i++) 29 | { 30 | if (arr[i] > max_elem) 31 | { 32 | max_elem = arr[i]; 33 | } 34 | } 35 | 36 | return max_elem; 37 | } 38 | -------------------------------------------------------------------------------- /ch10/exercises/10_4_double_max_index.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_4_double_max_index.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: 二 7/ 1 14:58:49 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | unsigned int get_index_of_max(double [], unsigned int); 11 | 12 | int main(void) 13 | { 14 | const int SIZE = 5; 15 | double arr[SIZE] = {1.1, 2.2, 5.5, 3.3, 4.4}; 16 | 17 | printf("Max index: %u\n", get_index_of_max(arr, SIZE)); 18 | 19 | return 0; 20 | } 21 | 22 | unsigned int get_index_of_max(double arr[], unsigned int size) 23 | { 24 | unsigned int i = 0; 25 | unsigned int max = 0; 26 | 27 | for (i = 1; i < size; i++) 28 | { 29 | if (arr[i] > arr[max]) 30 | { 31 | max = i; 32 | } 33 | } 34 | 35 | return max; 36 | } 37 | -------------------------------------------------------------------------------- /ch10/exercises/10_5_difference_of_max_min.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_5_difference_of_max_min.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: 二 7/ 1 15:35:47 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double difference_of_max_and_min(double [], unsigned int); 11 | 12 | int main(void) 13 | { 14 | const unsigned int SIZE = 5; 15 | double arr[SIZE] = {1.1, 2.2, 5.5, 3.3, 0.2}; 16 | 17 | printf("Difference of max and min: %.2f\n", difference_of_max_and_min(arr, SIZE)); 18 | 19 | return 0; 20 | } 21 | 22 | double difference_of_max_and_min(double arr[], unsigned int size) 23 | { 24 | unsigned int i = 0, max = 0, min = 0; 25 | 26 | for (i = 1; i < size; i++) 27 | { 28 | if (arr[i] > arr[max]) 29 | { 30 | max = i; 31 | } 32 | else if (arr[i] < arr[min]) 33 | { 34 | min = i; 35 | } 36 | } 37 | 38 | return arr[max] - arr[min]; 39 | } 40 | -------------------------------------------------------------------------------- /ch10/exercises/10_7_special_copy.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_7_special_copy.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed Jul 2 19:03:14 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void copy_array(double [], const double [], const unsigned int); 11 | 12 | void print(const double [], const unsigned int); 13 | 14 | int main(void) 15 | { 16 | const unsigned int SIZE1 = 7; 17 | const unsigned int SIZE2 = 3; 18 | const double src[SIZE1]= {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 19 | 20 | double dest[SIZE2]; 21 | 22 | copy_array(dest, &src[2], SIZE2); 23 | print(dest, SIZE2); 24 | printf("\n"); 25 | 26 | return 0; 27 | } 28 | 29 | void copy_array(double target[], const double source[], const unsigned int size) 30 | { 31 | unsigned int i = 0; 32 | 33 | for (i = 0; i < size; i++) 34 | { 35 | target[i] = source[i]; 36 | } 37 | } 38 | 39 | void print(const double arr[], const unsigned int size) 40 | { 41 | unsigned int i = 0; 42 | for (i = 0; i < size; i++) 43 | { 44 | printf("%8.2f", arr[i]); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ch10/exercises/10_9_array_add.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 10_9_array_add.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed Jul 2 20:45:02 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void add(const double [], const double [], double [], const unsigned int); 11 | 12 | void print(const double [], const unsigned int); 13 | 14 | int main(void) 15 | { 16 | const unsigned int SIZE = 4; 17 | const double arr_a[SIZE] = {2, 4, 5, 8}; 18 | const double arr_b[SIZE] = {1, 0, 4, 6}; 19 | 20 | double result[SIZE]; 21 | 22 | add(arr_a, arr_b, result, SIZE); 23 | print(result, SIZE); 24 | 25 | return 0; 26 | } 27 | 28 | void add(const double a[], const double b[], double result[], const unsigned int size) 29 | { 30 | unsigned int i = 0; 31 | 32 | for (i = 0; i < size; i++) 33 | { 34 | result[i] = a[i] + b[i]; 35 | } 36 | } 37 | 38 | void print(const double arr[], const unsigned int size) 39 | { 40 | unsigned int i = 0; 41 | 42 | for (i = 0; i < size; i++) 43 | { 44 | printf("%8.2f", arr[i]); 45 | } 46 | printf("\n"); 47 | } 48 | -------------------------------------------------------------------------------- /ch11/examples/11_12_put_put.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_12_put_put.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:44:58 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void put1(const char *); 11 | int put2(const char *); 12 | 13 | int main(void) 14 | { 15 | put1("If I'd as much money "); 16 | put1("as I could spend, \n"); 17 | printf("I count %d characters.\n", 18 | put2("I never would cry old chairs to mend.")); 19 | 20 | return 0; 21 | } 22 | 23 | void put1(const char * string) 24 | { 25 | while (*string) 26 | { 27 | putchar(*string++); 28 | } 29 | } 30 | 31 | int put2(const char * string) 32 | { 33 | int count = 0; 34 | 35 | while (*string) 36 | { 37 | putchar(*string++); 38 | count++; 39 | } 40 | putchar('\n'); 41 | 42 | return count; 43 | } 44 | -------------------------------------------------------------------------------- /ch11/examples/11_13_test.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_13_test.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 18:44:27 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | void fit(char *, unsigned int); 12 | 13 | int main(void) 14 | { 15 | char msg[] = "Hold on to your hats, hackers. "; 16 | 17 | puts(msg); 18 | fit(msg, 7); 19 | puts(msg); 20 | puts("Let's look at some more of the string. "); 21 | puts(msg + 8); 22 | 23 | return 0; 24 | } 25 | 26 | void fit(char * string, unsigned int size) 27 | { 28 | if (strlen(string) > size) 29 | { 30 | *(string + size) = '\0'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ch11/examples/11_14_str_cat.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_14_str_cat.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 18:48:35 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define SIZE 80 12 | 13 | int main(void) 14 | { 15 | char flower[SIZE]; 16 | char addon[] = "s smell like old shoes."; 17 | 18 | puts("What is your favorite flower?"); 19 | gets(flower); 20 | strcat(flower, addon); 21 | puts(flower); 22 | puts(addon); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch11/examples/11_15_join_chk.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_15_join_chk.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 18:51:27 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define SIZE 30 12 | #define BUGSIZE 13 13 | 14 | int main(void) 15 | { 16 | char flower[SIZE]; 17 | char addon[] = "s smell like old shoes."; 18 | char bug[BUGSIZE]; 19 | int available; 20 | 21 | puts("What is your favorite flower?"); 22 | gets(flower); 23 | if ((strlen(addon) + strlen(flower) + 1) <= SIZE) 24 | { 25 | strcat(flower, addon); 26 | } 27 | puts(flower); 28 | puts("What is your favorite bug?"); 29 | gets(bug); 30 | available = BUGSIZE - strlen(bug) - 1; 31 | strncat(bug, addon, available); 32 | puts(bug); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /ch11/examples/11_16_nogo.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_16_nogo.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:07:50 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define ANSWER "Grant" 11 | 12 | int main(void) 13 | { 14 | char try[40]; 15 | 16 | puts("Who is buried in Grant's tomb?"); 17 | gets(try); 18 | while (try != ANSWER) 19 | { 20 | puts("No, that's wrong. Try again. "); 21 | gets(try); 22 | } 23 | puts("That's right!"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch11/examples/11_17_compare.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_17_compare.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:13:58 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define ANSWER "Grant" 12 | #define MAX 40 13 | 14 | int main(void) 15 | { 16 | char try[MAX]; 17 | 18 | puts("Who is buried in Grant's tomb?"); 19 | gets(try); 20 | while (strcmp(try, ANSWER) != 0) 21 | { 22 | puts("No, that's wrong. Try again."); 23 | gets(try); 24 | } 25 | puts("That's right!"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch11/examples/11_18_compback.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_18_compback.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:16:42 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | printf("strcmp(\"A\",\"A\") is "); 14 | printf("%d\n", strcmp("A", "A")); 15 | 16 | printf("strcmp(\"A\", \"B\") is "); 17 | printf("%d\n", strcmp("A", "B")); 18 | 19 | printf("strcmp(\"B\", \"A\") is "); 20 | printf("%d\n", strcmp("B", "A")); 21 | 22 | printf("strcmp(\"C\", \"A\") is "); 23 | printf("%d\n", strcmp("C", "A")); 24 | 25 | printf("strcmp(\"A\", \"a\") is "); 26 | printf("%d\n", strcmp("Z", "a")); 27 | 28 | printf("strcmp(\"apples\", \"apple\") is "); 29 | printf("%d\n", strcmp("apples", "apple")); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /ch11/examples/11_19_quit_chk.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_19_quit_chk.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:40:23 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define SIZE 81 12 | #define LIM 100 13 | #define STOP "quit" 14 | 15 | int main(void) 16 | { 17 | char input[LIM][SIZE]; 18 | int ct = 0; 19 | 20 | printf("Enter up to %d lines(type quit to quit):\n", LIM); 21 | while (ct < LIM && gets(input[ct]) != NULL && strcmp(input[ct], STOP) != 0) 22 | { 23 | ct++; 24 | } 25 | printf("%d strings entered.\n", ct); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch11/examples/11_20_starsrch.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_20_starsrch.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:43:18 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define LISTSIZE 5 12 | 13 | int main(void) 14 | { 15 | char * list[LISTSIZE] = { 16 | "astronomy", "astounding", 17 | "astropysics", "ostracize", 18 | "asterism" }; 19 | int count = 0; 20 | int i; 21 | 22 | for (i = 0;i < LISTSIZE; i++) 23 | { 24 | if (strncmp(list[i], "astro", 5) == 0) 25 | { 26 | printf("Found: %s\n", list[i]); 27 | count++; 28 | } 29 | } 30 | printf("The list contained %d words beginning" 31 | " with astro.\n", count); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /ch11/examples/11_21_copy1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_21_copy1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 21:48:49 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define SIZE 40 12 | #define LIM 5 13 | 14 | int main(void) 15 | { 16 | char qwords[LIM][SIZE]; 17 | char temp[SIZE]; 18 | int i = 0; 19 | 20 | printf("Enter %d words beginning with q:\n", LIM); 21 | while (i < LIM && gets(temp)) 22 | { 23 | if (temp[0] != 'q') 24 | { 25 | printf("%s doesn't begin with q!\n", temp); 26 | } 27 | else 28 | { 29 | strcpy(qwords[i], temp); 30 | i++; 31 | } 32 | } 33 | puts("Here are the words accepted: "); 34 | for (i = 0; i < LIM; i++) 35 | { 36 | puts(qwords[i]); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /ch11/examples/11_22_copy2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_22_copy2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:27:19 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define WORDS "beast" 12 | #define SIZE 40 13 | 14 | int main(void) 15 | { 16 | char * orig = WORDS; 17 | char copy[SIZE] = "Be the best that you can be."; 18 | char * ps; 19 | 20 | puts(orig); 21 | puts(copy); 22 | ps = strcpy(copy + 7, orig); 23 | puts(copy); 24 | puts(ps); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch11/examples/11_23_copy3.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_23_copy3.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:29:42 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define SIZE 40 12 | #define TARGSIZE 7 13 | #define LIM 5 14 | 15 | int main(void) 16 | { 17 | char qwords[LIM][TARGSIZE]; 18 | char temp[SIZE]; 19 | int i = 0; 20 | 21 | printf("Enter %d words beginning with q: \n", LIM); 22 | while (i < LIM && gets(temp)) 23 | { 24 | if (temp[0] != 'q') 25 | { 26 | printf("%s doesn't begin with q!\n", temp); 27 | } 28 | else 29 | { 30 | strncpy(qwords[i], temp, TARGSIZE - 1); 31 | qwords[i][TARGSIZE - 1] = '\0'; 32 | i++; 33 | } 34 | } 35 | puts("Here are the words accepted: "); 36 | for (i = 0; i < LIM; i++) 37 | { 38 | puts(qwords[i]); 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /ch11/examples/11_24_format.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_24_format.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:34:51 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAX 20 11 | 12 | int main(void) 13 | { 14 | char first[MAX]; 15 | char last[MAX]; 16 | char formal[2 * MAX + 10]; 17 | double prize; 18 | 19 | puts("Enter your first name: "); 20 | gets(first); 21 | puts("Enter your last name: "); 22 | gets(last); 23 | puts("Enter your prize money: "); 24 | scanf("%lf", &prize); 25 | sprintf(formal, "%s, %-19s: $%6.2f\n", last, first, prize); 26 | puts(formal); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch11/examples/11_26_mod_str.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_26_mod_str.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:51:46 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define LIMIT 80 13 | 14 | void ToUpper(char *); 15 | int PunctCount(const char *); 16 | 17 | int main(void) 18 | { 19 | char line[LIMIT]; 20 | 21 | puts("Please enter a line: "); 22 | gets(line); 23 | ToUpper(line); 24 | puts(line); 25 | printf("That line has %d punctuation characters.\n", PunctCount(line)); 26 | 27 | return 0; 28 | } 29 | 30 | void ToUpper(char * str) 31 | { 32 | while (*str) 33 | { 34 | *str = toupper(*str); 35 | str++; 36 | } 37 | } 38 | 39 | int PunctCount(const char * str) 40 | { 41 | int ct = 0; 42 | 43 | while (*str) 44 | { 45 | if (ispunct(*str)) 46 | { 47 | ct++; 48 | } 49 | str++; 50 | } 51 | 52 | return ct; 53 | } 54 | -------------------------------------------------------------------------------- /ch11/examples/11_27_repeat.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_27_repeat.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:56:44 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(int argc, char * argv[]) 11 | { 12 | int count; 13 | 14 | printf("The command line has %d arguments: \n", argc - 1); 15 | for (count = 1; count < argc; count++) 16 | { 17 | printf("%d: %s\n", count, argv[count]); 18 | } 19 | printf("\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch11/examples/11_28_hello.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_28_hello.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 22:59:27 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(int argc, char * argv[]) 12 | { 13 | int i, times; 14 | 15 | if (argc < 2 || (times = atoi(argv[1])) < 1) 16 | { 17 | printf("Usage: %s positive-number\n", argv[0]); 18 | } 19 | else 20 | { 21 | for (i = 0; i < times; i++) 22 | { 23 | puts("Hello, good looking!"); 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch11/examples/11_29_strcnvt.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_29_strcnvt.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 23:02:01 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char number[30]; 14 | char * end; 15 | long value; 16 | 17 | puts("Enter a number (empty ling to quit): "); 18 | while (gets(number) && number[0] != '\0') 19 | { 20 | value = strtol(number, &end, 10); 21 | printf("value: %ld, stopped at %s(%d)\n", value, end, *end); 22 | value = strtol(number, &end, 16); 23 | printf("value: %ld, stopped at %s(%d)\n", value, end, *end); 24 | puts("Next number: "); 25 | } 26 | puts("Bye!\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch11/examples/11_2_quotes.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_2_quotes.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:16:20 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("%s, %p, %c\n", "We", "are", *"space farers"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /ch11/examples/11_3_p_and_s.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_3_p_and_s.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:20:43 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char * msg = "Don't be a fool! "; 13 | char * copy; 14 | 15 | copy = msg; 16 | printf("%s\n", copy); 17 | printf("msg = %s; &msg = %p; value = %p\n", msg, &msg, msg); 18 | printf("copy = %s; © = %p; value = %p\n", copy, ©, copy); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch11/examples/11_4_name1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_4_name1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:24:19 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAX 81 11 | 12 | int main(void) 13 | { 14 | char name[MAX]; 15 | printf("Hi, what's your name?\n"); 16 | gets(name); 17 | printf("Nice name, %s.\n", name); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch11/examples/11_5_name2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_5_name2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:26:11 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAX 81 11 | 12 | int main(void) 13 | { 14 | char name[MAX]; 15 | char * ptr; 16 | 17 | printf("Hi, what's your name?\n"); 18 | ptr = gets(name); 19 | printf("%s?Ah! %s!\n", name, ptr); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch11/examples/11_6_name3.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_6_name3.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:28:59 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAX 81 11 | 12 | int main(void) 13 | { 14 | char name[MAX]; 15 | char * ptr; 16 | 17 | printf("Hi, what's your name?\n"); 18 | ptr = fgets(name, MAX, stdin); 19 | printf("%s? Ah! %s!\n", name, ptr); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch11/examples/11_7_scan_str.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_7_scan_str.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:34:24 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char name1[11], name2[11]; 13 | int count; 14 | 15 | printf("Please enter 2 names.\n"); 16 | count = scanf("%5s %10s", name1, name2); 17 | printf("I read the %d names %s and %s.\n", count, name1, name2); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch11/examples/11_8_put_out.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_8_put_out.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:36:49 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define DEF "I am a #define string. " 11 | 12 | int main(void) 13 | { 14 | char str1[80] = "An array was initialized to me."; 15 | const char * str2 = "A pointer was initialized to me."; 16 | 17 | puts("I'm an argument to puts()."); 18 | puts(DEF); 19 | puts(str1); 20 | puts(str2); 21 | puts(&str1[5]); 22 | puts(str2+4); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch11/examples/11_9_nono.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_9_nono.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat Jul 5 17:39:54 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char side_a[] = "SIDE A"; 13 | char dont[] = {'W', 'O', 'W', '!'}; 14 | char side_b[] = "SIDE B"; 15 | 16 | puts(dont); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch11/exercises/11_13_power.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_13_power.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 08:02:06 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double power(double, int); 11 | 12 | int main(void) 13 | { 14 | double base; 15 | int exp; 16 | double ret; 17 | 18 | puts("Enter a double and a integer:"); 19 | while (2 == scanf("%lf %d", &base, &exp)) 20 | { 21 | ret = power(base, exp); 22 | printf("(%.3f)^(%d) = %.3f\n", base, exp, ret); 23 | puts("Enter a double and a integer:"); 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | double power(double base, int exp) 30 | { 31 | double ret = base; 32 | int exponent = exp; 33 | 34 | if (base == 0) 35 | { 36 | return 0.0; 37 | } 38 | 39 | if (exp == 0) 40 | { 41 | return 1.0; 42 | } 43 | 44 | if (exp < 0) 45 | { 46 | exponent = -exp; 47 | } 48 | 49 | while (exponent-- > 1) 50 | { 51 | ret *= base; 52 | } 53 | 54 | return (exp > 0) ? ret : 1 / ret; 55 | } 56 | -------------------------------------------------------------------------------- /ch11/exercises/11_1_read_n_char.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_1_read_n_char.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun Jul 6 15:03:12 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void read_char(char [], const unsigned int); 11 | 12 | int main(void) 13 | { 14 | const unsigned int MAX_SIZE = 128; 15 | char str[MAX_SIZE]; 16 | unsigned int n = 0; 17 | 18 | puts("Input the length of the string: "); 19 | scanf("%u", &n); 20 | puts("Input your string:"); 21 | read_char(str, n); 22 | puts(str); 23 | 24 | return 0; 25 | } 26 | 27 | void read_char(char str[], const unsigned int size) 28 | { 29 | unsigned int i = 0; 30 | 31 | while (getchar() != '\n'); 32 | for (i = 0; i < size; i++) 33 | { 34 | str[i] = getchar(); 35 | } 36 | str[size] = '\0'; 37 | } 38 | -------------------------------------------------------------------------------- /ch11/exercises/11_2_read_n_char.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_2_read_n_char.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun Jul 6 15:33:10 2014 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | void read_char(char [], const unsigned int); 12 | 13 | int main(void) 14 | { 15 | const unsigned int MAX_SIZE = 128; 16 | char str[MAX_SIZE]; 17 | unsigned int n = 0; 18 | 19 | puts("Input the length of the string: "); 20 | scanf("%u", &n); 21 | puts("Input your string:"); 22 | read_char(str, n); 23 | puts(str); 24 | 25 | return 0; 26 | } 27 | 28 | void read_char(char str[], const unsigned int size) 29 | { 30 | unsigned int i = 0; 31 | 32 | while (getchar() != '\n'); 33 | for (i = 0; i < size; i++) 34 | { 35 | str[i] = getchar(); 36 | if (isspace(str[i])) 37 | { 38 | break; 39 | } 40 | } 41 | str[i] = '\0'; 42 | } 43 | -------------------------------------------------------------------------------- /ch11/exercises/11_5_is_within.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_5_is_within.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun 06 Jul 2014 11:50:36 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | bool is_within(const char, const char *); 13 | 14 | 15 | int main(void) 16 | { 17 | const unsigned int LEN = 128; 18 | char str[LEN]; 19 | char ch; 20 | 21 | while (true) 22 | { 23 | puts("Input a string:"); 24 | __fpurge(stdin); 25 | fgets(str, LEN, stdin); 26 | puts("Input a char:"); 27 | __fpurge(stdin); 28 | ch = getchar(); 29 | 30 | if (is_within(ch, str)) 31 | { 32 | puts("In!"); 33 | } 34 | else 35 | { 36 | puts("Out!"); 37 | } 38 | } 39 | } 40 | 41 | bool is_within(const char ch, const char * str) 42 | { 43 | const char * ptr = str; 44 | 45 | while (*ptr != '\0') 46 | { 47 | if (*ptr == ch) 48 | { 49 | return true; 50 | } 51 | ptr++; 52 | } 53 | 54 | return false; 55 | } 56 | -------------------------------------------------------------------------------- /ch11/exercises/11_8_reverse_string.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 11_8_reverse_string.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 07 Jul 2014 04:55:27 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | void reverse_string(char *); 13 | 14 | int main(void) 15 | { 16 | const unsigned int SIZE = 128; 17 | char str[SIZE]; 18 | char ch; 19 | 20 | do 21 | { 22 | puts("Input a string:"); 23 | __fpurge(stdin); 24 | fgets(str, SIZE, stdin); 25 | str[strlen(str) - 1] = '\0'; 26 | 27 | puts("After Reverse:"); 28 | reverse_string(str); 29 | puts(str); 30 | 31 | puts("Enter any key except q to go on."); 32 | __fpurge(stdin); 33 | ch = getchar(); 34 | } while (ch != 'q'); 35 | 36 | return 0; 37 | } 38 | 39 | void reverse_string(char * str) 40 | { 41 | unsigned int i, j; 42 | char temp; 43 | i = 0; 44 | j = strlen(str) - 1; 45 | 46 | while (i < j) 47 | { 48 | temp = str[i]; 49 | str[i] = str[j]; 50 | str[j] = temp; 51 | i++; 52 | j--; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /ch12/examples/12_10_r_drive1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_10_r_drive1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:11:56 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | extern void srand1(unsigned int x); 11 | extern int rand1(void); 12 | 13 | int main(void) 14 | { 15 | int count; 16 | unsigned seed; 17 | 18 | printf("Please enter your choice for seed.\n"); 19 | while (scanf("%u", &seed) == 1) 20 | { 21 | srand1(seed); 22 | for (count = 0; count < 5; count++) 23 | { 24 | printf("%hd\n", rand1()); 25 | } 26 | printf("Please enter next seed(q to quit):\n"); 27 | } 28 | printf("Done!\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /ch12/examples/12_11_diceroll.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_11_diceroll.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:14:42 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include "12_12_diceroll.h" 11 | 12 | int roll_count = 0; 13 | 14 | static int rollem(int sides) 15 | { 16 | int roll; 17 | 18 | roll = rand() % sides + 1; 19 | ++roll_count; 20 | return roll; 21 | } 22 | 23 | int roll_n_dice(int dice, int sides) 24 | { 25 | int d; 26 | int total = 0; 27 | 28 | if (sides < 2) 29 | { 30 | printf("Need at least 2 sides.\n"); 31 | return -2; 32 | } 33 | if (dice < 1) 34 | { 35 | printf("Need at least 1 die.\n"); 36 | return -1; 37 | } 38 | for (d = 0; d < dice; d++) 39 | { 40 | total += rollem(sides); 41 | } 42 | 43 | return total; 44 | } 45 | -------------------------------------------------------------------------------- /ch12/examples/12_12_diceroll.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_12_diceroll.h 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:37:53 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef __12_12_DICEROLL_H__ 9 | #define __12_12_DICEROLL_H__ 10 | 11 | extern int roll_count; 12 | 13 | int roll_n_dice(int dice, int sides); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /ch12/examples/12_13_manydice.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_13_manydice.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:19:47 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include "12_12_diceroll.h" 12 | 13 | int main(void) 14 | { 15 | int dice, roll; 16 | int sides; 17 | 18 | srand((unsigned int) time(0)); 19 | printf("Enter the number of sides per die, 0 to stop.\n"); 20 | while (scanf("%d", &sides) == 1 && sides > 0) 21 | { 22 | printf("How many dice?\n"); 23 | scanf("%d", &dice); 24 | roll = roll_n_dice(dice, sides); 25 | printf("You have rolled a %d using %d %d-sides dice.\n", 26 | roll, dice, sides); 27 | printf("How many sides? Enter 0 to stop.\n"); 28 | } 29 | printf("The rollem() function was called %d times.\n", roll_count); 30 | printf("GOOD FORTUNE TO YOU!\n"); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ch12/examples/12_14_dyn_arr.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_14_dyn_arr.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:40:57 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | double * ptd; 14 | int max; 15 | int number; 16 | int i = 0; 17 | 18 | puts("What is the maximum number of type double entries?"); 19 | scanf("%d", &max); 20 | ptd = (double *) malloc(max * sizeof(double)); 21 | if (ptd == NULL) 22 | { 23 | puts("Memory allocation failed. Goodbye."); 24 | exit(EXIT_FAILURE); 25 | } 26 | puts("Enter the values (q to quit): "); 27 | while (i < max && scanf("%lf", &ptd[i]) == 1) 28 | { 29 | ++i; 30 | } 31 | printf("Here are your %d entries:\n", number = i); 32 | for (i = 0; i < number; i++) 33 | { 34 | printf("%7.2f ", ptd[i]); 35 | if (i % 7 == 6) 36 | { 37 | putchar('\n'); 38 | } 39 | } 40 | if (i % 7 != 0) 41 | { 42 | putchar('\n'); 43 | } 44 | puts("Done."); 45 | free(ptd); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /ch12/examples/12_1_hiding.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_1_hiding.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:37:23 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int x = 30; 13 | printf("x in outer block: %d\n", x); 14 | { 15 | int x = 77; 16 | printf("x in inner block: %d\n", x); 17 | } 18 | printf("x in outer block: %d\n", x); 19 | while (x++ < 33) 20 | { 21 | int x = 100; 22 | x++; 23 | printf("x in while loop: %d\n", x); 24 | } 25 | printf("x in outer block: %d\n", x); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch12/examples/12_2_forc99.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_2_forc99.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:40:55 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n = 10; 13 | 14 | printf("Initially, n = %d\n", n); 15 | for (int n = 1; n < 3; n++) 16 | { 17 | printf("loop 1: n = %d\n", n); 18 | } 19 | printf("After loop 1, n = %d\n", n); 20 | for (int n = 1; n < 3; n++) 21 | { 22 | printf("loop 2 index n = %d\n", n); 23 | int n = 30; 24 | printf("loop 2: n = %d\n", n); 25 | n++; 26 | } 27 | printf("After loop 2, n = %d\n", n); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ch12/examples/12_3_loc_stat.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_3_loc_stat.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:45:02 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void trystat(void); 11 | 12 | int main(void) 13 | { 14 | int count; 15 | 16 | for (count = 1; count <= 3; count++) 17 | { 18 | printf("Here comes iteration %d: \n", count); 19 | trystat(); 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | void trystat(void) 26 | { 27 | int fade = 1; 28 | static int stay = 1; 29 | 30 | printf("fade = %d and stay = %d\n", fade++, stay++); 31 | } 32 | -------------------------------------------------------------------------------- /ch12/examples/12_4_global.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_4_global.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:48:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int units = 0; 11 | 12 | void critic(void); 13 | 14 | int main(void) 15 | { 16 | extern int units; 17 | 18 | printf("How many pounds to firkin of butter?\n"); 19 | scanf("%d", &units); 20 | while (units != 56) 21 | { 22 | critic(); 23 | } 24 | printf("You must have looked it up!\n"); 25 | 26 | return 0; 27 | } 28 | 29 | void critic(void) 30 | { 31 | printf("No luck, chummy. Try again.\n"); 32 | scanf("%d", &units); 33 | } 34 | -------------------------------------------------------------------------------- /ch12/examples/12_5_parta.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_5_parta.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:52:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void report_count(); 11 | void accumulate(int k); 12 | int count = 0; 13 | 14 | int main(void) 15 | { 16 | int value; 17 | register int i; 18 | 19 | printf("Enter a positive integer(0 to quit): "); 20 | while (scanf("%d", &value) == 1 && value > 0) 21 | { 22 | ++count; 23 | for (i = value; i >= 0; i--) 24 | { 25 | accumulate(i); 26 | } 27 | printf("Enter a positive integer(0 to quit): "); 28 | } 29 | report_count(); 30 | 31 | return 0; 32 | } 33 | 34 | void report_count() 35 | { 36 | printf("Loop executed %d times\n", count); 37 | } 38 | -------------------------------------------------------------------------------- /ch12/examples/12_6_partb.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_6_partb.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 10:57:32 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | extern int count; 11 | 12 | static int total = 0; 13 | 14 | void accumulate(int k) 15 | { 16 | static int subtotal = 0; 17 | 18 | if (k <= 0) 19 | { 20 | printf("loop cycle: %d\n", count); 21 | printf("subtotal: %d; total: %d\n", subtotal, total); 22 | subtotal = 0; 23 | } 24 | else 25 | { 26 | subtotal += k; 27 | total += k; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /ch12/examples/12_7_rand0.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_7_rand0.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:05:58 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | static unsigned long int next = 1; 11 | 12 | int rand0(void) 13 | { 14 | next = next * 1103515245 + 12345; 15 | return (unsigned int) (next / 65536) % 32768; 16 | } 17 | -------------------------------------------------------------------------------- /ch12/examples/12_8_r_drive0.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_8_r_drive0.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:07:57 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | extern int rand0(void); 11 | 12 | int main(void) 13 | { 14 | int count; 15 | 16 | for (count = 0; count < 5; count++) 17 | { 18 | printf("%hd\n", rand0()); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch12/examples/12_9_s_and_r.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_9_s_and_r.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 09 Jul 2014 11:10:16 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | static unsigned long int next = 1; 11 | 12 | int rand1(void) 13 | { 14 | next = next * 1103515245 + 12345; 15 | return (unsigned int) (next / 65535) % 32768; 16 | } 17 | 18 | void srand1(unsigned int seed) 19 | { 20 | next = seed; 21 | } 22 | -------------------------------------------------------------------------------- /ch12/exercises/12_1_global.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_1_global.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 08:26:00 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void critic(int * units); 11 | 12 | int main(void) 13 | { 14 | static int units; 15 | 16 | printf("How many pounds to a firkin of butter?\n"); 17 | scanf("%d", &units); 18 | while (units != 56) 19 | { 20 | critic(&units); 21 | } 22 | printf("You must have looked it up!\n"); 23 | 24 | return 0; 25 | } 26 | 27 | void critic(int * units) 28 | { 29 | printf("No luck, chummy. Try again.\n"); 30 | scanf("%d", units); 31 | } 32 | -------------------------------------------------------------------------------- /ch12/exercises/12_2_pe/12_2_pe_a.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_2_pe_a.h 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 09:43:09 AM CST 6 | ************************************************************************/ 7 | 8 | #ifndef __12_2_PE_A_H__ 9 | #define __12_2_PE_A_H__ 10 | 11 | static int mode; 12 | static double distance; 13 | static double consumption; 14 | 15 | void set_mode(int); 16 | void get_info(void); 17 | void show_info(void); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /ch12/exercises/12_2_pe/12_2_pe_b.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_2_pe_b.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 09:40:20 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "12_2_pe_a.h" 10 | 11 | int main(void) 12 | { 13 | int mode; 14 | 15 | printf("Enter 0 for metric mode, 1 for US mode: "); 16 | scanf("%d", &mode); 17 | while (mode >= 0) 18 | { 19 | set_mode(mode); 20 | get_info(); 21 | show_info(); 22 | printf("Enter 0 for metric mode, 1 for US mode(-1 to quit): "); 23 | scanf("%d", &mode); 24 | } 25 | printf("Done.\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch12/exercises/12_3_pe/12_3_pe_a.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_3_pe_a.h 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 10:23:54 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef __12_3_PE_A_H__ 9 | #define __12_3_PE_A_H__ 10 | 11 | void set_mode(int *); 12 | void get_info(const int, double *, double *); 13 | void show_info(const int, const double, const double); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /ch12/exercises/12_3_pe/12_3_pe_b.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_3_pe_b.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 10:21:38 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "12_3_pe_a.h" 10 | 11 | int main(void) 12 | { 13 | int mode; 14 | double distance; 15 | double consumption; 16 | 17 | printf("Enter 0 for metric mode, 1 for US mode: "); 18 | scanf("%d", &mode); 19 | while (mode >= 0) 20 | { 21 | set_mode(&mode); 22 | get_info(mode, &distance, &consumption); 23 | show_info(mode, distance, consumption); 24 | printf("Enter 0 for metric mode, 1 for US mode(-1 to quit): "); 25 | scanf("%d", &mode); 26 | } 27 | printf("Done.\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ch12/exercises/12_4_func_call_times.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_4_func_call_times.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 10:42:39 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int func(int); 11 | 12 | int main(void) 13 | { 14 | int num = 0; 15 | 16 | while (scanf("%d", &num) == 1) 17 | { 18 | printf("The function has call %d times.\n", func(num)); 19 | } 20 | 21 | return 0; 22 | } 23 | 24 | int func(int num) 25 | { 26 | static int cnt = 0; 27 | int ret = 0; 28 | 29 | cnt++; 30 | num = num / 2; 31 | 32 | if (num == 0) 33 | { 34 | ret = cnt; 35 | cnt = 0; 36 | return ret; 37 | } 38 | 39 | return func(num); 40 | } 41 | -------------------------------------------------------------------------------- /ch12/exercises/12_6_count_frequency_of_random_numbers.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 12_6_count_frequency_of_random_numbers.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 10 Jul 2014 11:49:13 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void count_frequency(const int num, const int span); 14 | 15 | int main(void) 16 | { 17 | const int SPAN = 10; 18 | int num = 0; 19 | 20 | printf("Enter the number of random numbers: "); 21 | while (scanf("%d", &num) == 1) 22 | { 23 | count_frequency(num, SPAN); 24 | printf("Enter the number of random numbers: "); 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | void count_frequency(const int num, const int span) 31 | { 32 | int i = 0; 33 | int * temp = (int *) malloc(span * sizeof(int)); 34 | memset(temp, 0, sizeof(int) * span); 35 | 36 | srand((unsigned int)time(NULL)); 37 | 38 | for (i = 0; i < num; i++) 39 | { 40 | temp[rand() % span]++; 41 | } 42 | 43 | for (i = 0; i < span; i++) 44 | { 45 | printf("%d : %d\n", i, temp[i]); 46 | } 47 | 48 | free(temp); 49 | } 50 | -------------------------------------------------------------------------------- /ch12/exercises/12_7_diceroll/diceroll.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: diceroll.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 16 Jul 2014 03:52:11 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include "diceroll.h" 11 | 12 | int roll_count = 0; 13 | 14 | static int rollem(int sides) 15 | { 16 | int roll; 17 | 18 | roll = rand() % sides + 1; 19 | ++roll_count; 20 | 21 | return roll; 22 | } 23 | 24 | int roll_n_dice(int dice, int sides) 25 | { 26 | int d; 27 | int total = 0; 28 | 29 | if (sides < 2) 30 | { 31 | printf("Need at least 2 sides.\n"); 32 | return -2; 33 | } 34 | if (dice < 1) 35 | { 36 | printf("Need at least 1 die.\n"); 37 | return -1; 38 | } 39 | for (d = 0; d < dice; d++) 40 | { 41 | total += rollem(sides); 42 | } 43 | 44 | return total; 45 | } 46 | -------------------------------------------------------------------------------- /ch12/exercises/12_7_diceroll/diceroll.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: diceroll.h 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 16 Jul 2014 03:51:28 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef __DICEROLL_H__ 9 | #define __DICEROLL_H__ 10 | 11 | extern int roll_count; 12 | 13 | int roll_n_dice(int dice, int sides); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /ch12/exercises/12_7_diceroll/manydice.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: manydice.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 16 Jul 2014 03:57:27 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include "diceroll.h" 12 | 13 | int main(void) 14 | { 15 | int sets; 16 | int dice, sides, roll; 17 | int i = 0; 18 | 19 | srand((unsigned int) time(0)); 20 | printf("Enter the number of sets: enter q to stop.\n"); 21 | while (scanf("%d", &sets) == 1 && sets > 0) 22 | { 23 | printf("How many sides and how many dice?\n"); 24 | scanf("%d %d", &sides, &dice); 25 | printf("Here are %d sets of %d %d-sides throws.", sets, dice, sides); 26 | for (i = 0; i < sets; i++) 27 | { 28 | if (i % 15 == 0) 29 | { 30 | printf("\n"); 31 | } 32 | roll = roll_n_dice(dice, sides); 33 | printf("%3d", roll); 34 | } 35 | printf("\nHow many sets? Enter 0 to stop.\n"); 36 | } 37 | printf("The rollem() function was called %d times.\n", roll_count); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /ch13/examples/13_1_count.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_1_count.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 05:20:07 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(int argc, char * argv[]) 12 | { 13 | int ch; 14 | FILE * fp; 15 | long count = 0; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Usage: %s filename\n", argv[0]); 20 | exit(1); 21 | } 22 | if ((fp = fopen(argv[1], "r")) == NULL) 23 | { 24 | printf("Can't open %s\n", argv[1]); 25 | exit(1); 26 | } 27 | while ((ch = getc(fp)) != EOF) 28 | { 29 | putc(ch, stdout); 30 | count++; 31 | } 32 | fclose(fp); 33 | printf("File %s has %ld characters\n", argv[1], count); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /ch13/examples/13_2_reducto.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_2_reducto.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 05:25:47 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define LEN 40 13 | 14 | int main(int argc, char * argv[]) 15 | { 16 | FILE *in, *out; 17 | int ch; 18 | char name[LEN]; 19 | int count = 0; 20 | 21 | if (argc < 2) 22 | { 23 | fprintf(stderr, "Usage: %s filename\n", argv[0]); 24 | exit(1); 25 | } 26 | if ((in = fopen(argv[1], "r")) == NULL) 27 | { 28 | fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[1]); 29 | exit(2); 30 | } 31 | strcpy(name, argv[1]); 32 | strcat(name, ".red"); 33 | if ((out = fopen(name, "w")) == NULL) 34 | { 35 | fprintf(stderr, "Can't create output file.\n"); 36 | exit(3); 37 | } 38 | while ((ch = getc(in)) != EOF) 39 | { 40 | if (count++ % 3 == 0) 41 | { 42 | putc(ch, out); 43 | } 44 | } 45 | if (fclose(in) != 0 || fclose(out) != 0) 46 | { 47 | fprintf(stderr, "Error in closing files\n"); 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /ch13/examples/13_3_addaword.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_3_addaword.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 05:33:05 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define MAX 40 12 | 13 | int main(void) 14 | { 15 | FILE * fp; 16 | char words[MAX]; 17 | 18 | if ((fp = fopen("words", "a+")) == NULL) 19 | { 20 | fprintf(stdout, "Can't open \"words\" file.\n"); 21 | exit(1); 22 | } 23 | puts("Enter words to add to the file; press the Enter"); 24 | puts("key at the beginning of a line to terminate."); 25 | while (gets(words) != NULL && words[0] != '\0') 26 | { 27 | fprintf(fp, "%s ", words); 28 | } 29 | puts("File contents:"); 30 | rewind(fp); 31 | while (fscanf(fp, "%s ", words) == 1) 32 | { 33 | puts(words); 34 | } 35 | if (fclose(fp) != 0) 36 | { 37 | fprintf(stderr, "Error closing file\n"); 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /ch13/examples/13_4_parrot.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_4_parrot.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 05:40:53 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAXLINE 20 11 | 12 | int main(void) 13 | { 14 | char line[MAXLINE]; 15 | 16 | while (fgets(line, MAXLINE, stdin) != NULL && line[0] != '\n') 17 | { 18 | fputs(line, stdout); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch13/exercises/13_10_read_from_file.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_10_read_from_file.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 24 Sep 2014 05:15:35 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | int pos = 0; 14 | FILE * fp; 15 | char filename[128]; 16 | char buffer[512]; 17 | 18 | //get the filename to read 19 | printf("Please enter the filename to read:\n"); 20 | scanf("%s", filename); 21 | 22 | //open the file 23 | if ((fp = fopen(filename, "r")) == NULL) 24 | { 25 | fprintf(stdout, "Can't open the file %s!", filename); 26 | exit(EXIT_FAILURE); 27 | } 28 | 29 | printf("Please enter the position to read:"); 30 | while (scanf("%d", &pos) == 1) 31 | { 32 | rewind(fp); 33 | if (fseek(fp, pos, SEEK_SET) == 0) 34 | { 35 | fgets(buffer, 512, fp); 36 | fprintf(stdout, "%s", buffer); 37 | printf("Please enter the next position:"); 38 | } 39 | else 40 | { 41 | printf("Please enter the correct position:"); 42 | } 43 | } 44 | fclose(fp); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /ch13/exercises/13_11_search_substr.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_11_search_substr.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 24 Sep 2014 05:44:44 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char* argv[]) 13 | { 14 | FILE * fp; 15 | char perline[512]={0}; 16 | 17 | if (argc < 3) 18 | { 19 | printf("Usage:%s [substr] [filename]\n", argv[0]); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | if ((fp = fopen(argv[2], "r")) == NULL) 24 | { 25 | printf("Can't open the file %s!", argv[0]); 26 | exit(EXIT_FAILURE); 27 | } 28 | 29 | while (fgets(perline, 512, fp) != NULL) 30 | { 31 | if (strstr(perline, argv[1]) != NULL) 32 | { 33 | printf("%s", perline); 34 | } 35 | } 36 | fclose(fp); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /ch13/exercises/13_13_denoising.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henrypoter/C_Primer/bc2b5c1643fd331633fb802cfc02e26014b2615e/ch13/exercises/13_13_denoising.c -------------------------------------------------------------------------------- /ch13/exercises/13_1_count.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_1_count.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 10:33:41 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define MAXLEN 64 13 | 14 | int main(void) 15 | { 16 | char filename[MAXLEN]; 17 | FILE * fp; 18 | int ch; 19 | long count = 0; 20 | 21 | fputs("Enter the filename: ", stdout); 22 | fgets(filename, MAXLEN, stdin); 23 | filename[strlen(filename) - 1] = '\0'; 24 | if ((fp = fopen(filename, "r")) == NULL) 25 | { 26 | fprintf(stderr, "Can't open %s\n", filename); 27 | exit(EXIT_FAILURE); 28 | } 29 | while ((ch = getc(fp)) != EOF) 30 | { 31 | putc(ch, stdout); 32 | count++; 33 | } 34 | fclose(fp); 35 | printf("File %s has %ld characters.\n", filename, count); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /ch13/exercises/13_2_file_copy.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_2_file_copy.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 17 Jul 2014 10:49:05 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(int argc, char * argv[]) 12 | { 13 | FILE *fs, *fd; 14 | int ch; 15 | long count = 0; 16 | 17 | if (argc < 3) 18 | { 19 | fprintf(stderr, "Usage: %s [source_filename] [destination_filename]\n", argv[0]); 20 | exit(EXIT_FAILURE); 21 | } 22 | 23 | if ((fs = fopen(argv[1], "rb")) == NULL) 24 | { 25 | fprintf(stderr, "Can't open file %s\n", argv[1]); 26 | exit(EXIT_FAILURE); 27 | } 28 | if ((fd = fopen(argv[2], "wb")) == NULL) 29 | { 30 | fprintf(stderr, "Can't open file %s\n", argv[2]); 31 | exit(EXIT_FAILURE); 32 | } 33 | while ((ch = getc(fs)) != EOF) 34 | { 35 | putc(ch, fd); 36 | count++; 37 | } 38 | fclose(fs); 39 | fclose(fd); 40 | printf("%ld characters have been copied to file %s from file %s\n", 41 | count, argv[2], argv[1]); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /ch13/exercises/13_4_display_files.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_4_display_files.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 18 Jul 2014 04:45:50 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(int argc, char * argv[]) 12 | { 13 | int i = 0; 14 | FILE * fp; 15 | int ch; 16 | 17 | if (argc < 2) 18 | { 19 | fprintf(stderr, "Usage: %s [filename1] [filename2] ...\n", argv[0]); 20 | exit(EXIT_FAILURE); 21 | } 22 | for (i = 1; i < argc; i++) 23 | { 24 | if ((fp = fopen(argv[i], "r")) == NULL) 25 | { 26 | fprintf(stderr, "Can't open file %s.\n", argv[i]); 27 | exit(EXIT_FAILURE); 28 | } 29 | while ((ch = fgetc(fp)) != EOF) 30 | { 31 | fputc(ch, stdout); 32 | } 33 | fclose(fp); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /ch13/exercises/13_9_addaword.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 13_9_addaword.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 24 Jul 2014 03:49:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define MAX 40 12 | 13 | int main(void) 14 | { 15 | FILE * fp; 16 | char words[MAX]; 17 | int word_no = 1; 18 | char perline[128]; 19 | 20 | if ((fp = fopen("words", "a+")) == NULL) 21 | { 22 | fprintf(stderr, "Can't open \"words\" file.\n"); 23 | exit(EXIT_FAILURE); 24 | } 25 | puts("Enter words to add to the file; press the enter"); 26 | puts("key at the beginning of a line to terminate."); 27 | 28 | //get current word NO. 29 | while (fgets(perline, 128, fp) != NULL) 30 | { 31 | word_no++; 32 | } 33 | 34 | while (fscanf(stdin, "%s", words) == 1 && words[0] != '\n') 35 | { 36 | fprintf(fp, "%d %s\n", word_no, words); 37 | word_no++; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /ch2/examples/2_1_first.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_1_first.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 09 Jun 2014 09:47:50 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int num; 13 | num = 1; 14 | 15 | printf("I am a simple "); 16 | printf("computer.\n"); 17 | printf("My favorite number is %d because it is first.\n", num); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch2/examples/2_2_fathm_ft.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_2_fathm_ft.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 02:54:06 PM CST 6 | ************************************************************************/ 7 | /* 8 | * 功能:把两个fathoms(深度单位)换算成英尺 9 | */ 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | int feet, fathoms; 16 | 17 | fathoms = 2; 18 | feet = 6 * fathoms; 19 | printf("There are %d feet in %d fathoms!\n", feet, fathoms); 20 | printf("Yes, I said %d feet!\n", 6 * fathoms); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch2/examples/2_3_two_func.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_3_two_func.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 02:57:17 PM CST 6 | ************************************************************************/ 7 | /* 8 | * 功能:在一个文件中使用两个函数 9 | */ 10 | #include 11 | 12 | void butler(void);//ISO/ANSI C函数原型 13 | 14 | int main(void) 15 | { 16 | printf("I will summon the butler function.\n"); 17 | butler(); 18 | printf("Yes. Bring me some tea and writeable CD-ROMS.\n"); 19 | 20 | return 0; 21 | } 22 | 23 | void butler(void) 24 | { 25 | printf("You rang, sir?\n"); 26 | } 27 | -------------------------------------------------------------------------------- /ch2/exercises/2_1_print_name.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_1_print_name.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:10:28 PM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:打印姓名 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | printf("Hui Wang\n"); 15 | printf("Hui\nWang\n"); 16 | printf("Hui "); 17 | printf("Wang\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch2/exercises/2_2_print_name_address.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_2_print_name_address.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:13:35 PM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:输出姓名和地址 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | char name[] = "Hui Wang"; 15 | char addr[] = "Qinglong Road, Longhua New Zone, Shenzhen, Guangdong Province, China"; 16 | 17 | printf("Name: %s\n", name); 18 | printf("Address: %s\n", addr); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch2/exercises/2_3_convert_age_to_day.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_3_convert_age_to_day.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:32:43 PM CST 6 | ************************************************************************/ 7 | /* 8 | * 功能:将年龄转换为天数 9 | */ 10 | 11 | #include 12 | 13 | int main(void) 14 | { 15 | unsigned char age = 24; 16 | 17 | printf("Age: %d\n", age); 18 | printf("Day: %d\n", age * 365); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch2/exercises/2_4_print_prise_statement.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_4_print_prise_statement.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:36:22 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void print_prise(void); 11 | 12 | void print_end(void); 13 | 14 | int main(void) 15 | { 16 | print_prise(); 17 | print_prise(); 18 | print_prise(); 19 | print_end(); 20 | 21 | return 0; 22 | } 23 | 24 | void print_prise(void) 25 | { 26 | printf("For he's a jolly good fellow!\n"); 27 | } 28 | 29 | void print_end(void) 30 | { 31 | printf("Which nobody can deny!\n"); 32 | } 33 | -------------------------------------------------------------------------------- /ch2/exercises/2_5_compute_sum_square.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_5_compute_sum_square.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:45:06 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int toes = 10; 13 | int sum = toes + toes; 14 | int square = toes * toes; 15 | 16 | printf("toes = %d\n", toes); 17 | printf("sum = %d\n", sum); 18 | printf("square = %d\n", square); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch2/exercises/2_6_print_string_dec.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_6_print_string_dec.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:47:38 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void print_str(void); 11 | 12 | int main(void) 13 | { 14 | print_str(); 15 | print_str(); 16 | print_str(); 17 | printf("\n"); 18 | print_str(); 19 | print_str(); 20 | printf("\n"); 21 | print_str(); 22 | printf("\n"); 23 | 24 | return 0; 25 | } 26 | 27 | void print_str(void) 28 | { 29 | printf("Smile!"); 30 | } 31 | -------------------------------------------------------------------------------- /ch2/exercises/2_7_print_number.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 2_7_print_number.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 03:53:15 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void one_three(void); 11 | void two(void); 12 | 13 | int main(void) 14 | { 15 | printf("staring now:\n"); 16 | one_three(); 17 | printf("done!\n"); 18 | 19 | return 0; 20 | } 21 | 22 | void one_three(void) 23 | { 24 | printf("one\n"); 25 | two(); 26 | printf("three\n"); 27 | } 28 | 29 | void two(void) 30 | { 31 | printf("two\n"); 32 | } 33 | -------------------------------------------------------------------------------- /ch3/examples/3_10_escape.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_10_escape.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:11:57 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float salary; 13 | printf("\aEnter your desired monthly salary: "); 14 | printf(" $______\b\b\b\b\b\b"); 15 | scanf("%f", &salary); 16 | printf("\t$%.2f a month is $%.2f a year.", salary, salary * 12.0); 17 | printf("\rGee!\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch3/examples/3_1_rhodium.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_1_rhodium.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 04:40:17 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float weight; 13 | float value; 14 | printf("Are you worth your weight in rhodium?\n"); 15 | printf("Let's check it out.\n"); 16 | printf("Please enter your weight in pounds: "); 17 | scanf("%f", &weight); 18 | value = 770 * weight * 14.5833; 19 | printf("Your weight in rhodium is worth $%.2f.\n", value); 20 | printf("You are easily worth that! If rhodium prices drop, \n"); 21 | printf("eat more to maintain your value.\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch3/examples/3_2_print1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_2_print1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 05:20:02 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int ten = 10; 13 | int two = 2; 14 | 15 | printf("Doing it right: "); 16 | printf("%d minus %d is %d\n", ten, 2, ten - two); 17 | printf("Doing it wrong: "); 18 | //printf("%d minus %d is %d\n", ten);//错误写法,编译不通过 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch3/examples/3_3_bases.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_3_bases.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 05:32:52 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int x = 100; 13 | printf("dec = %d; octal = %o; hex = %x\n", x, x, x); 14 | printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ch3/examples/3_4_print2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_4_print2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 11:21:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | unsigned int un = 3000000000; 13 | short end = 200; 14 | long big = 65537; 15 | long long verybig = 12345678908642; 16 | printf("un = %u and not %d\n", un, un); 17 | printf("end = %hd and %d\n", end, end); 18 | //printf("big = %ld and not %hd\n", big, big); 19 | //printf("verybig = %lld and not %ld\n", verybig, verybig); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch3/examples/3_4_toobig.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_4_toobig.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 10 Jun 2014 05:43:11 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int i = 2147483647; 13 | unsigned int j = 4294967295; 14 | 15 | printf("%d\t%d\t%d\n", i, i+1, i+2); 16 | printf("%u\t%u\t%u\n", j, j+1, j+2); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch3/examples/3_5_charcode.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_5_charcode.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 12:33:53 AM CST 6 | ************************************************************************/ 7 | /* 8 | * 功能:显示一个字符的编码 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | char ch; 15 | printf("Please enter a character.\n"); 16 | scanf("%c", &ch); 17 | printf("The code for %c is %d.\n", ch, ch); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch3/examples/3_6_altnames.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_6_altnames.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 01:13:26 AM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:可移植的整数类型名 9 | */ 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | int16_t me16; 16 | 17 | me16 = 4593; 18 | printf("First, assume int16_t is short: "); 19 | printf("me16 = %hd\n", me16); 20 | printf("Next, let's not make any assumptions.\n"); 21 | printf("Instead, use a \"macro\" from inttypes.h: "); 22 | printf("me16 = %" PRId16"\n", me16); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch3/examples/3_7_floaterr.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_7_floaterr.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 10:47:38 AM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:测试浮点数的舍入误差 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | float a, b; 15 | 16 | b = 2.0e20 + 1.0; 17 | a = b - 2.0e20; 18 | printf("%f\n", a); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch3/examples/3_7_showr_pt.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_7_showr_pt.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 10:32:35 AM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:以两种方式显式浮点值 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | float aboat = 32000.0; 15 | double abet = 2.14e9; 16 | long double dip = 5.32e-5; 17 | printf("%f can be written %e\n", aboat, aboat); 18 | printf("%f can be written %e\n", abet, abet); 19 | //printf("%f can be written %e\n", dip, dip);//错误写法 20 | printf("%Lf can be written %Le\n", dip, dip); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch3/examples/3_8_typesize.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_8_typesize.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 10:55:48 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("Type int has a size of %u bytes.\n", sizeof(int)); 13 | printf("Type char has a size of %u bytes.\n", sizeof(char)); 14 | printf("Type long has a size of %u bytes.\n", sizeof(long)); 15 | printf("Type double has a size of %u bytes.\n", sizeof(double)); 16 | printf("Type float has a size of %zd bytes.\n", sizeof(float)); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch3/examples/3_9_badcount.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_9_badcount.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:04:32 AM CST 6 | ************************************************************************/ 7 | /* 8 | *功能:测试不正确的参数个数会有什么结果 9 | */ 10 | #include 11 | 12 | int main(void) 13 | { 14 | int f = 4; 15 | int g = 5; 16 | float h = 5.0f; 17 | 18 | printf("%d\n", f, g); 19 | printf("%d %d\n", f); 20 | printf("%d %f\n", h, g); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch3/exercises/3_1_test_overflow.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_1_test_overflow.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 01:54:58 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | unsigned int a = 4294967295; 13 | int b = 2147483647; 14 | float c = 3.4e38; 15 | float d = 0.1234e-2; 16 | 17 | printf("%u + 1 = %u\n", a, a+1); 18 | printf("%d + 1 = %d\n", b, b+1); 19 | printf("%e * 10 = %e\n", c, c*10); 20 | printf("%f / 10 = %f\n", d, d/10); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch3/exercises/3_2_ascii_to_char.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_2_ascii_to_char.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:01:21 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int ascii = 0; 13 | 14 | printf("ASCII: "); 15 | scanf("%d", &ascii); 16 | printf("character: %c\n", ascii); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch3/exercises/3_3_alarm_and_print.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_3_alarm_and_print.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:05:32 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("\a"); 13 | printf("Started by the sudden sound, Sally shouted, \"By the Great Pumpkin, what was that! \""); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /ch3/exercises/3_4_print_float.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_4_print_float.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:08:32 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float f = 0.0f; 13 | scanf("%f", &f); 14 | printf("The input is %f or %e.\n", f, f); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ch3/exercises/3_5_age_to_seconds.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_5_age_to_seconds.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:11:24 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | unsigned int age = 0; 13 | const unsigned long long per_year_seconds = 3.156e7; 14 | 15 | scanf("%u", &age); 16 | printf("Your age is %u, which is equal to %llu seconds.", age, age * per_year_seconds); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch3/exercises/3_6_quarts_to_molecules.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_6_quarts_to_molecules.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:25:57 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float f = 0.0f; 13 | 14 | printf("Please input how much quarts the water is: "); 15 | scanf("%f", &f); 16 | printf("%f quarts water has %e molecules.\n", f, f * 950 / 3.0e-23); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch3/exercises/3_7_inch_and_centimeter.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3_7_inch_and_centimeter.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:29:54 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float height = 0.0f; 13 | 14 | printf("Please input your height(unit:inch): "); 15 | scanf("%f", &height); 16 | printf("Your height is %f inches, which is equal to %f centimeters.\n", height, height * 2.54); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch4/examples/4_10_strings.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_10_strings.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 05:36:33 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define BLURB "Authentic imitation! " 11 | 12 | int main(void) 13 | { 14 | printf("/%2s/\n", BLURB); 15 | printf("/%24s/\n", BLURB); 16 | printf("/%24.5s/\n", BLURB); 17 | printf("/%-24.5s/\n", BLURB); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch4/examples/4_11_intconv.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_11_intconv.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 05:42:19 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define PAGES 336 11 | #define WORDS 65618 12 | 13 | int main(void) 14 | { 15 | short num = PAGES; 16 | short mnum = -PAGES; 17 | 18 | printf("num as short and unsigned short: %hd %hu\n", num, num); 19 | printf("-num as short and unsigned short: %hd %hu\n", mnum, mnum); 20 | printf("num as int and char: %d %c\n", num, num); 21 | printf("WORDS as int, short, and char: %d %hd %c\n", WORDS, WORDS, WORDS); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch4/examples/4_12_floatcnv.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_12_floatcnv.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:41:17 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float n1 = 3.0; 13 | double n2 = 3.0; 14 | long n3 = 2000000000; 15 | long n4 = 1234567890; 16 | 17 | printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4); 18 | printf("%ld %ld\n", n3, n4); 19 | printf("%ld %ld %ld %ld\n", n1, n2, n3, n4); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch4/examples/4_13_prntval.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_13_prntval.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:45:44 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int bph20 = 212; 13 | int rv; 14 | 15 | rv = printf("%d F is water's boiling point.\n", bph20); 16 | printf("The printf() function printed %d cahracters.\n", rv); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch4/examples/4_14_longstrg.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_14_longstrg.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:48:19 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("Here's one way to print a "); 13 | printf("long string.\n"); 14 | printf("Here's another way to print a \ 15 | long string.\n"); 16 | printf("Here's the newest way to print a " 17 | "long string.\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch4/examples/4_15_input.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_15_input.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:50:26 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int age; 13 | float assets; 14 | char pet[30]; 15 | 16 | printf("Enter your age, assets, and favorite pet.\n"); 17 | scanf("%d %f", &age, &assets); 18 | scanf("%s", pet); 19 | printf("%d $%.2f %s\n", age, assets, pet); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch4/examples/4_16_varwid.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_16_varwid.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:52:58 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | unsigned width, precision; 13 | int number = 256; 14 | double weight = 242.5; 15 | 16 | printf("What field width?\n"); 17 | scanf("%d", &width); 18 | printf("The number is: %*d: \n", width, number); 19 | printf("Now enter a width and a precision: \n"); 20 | scanf("%d %d", &width, &precision); 21 | printf("Weight = %*.*f\n", width, precision, weight); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch4/examples/4_17_skip2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_17_skip2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 11:56:42 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | printf("Please enter three integers: \n"); 15 | scanf("%*d %*d %d", &n); 16 | printf("The last integer was %d\n", n); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch4/examples/4_1_talkback.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_1_talkback.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:42:44 PM CST 6 | ************************************************************************/ 7 | /* 8 | * 功能:一个简单的对话程序 9 | */ 10 | #include 11 | #include 12 | 13 | #define DENSITY 62.4 14 | 15 | int main(void) 16 | { 17 | float weight, volume; 18 | int size, letters; 19 | char name[40]; 20 | 21 | printf("Hi! What's your name?\n"); 22 | scanf("%s", name); 23 | printf("%s, what's your weight in pounds?\n", name); 24 | scanf("%f", &weight); 25 | size = sizeof(name); 26 | letters = strlen(name); 27 | volume = weight / DENSITY; 28 | printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume); 29 | printf("Also, your first name has %d letters, \n", letters); 30 | printf("and we have %d bytes to store it in.\n", size); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ch4/examples/4_2_praise1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_2_praise1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 02:57:43 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define PRAISE "What a super marvelous name!" 11 | 12 | int main(void) 13 | { 14 | char name[40]; 15 | 16 | printf("What's your name?\n"); 17 | scanf("%s", name); 18 | printf("Hello, %s. %s\n", name, PRAISE); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch4/examples/4_3_praise2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_3_praise2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 03:07:57 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define PRAISE "What a super marvelous name!" 12 | 13 | int main(void) 14 | { 15 | char name[40]; 16 | 17 | printf("What's your name?\n"); 18 | scanf("%s", name); 19 | printf("Hello, %s. %s\n", name, PRAISE); 20 | printf("Your name of %d letters occupies %d memory cells.\n", 21 | strlen(name), sizeof(name)); 22 | printf("The phrase of praise has %d letters ", 23 | strlen(PRAISE)); 24 | printf("and occupies %d memory cells.\n", sizeof(PRAISE)); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch4/examples/4_4_pizza.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_4_pizza.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 03:53:53 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #define PI 3.14159 10 | 11 | int main(void) 12 | { 13 | float area, circum, radius; 14 | 15 | printf("What is the radius of your pizza?\n"); 16 | scanf("%f", &radius); 17 | area = PI * radius * radius; 18 | circum = 2.0 * PI * radius; 19 | printf("Your basic pizza parameters are as follows: \n"); 20 | printf("circumference = %1.2f, area = %1.2f\n", circum, area); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch4/examples/4_5_defines.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_5_defines.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 04:32:25 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(void) 13 | { 14 | printf("Some number limits for this system:\n"); 15 | printf("Biggest int: %d\n", INT_MAX); 16 | printf("Smallest unsigned long: %lld\n", LLONG_MIN); 17 | printf("One byte = %d bits on this system.\n", CHAR_BIT); 18 | printf("Largest double: %e\n", DBL_MAX); 19 | printf("Smallest normal float: %e\n", FLT_MIN); 20 | printf("float precision = %d digits\n", FLT_DIG); 21 | printf("float epsilon = %e\n", FLT_EPSILON); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch4/examples/4_6_printout.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_6_printout.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 04:46:06 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define PI 3.141593 11 | 12 | int main(void) 13 | { 14 | int number = 5; 15 | float expresso = 13.5; 16 | int cost = 3100; 17 | printf("The %d CEOs drank %f cups of expresso.\n", number, expresso); 18 | printf("The value of pi is %f.\n", PI); 19 | printf("Farewell! thou art too dear for my possessing, \n"); 20 | printf("%c%d\n", '$', 2 * cost); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch4/examples/4_7_width.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_7_width.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 04:56:38 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define PAGES 931 11 | 12 | int main(void) 13 | { 14 | printf("*%d*\n", PAGES); 15 | printf("*%2d*\n", PAGES); 16 | printf("*%10d*\n", PAGES); 17 | printf("*%-10d*\n", PAGES); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch4/examples/4_8_floats.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_8_floats.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 04:59:20 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const double RENT = 3852.99; 13 | 14 | printf("*%f*\n", RENT); 15 | printf("*%e*\n", RENT); 16 | printf("*%4.2f*\n", RENT); 17 | printf("*%3.1f*\n", RENT); 18 | printf("*%10.3f*\n", RENT); 19 | printf("*%10.3e*\n", RENT); 20 | printf("*%+4.2f*\n", RENT); 21 | printf("*%010.2f*\n", RENT); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch4/examples/4_9_flags.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_9_flags.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Wed 11 Jun 2014 05:05:24 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("%x %X %#x\n", 31, 31, 31); 13 | printf("**%d**% d**% d**\n", 42, 42, -42); 14 | printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ch4/exercises/4_1_print_name.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_1_print_name.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 12:27:03 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char first_name[32]; 13 | char last_name[32]; 14 | 15 | printf("Input your name(like Jack Smith): "); 16 | scanf("%s %s", first_name, last_name); 17 | printf("%s, %s\n", first_name, last_name); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch4/exercises/4_2_print_name.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* > File Name: 4_2_print_name.c 2 | > Author: Hui Wang 3 | > Mail: ncepuwanghui@gmail.com 4 | > Created Time: Thu 12 Jun 2014 12:30:55 AM CST 5 | ************************************************************************/ 6 | 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | char first_name[32]; 13 | 14 | printf("Input your first name: "); 15 | scanf("%s", first_name); 16 | printf("\"%s\"\n", first_name); 17 | printf("\"%20s\"\n", first_name); 18 | printf("\"%-20s\"\n", first_name); 19 | printf("\"%*s\"\n", strlen(first_name) + 3, first_name); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch4/exercises/4_3_print_float.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_3_print_float.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 12:40:48 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float f = 0.0f; 13 | 14 | printf("Input a float number: "); 15 | scanf("%f", &f); 16 | 17 | printf("a.The input is %3.1f or %2.1e\n", f, f); 18 | printf("b.The input is %+5.3f or %4.3E\n", f, f); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch4/exercises/4_4_print_height.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_4_print_height.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 12:46:08 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float height = 0.0f; 13 | char name[32] = {0}; 14 | 15 | printf("Please input your height and name:\n"); 16 | scanf("%f %s", &height, name); 17 | printf("%s, you are %.3f feet tall.\n", name, height / 12); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch4/exercises/4_5_print_name.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_5_print_name.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 10:56:57 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char first_name[32] = {0}; 14 | char last_name[32] = {0}; 15 | 16 | printf("Please input your first_name and last_name:\n"); 17 | scanf("%s %s", first_name, last_name); 18 | printf("%s %s\n", first_name, last_name); 19 | printf("%*d %*d\n", strlen(first_name), strlen(first_name), 20 | strlen(last_name), strlen(last_name)); 21 | printf("%s %s\n", first_name, last_name); 22 | printf("%-*d %-*d\n", strlen(first_name), strlen(first_name), 23 | strlen(last_name), strlen(last_name)); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch4/exercises/4_6_test_float_precision.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_6_test_float_precision.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 11:04:06 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | double d = 1.0 / 3.0; 14 | float f = 1.0 / 3.0; 15 | 16 | printf("float f = 1.0 / 3.0:\n"); 17 | printf("%.4f\n%.12f\n%.16f\n", f, f, f); 18 | printf("double d = 1.0 / 3.0:\n"); 19 | printf("%.4f\n%.12f\n%.16f\n", d, d, d); 20 | 21 | printf("FLT_DIG:%d\nDBL_DIG:%d\n", FLT_DIG, DBL_DIG); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch4/exercises/4_7_compute_miles_per_gallon.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 4_7_compute_miles_per_gallon.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 11:11:00 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const float PER_GALLON = 3.785f; 13 | const float PER_MILE = 1.609f; 14 | float miles = 0.0f; 15 | float gallons = 0.0f; 16 | 17 | printf("Please input the miles and gallons:\n"); 18 | scanf("%f %f", &miles, &gallons); 19 | 20 | float miles_per_gallon = miles / gallons; 21 | printf("Miles per gallon: %.1f\n", miles_per_gallon); 22 | printf("Liters per 100 kilometers: %f\n", (100 / PER_MILE) * (gallons / miles) * PER_GALLON); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch5/examples/5_10_add_one.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_10_add_one.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:18:15 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int ultra = 0, super = 0; 13 | while (super < 5) 14 | { 15 | super++; 16 | ++ultra; 17 | printf("super = %d, ultra = %d\n", super, ultra); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch5/examples/5_11_post_pre.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_11_post_pre.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:19:55 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int a = 1, b = 1; 13 | int aplus, plusb; 14 | 15 | aplus = a++; 16 | plusb = ++b; 17 | printf("a aplus b plusb \n"); 18 | printf("%1d %5d %5d %5d\n", a, aplus, b, plusb); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch5/examples/5_12_bottles.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_12_bottles.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:22:38 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define MAX 100 11 | 12 | int main(void) 13 | { 14 | int count = MAX + 1; 15 | 16 | while (--count > 0) 17 | { 18 | printf("%d bottles of spring water on the wall, " 19 | "%d bottles of spring water!\n", count, count); 20 | printf("Take one down and pass it around, \n"); 21 | printf("%d bottles of spring water!\n\n", count - 1); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch5/examples/5_13_addemup.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_13_addemup.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:25:47 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int count, sum; 13 | 14 | count = 0; 15 | sum = 0; 16 | while (count++ < 20) 17 | { 18 | sum = sum + count; 19 | } 20 | printf("sum = %d\n", sum); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch5/examples/5_14_convert.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_14_convert.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:27:11 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | int i; 14 | float fl; 15 | 16 | fl = i = ch = 'A'; 17 | printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); 18 | ch = ch + 1; 19 | i = fl + 2 * ch; 20 | fl = 2.0 * ch + i; 21 | printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); 22 | ch = 5212205.17; 23 | printf("Now ch = %c\n", ch); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch5/examples/5_15_pound.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_15_pound.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:30:05 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void pound(int n); 11 | 12 | int main(void) 13 | { 14 | int times = 5; 15 | char ch = '!'; 16 | float f = 6.0; 17 | pound(times); 18 | pound(ch); 19 | pound((int)f); 20 | 21 | return 0; 22 | } 23 | 24 | void pound(int n) 25 | { 26 | while (n-- > 0) 27 | { 28 | printf("#"); 29 | } 30 | printf("\n"); 31 | } 32 | -------------------------------------------------------------------------------- /ch5/examples/5_1_shoes1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_1_shoes1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 03:47:16 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define ADJUST 7.64 11 | #define SCALE 0.325 12 | 13 | int main(void) 14 | { 15 | double shoe, foot; 16 | shoe = 9.0; 17 | foot = SCALE * shoe + ADJUST; 18 | printf("Shoe size(men's) foot length\n"); 19 | printf("%10.1f %15.2f inches\n", shoe, foot); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch5/examples/5_2_shoes2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_2_shoes2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 03:52:22 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define ADJUST 7.64 11 | #define SCALE 0.325 12 | 13 | int main(void) 14 | { 15 | double shoe, foot; 16 | 17 | printf("Shoe size(men's) foot length\n"); 18 | shoe = 3.0; 19 | while (shoe < 18.5) 20 | { 21 | foot = SCALE * shoe + ADJUST; 22 | printf("%10.1f %15.2f inches\n", shoe, foot); 23 | shoe = shoe + 1.0; 24 | } 25 | printf("If the shoe fits, wear it.\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch5/examples/5_3_golf.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_3_golf.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 03:56:54 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int jane, tarzan, cheeta; 13 | 14 | cheeta = tarzan = jane = 68; 15 | printf(" cheeta tarzan jane\n"); 16 | printf("First round score %4d %8d %8d\n", cheeta, tarzan, jane); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch5/examples/5_4_squares.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_4_squares.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:00:12 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int num = 1; 13 | 14 | while (num < 21) 15 | { 16 | printf("%4d %6d\n", num, num * num); 17 | num = num + 1; 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch5/examples/5_5_wheat.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_5_wheat.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:01:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SQUARES 64 11 | #define CROP 1E15 12 | 13 | int main(void) 14 | { 15 | double current, total; 16 | int count = 1; 17 | 18 | printf("square grains total "); 19 | printf("fraction of \n"); 20 | printf(" added grain "); 21 | printf("US total\n"); 22 | total = current = 1.0; 23 | printf("%4d %13.2e %12.2e %12.2e\n", count, current, total, total / CROP); 24 | while (count < SQUARES) 25 | { 26 | count = count + 1; 27 | current = 2.0 * current; 28 | total = total + current; 29 | printf("%4d %13.2e %12.2e %12.2e\n", count, current, total, total / CROP); 30 | } 31 | printf("That's all.\n"); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /ch5/examples/5_6_divide.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_6_divide.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:08:09 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | printf("integer division: 5/4 is %d \n", 5/4); 13 | printf("integer division: 6/3 is %d \n", 6/3); 14 | printf("integer division: 7/4 is %d \n", 7/4); 15 | printf("floating division: 7./4. is %1.2f \n", 7./4.); 16 | printf("mixed division: 7./4 is %1.2f \n", 7./4); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch5/examples/5_7_rules.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_7_rules.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:11:10 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int top, score; 13 | top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); 14 | printf("top = %d\n", top); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ch5/examples/5_8_sizeof.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_8_sizeof.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:12:35 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n = 0; 13 | size_t intsize; 14 | intsize = sizeof(int); 15 | printf("n = %d, n has %zd bytes: all ints has %zd bytes.\n", n, sizeof(n), intsize); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /ch5/examples/5_9_min_sec.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_9_min_sec.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 04:14:06 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SEC_PER_MIN 60 11 | 12 | int main(void) 13 | { 14 | int sec, min, left; 15 | 16 | printf("Convert seconds to minutes and seconds!\n"); 17 | printf("Enter the number of seconds (<=0 to quit):\n"); 18 | scanf("%d", &sec); 19 | while (sec > 0) 20 | { 21 | min = sec / SEC_PER_MIN; 22 | left = sec % SEC_PER_MIN; 23 | printf("%d seconds is %d minutes, %d seconds.\n", sec, min, left); 24 | printf("Enter next value (<=0 to quit):\n"); 25 | scanf("%d", &sec); 26 | } 27 | printf("Done!\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ch5/exercises/5_1_minutes_to_hour.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_1_minutes_to_hour.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 05:20:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int PER_HOUR_MINUTES = 60; 13 | int minutes = 0; 14 | 15 | printf("Please input minutes(<=0 to quit): "); 16 | scanf("%d", &minutes); 17 | while (minutes > 0) 18 | { 19 | printf("%d minutes is equal to %d hour, %d minutes.\n", minutes, 20 | minutes / PER_HOUR_MINUTES, minutes % PER_HOUR_MINUTES); 21 | printf("Please input minutes(<=0 to quit): "); 22 | scanf("%d", &minutes); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch5/exercises/5_2_print_num.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_2_print_num.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 05:26:56 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int num = 0; 13 | int scope = 10; 14 | 15 | printf("Please input a number: "); 16 | scanf("%d", &num); 17 | while (scope-- >= 0) 18 | { 19 | printf("%d ", num); 20 | num++; 21 | } 22 | printf("\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch5/exercises/5_3_days_to_weeks.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_3_days_to_weeks.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 05:32:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int PER_WEEK_DAYS = 7; 13 | int days = 0; 14 | 15 | printf("Please input days(<=0 to quit): "); 16 | scanf("%d", &days); 17 | while (days > 0) 18 | { 19 | printf("%d days are %d weeks, %d days.\n", 20 | days, days / PER_WEEK_DAYS, days % PER_WEEK_DAYS); 21 | printf("Please input days(<=0 to quit): "); 22 | scanf("%d", &days); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch5/exercises/5_4_centimeter_to_feet.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_4_centimeter_to_feet.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 05:39:07 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const float CM_PER_INCH = 2.54f; 13 | const int INCHES_PER_FOOT = 12; 14 | float centimeters = 0.0f; 15 | 16 | printf("Enter a height in centimeters: "); 17 | scanf("%f", ¢imeters); 18 | while (centimeters > 0) 19 | { 20 | float inches = centimeters / CM_PER_INCH; 21 | int feet = (int) inches / INCHES_PER_FOOT; 22 | printf("%.1f cm = %d feet, %.1f inches\n", centimeters, feet, inches - (feet * INCHES_PER_FOOT)); 23 | printf("Enter a height in centimeters: "); 24 | scanf("%f", ¢imeters); 25 | } 26 | printf("bye\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch5/exercises/5_5_addemup.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_5_addemup.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 09:27:32 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int count, sum, max; 13 | 14 | count = 0; 15 | sum = 0; 16 | printf("Please input the max: "); 17 | scanf("%d", &max); 18 | while (count++ < max) 19 | { 20 | sum = sum + count; 21 | } 22 | printf("sum = %d\n", sum); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch5/exercises/5_6_add_square.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_6_add_square.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 09:30:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int count, sum, max; 13 | 14 | count = 0; 15 | sum = 0; 16 | printf("Please input the max: "); 17 | scanf("%d", &max); 18 | while (count++ < max) 19 | { 20 | sum = sum + count * count; 21 | } 22 | printf("sum = %d\n", sum); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch5/exercises/5_7_print_cubic.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_7_print_cubic.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 09:35:00 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | float cube(float n); 11 | 12 | int main(void) 13 | { 14 | float num = 0.0f; 15 | 16 | printf("Please input a float: "); 17 | scanf("%f", &num); 18 | printf("The cube of %f is %f\n", num, cube(num)); 19 | 20 | return 0; 21 | } 22 | 23 | float cube(float n) 24 | { 25 | return n * n * n; 26 | } 27 | -------------------------------------------------------------------------------- /ch5/exercises/5_8_fahrenheit_to_celsius.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 5_8_fahrenheit_to_celsius.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 12 Jun 2014 09:38:36 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void Temperatures(double temperature); 11 | 12 | int main(void) 13 | { 14 | double temperature = 0.0; 15 | 16 | printf("Please input Fahrenheit(Enter 'q' or other non-num to quit): "); 17 | while (scanf("%lf", &temperature) == 1) 18 | { 19 | Temperatures(temperature); 20 | printf("Please input Fahrenheit(Enter 'q' or other non-num to quit): "); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | void Temperatures(double temperature) 27 | { 28 | const double A = 1.8f; 29 | const double B = 32.0f; 30 | const double C = 273.16; 31 | 32 | printf("Fahrenheit: %.2f\n", temperature); 33 | printf("Celsius: %.2f\n", A * temperature + B); 34 | printf("Kelvin: %.2f\n", A * temperature + B + C); 35 | } 36 | -------------------------------------------------------------------------------- /ch6/examples/6_10_sweetie1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_10_sweetie1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:05:37 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int NUMBER = 22; 13 | int count = 1; 14 | 15 | while (count <= NUMBER) 16 | { 17 | printf("Be my Valentine!\n"); 18 | count++; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch6/examples/6_11_sweetie2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_11_sweetie2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:06:49 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int NUMBER = 22; 13 | int count; 14 | 15 | for (count = 1; count <= NUMBER; count++) 16 | { 17 | printf("Be my Valentine!\n"); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch6/examples/6_12_for_cube.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_12_for_cube.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:07:53 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int num; 13 | 14 | printf(" n n cubed\n"); 15 | for (num = 1; num <= 6; num++) 16 | { 17 | printf("%5d %5d\n", num, num * num * num); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch6/examples/6_13_postage.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_13_postage.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:10:00 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int FIRST_OZ = 37; 13 | const int NEXT_OZ = 23; 14 | int ounces, cost; 15 | 16 | printf(" ounces cost\n"); 17 | for (ounces = 1, cost = FIRST_OZ; ounces <= 16; 18 | ounces++, cost += NEXT_OZ) 19 | { 20 | printf("%5d $%4.2f\n", ounces, cost / 100.0); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch6/examples/6_14_zeno.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_14_zeno.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:13:01 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int t_ct; 13 | double time, x; 14 | int limit; 15 | 16 | printf("Enter the number of terms you want: "); 17 | scanf("%d", &limit); 18 | for (time = 0, x = 1, t_ct = 1; t_ct <= limit; t_ct++, x *= 2.0) 19 | { 20 | time += 1.0 / x; 21 | printf("time = %f when terms = %d.\n", time, t_ct); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch6/examples/6_15_do_while.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_15_do_while.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:15:55 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int secret_code = 13; 13 | int code_entered; 14 | 15 | do 16 | { 17 | printf("To enter the triskaidekaphobia therapy club, \n"); 18 | printf("please enter the secret code number: "); 19 | scanf("%d", &code_entered); 20 | } while (code_entered != secret_code); 21 | printf("Congratulations! You are cured!\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ch6/examples/6_16_entry.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_16_entry.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:18:45 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int secret_code = 13; 13 | int code_entered; 14 | printf("To enter the triskaidekaphobia therapy club, \n"); 15 | printf("please enter the secret code number: "); 16 | scanf("%d", &code_entered); 17 | while (code_entered != secret_code) 18 | { 19 | printf("To enter the triskaidekaphobia therapy club, \n"); 20 | printf("please enter the secret code number: "); 21 | scanf("%d", &code_entered); 22 | } 23 | printf("Congratulations! You are cured!\n"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch6/examples/6_17_rows1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_17_rows1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:21:47 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define ROWS 6 11 | #define CHARS 10 12 | 13 | int main(void) 14 | { 15 | int row; 16 | char ch; 17 | 18 | for (row = 0; row < ROWS; row++) 19 | { 20 | for (ch = 'A'; ch < ('A' + CHARS); ch++) 21 | { 22 | printf("%c", ch); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch6/examples/6_18_rows2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_18_rows2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:23:58 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int ROWS = 6; 13 | const int CHARS = 6; 14 | int row; 15 | char ch; 16 | 17 | for (row = 0; row < ROWS; row++) 18 | { 19 | for (ch = ('A' + row); ch < ('A' + CHARS); ch++) 20 | { 21 | printf("%c", ch); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch6/examples/6_19_scores_in.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_19_scores_in.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:26:20 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SIZE 10 11 | #define PAR 72 12 | 13 | int main(void) 14 | { 15 | int index, score[SIZE]; 16 | int sum = 0; 17 | float average; 18 | 19 | printf("Enter %d golf scores:\n", SIZE); 20 | for (index = 0; index < SIZE; index++) 21 | { 22 | scanf("%d", &score[index]); 23 | } 24 | printf("The scores read in are as follows:\n"); 25 | for (index = 0; index < SIZE; index++) 26 | { 27 | printf("%5d", score[index]); 28 | } 29 | printf("\n"); 30 | for (index = 0; index < SIZE; index++) 31 | { 32 | sum += score[index]; 33 | } 34 | average = (float) sum / SIZE; 35 | printf("Sum of scores = %d, average = %.2f\n", sum, average); 36 | printf("That's a handicap of %.0f.\n", average - PAR); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /ch6/examples/6_1_summing.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_1_summing.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:37:55 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | long num; 13 | long sum = 0L; 14 | int status; 15 | 16 | printf("Please enter an integer to be summed"); 17 | printf("(q to quit): "); 18 | status = scanf("%ld", &num); 19 | while (1 == status) 20 | { 21 | sum = sum + num; 22 | printf("Please enter next integer (q to quit): "); 23 | status = scanf("%ld", &num); 24 | } 25 | printf("Those integers sum to %ld.\n", sum); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch6/examples/6_20_power.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_20_power.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:32:38 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double power(double n, int p); 11 | 12 | int main(void) 13 | { 14 | double x, xpow; 15 | int exp; 16 | 17 | printf("Enter a number and the positive integer power"); 18 | printf(" to which\nthe number will be raised. Enter q"); 19 | printf(" to quit.\n"); 20 | while (2 == scanf("%lf %d", &x, &exp)) 21 | { 22 | xpow = power(x, exp); 23 | printf("%.3g to the power %d is %.5g\n", x, exp, xpow); 24 | printf("Enter next pair of number or q to quit.\n"); 25 | } 26 | printf("Hope you enjoyed this poer trip -- bye!\n"); 27 | 28 | return 0; 29 | } 30 | 31 | double power(double n, int p) 32 | { 33 | double pow = 1.0; 34 | int i; 35 | 36 | for (i = 1; i <= p; i++) 37 | { 38 | pow *= n; 39 | } 40 | 41 | return pow; 42 | } 43 | -------------------------------------------------------------------------------- /ch6/examples/6_2_when.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_2_when.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:41:30 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n = 5; 13 | while (n < 7) 14 | { 15 | printf("n = %d\n", n); 16 | n++; 17 | printf("Now n = %d\n", n); 18 | } 19 | printf("The loop has finished.\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch6/examples/6_3_while.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_3_while.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:43:07 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n = 0; 13 | 14 | //错误写法如下,不管是简单语句还是复合语句都应该加花括号 15 | while (n < 3) 16 | printf("n is %d\n", n); 17 | n++; 18 | printf("That's all this program does\n"); 19 | 20 | //不要在while后加分号,如果确实需要加分号表示空语句,应该另起一行,明显地表示 21 | n = 0; 22 | while (n++ < 3); 23 | printf("n is %d\n", n); 24 | printf("That's all this program does\n"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch6/examples/6_5_cmpflt.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_5_cmpflt.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:49:06 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | const double ANSWER = 3.14159; 14 | double response; 15 | printf("What is the value of pi?\n"); 16 | scanf("%lf", &response); 17 | while (fabs(response - ANSWER) > 0.0001) 18 | { 19 | printf("Try again!\n"); 20 | scanf("%lf", &response); 21 | } 22 | printf("Close enough!\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch6/examples/6_6_t_and_f.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_6_t_and_f.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:51:52 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int true_val, false_val; 13 | 14 | true_val = (10 > 2); 15 | false_val = (10 == 2); 16 | printf("true = %d; false = %d\n", true_val, false_val); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ch6/examples/6_7_truth.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_7_truth.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 12:53:33 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int n = 3; 13 | 14 | while (n) 15 | { 16 | printf("%2d is true\n", n--); 17 | } 18 | printf("%2d is false\n", n); 19 | n = -3; 20 | while (n) 21 | { 22 | printf("%2d is true\n", n++); 23 | } 24 | printf("%2d is false\n", n); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch6/examples/6_8_trouble.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/henrypoter/C_Primer/bc2b5c1643fd331633fb802cfc02e26014b2615e/ch6/examples/6_8_trouble.c -------------------------------------------------------------------------------- /ch6/examples/6_9_boolean.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_9_boolean.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 01:02:00 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | long num; 13 | long sum = 0L; 14 | _Bool input_is_good; 15 | printf("Please enter an integer to be summed "); 16 | printf("(q to quit): "); 17 | 18 | input_is_good = (1 == scanf("%ld", &num)); 19 | while (input_is_good) 20 | { 21 | sum = sum + num; 22 | printf("Please enter next integer (q to quit): "); 23 | input_is_good = (1 == scanf("%ld", &num)); 24 | } 25 | printf("Those integers sum to %ld.\n", sum); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch6/exercises/6_10_print_integer_reversed_order.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_10_print_integer_reversed_order.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 01:08:15 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define NUM 8 11 | 12 | int main(void) 13 | { 14 | int integer[NUM]; 15 | int i = 0; 16 | 17 | printf("Please input %d integers:\n", NUM); 18 | for (i = 0; i < NUM; i++) 19 | { 20 | scanf("%d", &integer[i]); 21 | } 22 | for (i = NUM - 1; i >= 0; i--) 23 | { 24 | printf("%d ", integer[i]); 25 | } 26 | printf("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch6/exercises/6_11_series_sum.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_11_series_sum.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 01:14:03 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int count = 0, sign = 0; 13 | double i = 0.0, sum1 = 0.0, sum2 = 0.0; 14 | 15 | printf("Please input the max times: "); 16 | 17 | while (1 == scanf("%d", &count) && count > 0) 18 | { 19 | sum1 = 0.0; 20 | sum2 = 0.0; 21 | for (i = 1.0, sign = 1; i < count; i++, sign *= -1) 22 | { 23 | sum1 += 1.0 / i; 24 | sum2 += sign * 1.0 / i; 25 | } 26 | printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 ... = %lf\n", sum1); 27 | printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 ... = %lf\n", sum2); 28 | printf("Please input the max times: "); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /ch6/exercises/6_12_print_power.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_12_print_power.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 03:06:29 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | const int SIZE = 8; 14 | int array[SIZE]; 15 | int i = 0; 16 | 17 | for (i = 0; i < SIZE; i++) 18 | { 19 | array[i] = pow(2, i); 20 | } 21 | i = 0; 22 | do 23 | { 24 | printf("%d\t", array[i]); 25 | } while (++i < SIZE); 26 | printf("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch6/exercises/6_13_get_double_sum.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_13_get_double_sum.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 03:18:18 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int SIZE = 8; 13 | double num[SIZE], sum[SIZE]; 14 | int i = 0; 15 | double pre_sum = 0.0; 16 | 17 | for(i = 0; i < SIZE; i++) 18 | { 19 | scanf("%lf", &num[i]); 20 | sum[i] = pre_sum + num[i]; 21 | pre_sum = sum[i]; 22 | } 23 | 24 | for (i = 0; i < SIZE; i++) 25 | { 26 | printf("%8.3f", num[i]); 27 | } 28 | printf("\n"); 29 | 30 | for (i = 0; i < SIZE; i++) 31 | { 32 | printf("%8.3f", sum[i]); 33 | } 34 | printf("\n"); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /ch6/exercises/6_14_print_line_string.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_14_print_line_string.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 03:28:51 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | const int SIZE = 255; 14 | char line[SIZE]; 15 | int i = 0; 16 | 17 | printf("Please input a line:\n"); 18 | while (scanf("%c", &line[i]) && line[i] != '\n') 19 | { 20 | i++; 21 | } 22 | line[i] = '\0'; 23 | 24 | for (i = strlen(line) - 1; i >= 0; i--) 25 | { 26 | printf("%c", line[i]); 27 | } 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /ch6/exercises/6_15_compute_interest.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_15_compute_interest.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 03:38:59 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | double Daphne = 100.0; 13 | double Deirdre = 100.0; 14 | int year = 0; 15 | 16 | while (Daphne >= Deirdre) 17 | { 18 | Daphne += 100.0 * 0.1; 19 | Deirdre += Deirdre * 0.05; 20 | year++; 21 | } 22 | 23 | printf("After %d years, Deirdre's investment is %lf, Daphne's investment is %lf\n", year, Deirdre, Daphne); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ch6/exercises/6_16_how_many_years.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_16_how_many_years.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 03:49:40 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | double total = 100.0; 13 | int year = 0; 14 | 15 | while (total > 0) 16 | { 17 | total += total * 0.08; 18 | total -= 10.0; 19 | year++; 20 | } 21 | 22 | printf("After %d years, Chuckie's account will clear\n", year); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch6/exercises/6_1_print_array.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_1_print_array.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 03:07:13 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char letters[26] = {0}; 13 | int i = 0; 14 | 15 | for (i = 0; i < 26; ++i) 16 | { 17 | letters[i] = i + 'A'; 18 | } 19 | 20 | for (i = 0; i < 26; ++i) 21 | { 22 | printf("%c ", letters[i]); 23 | } 24 | printf("\n"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch6/exercises/6_2_print_triangle.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_2_print_triangle.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 03:13:01 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int level = 0; 13 | int i = 0, j = 0; 14 | 15 | printf("Please input the level of triangle: "); 16 | scanf("%d", &level); 17 | for (i = 0; i < level; ++i) 18 | { 19 | j = i + 1; 20 | while (j-- > 0) 21 | { 22 | printf("$"); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch6/exercises/6_3_print_letter_triangle.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_3_print_letter_triangle.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 03:19:55 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int level = 0; 13 | int i = 0, j = 0; 14 | char ch = 'A'; 15 | 16 | printf("Please input the level: "); 17 | scanf("%d", &level); 18 | for (i = 1; i <= level; ++i) 19 | { 20 | for (j = 1; j <= i; j++) 21 | { 22 | printf("%c", ch + level - j); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch6/exercises/6_4_print_pyramid.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_4_print_pyramid.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 13 Jun 2014 03:30:22 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void print_per_level(int cur_level, int total_level); 11 | 12 | int main(void) 13 | { 14 | char ch; 15 | int level = 0; 16 | int i = 0, j = 0; 17 | 18 | do 19 | { 20 | printf("Please input a upper letter: "); 21 | scanf("%c", &ch); 22 | } while ( ch < 'A' || ch > 'Z'); 23 | 24 | level = ch - 'A' + 1; 25 | for (i = 0; i < level; ++i) 26 | { 27 | for (j = 1; j < level - i; j++) 28 | { 29 | printf(" "); 30 | } 31 | for (j = 0; j <= i; j++) 32 | { 33 | printf("%c", 'A' + j); 34 | } 35 | for (j = i - 1; j >= 0; j--) 36 | { 37 | printf("%c", 'A' + j); 38 | } 39 | printf("\n"); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /ch6/exercises/6_5_print_table.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_5_print_table.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 12:23:52 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int low = 0, high = 0; 13 | int i = 0; 14 | 15 | printf("Please input the low and high: "); 16 | scanf("%d %d", &low, &high); 17 | 18 | printf("%10s %10s %10s\n", "integer", "square", "cube"); 19 | for (i = low; i <= high; i++) 20 | { 21 | printf("%10d %10d %10d\n", i, i * i, i * i * i); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch6/exercises/6_6_print_word.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_6_print_word.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 12:31:57 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char word[32] = {0}; 14 | int i = 0; 15 | 16 | printf("Please input a word: "); 17 | scanf("%s", word); 18 | for (i = strlen(word) - 1; i >= 0; i--) 19 | { 20 | printf("%c", word[i]); 21 | } 22 | printf("\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ch6/exercises/6_7_compute_float.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_7_compute_float.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 12:43:20 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float a = 0.0f, b = 0.0f; 13 | 14 | printf("Please input two floats:\n"); 15 | while (2 == scanf("%f %f", &a, &b)) 16 | { 17 | printf("(%.2f - %.2f) / (%.2f * %.2f) = %.2f\n", a, b, a, b, (a-b)/(a*b)); 18 | printf("Please input two floats:\n"); 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ch6/exercises/6_8_compute_float.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_8_compute_float.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 12:53:17 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | float compute(float a, float b); 11 | 12 | int main(void) 13 | { 14 | float a = 0.0f, b = 0.0f; 15 | 16 | printf("Please input two floats:\n"); 17 | while (2 == scanf("%f %f", &a, &b)) 18 | { 19 | printf("(%.2f - %.2f) / (%.2f * %.2f) = %.2f\n", a, b, a, b, compute(a, b)); 20 | printf("Please input two floats:\n"); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | float compute(float a, float b) 27 | { 28 | return (a - b) / (a * b); 29 | } 30 | -------------------------------------------------------------------------------- /ch6/exercises/6_9_square_sum.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 6_9_square_sum.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 12:54:35 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int min = 0, max = 0; 13 | int i = 0, sum = 0; 14 | 15 | printf("Enter lower and upper integer limits: "); 16 | while (2 == scanf("%d %d", &min, &max) && min < max) 17 | { 18 | sum = 0; 19 | for (i = min; i <= max; ++i) 20 | { 21 | sum += i * i; 22 | } 23 | printf("The sums of the squares from %d to %d is %d\n", min, max, sum); 24 | printf("Enter next set of limits: "); 25 | } 26 | printf("Done\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch7/examples/7_10_break.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_10_break.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 10:40:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | float length, width; 13 | 14 | printf("Enter the length of the rectangle: \n"); 15 | while (1 == scanf("%f", &length)) 16 | { 17 | printf("Length = %0.2f: \n", length); 18 | printf("Enter its width: \n"); 19 | if (1 != scanf("%f", &width)) 20 | { 21 | break; 22 | } 23 | printf("Width = %0.2f: \n", width); 24 | printf("Area = %0.2f: \n", length * width); 25 | printf("Enter the length of the rectangle: \n"); 26 | } 27 | printf("Done.\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ch7/examples/7_1_colddays.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_1_colddays.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 09:15:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const int FREEZING = 0; 13 | float temperature; 14 | int cold_days = 0; 15 | int all_days = 0; 16 | 17 | printf("Enter the list of daily low temperatures.\n"); 18 | printf("Use Celsius, and enter q to quit.\n"); 19 | while (1 == scanf("%f", &temperature)) 20 | { 21 | all_days++; 22 | if (temperature < FREEZING) 23 | { 24 | cold_days++; 25 | } 26 | } 27 | if (all_days != 0) 28 | { 29 | printf("%d days total: %.1f%% were below freezing.\n", all_days, 100.0 * (float) cold_days / all_days); 30 | } 31 | if (0 == all_days) 32 | { 33 | printf("No data entered.\n"); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /ch7/examples/7_2_cypher1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_2_cypher1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 09:21:39 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define SPACE ' ' 11 | 12 | int main(void) 13 | { 14 | char ch; 15 | 16 | ch = getchar(); 17 | while (ch != '\n') 18 | { 19 | if (ch == SPACE) 20 | { 21 | putchar(ch); 22 | } 23 | else 24 | { 25 | putchar(ch + 1); 26 | } 27 | ch = getchar(); 28 | } 29 | putchar(ch); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /ch7/examples/7_3_cypher2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_3_cypher2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 09:24:21 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char ch; 14 | 15 | while ((ch = getchar()) != '\n') 16 | { 17 | if (isalpha(ch)) 18 | { 19 | putchar(ch + 1); 20 | } 21 | else 22 | { 23 | putchar(ch); 24 | } 25 | } 26 | putchar(ch); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ch7/examples/7_4_electric.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_4_electric.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 09:26:47 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define RATE1 0.12589 11 | #define RATE2 0.17901 12 | #define RATE3 0.20971 13 | #define BREAK1 360.0 14 | #define BREAK2 680.0 15 | #define BASE1 (RATE1 * BREAK1) 16 | #define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1))) 17 | 18 | int main(void) 19 | { 20 | double kwh; 21 | double bill; 22 | 23 | printf("Please enter the kwh used.\n"); 24 | scanf("%lf", &kwh); 25 | if (kwh <= BREAK1) 26 | { 27 | bill = RATE1 * kwh; 28 | } 29 | else if (kwh <= BREAK2) 30 | { 31 | bill = BASE1 + (RATE2 * (kwh - BREAK1)); 32 | } 33 | else 34 | { 35 | bill = BASE2 + (RATE3 * (kwh - BREAK2)); 36 | } 37 | printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /ch7/examples/7_6_chcount.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_6_chcount.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 09:41:34 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define PERIOD '.' 11 | 12 | int main(void) 13 | { 14 | int ch; 15 | int charcount = 0; 16 | 17 | while ((ch = getchar()) != PERIOD) 18 | { 19 | if (ch != '"' && ch != '\'') 20 | { 21 | charcount++; 22 | } 23 | } 24 | printf("There are %d non-quote characters.\n", charcount); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch7/examples/7_7_wordcnt.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_7_wordcnt.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 10:19:38 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define STOP '|' 13 | 14 | int main(void) 15 | { 16 | char c; 17 | char prev; 18 | long n_chars = 0L; 19 | int n_lines = 0; 20 | int n_words = 0; 21 | int p_lines = 0; 22 | bool inword = false; 23 | 24 | printf("Enter text to be analyzed(| to terminate): \n"); 25 | prev = '\n'; 26 | while ((c = getchar()) != STOP) 27 | { 28 | n_chars++; 29 | if (c == '\n') 30 | { 31 | n_lines++; 32 | } 33 | if (!isspace(c) && !inword) 34 | { 35 | inword = true; 36 | n_words++; 37 | } 38 | if (isspace(c) && inword) 39 | { 40 | inword = false; 41 | } 42 | prev = c; 43 | } 44 | if (prev != '\n') 45 | { 46 | p_lines = 1; 47 | } 48 | printf("characters = %ld, words = %d, lines = %d, ", n_chars, n_words, n_lines); 49 | printf("partial lines = %d\n", p_lines); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /ch7/examples/7_8_paint.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_8_paint.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 10:27:25 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define COVERAGE 200 11 | 12 | int main(void) 13 | { 14 | int sq_feet; 15 | int cans; 16 | 17 | printf("Enter number of square feet to be painted:\n"); 18 | while (1 == scanf("%d", &sq_feet)) 19 | { 20 | cans = sq_feet / COVERAGE; 21 | cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1; 22 | printf("You need %d %s of paint.\n", cans, cans == 1 ? "can" : "cans"); 23 | printf("Enter next value (q to quit): \n"); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ch7/exercises/7_1_word_count.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_1_word_count.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Mon 16 Jun 2014 11:18:27 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | int blank_cnt = 0; 14 | int line_cnt = 0; 15 | int char_cnt = 0; 16 | 17 | while ((ch = getchar()) != '#') 18 | { 19 | switch (ch) 20 | { 21 | case ' ': 22 | blank_cnt++; 23 | break; 24 | case '\n': 25 | line_cnt++; 26 | default: 27 | char_cnt++; 28 | } 29 | } 30 | printf("blank: %d\nline: %d\nchar: %d\n", blank_cnt, line_cnt, char_cnt); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ch7/exercises/7_2_print_char_ascii.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_2_print_char_ascii.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 09:56:08 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | int count = 0; 14 | 15 | while ((ch = getchar()) != '#') 16 | { 17 | count++; 18 | printf("%c/%-5d", ch, ch); 19 | if (0 == (count % 8)) 20 | { 21 | printf("\n"); 22 | count = 0; 23 | } 24 | } 25 | printf("\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch7/exercises/7_3_count_odd_even.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_3_count_odd_even.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 10:06:42 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int odd = 0, even = 0; 13 | int odd_sum = 0, even_sum = 0; 14 | int num = 0; 15 | 16 | while ((1 == scanf("%d", &num)) && num != 0) 17 | { 18 | if (num % 2) 19 | { 20 | odd++; 21 | odd_sum += num; 22 | } 23 | else 24 | { 25 | even++; 26 | even_sum += num; 27 | } 28 | } 29 | printf("even count: %d\taverage: %.2f\n", even, (float)even_sum / even); 30 | printf("odd count: %d\taverage: %.2f\n", odd, (float)odd_sum / odd); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ch7/exercises/7_4_if_else.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_4_if_else.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 10:15:39 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | int cnt1 = 0, cnt2 = 0; 14 | 15 | while ((ch = getchar()) != '#') 16 | { 17 | if ('.' == ch) 18 | { 19 | putchar('!'); 20 | cnt1++; 21 | } 22 | else if ('!' == ch) 23 | { 24 | putchar('!'); 25 | putchar('!'); 26 | cnt2++; 27 | } 28 | } 29 | printf("\nthe times of '.' replaced by '!': %d\n", cnt1); 30 | printf("the times of '!' replaced by '!!': %d\n", cnt2); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /ch7/exercises/7_5_count_odd_even.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_5_count_odd_even.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 11:22:14 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int odd = 0, even = 0; 13 | int odd_sum = 0, even_sum = 0; 14 | int num = 0; 15 | 16 | while ((1 == scanf("%d", &num)) && num != 0) 17 | { 18 | switch (num % 2) 19 | { 20 | case 0: 21 | even++; 22 | even_sum += num; 23 | break; 24 | case 1: 25 | odd++; 26 | odd_sum += num; 27 | break; 28 | default: 29 | printf("Invalid data!\n"); 30 | } 31 | } 32 | printf("odd count: %d\t average: %.2f\n", odd, (float)odd_sum / odd); 33 | printf("even count: %d\t average: %.2f\n", even, (float)even_sum / even); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /ch7/exercises/7_6_count_ei.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_6_count_ei.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 11:38:22 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | const char cmp[] = "ei"; 13 | char cur, pre; 14 | int cnt = 0; 15 | 16 | pre = ' '; 17 | while ((cur = getchar()) != '#') 18 | { 19 | if (pre == cmp[0] && cur == cmp[1]) 20 | { 21 | cnt++; 22 | } 23 | pre = cur; 24 | } 25 | printf("Count of %s : %d\n", cmp, cnt); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ch7/exercises/7_7_salary.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_7_salary.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 11:47:59 AM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define BASE 10.00 11 | #define RATE 1.5 12 | #define TAX_RATE1 0.15 13 | #define TAX_RATE2 0.20 14 | #define TAX_RATE3 0.25 15 | 16 | int main(void) 17 | { 18 | float hours = 0.0f; 19 | float salary = 0.0f; 20 | float tax = 0.0f; 21 | 22 | printf("Please input the hours: "); 23 | scanf("%f", &hours); 24 | 25 | if (hours > 40) 26 | { 27 | hours = (hours - 40) * (float)RATE + 40; 28 | } 29 | salary = (float)BASE * hours; 30 | if (salary <= 300) 31 | { 32 | tax = salary * (float)TAX_RATE1; 33 | } 34 | else if (salary <= 450) 35 | { 36 | tax = 300.0f * (float)TAX_RATE1 + (salary - 300.0f) * TAX_RATE2; 37 | } 38 | else 39 | { 40 | tax = 300.0f * (float)TAX_RATE1 + 150.0f * (float)TAX_RATE2 41 | + (salary - 450.0f) * (float)TAX_RATE3; 42 | } 43 | 44 | printf("Salary: %.2f\n", salary); 45 | printf("Tax: %.2f\n", tax); 46 | printf("Net pay: %.2f\n", salary - tax); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /ch7/exercises/7_9_get_prime.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 7_9_get_prime.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Tue 17 Jun 2014 04:42:49 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | bool is_prime(int integer); 12 | 13 | int main(void) 14 | { 15 | int num = 0; 16 | int i = 0; 17 | 18 | printf("Please input a integer: "); 19 | scanf("%d", &num); 20 | 21 | for (i = 2; i <= num; i++) 22 | { 23 | if (is_prime(i)) 24 | { 25 | printf("%d ", i); 26 | } 27 | } 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | 33 | bool is_prime(int integer) 34 | { 35 | int i = 0; 36 | for (i = 2; i * i <= integer; i++) 37 | { 38 | if (!(integer % i)) 39 | { 40 | return false; 41 | } 42 | } 43 | return true; 44 | } 45 | -------------------------------------------------------------------------------- /ch8/examples/8_1_echo.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_1_echo.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 04:23:07 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | 14 | while ((ch = getchar()) != '#') 15 | { 16 | putchar(ch); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch8/examples/8_2_echo_eof.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_2_echo_eof.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 04:24:41 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int ch; 13 | 14 | while ((ch = getchar()) != EOF) 15 | { 16 | putchar(ch); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /ch8/examples/8_3_file_eof.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_3_file_eof.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 04:26:33 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | int ch; 14 | FILE * fp; 15 | char fname[50]; 16 | 17 | printf("Enter the name of the file: "); 18 | scanf("%s", fname); 19 | fp = fopen(fname, "r"); 20 | if (fp == NULL) 21 | { 22 | printf("Failed to open file. Bye\n"); 23 | exit(1); 24 | } 25 | while ((ch = getc(fp)) != EOF) 26 | { 27 | putchar(ch); 28 | } 29 | fclose(fp); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /ch8/examples/8_4_guess.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_4_guess.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 04:30:45 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int guess = 1; 13 | printf("Pick an integer from 1 to 100. I will try to guess it.\n"); 14 | printf("Respond with a y if my guess is right and with an n if it is wrong.\n"); 15 | printf("Uh...is your number %d\n", guess); 16 | while (getchar() != 'y') 17 | { 18 | printf("Well, then, is it %d?\n", ++guess); 19 | } 20 | printf("I knew I could do it!\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ch8/exercises/8_1_count_char.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_1_count_char.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 09:19:10 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int cnt = 0; 13 | 14 | while (getchar() != EOF) 15 | { 16 | cnt++; 17 | } 18 | printf("Characters: %d\n", cnt); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ch8/exercises/8_2_print_ascii.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_2_print_ascii.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 10:19:50 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | char ch; 13 | int i = 0; 14 | 15 | while ((ch = getchar()) != EOF) 16 | { 17 | if (ch >= ' ') 18 | { 19 | printf("%-5c", ch); 20 | } 21 | else if (ch == '\n') 22 | { 23 | printf("%-5s", "\\n"); 24 | } 25 | else if (ch == '\t') 26 | { 27 | printf("%-5s", "\\t"); 28 | } 29 | else 30 | { 31 | printf("^%-4c", ch + 64); 32 | } 33 | printf("%-5d", ch); 34 | 35 | i++; 36 | if (i % 8 == 0) 37 | { 38 | i = 0; 39 | printf("\n"); 40 | } 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /ch8/exercises/8_3_count_lower_upper.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_3_count_lower_upper.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Thu 19 Jun 2014 10:36:57 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | char ch; 14 | int lower_cnt = 0, upper_cnt = 0; 15 | 16 | while ((ch = getchar()) != EOF) 17 | { 18 | if (islower(ch)) 19 | { 20 | lower_cnt++; 21 | } 22 | else if (isupper(ch)) 23 | { 24 | upper_cnt++; 25 | } 26 | } 27 | printf("Lower: %d\n", lower_cnt); 28 | printf("Upper: %d\n", upper_cnt); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ch8/exercises/8_4_word_letters_count.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_4_word_letters_count.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 20 Jun 2014 03:38:46 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(void) 13 | { 14 | int word_cnt = 0, letter_cnt = 0; 15 | bool inword = false; 16 | char ch; 17 | 18 | while ((ch = getchar()) != EOF) 19 | { 20 | if (isalpha(ch)) 21 | { 22 | if (!inword) 23 | { 24 | inword = true; 25 | word_cnt++; 26 | } 27 | letter_cnt++; 28 | } 29 | else 30 | { 31 | inword = false; 32 | } 33 | } 34 | printf("\n"); 35 | printf("Words: %d\n", word_cnt); 36 | printf("Letters: %d\n", letter_cnt); 37 | printf("The average number of letters per word: %.1f\n", (float)letter_cnt / word_cnt); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /ch8/exercises/8_6_get_first.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 8_6_get_first.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Fri 20 Jun 2014 05:41:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | char get_first(void); 12 | 13 | int main(void) 14 | { 15 | char ch; 16 | 17 | while ((ch = get_first()) != EOF) 18 | { 19 | printf("%c", ch); 20 | } 21 | printf("\n"); 22 | 23 | return 0; 24 | } 25 | 26 | char get_first(void) 27 | { 28 | char ch; 29 | 30 | while ( (ch = getchar()) && isspace(ch)) 31 | { 32 | continue; 33 | } 34 | 35 | return ch; 36 | } 37 | -------------------------------------------------------------------------------- /ch9/examples/9_12_loccheck.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_12_loccheck.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:45:21 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void mikado(int); 11 | 12 | int main(void) 13 | { 14 | int pooh = 2, bah = 5; 15 | 16 | printf("In main(), pooh = %d and &pooh = %p\n", pooh, &pooh); 17 | printf("In main(), bah = %d and &bah = %p\n", bah, &bah); 18 | mikado(pooh); 19 | 20 | return 0; 21 | } 22 | 23 | void mikado(int bah) 24 | { 25 | int pooh = 10; 26 | 27 | printf("In mikado(), pooh = %d and &pooh = %p\n", pooh, &pooh); 28 | printf("In mikado(), bah = %d and &bah = %p\n", bah, &bah); 29 | } 30 | -------------------------------------------------------------------------------- /ch9/examples/9_13_swap1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_13_swap1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:49:13 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void interchange(int u, int v); 11 | 12 | int main(void) 13 | { 14 | int x = 5, y = 10; 15 | printf("Originally x = %d and y = %d.\n", x, y); 16 | interchange(x, y); 17 | printf("Now x = %d and y = %d.\n", x, y); 18 | 19 | return 0; 20 | } 21 | 22 | void interchange(int u, int v) 23 | { 24 | int temp; 25 | 26 | temp = u; 27 | u = v; 28 | v = temp; 29 | } 30 | -------------------------------------------------------------------------------- /ch9/examples/9_14_swap2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_14_swap2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:51:16 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void interchange(int u, int v); 11 | 12 | int main(void) 13 | { 14 | int x = 5, y = 10; 15 | 16 | printf("Originally x = %d and y = %d.\n", x, y); 17 | interchange(x, y); 18 | printf("Now x = %d and y = %d.\n", x, y); 19 | 20 | return 0; 21 | } 22 | 23 | void interchange(int u, int v) 24 | { 25 | int temp; 26 | 27 | printf("Originally u = %d and v = %d.\n", u, v); 28 | temp = u; 29 | u = v; 30 | v = temp; 31 | printf("Now u = %d and v = %d.\n", u, v); 32 | } 33 | -------------------------------------------------------------------------------- /ch9/examples/9_15_swap3.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_15_swap3.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:54:32 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void interchange(int * u, int * v); 11 | 12 | int main(void) 13 | { 14 | int x = 5, y = 10; 15 | 16 | printf("Originally x = %d and y = %d.\n", x, y); 17 | interchange(&x, &y); 18 | printf("Now x = %d and y = %d.\n", x, y); 19 | 20 | return 0; 21 | } 22 | 23 | void interchange(int * u, int * v) 24 | { 25 | int temp; 26 | 27 | temp = *u; 28 | *u = *v; 29 | *v = temp; 30 | } 31 | -------------------------------------------------------------------------------- /ch9/examples/9_1_lethead1.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_1_lethead1.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 08:19:29 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | #define NAME "GIGATHINK, INC" 11 | #define ADDRESS "101 Megabuck Plaza" 12 | #define PLACE "Megapolis, CA 94904" 13 | #define WIDTH 40 14 | 15 | void starbar(void); 16 | 17 | int main(void) 18 | { 19 | starbar(); 20 | printf("%s\n", NAME); 21 | printf("%s\n", ADDRESS); 22 | printf("%s\n", PLACE); 23 | starbar(); 24 | 25 | return 0; 26 | } 27 | 28 | void starbar(void) 29 | { 30 | int count; 31 | 32 | for (count = 1; count <= WIDTH; count++) 33 | { 34 | putchar('*'); 35 | } 36 | putchar('\n'); 37 | } 38 | -------------------------------------------------------------------------------- /ch9/examples/9_2_lethead2.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_2_lethead2.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 08:24:12 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | #define NAME "GIGATHINK, INC" 12 | #define ADDRESS "101 Megabuck Plaza" 13 | #define PLACE "Megapolis, CA, 94904" 14 | #define WIDTH 40 15 | #define SPACE ' ' 16 | 17 | 18 | void show_n_char(char ch, int num); 19 | 20 | int main(void) 21 | { 22 | int spaces; 23 | 24 | show_n_char('*', WIDTH); 25 | putchar('\n'); 26 | 27 | spaces = (WIDTH - strlen(NAME)) / 2; 28 | show_n_char(SPACE, spaces); 29 | printf("%s\n", NAME); 30 | 31 | spaces = (WIDTH - strlen(ADDRESS)) / 2; 32 | show_n_char(SPACE, spaces); 33 | printf("%s\n", ADDRESS); 34 | 35 | spaces = (WIDTH - strlen(PLACE)) / 2; 36 | show_n_char(SPACE, spaces); 37 | printf("%s\n", PLACE); 38 | 39 | show_n_char('*', WIDTH); 40 | putchar('\n'); 41 | 42 | return 0; 43 | } 44 | 45 | void show_n_char(char ch, int num) 46 | { 47 | int count; 48 | 49 | for (count = 1; count <= num; count++) 50 | { 51 | putchar(ch); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /ch9/examples/9_3_lesser.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_3_lesser.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 08:40:43 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int imin(int, int); 11 | 12 | int main(void) 13 | { 14 | int evil1, evil2; 15 | 16 | printf("Enter a pair of integers (q to quit): \n"); 17 | 18 | while (2 == scanf("%d %d", &evil1, &evil2)) 19 | { 20 | printf("The lesser of %d and %d is %d.\n", evil1, evil2, imin(evil1, evil2)); 21 | printf("Enter a pair of integers (q to quit): \n"); 22 | } 23 | printf("Bye.\n"); 24 | 25 | return 0; 26 | } 27 | 28 | int imin(int n, int m) 29 | { 30 | int min; 31 | 32 | if (n < m) 33 | { 34 | min = n; 35 | } 36 | else 37 | { 38 | min = m; 39 | } 40 | 41 | return min; 42 | } 43 | -------------------------------------------------------------------------------- /ch9/examples/9_5_proto.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_5_proto.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 08:48:41 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | int imax(int, int); 11 | 12 | int main(void) 13 | { 14 | printf("The maximum of %d and %d is %d.\n", 3, 5, imax(3, 5)); 15 | printf("The maximum of %d and %d is %d.\n", 3, 5, imax(3.0, 5.0)); 16 | 17 | return 0; 18 | } 19 | 20 | int imax(int n, int m) 21 | { 22 | int max; 23 | 24 | if (n > m) 25 | { 26 | max = n; 27 | } 28 | else 29 | { 30 | max = m; 31 | } 32 | 33 | return max; 34 | } 35 | -------------------------------------------------------------------------------- /ch9/examples/9_6_recur.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_6_recur.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 08:51:00 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void up_and_down(int); 11 | 12 | int main(void) 13 | { 14 | up_and_down(1); 15 | 16 | return 0; 17 | } 18 | 19 | void up_and_down(int n) 20 | { 21 | printf("Level %d: n location %p\n", n, &n); 22 | if (n < 4) 23 | { 24 | up_and_down(n+1); 25 | } 26 | printf("LEVEL %d: n location %p\n", n, &n); 27 | } 28 | -------------------------------------------------------------------------------- /ch9/examples/9_8_binary.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_8_binary.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:22:59 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void to_binary(unsigned long n); 11 | 12 | int main(void) 13 | { 14 | unsigned long number; 15 | printf("Enter an integer (q to quit): \n"); 16 | while (1 == scanf("%lu", &number)) 17 | { 18 | printf("Binary equivalent: "); 19 | to_binary(number); 20 | putchar('\n'); 21 | printf("Enter an integer (q to quit): \n"); 22 | } 23 | printf("Done.\n"); 24 | 25 | return 0; 26 | } 27 | 28 | void to_binary(unsigned long n) 29 | { 30 | int r; 31 | 32 | r = n % 2; 33 | if (n >= 2) 34 | { 35 | to_binary(n / 2); 36 | } 37 | putchar('0' + r); 38 | 39 | return; 40 | } 41 | -------------------------------------------------------------------------------- /ch9/examples/9_9_usehotel/hotel.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: hotel.h 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:29:03 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef __HOTEL_H__ 9 | #define __HOTEL_H__ 10 | 11 | #define QUIT 5 12 | #define HOTEL1 80.00 13 | #define HOTEL2 125.00 14 | #define HOTEL3 155.00 15 | #define HOTEL4 200.00 16 | #define DISCOUNT 0.95 17 | #define STARS "***********************************" 18 | 19 | //给出选项列表 20 | int menu(void); 21 | //返回预定的天数 22 | int getnights(void); 23 | //按饭店的星级和预定的天数计算价格并显示出来 24 | void showprice(double, int); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /ch9/examples/9_9_usehotel/usehotel.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: usehotel.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 09:39:45 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "hotel.h" 10 | 11 | int main(void) 12 | { 13 | int nights; 14 | double hotel_rate; 15 | int code; 16 | 17 | while ((code = menu()) != QUIT) 18 | { 19 | switch (code) 20 | { 21 | case 1: 22 | hotel_rate = HOTEL1; 23 | break; 24 | case 2: 25 | hotel_rate = HOTEL2; 26 | break; 27 | case 3: 28 | hotel_rate = HOTEL3; 29 | break; 30 | case 4: 31 | hotel_rate = HOTEL4; 32 | break; 33 | default: 34 | hotel_rate = 0.0; 35 | printf("Oops!\n"); 36 | break; 37 | } 38 | nights = getnights(); 39 | showprice(hotel_rate, nights); 40 | } 41 | printf("Thank you and goodbye.\n"); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /ch9/exercises/9_10_fibonacci.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_10_fibonacci.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun 22 Jun 2014 04:24:51 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | unsigned long fibonacci(unsigned int); 11 | 12 | int main(void) 13 | { 14 | unsigned int n; 15 | 16 | printf("Please enter a positive integer: "); 17 | while (1 == scanf("%u", &n)) 18 | { 19 | printf("Fibonacci(%u) = %lu\n", n, fibonacci(n)); 20 | printf("Please enter a positive integer: "); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | unsigned long fibonacci(unsigned int n) 27 | { 28 | unsigned long n_1 = 0, n_2 = 0, ret = 0; 29 | unsigned int i = 0; 30 | 31 | if (n <= 2) 32 | { 33 | return 1; 34 | } 35 | else 36 | { 37 | n_1 = 1; 38 | n_2 = 1; 39 | for (i = 3; i <= n; i++) 40 | { 41 | ret = n_1 + n_2; 42 | n_1 = n_2; 43 | n_2 = ret; 44 | } 45 | return ret; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /ch9/exercises/9_1_double_min.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_1_double_min.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 10:01:00 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double min(double, double); 11 | 12 | int main(void) 13 | { 14 | double x = 0.0, y = 0.0; 15 | 16 | printf("Please enter two double:\n"); 17 | scanf("%lf %lf", &x, &y); 18 | 19 | printf("Min: %.2f\n", min(x, y)); 20 | 21 | return 0; 22 | } 23 | 24 | double min(double x, double y) 25 | { 26 | if (x < y) 27 | { 28 | return x; 29 | } 30 | else 31 | { 32 | return y; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ch9/exercises/9_2_chline.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_2_chline.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 10:05:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | void chline(char, int, int); 12 | 13 | int main(void) 14 | { 15 | char ch; 16 | int x, y; 17 | 18 | printf("Please input a char: "); 19 | ch = getchar(); 20 | __fpurge(stdin); 21 | printf("Please input the coordinate to print: "); 22 | scanf("%d %d", &x, &y); 23 | chline(ch, x, y); 24 | printf("\n"); 25 | 26 | return 0; 27 | } 28 | 29 | void chline(char ch, int x, int y) 30 | { 31 | while(--x != 0) 32 | { 33 | putchar('\n'); 34 | } 35 | while (--y != 0) 36 | { 37 | putchar(' '); 38 | } 39 | putchar(ch); 40 | } 41 | -------------------------------------------------------------------------------- /ch9/exercises/9_3_print_char.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_3_print_char.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 10:12:07 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | void print(char, int, int); 12 | 13 | int main(void) 14 | { 15 | char ch; 16 | int rows = 0, cols = 0; 17 | 18 | printf("Please enter the char to print: "); 19 | ch = getchar(); 20 | __fpurge(stdin); 21 | printf("Please enter the rows and cols to print: "); 22 | scanf("%d %d", &rows, &cols); 23 | print(ch, rows, cols); 24 | 25 | return 0; 26 | } 27 | 28 | void print(char ch, int rows, int cols) 29 | { 30 | int i = 0; 31 | 32 | while (rows-- != 0) 33 | { 34 | for (i = 0; i < cols; i++) 35 | { 36 | putchar(ch); 37 | } 38 | putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ch9/exercises/9_4_harmonic_mean.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_4_harmonic_mean.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 11:29:50 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double harmonic_mean(double, double); 11 | 12 | int main(void) 13 | { 14 | double a = 0.0, b = 0.0; 15 | 16 | printf("Please enter two double:\n"); 17 | scanf("%lf %lf", &a, &b); 18 | printf("Harmonic mean: %.2f\n", harmonic_mean(a, b)); 19 | 20 | return 0; 21 | } 22 | 23 | double harmonic_mean(double a, double b) 24 | { 25 | return 2 / (1 / a + 1 / b); 26 | } 27 | -------------------------------------------------------------------------------- /ch9/exercises/9_5_larger_of.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_5_larger_of.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 11:34:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void larger_of(double *, double *); 11 | 12 | int main(void) 13 | { 14 | double a = 0.0, b = 0.0; 15 | 16 | printf("Please enter two double:\n"); 17 | scanf("%lf %lf", &a, &b); 18 | larger_of(&a, &b); 19 | printf("After replace:\na = %.2f\nb = %.2f\n", a, b); 20 | 21 | return 0; 22 | } 23 | 24 | void larger_of(double * a, double * b) 25 | { 26 | if (a > b) 27 | { 28 | *b = *a; 29 | } 30 | else 31 | { 32 | *a = *b; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ch9/exercises/9_6_check_alphabet.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_6_check_alphabet.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sat 21 Jun 2014 11:41:21 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | 11 | int locate_alphabet(char); 12 | 13 | int main(void) 14 | { 15 | char ch; 16 | 17 | while ((ch = getchar()) != EOF) 18 | { 19 | if (isalpha(ch)) 20 | { 21 | printf("The location of %c is %d.\n", ch, locate_alphabet(ch)); 22 | } 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | int locate_alphabet(char letter) 29 | { 30 | if (islower(letter)) 31 | { 32 | return letter - 'a' + 1; 33 | } 34 | else if (isupper(letter)) 35 | { 36 | return letter - 'A' + 1; 37 | } 38 | else 39 | { 40 | return -1; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ch9/exercises/9_7_power.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_7_power.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun 22 Jun 2014 02:49:10 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double power(double, int); 11 | 12 | int main(void) 13 | { 14 | double n = 0.0; 15 | int p = 0; 16 | 17 | printf("Please enter the base number and the exponent: "); 18 | scanf("%lf %d", &n, &p); 19 | 20 | printf("%.3g to the power %d is %.5g\n", n, p, power(n, p)); 21 | 22 | return 0; 23 | } 24 | 25 | double power(double n, int p) 26 | { 27 | double result = n; 28 | int exp = 0; 29 | 30 | if (n == 0) 31 | { 32 | return 0.0; 33 | } 34 | 35 | if (p == 0) 36 | { 37 | return 1.0; 38 | } 39 | 40 | exp = (p > 0) ? p : (-p); 41 | 42 | while (--exp != 0) 43 | { 44 | result *= n; 45 | } 46 | 47 | return (p > 0) ? result : ((double)1 / result); 48 | } 49 | -------------------------------------------------------------------------------- /ch9/exercises/9_8_power.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_8_power.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun 22 Jun 2014 03:10:43 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | double power(double, int); 11 | 12 | int main(void) 13 | { 14 | double n = 0.0; 15 | int exp = 0; 16 | 17 | printf("Enter the base number and the exponent: "); 18 | scanf("%lf %d", &n, &exp); 19 | printf("%.3g to the power %d is %.5g\n", n, exp, power(n, exp)); 20 | 21 | return 0; 22 | } 23 | 24 | double power(double n, int p) 25 | { 26 | if (n == 0) 27 | { 28 | return 0.0; 29 | } 30 | if (p == 0) 31 | { 32 | return 1.0; 33 | } 34 | if (p > 0) 35 | { 36 | return n * power(n, p-1); 37 | } 38 | else 39 | { 40 | return (double)1 / n * power(n, p+1); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ch9/exercises/9_9_base_converter.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 9_9_base_converter.c 3 | > Author: Hui Wang 4 | > Mail: ncepuwanghui@gmail.com 5 | > Created Time: Sun 22 Jun 2014 04:09:37 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | 10 | void to_base_n(unsigned long, unsigned int); 11 | 12 | int main(void) 13 | { 14 | unsigned long number; 15 | unsigned int base; 16 | printf("Enter an integer and the base(from 2 to 16) to convert (q to quit): \n"); 17 | while (2 == scanf("%lu %u", &number, &base)) 18 | { 19 | printf("Result:\n"); 20 | to_base_n(number, base); 21 | putchar('\n'); 22 | printf("Enter an integer and the base(from 2 to 16) to convert (q to quit): \n"); 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | void to_base_n(unsigned long n, unsigned int base) 29 | { 30 | int r; 31 | r = n % base; 32 | if (n >= base) 33 | { 34 | to_base_n(n / base, base); 35 | } 36 | 37 | if (r >= 0 && r <= 9) 38 | { 39 | putchar('0' + r); 40 | } 41 | else if (r >= 10 && r <= 15) 42 | { 43 | putchar('A' + r - 10); 44 | } 45 | else 46 | { 47 | printf("Error base!"); 48 | } 49 | } 50 | --------------------------------------------------------------------------------