[
  {
    "path": ".gitignore",
    "content": "# Prerequisites\n*.d\n\n# Object files\n*.o\n*.ko\n*.obj\n*.elf\n\n# Linker output\n*.ilk\n*.map\n*.exp\n\n# Precompiled Headers\n*.gch\n*.pch\n\n# Libraries\n*.lib\n*.a\n*.la\n*.lo\n\n# Shared objects (inc. Windows DLLs)\n*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n*.i*86\n*.x86_64\n*.hex\n\n# Debug files\n*.dSYM/\n*.su\n*.idb\n*.pdb\n\n# Kernel Module Compile Results\n*.mod*\n*.cmd\n.tmp_versions/\nmodules.order\nModule.symvers\nMkfile.old\ndkms.conf\n\n# C\nDebug\n*.dsp\n*.dsw\n*.ncb\n*.opt\n*.plg\n\n# mac\n.DS_Store\n"
  },
  {
    "path": "Chapter_01/1.c",
    "content": "#include <stdio.h>\nint main(void) {\n    double inch, cm;\n    \n    printf(\"Please input the inches: \");\n    scanf(\"%lf\", &inch);\n    cm = inch * 2.54;\n    printf(\"%g cm\\n\", cm);\n    \n    return 0;\n}"
  },
  {
    "path": "Chapter_02/3.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int days,years=21;\n\n    days=years*365;\n    printf(\"我的年龄是%d岁，%d天\\n\",years,days);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_02/4.c",
    "content": "#include<stdio.h>\nvoid jolly(void);\nvoid deny(void);\n\nint main(void)\n{\n    jolly();\n    jolly();\n    deny();\n    return 0;\n}\n\nvoid jolly(void)\n{\n    printf(\"For he's a jolly good fellow!\\n\");\n}\nvoid deny(void)\n{\n    printf(\"Which nobody can deny!\\n\");\n}\n"
  },
  {
    "path": "Chapter_02/5.c",
    "content": "#include<stdio.h>\nvoid br(void);\nvoid ic(void);\nint main(void)\n{\n    br();\n    printf(\",\");\n    ic();\n    printf(\"\\n\");\n    ic();\n    printf(\"\\n\");\n    br();\n    printf(\"\\n\");\n    \n    return 0;\n}\n\nvoid br(void)\n{\n    printf(\"Brazil,Russia\");\n}\nvoid ic(void)\n{\n    printf(\"India,China\");\n}\n"
  },
  {
    "path": "Chapter_02/6.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int toes=10;\n    int toes_2,toes2;\n    toes_2=2*toes;\n    toes2=toes*toes;\n    printf(\"toes是%d,toes的两倍是%d,toes的平方是%d\\n\",toes,toes_2,toes2);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_02/8.c",
    "content": "#include<stdio.h>\nvoid one_three(void);\nvoid two(void);\nint main(void)\n{\n    printf(\"starting now\\n\");\n    one_three();\n}\n\nvoid one_three(void)\n{\n    printf(\"one\\n\");\n    two();\n    printf(\"three\\n\");\n    printf(\"done!\\n\");\n}\nvoid two(void)\n{\n    printf(\"two\\n\");\n}\n"
  },
  {
    "path": "Chapter_03/2.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    char ch;\n    printf(\"please input a number:\");\n    scanf(\"%d\",&ch);\n    printf(\"%c\\n\",ch);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_03/4.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    float a;\n    printf(\"Enter a floating-point value: \");\n    scanf(\"%f\",&a);\n    printf(\"fixed-point notation: %f\\n\",a);\n    printf(\"exponential notation: %e\\n\",a);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_03/5.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int age;\n    double seconds;\n    \n    printf(\"please input your age: \");\n    scanf(\"%d\",&age);\n    seconds=age*3.156e7;\n    printf(\"the corresponding seconds are: %e\\n\",seconds);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_03/7.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    float inches,cms;\n    printf(\"input your height(inch): \");\n    scanf(\"%f\",&inches);\n    cms=inches*2.54;\n    printf(\"your height(cm): %f\\n\",cms);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_03/8.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    float pint,ounce,soupspoon,teaspoon,cup;\n    \n    printf(\"input the number of cups: \");\n    scanf(\"%f\",&cup);\n    pint=cup/2;\n    ounce=cup*8;\n    soupspoon=ounce*2;\n    teaspoon=soupspoon*3;\n    printf(\"they are equivalent of:\\n%f pint\\n%f ounce\\n%f soupspoons\\n%f teaspoons\\n\",pint,ounce,soupspoon,teaspoon);\n\n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_04/1.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    char firstname[40],lastname[40];\n    printf(\"Input your firstname: \");\n    scanf(\"%s\",firstname);\n    printf(\"Input your lastname: \");\n    scanf(\"%s\",lastname);\n    printf(\"Your name is %s,%s\\n\",firstname,lastname);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_04/2.c",
    "content": "#include<stdio.h>\n#include<string.h>\nint main(void)\n{\n    char name[40];\n    int width;\n    \n    \n    printf(\"Input your name: \");\n    scanf(\"%s\",name);\n    width=strlen(name)+3;\n    printf(\"%*s\\n\",width,name); //输入的名和姓中间不能分隔\n    \n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_04/4.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    float height;\n    char name[40];\n    printf(\"Input your height(cm) and name: \");\n    scanf(\"%f%s\",&height,name);\n    height=height/100;\n    printf(\"%s, you are %.3fm tall\\n\",name,height);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_04/5.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    float speed,size,time;\n    printf(\"Input the download speed(Mb/s) and the file size(MB):\\n\");\n    scanf(\"%f%f\",&speed,&size);\n    time=size/speed*8.0;\n    printf(\"At %.2f megabits per second, a file of %.2f megabytes\\n\",speed,size);\n    printf(\"downloads in %.2f seconds.\\n\",time);\n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_04/6.c",
    "content": "#include<stdio.h>\n#include<string.h>\nint main(void)\n{\n    char firstname[40],lastname[40];\n    printf(\"Input your firstname: \");\n    scanf(\"%s\",firstname);\n    printf(\"Input your lastname: \");\n    scanf(\"%s\",lastname);\n    printf(\"%s %s\\n\",firstname,lastname);\n    printf(\"%*d %*d\\n\",\n    strlen(firstname),strlen(firstname),strlen(lastname),strlen(lastname));\n    printf(\"%s %s\\n\",firstname,lastname);\n    printf(\"%*d %*d\\n\",\n    -strlen(firstname),strlen(firstname),-strlen(lastname),strlen(lastname));\n    return 0;\n}\n\n\n"
  },
  {
    "path": "Chapter_04/7.c",
    "content": "#include<stdio.h>\n#include<float.h>\nint main(void)\n{\n    double a=1.0/3.0;\n    float b=1.0/3.0;\n    printf(\"%.6f   %.6f\\n\",a,b);   //左侧double型 右侧float型\n    printf(\"%.12f,   %.12f\\n\",a,b);\n    printf(\"%.16f,   %.16f\\n\",a,b);\n    printf(\"DBL_DIG: %d\\n\",DBL_DIG);\n    printf(\"FLT_DIG: %d\\n\",FLT_DIG);\n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_04/8.c",
    "content": "#include<stdio.h>\n#define GALLON 3.758 //1 gallon=3.785 liters\n#define MILE 1.609 //1 mile=1.609 kilometers\nint main(void)\n{\n    float gallon,mile;\n    printf(\"Input miles and gallons: \");\n    scanf(\"%f%f\",&mile,&gallon);\n    printf(\"Miles per gallon: %.1f\\n\",mile/gallon);\n    printf(\"Litre per 100 kilometers: %.1f\\n\",\n    gallon*GALLON/(mile*MILE)*100);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_05/1.c",
    "content": "#include<stdio.h>\n#define H_P_M 60  //1h=60min\nint main(void)\n{\n    int hour,min,left;\n    printf(\"Enter the number of minutes: \");\n    scanf(\"%d\",&min);\n    while(min>0)\n    {\n        hour=min/H_P_M;\n        left=min%H_P_M;\n        printf(\"%d minutes is %d hours and %d minutes.\\n\",min,hour,left);\n        printf(\"Enter your next value: \");\n        scanf(\"%d\",&min);\n    }\n    printf(\"Good bye!\\n\");\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_05/2.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int num,count;\n    printf(\"Input a integer: \");\n    scanf(\"%d\",&num);\n    count=0;\n    while(count++<11)\n    {\n        printf(\"%d \",num);\n        num++;\n    }\n    printf(\"\\n\");\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_05/3.c",
    "content": "#include<stdio.h>\n#define DAYS_PER_WEEK 7 //一周7天\nint main(void)\n{\n    int day,week,left;\n    printf(\"Input the number of days: \");\n    scanf(\"%d\",&day);\n    while(day>0)\n    {\n        week=day/DAYS_PER_WEEK;\n        left=day%DAYS_PER_WEEK;\n        printf(\"%d days are %d weeks, %d days.\\n\",day,week,left);\n        printf(\"Next input: \");\n        scanf(\"%d\",&day);\n    }\n        return 0;\n}\n"
  },
  {
    "path": "Chapter_05/4.c",
    "content": "#include<stdio.h>\n#define CM_PER_FEET 30.48 //1feet=30.48cm\n#define CM_PER_INCH 2.54  //1inch=2.54cm\nint main(void)\n{\n    int feet;\n    float cm,inch;\n    printf(\"Enter a height in centimeters: \");\n    scanf(\"%f\",&cm);\n    while(cm>0)\n    {\n        feet=(int)(cm/CM_PER_FEET);\n        inch=(cm-feet*CM_PER_FEET)/CM_PER_INCH;\n        printf(\"%.1f cm = %d feet, %.1f inches\\n\",cm,feet,inch);\n        printf(\"Enter a height in centimeters (<=0 to quit): \");\n        scanf(\"%f\",&cm);\n    }\n    printf(\"bye\\n\");\n    return 0;\n}\n        \n"
  },
  {
    "path": "Chapter_05/5.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int count,sum,days;\n    printf(\"Input the number of days: \");\n    scanf(\"%d\",&days);\n    count=sum=0;\n    while(count++<days)\n        sum=sum+count;\n    printf(\"The money you earned: %d\\n\",sum);\n    return 0;\n}\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_05/6.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int count,sum,days;\n    printf(\"Input the number of days: \");\n    scanf(\"%d\",&days);\n    count=sum=0;\n    while(count++<days)\n        sum=sum+count*count;\n    printf(\"The money you earned: %d\\n\",sum);\n    return 0;\n}\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_05/7.c",
    "content": "#include<stdio.h>\nvoid cube(double n);\nint main(void)\n{\n    double num;\n    printf(\"Input a number: \");\n    scanf(\"%lf\",&num);\n    cube(num);\n}\n\nvoid cube(double n)\n{\n    printf(\"The cube of %f is %f\\n\",n,n*n*n);\n}\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_05/8.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int num1,num2;\n    printf(\"This program computes moduli.\\n\");\n    printf(\"Enter an integer to serve as the second operand: \");\n    scanf(\"%d\",&num1);\n    printf(\"Now enter the first operand: \");\n    scanf(\"%d\",&num2);\n    while(num2>0)\n    {\n        printf(\"%d %% %d is %d\\n\",num2,num1,num2%num1);\n        printf(\"Enter next number for first operand (<= 0 to quit): \");\n        scanf(\"%d\",&num2);\n    }\n        printf(\"Done\\n\");\n}\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_05/9.c",
    "content": "#include<stdio.h>\nvoid Temperatures(double fah);\nint main(void)\n{\n    double fah,cel,kel;//华氏温度，摄氏温度，开氏温度\n    printf(\"Input the Fahrenheit temperature: \");\n    while(scanf(\"%lf\",&fah)==1)\n    {\n        Temperatures(fah);\n        printf(\"Next input: \");\n    }\n    printf(\"Done.\\n\");\n}\n\n\nvoid Temperatures(double fah)\n{\n    const double a=5.0,b=9.0,c=32.0,d=276.13;\n    printf(\"%.2f ℉ is %.2f ℃, %.2f K.\\n\",\n    fah,a/b*(fah-c),a/b*(fah-c)+d);\n}\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_06/1.c",
    "content": "#include<stdio.h>\n#define SIZE 26\nint main(void)\n{\n    \n    char ch[SIZE];\n    int index;\n    \n    for(index=0;index<SIZE;index++)\n    {\n        ch[index]='a'+index;\n        printf(\"%c \",ch[index]);\n    }\n    printf(\"\\n\");\n    return 0;\n}\n        \n    \n\n        \n"
  },
  {
    "path": "Chapter_06/10.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    int lower, upper;\n    int num, sum;\n    \n    printf(\"Enter lower and upper integer limits: \");\n    scanf(\"%d%d\", &lower, &upper);\n    while (lower < upper)\n    {\n        for (sum=0, num=lower; num <= upper; num++)\n            sum = sum + num * num;                //计算平方和\n        printf(\"The sums of the squares from %d to %d is %d\\n\",\n        lower * lower, upper * upper, sum);       //输出结果\n    \n        printf(\"Enter next set of limits: \");\n        scanf(\"%d%d\", &lower, &upper);            //下一次输入\n    }\n    printf(\"Done\\n\");\n    \n    return 0;\n}\n\n        \n\n"
  },
  {
    "path": "Chapter_06/11.c",
    "content": "#include <stdio.h>\n#define SIZE 8\nint main(void)\n{\n    int num[SIZE];\n    int index;\n    \n    printf(\"Enter 8 integers: \");\n    for (index=0; index<SIZE; index++)        //输入8个整数\n        scanf(\"%d\", &num[index]);\n    for (index=SIZE-1; index >= 0; index--)\t  //倒序输出\n        printf(\"%d \", num[index]);\n    printf(\"\\n\");\n\n    return 0;\n}\n\n\n"
  },
  {
    "path": "Chapter_06/12.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    double sum1=0, sum2=0;\n    int count, items, sign;\n    \n    printf(\"Enter the items: \");\n    scanf(\"%d\", &items);                                           //输入序列的项数\n    for (count=1, sign=1; count <= items; count++, sign *= -1)\n    {\n        sum1 += 1.0 / count;\n        sum2 += 1.0 * sign / count;\n    }\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   //分别计算两序列的和\n    printf(\"1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... = %f\\n\", sum1);\n    printf(\"1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ... = %f\\n\", sum2);\n\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/13.c",
    "content": "#include <stdio.h>\n#define SIZE 8\nint main(void)\n{\n    int index, count, num[SIZE];\n     \n    for (index = 0, count = 1; index < SIZE; index++)\n    {\n        count *= 2;\n        num[index] = count;\n    }\t\t\t\t\t\t\t\t\t\t\t//for循环将数组元素设为2的前8次幂\n    \n    index=0;\t\t\t\t\t\t\t\t\t//初始化index的值\n    do\n    printf(\"%d \", num[index++]);\n    while (index < SIZE);\t\t\t\t\t\t//do while循环显示数组元素的值\n    printf(\"\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/14.c",
    "content": "#include <stdio.h>\n#define SIZE 8\nint main()\n{\n    double num1[SIZE], num2[SIZE];\n    int index1, index2, index;\n\n    printf(\"Enter 8 numbers to the first array:\\n\");\n    for (index1 = 0; index1 < SIZE; index1++)\n        scanf(\"%lf\", &num1[index1]);\t\t\t\t\t\t\t//向第一个数组输入8个数\n    num2[0] = num1[0];\n    for (index1 = 1, index2 = 1; index1 < SIZE; index1++, index2++)\n        num2[index2] = num2[index2-1] + num1[index1];\n    //为第二个数组赋值（是第一个数组对应的元素之和）\n    printf(\"The first array:  \");\n    for (index=0; index < SIZE; index++) {\n        printf(\"%6.2f\", num1[index]);\n    }\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t//输出第一个数组的内容\n    printf(\"\\nThe second array: \");\n    for (index=0; index < SIZE; index++) {\n        printf(\"%6.2f\", num2[index]);\t\t\t\t\t\t\t//输出第二个数组的内容\n    }\n    printf(\"\\n\");\n\n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_06/15.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define SIZE 255\nint main(void)\n{\n    int index;\n    char ch[SIZE];\n\n    printf(\"Enter a line: \");\n    for(index = 0, scanf(\"%c\", &ch[0]); ch[index] != '\\n';)\n    {\n        index++;\n        scanf(\"%c\", &ch[index]);\n    }\t\t\t\t\t\t\t\t\t\t\t//输入内容到字符数组中,回车时结束\n    for(index += 1; index < SIZE; index++)\n        ch[index] = '\\0';\t\t\t\t\t\t//将数组剩余空间补充为'\\0'\n    for(index = strlen(ch); index >=0; index--)\n        printf(\"%c\", ch[index]);\t\t\t\t//倒序输出内容\n    printf(\"\\n\");\n\n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_06/16.c",
    "content": "#include <stdio.h>\n#define RATE_DAPHNE 0.1\n#define RATE_DEIRDRE 0.05\t\t\t\t\t//两人的利率\n#define\tMONEY 100\nint main(void)\n{\n    int year;\n    double daphne = MONEY, deirdre = MONEY;\t//两人的初始投资额相同\n\n    for (year = 1; daphne >= deirdre; year++)\n    {\n        daphne += MONEY * RATE_DAPHNE;\n        deirdre += deirdre * RATE_DEIRDRE;\n    }\n    //计算Deirdre投资额超过Daphne需要的年数和当时的金额\n    printf(\"After %d year, Deirdre's investment will be more than Daphne's,\\n\"\n        \"Daphne's investment will be $%lf,\\nand Deirdre's investment will be $%lf.\\n\",\n        year, daphne, deirdre);\t\t\t\t//输出结果\n\n    return 0;\n}\n    \n"
  },
  {
    "path": "Chapter_06/17.c",
    "content": "#include <stdio.h>\n#define INITIAL_MONEY 100\t\t\t//账户初始金额为100万元\n#define ANNUAL_RATE 0.08\t\t\t//年利率为8%\nint main(void)\n{\n    int year;\n    double money;\n    \n    for(year = 1, money=INITIAL_MONEY; money>0; year++)\n        money += money * ANNUAL_RATE - 10;\t\t\t//计算每年年终的账户余额\n    printf(\"After %d years, Chuckie will draw all money from his account.\\n\", year);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/18.c",
    "content": "#include <stdio.h>\n#define\tINITIAL_NUMBER 5   //初始朋友数为5人\n#define DUNBAR_NUMBER 150  //邓巴数\nint main(void)\n{\n    int week;\n    int number = INITIAL_NUMBER;\n\n    for (week = 1; number <= DUNBAR_NUMBER; week++)\n    {\n        number = (number - week) * 2;\t\t//计算每周的朋友数量\n        printf(\"After %d week, the number of Rabnud's friends is %d\\n\", week, number);\n    }\n\n    return 0;\n}\n    \n\n    \n"
  },
  {
    "path": "Chapter_06/2.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int i,j;\n\n    for(i=1;i<=5;i++)\n    {\n        for(j=1;j<=i;j++)\n            printf(\"$\");\n        printf(\"\\n\");\n    }\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/3.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    int i,j;\n    for(i=1;i<=6;i++)\n    {\n        for(j=0;j<i;j++)\n            printf(\"%c\",'F'-j);\n        printf(\"\\n\");\n    }\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/4.c",
    "content": "#include<stdio.h>\n#define ROWS 6\nint main(void)\n{\n    char ch;\n    int i,j;\n    for(ch='A',i=0;i<ROWS;i++)\n    {\n        for(j=0;j<=i;j++)\n            printf(\"%c\",ch++);\n        printf(\"\\n\");\n\n    }\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/5.c",
    "content": "#include<stdio.h>\n\nint main(void)\n{\n    char ch='A';\n    char a;\n    int i, j,ROW;\n\n    printf(\"please input a letter:\");\n    scanf(\"%c\", &a);\n    ROW = a - ch + 1;\n    \n    for (i = 1; i <= ROW; i++)\n    {\n        for(j=1;j<=ROW-i;j++)\n            printf(\" \");\n        for(j=0;j<i;j++)\n            printf(\"%c\",ch+j);\n        for(j=i-2;j>=0;j--)\n            printf(\"%c\",ch+j);\n        printf(\"\\n\");\n    }\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/6.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n\n    int max,min,num;\n\n    printf(\"Input the min and max: \");\n    scanf(\"%d%d\",&min,&max);\n    printf(\"%10s%10s%10s\\n\",\"number\",\"square\",\"cube\");\n    for(num=min;num<=max;num++)\n        printf(\"%10d%10d%10d\\n\",num,num*num,num*num*num);\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/7.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define WORD_SIZE 26\nint main (void)\n{\n    char word[WORD_SIZE];\n    printf(\"Input a word:\\n\");\n    scanf(\"%s\", word);\n    for(long i = strlen(word); i >= 0; i--) {\n        printf(\"%c\", word[i]);\n    }\n    printf(\"\\n\");\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/8.c",
    "content": "#include<stdio.h>\nint main(void)\n{\n    double n1,n2;\n    printf(\"Input two numbers: \");\n    while(2==scanf(\"%lf%lf\",&n1,&n2))\n    {\n        printf(\"%f\\n\",(n1-n2)/n1*n2);\n        printf(\"Input your next pair of numbers: \");\n    }\n    printf(\"Bye!\\n\");\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_06/9.c",
    "content": "#include<stdio.h>\ndouble calculate(double n1, double n2);\nint main(void)\n{\n    double num1, num2;\n    \n    printf(\"Input two numbers: \");\n    while (2 == scanf(\"%lf%lf\", &num1, &num2))   //输入两个浮点数\n    {\n        printf(\"%f\\n\", calculate(num1, num2));   //函数调用\n        printf(\"Input your next pair of numbers: \");\n    }\n    printf(\"Bye!\\n\");\n    return 0;\n}\n\ndouble calculate(double n1, double n2)\n{\n    return ((n1 - n2) / (n1 * n2));     //返回运算结果\n}\n"
  },
  {
    "path": "Chapter_07/1.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    char ch;\n    int n_space = 0;\t\t//空格数\n    int n_newline = 0;\t\t//换行数\n    int n_others = 0;\t\t//其他字符数\n    \n    printf(\"Enter some text; Enter # to quit.\\n\");\n    while ((ch = getchar()) != '#')\n    {\n        if (ch == ' ')\n            n_space++;\n        else if (ch == '\\n')\n            n_newline++;\n        else\n            n_others++;\n    }\n    printf(\"Spaces: %d, newlines: %d, others: %d\\n\",\n            n_space, n_newline, n_others);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_07/10.c",
    "content": "#include <stdio.h>\n#define RATE1 0.15\n#define RATE2 0.28\n#define SINGLE 17850\t//单身人群的税率分界点\n#define HOST 23900\t\t//户主人群的税率分界点\n#define MAR_SHA\t29750\t//已婚共有人群的分界点\n#define MAR_DEV\t14875\t//已婚离异人群的分界点\nint main(void)\n{\n    int num;\n    double income, tax_break, tax;\n\n    printpart: printf(\"Please enter Corresponding\"\n                    \"figures to select the type\\n\");\n    printf(\"1 single, 2 host, 3 married and shared, \"\n            \"4 married but devoced and 5 to quit.\\n\");\n    scanf(\"%d\", &num);\n    switch (num)\n    {\n        case 1: tax_break = SINGLE;\n                break;\n        case 2: tax_break = HOST;\n                break;\n        case 3: tax_break = MAR_SHA;\n                break;\n        case 4: tax_break = MAR_DEV;\n                break;\n        case 5:\tprintf(\"quit.\\n\");\n                return 0;\n        default: printf(\"Please input right number.\");\n                 goto printpart;\t\t//回到输入阶段\n    }\n    printf(\"Enter your income: \");\t\t//指定种类和收入\n    while (scanf(\"%lf\", &income) == 1)\n    {\n        if (income <= tax_break)\n            tax = income * RATE1;\n        else\n            tax = tax_break * RATE1 + (income - tax_break) * RATE2;\n        //计算税金\n        printf(\"The tax is $%.2lf.\\n\", tax);\n        printf(\"Your next input: \\n\");\n        goto printpart;\t\t\t\t\t//回到输入阶段\n    }\n\n    return 0;\n}\n    \n\n\n\n    \n    \n"
  },
  {
    "path": "Chapter_07/11.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\n#define ARTICHOKE 2.05\t\t//洋蓟2.05美元/磅\n#define BEET 1.15\t\t\t//甜菜1.15美元/磅\n#define CARROT 1.09\t\t\t//胡萝卜1.09美元/磅\n#define\tDISCOUNT_LIMIT 100\n//包装费和运费打折要求订单100美元\n#define DISCOUNT_RATE 0.05\t//折扣为%5\n#define BREAK1 5\n#define BREAK2 20\t\t\t//装运费的分界点\n#define\tFEE1 6.5\n#define\tFEE2 14\n#define FEE3_RATE 0.5\n//不同重量区间的装运费，其中超过20磅的每续重一磅\n//增加0.5元\nint main(void)\n{\n    double weight;\n    double weight_artichoke = 0;\n    double weight_beet = 0;\n    double weight_carrot = 0;\t//购买三种蔬菜的重量\n    double total_weight;\t//总重量\n    double veg_cost;\t\t//三种蔬菜总共花费\n    double order_cost;\t\t//订单总额\n    double total_cost;\t\t//费用总额\n    double pack_tran_fee;\t//装运费\n    double discount;\n    int count = 0;\n    char ch;\n\n    printf(\"Please select the vegetables you want to buy:\\n\");\n    printf(\"a: artichoke  $%.2f/lb\\n\", ARTICHOKE);\n    printf(\"b: beet       $%.2f/lb\\n\", BEET);\n    printf(\"c: carrot     $%.2f/lb\\n\", CARROT);\n    printf(\"q: quit.\\n\");\n    //打印选择信息\n    while ((ch = tolower(getchar())) != 'q')\n    {\n    //\tif (ch == '\\n')\n    //\t\tcontinue;\t\t\t\t\t//滤掉回车\n        switch (ch)\n        {\n            case 'a': printf(\"Input the weight of artichoke in pound: \");\n                      scanf(\"%lf\", &weight);\n                      weight_artichoke += weight;\n                      count++;\n                      printf(\"Continue entering a, b, c or q: \");\n                      break;\n            case 'b': printf(\"Input the weight of beet in pound: \");\n                      scanf(\"%lf\", &weight);\n                      weight_beet += weight;\n                      count++;\n                      printf(\"Continue entering a, b, c or q: \");\n                      break;\n            case 'c': printf(\"Input the weight of carrot in pound: \");\n                      scanf(\"%lf\", &weight);\n                      weight_carrot += weight;\n                      count++;\n                      printf(\"Continue entering a, b, c or q: \");\n                      break;\n            default: printf(\"Please enter the right character.\");\n        }\n        while (getchar () != '\\n')\n            continue;\t\t\t\t\t//滤掉输入重量后面的所有字符\n        \n    }\n    if (!count)\n    {\n        printf(\"Bye.\\n\");\n        return 0;\n    }\t\t\t\t\t\t\t\t\t\t//开始输出q，直接退出\n    total_weight = weight_artichoke + weight_beet + weight_carrot;\n    veg_cost = weight_artichoke * ARTICHOKE + weight_beet * BEET\n                + weight_carrot * CARROT;\n    discount = 0;\n    if (veg_cost >= DISCOUNT_LIMIT)\n    {\n        discount = veg_cost * DISCOUNT_RATE;\n        order_cost = veg_cost - discount;\n    }\n    else\n        order_cost = veg_cost;\t\t\t\t//折扣计算\n    \n    if (total_weight <= BREAK1)\n        pack_tran_fee = FEE1;\n    else if (total_weight <= BREAK2)\n        pack_tran_fee = FEE2;\n    else\n        pack_tran_fee = FEE2 + (total_weight - BREAK2) * FEE3_RATE;\n    //装运费计算\n    total_cost = order_cost + pack_tran_fee;\n    \n    printf(\"\\nHere is what you choose:\\n\");\n    if (weight_artichoke) {\n        printf(\"artichoke   Price: $%.2f/lb  weight: %.2f pounds  cost: $%.2f\\n\",\n                ARTICHOKE, weight_artichoke, weight_artichoke * ARTICHOKE);\n    }\n    if (weight_beet) {\n        printf(\"beet        Price: $%.2f/lb  weight: %.2f pounds  cost: $%.2f\\n\",\n                BEET, weight_beet, weight_beet * BEET);\n    }\n    if (weight_carrot) {\n        printf(\"carrot      Price: $%.2f/lb  weight: %.2f pounds  cost: $%.2f\\n\",\n                CARROT, weight_carrot, weight_carrot * CARROT);\n    }\n    printf(\"The order cost: $%.2f\\n\", veg_cost);\n\n    if (discount)\n        printf (\"You have %%%.f discount, and the reduced money is $%.2f\\n\",\n                DISCOUNT_RATE * 100, discount);\n    printf(\"The packing and transpoting fee is $%.2f\\n\", pack_tran_fee);\n    printf(\"The total cost is $%.2f\\n\", total_cost);\n    //输出订单信息\n\n    return 0;\n}\n    \n\n\n\n\n\n        \n"
  },
  {
    "path": "Chapter_07/2.c",
    "content": "#include <stdio.h>\n#define CHARS_PER_LINE 8\t//每行字符数\nint main(void)\n{\n    char ch;\n    int n_chars = 1;\t\t\t//字符数\n\n    printf(\"Enter some characters(# to quit):\\n\");\n    while ((ch = getchar()) != '#')\n    {\n        printf(\"%3c(%3d) \", ch, ch);\n        if (n_chars++ % CHARS_PER_LINE == 0)\n            printf(\"\\n\");\n    }\n    printf(\"\\n\");\n\n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_07/3.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    int num;\n    int n_even = 0, n_odd = 0;\t\t//偶数和奇数个数\n    int sum_even = 0, sum_odd = 0;\t//偶数和奇数和\n\n    printf(\"Enter some integers(0 to quit):\\n\");\n    scanf(\"%d\", &num);\n    while (num != 0)\n    {\n        if (num % 2 == 0)\n        {\n            n_even++;\n            sum_even += num;\n        }\t\t\t\t\t\t\t//计算偶数个数和偶数和\n        else\n        {\n            n_odd++;\n            sum_odd +=num;\n        }\t\t\t\t\t\t\t//计算奇数个数和奇数和\n        scanf(\"%d\",&num);\n    }\n    printf(\"The number of even numbers is %d, \"\n            \"and the everage of even numbers is %.2f\\n\",\n            n_even, (n_even == 0) ? 0 : (float)sum_even / n_even);\n    printf(\"The number of odd numbers is %d, \"\n            \"and the everrage of odd numers is %.2f\\n\",\n            n_odd, (n_odd == 0) ? 0 : (float)sum_odd / n_odd);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_07/4.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    char ch;\n    int n_repl = 0;         //替换次数\n    \n    printf(\"Enter some texts(# to quit):\\n\");\n    while ((ch = getchar()) != '#') {\n        if (ch == '.')\n        {\n            ch = '!';\n            n_repl++;\n        }\t\t\t\t\t//替换句号\n        else if (ch == '!')\n        {\n            printf(\"!\");\n            n_repl++;\n        }\t\t\t\t\t//替换感叹号\n        printf(\"%c\", ch);\n    }\n    printf(\"\\n%d substitutions were made.\\n\", n_repl);\n    \n    return 0;\n}\n"
  },
  {
    "path": "Chapter_07/5.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    char ch;\n    int n_repl = 0;\t\t\t\t//替换次数\n    \n    printf(\"Enter some texts(# to quit):\\n\");\n    while ((ch = getchar()) != '#') {\n        switch (ch)\n        {\n            case '.': ch = '!';\n                      n_repl++;\n                      break;\n            case '!': printf(\"!\");\n                      n_repl++;\n                      break;\n            default: break;\n        }\t\t\t\t\t\t//利用switch语句进行替换\n        printf(\"%c\",ch);\n    }\n    printf(\"\\n%d substitutions were made.\\n\", n_repl);\n\n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_07/6.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    char ch;\n    char last_ch = 0;\t\t //前一个字符\n    int count=0;\n\n    printf(\"Enter some texts(# to quit):\\n\");\n    while ((ch = getchar()) != '#')\n    {\n        if ((ch == 'i') && (last_ch == 'e'))\n            count++;\n        last_ch = ch;\t\t//出现ei时，计数+1\n    }\n    printf(\"\\\"ei\\\" appeared %d times.\\n\", count);\n    \n    return 0;\n}\n\n\n"
  },
  {
    "path": "Chapter_07/7.c",
    "content": "#include <stdio.h>\n#define BASE 1000\t\t//基本工资 100美元/h\n#define TIME 40\t\t\t//超过40h为加班\n#define\tMUL\t1.5\t\t\t//加班时间算作平时的1.5倍\n#define\tRATE1 0.15\t\t//前300美元的税率\n#define RATE2 0.2\t\t//300-450美元的税率\n#define RATE3 0.25\t\t//大于450美元的税率\n#define BREAK1 300\t\t//税率的第一个分界点\n#define BREAK2 450\t\t//税率的第二个分界点\nint main(void)\n{\n    double hour, tax, gross;\n    \n    printf(\"Input your work hours in a week: \");\n    scanf(\"%lf\", &hour);\n    if (hour <= TIME)\n        gross = hour * BASE;\n    else\n        gross = TIME * BASE + (hour - TIME) * MUL * BASE;\n    //计算总收入\n    if (gross <= BREAK1)\n        tax = gross * RATE1;\n    else if (gross <= BREAK2)\n        tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;\n    else\n        tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2\n            + (gross - BREAK2) * RATE3;\n    //计算税金\n    printf(\"Your gross income is $%.2lf\\nYour tax is $%.2lf\\n\"\n            \"Your net income is $%.2lf\\n\",\n            gross, tax, (gross - tax));\n    \n    return 0;\n}\n\n\n"
  },
  {
    "path": "Chapter_07/8.c",
    "content": "#include <stdio.h>\n#define BASE1 8.75\n#define BASE2 9.33\n#define BASE3 10.00\n#define BASE4 11.20\n//四种等级的基本工资\n#define TIME 40\t\t\t//超过40h为加班\n#define\tMUL\t1.5\t\t\t//加班时间算作平时的1.5倍\n#define\tRATE1 0.15\t\t//前300美元的税率\n#define RATE2 0.2\t\t//300-450美元的税率\n#define RATE3 0.25\t\t//大于450美元的税率\n#define BREAK1 300\t\t//税率的第一个分界点\n#define BREAK2 450\t\t//税率的第二个分界点\nint main(void)\n{\n    double base, hour, tax, gross;\n    int count, num;\n    const int LENGTH = 65;\t//*的长度\n    \n    printpart: for (count = 0; count < LENGTH; count++)\n                    printf(\"*\");\n    printf(\"\\nEnter the number corresponding to the desired pay rate or action:\\n\");\n    printf(\"%-36s%s\",\"1) $8.75/hr\", \"2) $9.33/hr\\n\");\n    printf(\"%-36s%s\",\"3) $10.00/hr\", \"4) $11.20/hr\\n\");\n    printf(\"%s\\n\", \"5) quit\");\n    for (count = 0; count < LENGTH; count++)\n        printf(\"*\");\n    printf(\"\\n\");\n    //打印表格\n    while (scanf(\"%d\", &num) == 1) {\n    switch (num)\n    {\n        case 1:\tbase = BASE1;\n                break;\n        case 2: base = BASE2;\n                break;\n        case 3: base = BASE3;\n                break;\n        case 4: base = BASE4;\n                break;\n        case 5: printf(\"quit.\\n\");\n                return 0;\n        default: printf(\"Please input the right option.\\n\");\n                goto printpart;\n    }\t//选择基本工资等级\n\n    printf(\"Input your work hours in a week: \");\n    scanf(\"%lf\", &hour);\n    if (hour <= TIME)\n        gross = hour * base;\n    else\n        gross = TIME * base + (hour - TIME) * MUL * base;\n    //计算总收入\n    if (gross <= BREAK1)\n        tax = gross * RATE1;\n    else if (gross <= BREAK2)\n        tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;\n    else\n        tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2\n            + (gross - BREAK2) * RATE3;\n    //计算税金\n    printf(\"Your gross income is $%.2lf\\nYour tax is $%.2lf\\n\"\n            \"Your net income is $%.2lf\\n\",\n            gross, tax, (gross - tax));\n    printf(\"\\nYour next choice:\\n\");\n    }\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_07/9.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    int div, prime;\n    int num, count;\n    int flag;\n\n    printf(\"Input a positive integer: \");\n    scanf(\"%d\", &num);\n    printf(\"The prime numbers in range:\\n\");\n    for (prime = 2; prime <= num; prime++)\t//外层循环显示所有素数\n    {\n        flag = 1;\n        for (div = 2; (div * div) <= prime; div++)\n        {\n            if (prime % div == 0)\n                flag = 0;\n        }\t\t\t\t\t\t\t\t\t//内层循环检验是否为素数\n        if (flag)\t\t\t\t\t\t\t//利用标记flag判断\n            printf(\"%d \",prime);\n    }\n    printf(\"\\n\");\n    \n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_08/1.c",
    "content": "#include <stdio.h>\nint main(void)\n{\n    int ch;\n    int count = 0;\n\n    while ((ch = getchar()) != EOF)\n        count++;\n    printf(\"The number of characters is %d\\n\", count);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_08/2.c",
    "content": "#include <stdio.h>\n#define SPACE 32\n#define CTRL 64\n#define COL 10\t\t\t\t//每行打印10对\nint main(void)\n{\n    char ch;\n    int count = 0;\n\n    while ((ch = getchar()) != EOF)\n    {\n        count++;\n    //打印字符输出\n        if (ch >= SPACE)\n            printf(\"%c \", ch);\n    //换行符和制表符的输出\n        else if (ch == '\\n' || ch == '\\t')\n            printf(\"%s\", ch == '\\n' ? \"\\\\n\" : \"\\\\t\");\n    //一般控制字符的输出\n        else\n            printf(\"^%c\", ch + CTRL);\n        printf(\"%-5d\", ch);\n        if (ch == '\\n')\n            count = 0;\n        if (count % COL == 0)\t//10对换行\n            printf(\"\\n\");\n    }\n\n    return 0;\n}\n\n    \n\n\n\n"
  },
  {
    "path": "Chapter_08/3.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nint main(void)\n{\n    int lower = 0, upper = 0;\n    char ch;\n\n    printf(\"Input some texts:\\n\");\n    while ((ch = getchar()) != EOF)\n    {\n        if (islower(ch))\n            lower++;\n        if (isupper(ch))\n            upper++;\n    }\n    printf(\"lower:%d, upper:%d\\n\", lower, upper);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_08/4.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nint main(void)\n{\n    char ch;\n    int inword = 0;\t\t\t//在单词中为1否则为0\n    int words = 0;\t\t\t//单词数\n    int chars = 0;\t\t\t//字符数\n\n    printf(\"Please enter some words:\\n\");\n    while ((ch = getchar()) != EOF)\n    {\n        if (!isspace(ch) && !ispunct(ch))\n            chars++;\n    //开始一个新的单词\n        if (!isspace(ch) && !ispunct(ch) && !inword)\n        {\n            inword = 1;\n            words++;\n        }\n    //单词末尾\n        if (isspace(ch) || ispunct(ch) && inword)\n            inword = 0;\n    }\n        printf(\"There are %d words and %d letters\\n\",\n                words, chars);\n        printf(\"The average number of letters of \");\n        printf(\"each word is %.2f.\\n\",\n                ((double)chars / (double)words));\n\n        return 0;\n    }\n\n\n\n\n"
  },
  {
    "path": "Chapter_08/5.c",
    "content": "#include <stdio.h>\n#define LOW 1\t\t\t//初始下限\n#define HIGH 100\t\t//初始上限\nint get_guess(void);\nint main(void)\n{\n    char response;\n    int guess;\n    int low = LOW;\n    int high = HIGH;\n\n    guess = (low + high) / 2;\n    printf(\"Pick an integer from %d to %d. I will try to guess \",\n            LOW, HIGH);\n    printf(\"it.\\nRespond with a y if my guess is right, with an s if it\\n\");\n    printf(\"is small and with a b if it is big.\\n\");\n    printf(\"Uh...is your number %d?\\n\", guess);\n    while ((response = getchar()) != 'y')\n    {\n        if (response == 's')\n        {\n            low = guess;\n            guess = (guess + high) / 2;\n        }\n        else if (response == 'b')\n        {\n            high = guess;\n            guess = (guess + low) / 2;\n        }\n        else\n            printf(\"Sorry, I understand only y, s and b.\\n\");\n        \n        printf(\"Well, then, is it %d?\\n\", guess);\n        //跳过剩余输入行\n        while (getchar() != '\\n')\n            continue;\n        //使取值能到达上限\n        if (guess == HIGH - 1)\n            guess++;\n    }\n    printf(\"I knew i could do it!\\n\");\n    \n    return 0;\n}\n"
  },
  {
    "path": "Chapter_08/6.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nchar get_first(void);\nint main(void)\n{\n    char ch;\n    while ((ch = get_first()) != EOF)\n    {\n        putchar(ch);\n        printf(\"\\n\");\n    }\n    return 0;\n}\n\nchar get_first(void)\n{\n    char ch;\n    while (isspace(ch = getchar()))\n        continue;\n    while (getchar() != '\\n')\n        continue;\n\n    return ch;\n}\n\n\n"
  },
  {
    "path": "Chapter_08/7.c",
    "content": "#include <stdio.h>\n#define BASE1 8.75\n#define BASE2 9.33\n#define BASE3 10.00\n#define BASE4 11.20\n//四种等级的基本工资\n#define TIME 40\t\t\t//超过40h为加班\n#define\tMUL\t1.5\t\t//加班时间算作平时的1.5倍\n#define\tRATE1 0.15\t\t//前300美元的税率\n#define RATE2 0.2\t\t\t//300-450美元的税率\n#define RATE3 0.25\t\t//大于450美元的税率\n#define BREAK1 300\t\t//税率的第一个分界点\n#define BREAK2 450\t\t//税率的第二个分界点\n#define LENGTH 65\t\t//*的长度\n\nchar get_choice(void);\nvoid calculate(double base);\nint main(void)\n{\n    double base;\n    char choice;\n    \n    while ((choice = get_choice()) != 'q')\n    {\n        switch (choice)\n        {\n            case 'a': base = BASE1;\n                      break;\n            case 'b': base = BASE2;\n                      break;\n            case 'c': base = BASE3;\n                      break;\n            case 'd': base = BASE4;\n                      break;\n            default: printf(\"Please respond with a, b, c, d or q.\\n\");\n                     break;\n        }\n        if (choice >= 'a' && choice <= 'd')\n            calculate(base);\n    }\n\n    return 0;\n}\n    \n//计算和输出结果的函数\nvoid calculate(double base)\n{\n    double hour, gross, tax;\n\n    printf(\"Input your work hours in a week: \");\n    scanf(\"%lf\", &hour);\n    while (getchar() != '\\n')\n        continue;\t\t\t\t\t//跳过回车\n    if (hour <= TIME)\n        gross = hour * base;\n    else\n        gross = TIME * base + (hour - TIME) * MUL * base;\n    //计算总收入\n    if (gross <= BREAK1)\n        tax = gross * RATE1;\n    else if (gross <= BREAK2)\n        tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;\n    else\n        tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2\n            + (gross - BREAK2) * RATE3;\n    //计算税金\n    printf(\"Your gross income is $%.2lf\\nYour tax is $%.2lf\\n\"\n            \"Your net income is $%.2lf\\n\",\n            gross, tax, (gross - tax));\n    printf(\"\\n\");\n}\n\n//打印选择界面并让用户输入的函数\nchar get_choice(void)\n{\n    char ch;\n    int count;\n\n    for (count = 0; count < LENGTH; count++)\n        printf(\"*\");\n    printf(\"\\nEnter the letter corresponding to the desired pay rate or action:\\n\");\n    printf(\"%-36s%s\",\"a) $8.75/hr\", \"b) $9.33/hr\\n\");\n    printf(\"%-36s%s\",\"c) $10.00/hr\", \"d) $11.20/hr\\n\");\n    printf(\"%s\\n\", \"q) quit\");\n    for (count = 0; count < LENGTH; count++)\n        printf(\"*\");\n    printf(\"\\n\");\n\n    ch = getchar();\n    while (getchar() != '\\n')\n        continue;\t\t\t\t\t//跳过输出行剩余内容\n\n    return ch;\n}\n"
  },
  {
    "path": "Chapter_08/8.c",
    "content": "#include <stdio.h>\nfloat get_float(void);\nchar get_choice(void);\nchar get_first(void);\nint main(void)\n{\n    char choice;\n    float num1, num2;\n    \n    while ((choice = get_choice()) != 'q')\n    {\n        printf(\"Enter first number: \");\n        num1 = get_float();\n        printf(\"Enter second number: \");\n        num2 = get_float();\n        \n        switch (choice)\n        {\n            case 'a': printf(\"%.2f + %.2f = %.2f\\n\",\n                        num1, num2, num1 + num2);\n                      break;\n            case 's': printf(\".2%f - %.2f = %.2f\\n\",\n                        num1, num2, num1 - num2);\n                      break;\n            case 'm': printf(\"%.2f * %.2f = %.2f\\n\",\n                        num1, num2, num1 * num2);\n                      break;\n            case 'd':\n                      if (!num2)\n                      {\n                          printf(\"Enter a number other than 0: \");\n                          num2 = get_float();\n                      }\n                      printf(\"%.2f / %.2f = %.2f\\n\",\n                      num1, num2, num1 / num2);\n                      break;\n            default:  printf(\"Program error!\\n\");\n                      break;\n        }\n    }\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\n//打印界面和让用户选择的函数\nchar get_choice(void)\n{\n    char ch;\n    printf(\"Enter the operation of your choice:\\n\");\n    printf(\"%-36s%s\\n\", \"a. add\", \"s. subtract\");\n    printf(\"%-36s%s\\n\", \"m. multiply\", \"d. divide\");\n    printf(\"q. quit\\n\");\n    ch = get_first();\n    while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd'\n            && ch != 'q')\n    {\n        printf(\"Please enter a, s, m, d or q.\\n\");\n        ch = get_first();\n    }\n\n    return ch;\n}\n\n//获得输入值并处理错误输入的函数\nfloat get_float(void)\n{\n    float input;\n    char ch;\n    \n    while (scanf(\"%f\", &input) != 1)\n    {\n        while ((ch = getchar()) != '\\n')\n            putchar(ch);\n        printf(\" is not an number.\\n\");\n        printf(\"Please enter a number, such as 2.5, -1.78E8, or 3: \");\n    }\n    while (getchar() != '\\n')\n        continue;\n\n    return input;\n}\n\n//读取输入行的第一个字符\nchar get_first(void)\n{\n    char ch;\n\n    ch = getchar();\n    while (getchar() != '\\n')\n        continue;\n\n    return ch;\n}\n\n"
  },
  {
    "path": "Chapter_09/1.c",
    "content": "#include <stdio.h>\ndouble min(double x, double y);\nint main(void)\n{\n    double num1, num2;\n\n    printf(\"Enter two numbers: \");\n    scanf(\"%lf%lf\", &num1, &num2);\n    printf(\"The smaller one is: %f\\n\", min(num1, num2));\n\n    return 0;\n}\n\ndouble min(double n1, double n2)\n{\n    return (n1 < n2 ? n1 : n2);\n}\n"
  },
  {
    "path": "Chapter_09/10.c",
    "content": "#include <stdio.h>\nvoid to_base_n(unsigned long n, unsigned int base);\nint main(void)\n{\n    unsigned long num;\n    unsigned int base;\n\n    printf(\"Enter an integer and the base from 2 to 10 (q to quit): \");\n    while (scanf(\"%lu%u\", &num, &base) == 2)\n    {\n        printf(\"Equivalent number on the base of %u: \", base);\n        to_base_n(num, base);\n        printf(\"\\nEnter your next pair of numbers(q to quit): \");\n    }\n    printf(\"Done.\\n\");\n\n    return 0;\n}\n\nvoid to_base_n(unsigned long n, unsigned int base)\n{\n    int r;\n\n    r = n % base;\n    if (n >= base)\n        to_base_n(n / base, base);\n    printf(\"%d\", r);\n}\n\n\n\n\n"
  },
  {
    "path": "Chapter_09/11.c",
    "content": "#include <stdio.h>\nunsigned long Fibonacci(unsigned n);\nint main(void)\n{\n    unsigned int num;\n    int count;\n\n    printf(\"Input the number of the items (q to quit): \");\n    while (scanf(\"%u\", &num) == 1)\n    {\n        for (count = 1; count <= num; count++)\n            printf(\"%lu \", Fibonacci(count));\n        printf(\"\\nYour next input(q to quit): \");\n    }\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nunsigned long Fibonacci(unsigned n)\n{\n    int count, temp;\n    int n1 = 1, n2 = 1;\n\n    if (n > 2)\n        for (count = 3; count <= n; count++)\n        {\n            temp = n1 + n2;\n            n1 = n2;\n            n2 = temp;\n        }\n    else\n        n2 = 1;\n\n    return n2;\n}\n        \n"
  },
  {
    "path": "Chapter_09/2.c",
    "content": "#include <stdio.h>\nvoid chline(char ch, int i, int j);\nint main(void)\n{\n    int row, col;\n    char ch;\n    printf(\"Input a character you want to print: \");\n    ch = getchar();\n    printf(\"Input the number of rows and columns: \");\n    scanf(\"%d%d\", &row, &col);\n    chline(ch, row, col);\n\n    return 0;\n}\n\nvoid chline(char ch, int i , int j)\n{\n    int row, col;\n    for (row = 1; row <= i; row++)\n    {\n        for(col = 1; col <=j; col++)\n            putchar(ch);\n        putchar('\\n');\n    }\n\n    return;\n}\n"
  },
  {
    "path": "Chapter_09/4.c",
    "content": "#include <stdio.h>\ndouble har_mean(double num1, double num2);\nint main(void)\n{\n    double num1, num2;\n    printf(\"Enter two numbers: \");\n    scanf(\"%lf%lf\", &num1, &num2);\n    printf(\"The harmonic mean is: %f\\n\", har_mean(num1, num2));\n}\n\ndouble har_mean(double num1, double num2)\n{\n    return 1 / (1 / num1 + 1 / num2);\n}\n"
  },
  {
    "path": "Chapter_09/5.c",
    "content": "#include <stdio.h>\nvoid larger_of(double * x, double * y);\nint main(void)\n{\n    double num1, num2;\n\n    printf(\"Input two numbers: \");\n    scanf(\"%lf%lf\", &num1, &num2);\n    printf(\"Originally, num1 = %f, num2 = %f\\n\",\n            num1, num2);\n    larger_of(&num1, &num2);\n    printf(\"Now, num1 = %f, num2 = %f\\n\", num1, num2);\n\n    return 0;\n}\n\nvoid larger_of(double * x, double * y)\n{\n    *x > *y ? (*y = *x) : (*x = *y);\n}\n\n\n"
  },
  {
    "path": "Chapter_09/6.c",
    "content": "#include <stdio.h>\nvoid sort(double * x, double * y, double * z);\nint main(void)\n{\n    double num1, num2 ,num3;\n\n    printf(\"Enter 3 numbers: \");\n    scanf(\"%lf%lf%lf\", &num1, &num2, &num3);\n    printf(\"Originally\\nnum1 = %.2f, num2 = %.2f, num3 = %.2f\\n\",\n            num1, num2, num3);\n    sort(&num1, &num2, &num3);\n    printf(\"Now\\nnum1 = %.2f, num2 = %.2f, num3 = %.2f\\n\",\n            num1, num2, num3);\n\n    return 0;\n}\n\nvoid sort(double * x, double * y, double * z)\n{\n    double temp;\n    if (*y < *x)\n    {\n        temp = *y;\n        *y = *x;\n        *x = temp;\n    }\n    if (*z < *x)\n    {\n        temp = *z;\n        *z = *x;\n        *x = temp;\n    }\n    if (*z < *y)\n    {\n        temp = *z;\n        *z = *y;\n        *y = temp;\n    }\n}\n\n        \n"
  },
  {
    "path": "Chapter_09/7.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nint get_ch(char ch);\nint main(void)\n{\n    char ch;\n\n    printf(\"Input some texts:\\n\");\n    while ((ch = getchar()) != EOF)\n        printf(\"%d \", get_ch(ch));\n    return 0;\n}\n\nint get_ch(char ch)\n{\n    if (isalpha(ch))\n        return tolower(ch) - 'a' +1;\n    else\n        return -1;\n}\n    \n\n    \n        \n"
  },
  {
    "path": "Chapter_09/8.c",
    "content": "#include <stdio.h>\ndouble power(double n, int p);\nint main(void)\n{\n    double x, xpow;\n    int exp;\n\n    printf(\"Enter a number and the interger power (q to quit):\\n\");\n    while (scanf(\"%lf%d\", &x, &exp) == 2)\n    {\n        xpow = power(x, exp);\n        printf(\"%.3g to the power %d is %g\\n\", x, exp, xpow);\n        printf(\"Enter the next pair of numbers or q to quit.\\n\");\n    }\n    printf(\"Bye.\\n\");\n}\n\ndouble power(double n, int p)\n{\n    int count;\n    double pow = 1;\n    \n    if (p == 0)\n        if (n == 0)\n        {\n            pow = 1;\n            printf(\"It is not defined, so the value is treated as 1\\n\");\n        }\n        else\n            pow = 1;\n    \n    else if (p > 0)\n        for (count = 0; count < p; count++)\n            pow *= n;\n    \n    else\n        if (n != 0)\n            for (count = 0; count > p; count--)\n                pow /= n;\n        else\n            pow = 0;\n\n        return pow;\n}\n"
  },
  {
    "path": "Chapter_09/9.c",
    "content": "#include <stdio.h>\ndouble power(double n, int p);\nint main(void)\n{\n    double x, xpow;\n    int exp;\n\n    printf(\"Enter a number and the interger power (q to quit):\\n\");\n    while (scanf(\"%lf%d\", &x, &exp) == 2)\n    {\n        xpow = power(x, exp);\n        printf(\"%.3g to the power %d is %g\\n\", x, exp, xpow);\n        printf(\"Enter the next pair of numbers or q to quit.\\n\");\n    }\n    printf(\"Bye.\\n\");\n}\n\ndouble power(double n, int p)\n{\n    int count;\n    double pow;\n\n    if (p > 0)\n        pow = n * power(n, p - 1);\n    else if (p < 0)\n        if (n != 0)\n            pow = (1.0 / n) * power(n, p + 1);\n        else\n            pow = 0;\n    else\n        pow = 1;\n\n    return pow;\n}\n"
  },
  {
    "path": "Chapter_10/1.c",
    "content": "#include <stdio.h>\n#define\tMONTHS 12\t\t\t//一年的月份数\n#define YEARS 5\t\t\t\t//一年的年数\nint main(void)\n{\n    //用2010~2014年的降水量数据初始化数组\n    const float rain[YEARS][MONTHS] =\n    {\n        {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},\n        {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},\n        {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},\n        {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},\n        {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}\n    };\n    int year, month;\n    float subtot, total;\n\n    printf(\" YEAR\t    RAINFALL (inches)\\n\");\n    for (year = 0, total = 0; year < YEARS; year++)\n    {\t//每一年，个月降水量总和\n        for (month = 0, subtot = 0; month < MONTHS; month++)\n            subtot += *(*(rain + year) + month);\n        printf(\"%5d %15.1f\\n\", 2010 + year, subtot);\n        total += subtot;\t//5年的总降水量\n    }\n    printf(\"\\n The yearly average is %.1f inches.\\n\\n\", total / YEARS);\n    printf(\" MONTHLY AVERAGES:\\n\\n\");\n    printf(\" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct \");\n    printf(\" Nov  Dec\\n\");\n\n    for (month = 0; month < MONTHS; month++)\n    {\n        for (year = 0, subtot = 0; year < YEARS; year++)\n            subtot += *(*(rain + year) +month);\n        printf(\"%4.1f \", subtot / YEARS);\n    }\n    printf(\"\\n\");\n\n    return 0;\n}\n\n"
  },
  {
    "path": "Chapter_10/10.c",
    "content": "#include <stdio.h>\n#define SIZE 4\nvoid print_ar(double ar[], int n);\t\t//打印数组元素的函数\n//将两个数组相加放入第三个数组的函数\nvoid add_ar(double ar1[], double ar2[], double ar3[], int n);\nint main(void)\n{\n    double ar1[SIZE] = {2, 4, 5, 8};\n    double ar2[SIZE] = {1, 0, 4, 6};\n    double ar3[SIZE];\n    \n    printf(\"The first array:  \");\n    print_ar(ar1, SIZE);\n    printf(\"The second array: \");\n    print_ar(ar2, SIZE);\n    add_ar(ar1, ar2, ar3, SIZE);\n    printf(\"The third array:  \");\n    print_ar(ar3, SIZE);\n\n    return 0;\n}\n\nvoid print_ar(double ar[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        printf(\"%g \", ar[i]);\n    printf(\"\\n\");\n}\n\nvoid add_ar(double ar1[], double ar2[], double ar3[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        ar3[i] = ar1[i] + ar2[i];\n}\n"
  },
  {
    "path": "Chapter_10/11.c",
    "content": "#include <stdio.h>\n#define ROWS 3\n#define COLS 5\nvoid change_ar(int ar[][COLS], int rows);\t\t//变化数组的函数\nvoid print_ar(int ar[][COLS], int rows);\t\t//打印数组的函数\nint main(void)\n{\n    int array[ROWS][COLS] =\n    {\n        {1, 2, 6, 3, 3},\n        {6, 5, 2, 3, 2},\n        {7, 8, 7, 3, 6}\n    };\n    \n    printf(\"The original array:\\n\");\n    print_ar(array, ROWS);\n    change_ar(array, ROWS);\n    printf(\"The present array:\\n\");\n    print_ar(array, ROWS);\n\n    return 0;\n}\n\nvoid change_ar(int ar[][COLS], int rows)\n{\n    int r, c;\n    for (r = 0; r < rows; r++)\n        for (c = 0; c < COLS; c++)\n            ar[r][c] *= 2;\n}\n\nvoid print_ar(int ar[][COLS], int rows)\n{\n    int r, c;\n    for (r = 0; r < rows; r++)\n    {\n        for (c = 0; c < COLS; c++)\n            printf(\"%3d \", ar[r][c]);\n        printf(\"\\n\");\n    }\n}\n"
  },
  {
    "path": "Chapter_10/12.c",
    "content": "#include <stdio.h>\n#define\tMONTHS 12\t\t\t//一年的月份数\n#define YEARS 5\t\t\t\t//一年的年数\nvoid rainfall(float rain[][MONTHS], int years);\nint main(void)\n{\n    //用2010~2014年的降水量数据初始化数组\n    const float rain[YEARS][MONTHS] =\n    {\n        {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},\n        {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},\n        {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},\n        {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},\n        {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}\n    };\n    rainfall(rain, YEARS);\n\n    return 0;\n}\n    \n    \nvoid rainfall(float rain[][MONTHS], int years)\n{\n    float subtot, total;\n    int y, m;\n    printf(\" YEAR\t    RAINFALL (inches)\\n\");\n    for (y = 0, total = 0; y < years; y++)\n    {\t//每一年，各月降水量总和\n        for (m = 0, subtot = 0; m < MONTHS; m++)\n            subtot += rain[y][m];\n        printf(\"%5d %15.1f\\n\", 2010 + y, subtot);\n        total += subtot;\t//5年的总降水量\n    }\n    printf(\"\\n The yearly average is %.1f inches.\\n\\n\", total / YEARS);\n    printf(\" MONTHLY AVERAGES:\\n\\n\");\n    printf(\" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct \");\n    printf(\" Nov  Dec\\n\");\n\n    for (m = 0; m < MONTHS; m++)\n    {\n        for (y = 0, subtot = 0; y < years; y++)\n            subtot += rain[y][m];\n        printf(\"%4.1f \", subtot / years);\n    }\n    printf(\"\\n\");\n}\n\n\n"
  },
  {
    "path": "Chapter_10/13.c",
    "content": "#include <stdio.h>\n#define ROWS 3\n#define\tCOLS 5\n#define NUM 15\t\t//共计15个数\nvoid get_num(double ar[][COLS], int rows);\t\t//把用户输入的数据储存在数组中\ndouble aver_row(double ar[]);\t\t\t\t\t//计算每组数据的平均值\ndouble aver_all(double ar[][COLS], int rows);\t//计算所有数据的平均值\ndouble max_num(double ar[][COLS], int rows);\t\t//找出15个数据的最大值\n//打印结果\nvoid print_result(double ar[][COLS], int rows);\n\nint main(void)\n{\n    double array[ROWS][COLS];\n\n    get_num(array, ROWS);\n    print_result(array, ROWS);\n\n    return 0;\n}\n\n\nvoid get_num(double ar[][COLS], int rows)\n{\n    int r, c;\n    double a = 1.0;\t\t\t//是为了让vc6.0编译器链接浮点链接库\n\n    printf(\"Please enter 3 groups of numbers, and there\\n\"\n           \"are 5 numbers in each group:\\n\");\n\n    for (r = 0; r < rows; r++)\n        for (c = 0; c < COLS; c++)\n            scanf(\"%lf\", &ar[r][c]);\n}\n\ndouble aver_row(double ar[])\n{\n    int c;\n    double sub;\n\n    for (c = 0, sub = 0.0; c < COLS; c++)\n        sub += ar[c];\n\n    return (sub / COLS);\n}\n\ndouble aver_all(double ar[][COLS], int rows)\n{\n    int r, c;\n    double total;\n\n    for (r = 0, total = 0.0; r < rows; r++)\n        for (c = 0; c < COLS; c++)\n            total += ar[r][c];\n    return (total / NUM);\n}\n\ndouble max_num(double ar[][COLS], int rows)\n{\n    int r, c;\n    double max;\n    \n    for (r = 0, max = ar[0][0]; r < rows; r++)\n        for (c = 0; c < COLS; c++)\n            if (max < ar[r][c])\n                max = ar[r][c];\n    \n    return max;\n}\n\nvoid print_result(double ar[][COLS], int rows)\n{\n    int r, c, row;\n    //打印数组\n    printf(\"The array:\\n\");\n    for (r = 0; r < rows; r++)\n    {\n        for (c = 0; c < COLS; c++)\n            printf(\"%-5g\", ar[r][c]);\n        printf(\"\\n\");\n    }\n    //输出每组平均值\n    for (row = 0; row <ROWS; row++)\n        printf(\"The average value in row %d is: %g\\n\",\n                row, aver_row(ar[row]));\n    //输出所有数据平均值\n    printf(\"The average value in the array is: %g\\n\",\n            aver_all(ar, ROWS));\n    //输出15个数据的最大值\n    printf(\"The maximum number in the array is: %g\\n\",\n            max_num(ar, ROWS));\n    printf(\"Bye.\\n\");\n}\n"
  },
  {
    "path": "Chapter_10/2.c",
    "content": "#include <stdio.h>\n#define SIZE 5\nvoid copy_arr(double ar1[], double ar[], int n);\nvoid copy_ptr(double * ar2, double * ar, int n);\nvoid copy_ptrs(double * ar3, double * ar, double * end);\nvoid print_array(double ar[], int n);\t\t\t\t//打印数组的函数\n\nint main(void)\n{\n    double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};\n    double target1[SIZE];\n    double target2[SIZE];\n    double target3[SIZE];\n\n    copy_arr(target1, source, SIZE);\n    copy_ptr(target2, source, SIZE);\n    copy_ptrs(target3, source, source + SIZE);\n    print_array(target1, SIZE);\n    print_array(target2, SIZE);\n    print_array(target3, SIZE);\n\n    return 0;\n}\n\nvoid copy_arr(double ar1[], double ar[], int n)\n{\n    int index;\n    for (index = 0; index < n; index++)\n        ar1[index] = ar[index];\n}\n\nvoid copy_ptr(double * ar2, double * ar, int n)\n{\n    int i;\n    for (i = 0; i < n; i++)\n        *(ar2++) = *(ar++);\n}\n\nvoid copy_ptrs(double * ar3, double * ar, double * end)\n{\n    int i;\n    for (i = 0; ar < end; i++)\n        *(ar3++) = *(ar++);\n}\n\nvoid print_array(double ar[], int n)\n{\n    int index;\n    for (index = 0; index < n; index++)\n        printf(\"%g \", ar[index]);\n    printf(\"\\n\");\n}\n"
  },
  {
    "path": "Chapter_10/3.c",
    "content": "#include <stdio.h>\nint max(int ar[], int n);\nint main(void)\n{\n    int num[] = {23,2,32,66,3,5,25,7,3,20};\n    \n    printf(\"The biggest number is %d\\n\", max(num, 10));\n\n    return 0;\n}\n\nint max(int ar[], int n)\n{\n    int i, max;\n\n    for (i = 0, max = ar[0]; i < n; i++)\n    {\n        if (max < ar[i])\n            max = ar[i];\n    }\n    return max;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_10/4.c",
    "content": "#include <stdio.h>\nint max_index(double *ar, int n);\nint main(void)\n{\n    double array[] = {1.5, 3.75, 4.3, 2.6, 2.15, 6.66};\n\n    printf(\"The index of the maximum number is: %d\\n\", max_index(array, 6));\n\n    return 0;\n}\n\nint max_index(double ar[], int n)\n{\n    int i, max;\n\n    for (max = 0, i = 1; i < n; i++)\n        if (ar[max] < ar[i])\n            max = i;\n\n    return max;\n}\n\n"
  },
  {
    "path": "Chapter_10/5.c",
    "content": "#include <stdio.h>\ndouble gap(double ar[], int n);\nint main(void)\n{\n    double array[] = {1.5, 3.75, 4.3, 2.6, 2.15, 6.66};\n\n    printf(\"The difference between the maximum and minimum \"\n           \"values is:\\n%g\\n\", gap(array, 6));\n    \n    return 0;\n}\n\ndouble gap(double ar[], int n)\n{\n    int i;\n    double max, min;\n\n    max = min = ar[0];\n    for (i = 0; i < n; i++)\n    {\n        if (max < ar[i])\n            max = ar[i];\n        if (min > ar[i])\n            min = ar[i];\n    }\n\n    return (max - min);\n}\n"
  },
  {
    "path": "Chapter_10/6.c",
    "content": "#include <stdio.h>\n#define SIZE 6\nvoid reverse(double ar [], int n);\nvoid print_ar(double ar[], int n);\t\t\t//打印数组的函数\nint main(void)\n{\n    double array[] = {1.5, 3.75, 4.3, 2.6, 2.15, 6.66};\n\n    printf(\"The original array:\\n\");\n    print_ar(array, SIZE);\n    reverse(array, SIZE);\t\t\t\t\t//使数据倒序排列\n    printf(\"The present array:\\n\");\n    print_ar(array, SIZE);\n\n    return 0;\n}\n\nvoid reverse(double ar [], int n)\n{\n    int i;\n    double temp;\n\n    for (i = 0; i < n / 2; i++)\n    {\n        temp = ar[i];\n        ar[i] = ar[n - i -1];\n        ar[n - i -1] = temp;\n    }\n}\n\n\nvoid print_ar(double ar[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        printf(\"%g \", ar[i]);\n    printf(\"\\n\");\n}\n\n\n"
  },
  {
    "path": "Chapter_10/7.c",
    "content": "#include <stdio.h>\n#define ROWS 3\n#define COLS 2\nvoid copy_ar(double ar1[], double ar[], int n);\t\t//拷贝数组的函数\nvoid print_ar(double ar[][COLS], int rows);\t\t\t//打印数组的函数\nint main(void)\n{\n    int rows;\n    double target[ROWS][COLS];\n    double source[ROWS][COLS] = { {1.2, 2.3}, {4.6, 5.2}, {7.8, 8.6} };\n    \n    printf(\"The original array:\\n\");\n    print_ar(source, ROWS);\n    for (rows = 0; rows < ROWS; rows++)\n        copy_ar(target[rows], source[rows], COLS);\n    printf(\"The copied array:\\n\");\n    print_ar(target, ROWS);\n\n    return 0;\n}\n\nvoid copy_ar(double ar1[], double ar[], int n)\n{\n    int index;\n    for (index = 0; index < n; index++)\n        ar1[index] = ar[index];\n}\n\nvoid print_ar(double ar[][COLS], int rows)\n{\n    int r, c;\n    for (r = 0; r < rows; r++)\n    {\n        for (c = 0; c < COLS; c++)\n            printf(\"%g \", ar[r][c]);\n        printf(\"\\n\");\n    }\n}\n\n\n"
  },
  {
    "path": "Chapter_10/8.c",
    "content": "#include <stdio.h>\n#define SIZE_SOU 7\t\t\t//源数组元素个数\n#define SIZE_TAR 3\t\t\t//待拷贝元素个数\nvoid copy_ptr(double * ar2, double * ar, int n);\nint main(void)\n{\n    int i;\n\n    double target[SIZE_TAR];\n    double souce[SIZE_SOU] = {1.5, 3.75, 4.3, 2.6, 2.15, 3.27, 6.66};\n    copy_ptr(target, souce + 2, SIZE_TAR);\n    for (i = 0; i < SIZE_TAR; i++)\n        printf(\"%g \", target[i]);\n    printf(\"\\n\");\n\n    return 0;\n}\n\nvoid copy_ptr(double * ar2, double * ar, int n)\n{\n    int i;\n    for (i = 0; i < n; i++)\n        *(ar2++) = *(ar++);\n}\n\n"
  },
  {
    "path": "Chapter_10/9.c",
    "content": "#include <stdio.h>\n#define ROWS 3\n#define COLS 5\nvoid copy_ar(double ar1[][COLS], double ar[][COLS], int n);\t\t//拷贝数组的函数\nvoid print_ar(double ar[][COLS], int rows);\t\t\t\t\t\t//打印数组的函数\nint main(void)\n{\n    double target[ROWS][COLS];\n    double source[ROWS][COLS] =\n    {\n        {1.2, 2.3, 2.6, 3.5, 3.2},\n        {4.6, 5.2, 2.8, 3.3, 2.1},\n        {7.8, 8.6, 7.2, 3.9, 6.6}\n    };\n    \n    printf(\"The original array:\\n\");\n    print_ar(source, ROWS);\n    copy_ar(target, source, ROWS);\n    printf(\"The copied array:\\n\");\n    print_ar(target, ROWS);\n\n    return 0;\n}\n\nvoid copy_ar(double ar1[][COLS], double ar[][COLS], int rows)\n{\n    int r, c;\n    for (r = 0; r < rows; r++)\n        for (c = 0; c < COLS; c++)\n            ar1[r][c] = ar[r][c];\n}\n\nvoid print_ar(double ar[][COLS], int rows)\n{\n    int r, c;\n    for (r = 0; r < rows; r++)\n    {\n        for (c = 0; c < COLS; c++)\n            printf(\"%g \", ar[r][c]);\n        printf(\"\\n\");\n    }\n}\n"
  },
  {
    "path": "Chapter_11/1.c",
    "content": "#include <stdio.h>\n#define SIZE 40\nchar * gets_n(char ar[], int n);\nint main(void)\n{\n    char str[SIZE];\n\n    gets_n(str, 10);\n    puts(str);\n\n    return 0;\n}\n\nchar * gets_n(char ar[], int n)\n{\n    int count = 0;\n\n    while (count < n)\n        ar[count++] = getchar();\n    ar[count] = '\\0';\n\n    return ar;\n}\n"
  },
  {
    "path": "Chapter_11/10.c",
    "content": "#include <stdio.h>\n#define SIZE 40\nchar * s_gets(char * st, int n);\nvoid delete_space(char * st);\nint main(void)\n{\n    char str[SIZE];\n    while (*s_gets(str, SIZE) != '\\n')\n        delete_space(str);\n    printf(\"Bye.\\n\");\n    \n    return 0;\n}\n\n//删除空格的函数\nvoid delete_space(char * st)\n{\n    int i;\n    char * p = st;\n    \n    printf(\"\\nThe original string is:\\n\");\n    puts(st);\n    while (*st)\n    {\t//如果遇到空格，后面所有字符往前进一格\n        if (*st == ' ')\n            for (i = 0; st[i] != '\\0'; i++)\n                st[i] = st[i + 1];\n        else\n            st++;\n    }\n    printf(\"Now, the string is:\\n\");\n    puts(p);\n    printf(\"\\n\");\n}\n\n//自定义的字符串输入函数\nchar * s_gets(char * st, int n)\n{\n    int i = 0;\n    \n    printf(\"Input the first string (blank line to quit):\\n\");\n    if (fgets(st, n, stdin) != NULL && st[0] != '\\n')\t//即返回值st不等于NULL\n    {\n        while (st[i] != '\\n' && st[i] != '\\0')\n            i++;\n        if (st[i] == '\\n')\n            st[i] = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return st;\n}\n"
  },
  {
    "path": "Chapter_11/11.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#define LIM 10\n#define SIZE 80\nchar * s_gets(char * st, int n);\t\t\t//获取字符串输入\nchar get_choice(void);\t\t\t\t\t\t//获取选择\nvoid print_sour(char * st[], int n);\t\t//打印源字符串\nvoid print_ascii(char * st[], int n);\t\t//按ASCII码顺序打印\nvoid print_length(char * st[], int n);\t\t//按长度顺序打印\nvoid print_first_word(char * st[], int n);\t//按第一个单词的长度顺序打印\nint first_word(char * st);\t\t\t\t\t//计算第一个单词数\n\nint main(void)\n{\n    char str[LIM][SIZE];\n    char * ptr[LIM];\n    int row = 0;\t\t\t\t\t\t\t//输入的字符串数量\n    char choice;\t\t\t\t\t\t\t//用户输入的选择字符\n\n    printf(\"Input some strings before EOF (no more than 10):\\n\");\n    while (row < LIM && s_gets(str[row], SIZE) != NULL)\n    {\n        ptr[row] = str[row];\t\t\t\t//设置指针指向字符串\n        row++;\n    }\n    while((choice = get_choice()) != 'q')\n    {\n        printf(\"\\n\");\n        switch (choice)\n        {\n            case 'a': print_sour(ptr, row);\n                      break;\n            case 'b': print_ascii(ptr, row);\n                      break;\n            case 'c': print_length(ptr, row);\n                      break;\n            case 'd': print_first_word(ptr, row);\n                      break;\n            default: printf(\"Error!\\n\");\n                     break;\n        }\n        printf(\"\\n\");\n    }\n    printf(\"Bye.\\n\");\n    \n    return 0;\n}\n\n//获取用户选择\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"a. print the sourse strings.     b. print in order of ASCII.\\n\");\n    printf(\"c. print in order of length.\t d. print in order of the first word's length.\\n\");\n    printf(\"q. quit.\\n\");\n    printf(\"Enter a character (a, b, c, d or q):\");\n    ch = getchar();\n    while (getchar() != '\\n')\n        continue;\n    while (ch < 'a' || ch > 'd' && ch != 'q')\n    {\n        printf(\"Please input the right option (a, b, c, d or q): \");\n        ch = getchar();\n        while (getchar() != '\\n')\n        continue;\n    }\n    return ch;\n}\n    \n//打印源字符串\nvoid print_sour(char * st[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        puts(st[i]);\n}\n    \n//按ASCII中的顺序打印字符串\nvoid print_ascii(char * st[], int n)\n{\n    int i, j;\n    char * temp;\n\n    for (i = 0; i < n - 1; i++)\n        for (j = i + 1; j < n; j++)\n            if (strcmp(st[i], st[j]) > 0)\n            {\n                temp = st[j];\n                st[j] = st[i];\n                st[i] = temp;\n            }\n    for (i = 0; i < n; i++)\n        puts(st[i]);\n}\n\n//按长度顺序打印\nvoid print_length(char * st[], int n)\n{\n    int i, j;\n    char * temp;\n\n    for (i = 0; i < n - 1; i++)\n        for (j = i + 1; j < n; j++)\n            if (strlen(st[i]) > strlen(st[j]))\n                {\n                    temp = st[j];\n                    st[j] = st[i];\n                    st[i] = temp;\n                }\n    for (i = 0; i < n; i++)\n        puts(st[i]);\n}\n//按第一个单词的长度顺序打印\nvoid print_first_word(char * st[], int n)\n{\n    int i, j;\n    char * temp;\n\n    for (i = 0; i < n - 1; i++)\n        for (j = i + 1; j < n; j++)\n            if (first_word(st[i]) > first_word(st[j]))\n                {\n                    temp = st[j];\n                    st[j] = st[i];\n                    st[i] = temp;\n                }\n    for (i = 0; i < n; i++)\n    puts(st[i]);\n}\n\n//计算第一个单词的长度\nint first_word(char * st)\n{\n    int i = 0;\n    int count = 0;\n\n    while (!isalpha(st[i]))\n            i++;\n    while (isalpha(st[i]))\n    {\n        i++;\n        count++;\n    }\n\n    return count;\n}\n    \n//自定义的输入函数\nchar * s_gets(char * st, int n)\n{\n    int i = 0;\n    if (fgets(st, n, stdin) != NULL)\t//即返回值st不等于NULL\n    {\n        while (st[i] != '\\n' && st[i] != '\\0')\n            i++;\n        if (st[i] == '\\n')\n            st[i] = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n        return st;\n    }\n    else\n        return NULL;\n}\n"
  },
  {
    "path": "Chapter_11/12.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nint main(void)\n{\n    char ch;\n    int word = 0, upper = 0, lower = 0;\n    int punct = 0, digit = 0, inword = 0;\n    \n    while ((ch = getchar()) != EOF)\n    {\n        if (!isspace(ch) && (!ispunct(ch)) && !inword)\n        {\n            inword = 1;\n            word++;\n        }\n        if (isspace(ch) || ispunct(ch) && inword)\n            inword = 0;\n        if (isupper(ch))\n            upper++;\n        if (islower(ch))\n            lower++;\n        if (ispunct(ch))\n            punct++;\n        if (isdigit(ch))\n            digit++;\n    }\n    printf(\"words: %d, upper: %d, lower: %d, punct: %d, digit: %d.\\n\",\n            word, upper, lower, punct, digit);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_11/13.c",
    "content": "#include <stdio.h>\nint main(int argc, char * argv[])\n{\n    int count;\n\n    for (count = argc - 1; count > 0; count--)\n        printf(\"%s \", argv[count]);\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_11/14.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\ndouble mypower(double n, int exp);\nint main(int argc, char * argv[])\n{\n    printf(\"The result is: %g\",\n            mypower(atof(argv[1]), atoi(argv[2])));\n}\n\n\ndouble mypower(double n, int exp)\n{\n    double pow;\n\n    if (exp > 0)\n        for (pow = 1; exp > 0; exp--)\n            pow *= n;\n    else if (exp < 0)\n    {\tif (!n)\n            printf(\"It's not defined!\\n\");\n        for (pow = 1; exp < 0; exp++)\n            pow *= (1 / n);\n    }\n    else if (n)\n        pow = 1;\n    else\n    {\tpow = 1 / n;\n        printf(\"It's not defined!\\n\");\n    }\n\n    return pow;\n}\n"
  },
  {
    "path": "Chapter_11/15.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\nint myatoi(char * st);\nint main(void)\n{\n    char str[40];\n    gets(str);\n    printf(\"%d\\n\", myatoi(str));\n}\n\nint myatoi(char * st)\n{\n    int sum = 0;\n\n    while (*st)\n    {\t//把字符数字逐位转化为数值\n        if (isdigit(*st))\n            sum = 10 * sum + (*st - '0');\n        else\n            return 0;\t\t//不是纯数字则返回0\n        st++;\n    }\n\n    return sum;\n}\n"
  },
  {
    "path": "Chapter_11/16.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#define SIZE 80\n#define LIM 10\nint main(int argc, char * argv[])\n{\n    int col;\n    int i;\n    int row = 0;\n    char str[LIM][SIZE];\n    \n    while (row < LIM && fgets(str[row], SIZE, stdin) != NULL)\n    {\n        if (argc == 1);\n        else if (!strcmp(argv[1], \"-p\"));\n        else if (!strcmp(argv[1], \"-u\"))\n            for (col = 0; str[row][col] != '\\0'; col++)\n                str[row][col] = toupper(str[row][col]);\n        else if (!strcmp(argv[1], \"-l\"))\n            for (col = 0; str[row][col] != '\\0'; col++)\n                str[row][col] = tolower(str[row][col]);\n        row++;\n    }\n    for (i = 0; i < row; i++)\n        fputs(str[i], stdout);\n\n    return 0;\n}\n\n\n"
  },
  {
    "path": "Chapter_11/2.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\n#define SIZE 80\nchar * gets_n(char *ar, int n);\nint main(void)\n{\n    char str[SIZE];\n    \n    printf(\"Enter some characters:\\n\");\n    gets_n(str, 10);\n    puts(str);\n\n    return 0;\n}\n\nchar * gets_n(char *ar, int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n    {\n        *(ar + i) = getchar();\n        if (isspace(*(ar + i)))\n            break;\n    }\n    *(ar + i) = '\\0';\n\n    return ar;\n}\n"
  },
  {
    "path": "Chapter_11/3.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\n#define SIZE 40\nvoid get_words(char * ar);\nint main(void)\n{\n    char str[SIZE];\n    \n    printf(\"Enter some characters:\\n\");\n    get_words(str);\n    puts(str);\n\n    return 0;\n\n}\n\nvoid get_words(char * ar)\n{\n    char ch;\n\n    while (isspace(ch = getchar()))\n        continue;\n    do\n        *ar++ = ch;\n    while (!isspace(ch = getchar()));\n    *ar = '\\0';\n}\n        \n"
  },
  {
    "path": "Chapter_11/4.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\n#define SIZE 10\nvoid get_words(char * ar, int n);\nint main(void)\n{\n    char str[SIZE];\n    \n    printf(\"Enter some characters:\\n\");\n    get_words(str, SIZE);\n    puts(str);\n\n    return 0;\n\n}\n\nvoid get_words(char * ar, int n)\n{\n    char ch;\n    int i = 0;\n\n    while (isspace(ch = getchar()))\n        continue;\n    do\n    {\n        *(ar + i) = ch;\n        i++;\n    }\n    while (!isspace(ch = getchar()) && i < (n - 1));\n    *(ar + i) = '\\0';\n}\n        \n"
  },
  {
    "path": "Chapter_11/5.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define QUIT \"quit\"\n#define SIZE 80\n//搜索字符在字符串中首次出现的位置\nchar * str_ch(char * str, char c);\nint main(void)\n{\n    char str[SIZE];\n    char ch;\n    char * ret_val;\n\n    printf(\"Input a string (type quit to quit):\\n\");\n    gets(str);\n    while (strcmp(str, QUIT))\n    {\n        printf(\"Input a character: \");\n        scanf(\"%c\", &ch);\n        while (getchar() != '\\n')\t\t//跳过剩余输入部分\n            continue;\n        ret_val = str_ch(str, ch);\n        if (ret_val != NULL)\n            printf(\"Find! The string start with the %c:\\n\"\n            \"%s\\n\", ch, ret_val);\n        else\n            printf(\"Can't find!\\n\");\n        printf(\"Input a string (type quit to quit):\\n\");\n        gets(str);\n    }\n    printf(\"Bye.\\n\");\n        \n    return 0;\n}\n\n\n\nchar * str_ch(char * str, char c)\n{\n    int flag = 0;\n    int count = 0;\n    \n    while (count++ < strlen(str))\n        if (*str++ == c)\n        {\n            flag = 1;\n            break;\n        }\n    if (flag)\n        return str - 1;\n    else\n        return NULL;\n}\n"
  },
  {
    "path": "Chapter_11/6.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define QUIT \"quit\"\n#define SIZE 80\n//搜索字符在字符串中首次出现的位置\nint is_within(char * str, char c);\nint main(void)\n{\n    char str[SIZE];\n    char ch;\n\n    printf(\"Input a string (type quit to quit):\\n\");\n    while (strcmp(gets(str), QUIT))\n    {\n        printf(\"Input a character: \");\n        scanf(\"%c\", &ch);\n        while (getchar() != '\\n')\t\t//跳过剩余输入部分\n            continue;\n        if (is_within(str, ch))\n            printf(\"Find!\\n\");\n        else\n            printf(\"Can't find!\\n\");\n        printf(\"Input a string (type quit to quit):\\n\");\n    }\n    printf(\"Bye.\\n\");\n        \n    return 0;\n}\n\n\n\nint is_within(char * str, char c)\n{\n    int flag = 0;\n    size_t count = 0;\n    \n    while (count++ < strlen(str))\n        if (*str++ == c)\n        {\n            flag = 1;\n            break;\n        }\n\n    return flag;\n}\n"
  },
  {
    "path": "Chapter_11/7.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define QUIT \"quit\"\n#define SIZE 40\nchar * mystrncpy(char * p1, char * p2, int n);\nint main(void)\n{\n    char s1[SIZE];\n    char s2[SIZE];\n    int num;\n    \n    printf(\"Input the first string (type quit to quit):\\n\");\n    while (strcmp(gets(s1), QUIT))\t\t//输入quit结束\n    {\n        printf(\"Input the second string:\\n\");\n        gets(s2);\n        printf(\"Input the number of char copied from s2: \");\n        scanf(\"%d\", &num);\n        getchar();\t\t\t\t\t\t//跳过回车\n        //当s2的长度大于或等于要拷贝的字符数时，要给s1末尾加上'\\0',\n        //并且注意要在调用函数之前，不然strlen(s1)会发生改变\n        if (strlen(s2) >= num)\n            s1[strlen(s1) + num] = '\\0';\n        puts(mystrncpy(s1, s2, num));\t//输出拷贝后的字符串s1\n        printf(\"Input the first string (type quit to quit):\\n\");\n    }\n    \n    printf(\"Bye.\\n\");\n}\n\nchar * mystrncpy(char * p1, char * p2, int n)\n{\n    int i = 1;\n    char * p = p1;\n\n    while (*p1++)\n        continue;\n    //p1指针指向位置已经超过空字符\n    *--p1 = *p2;\n    while (i < n && *p2)\n    {\n        *++p1 = *++p2;\n        i++;\n    }\n\n    return p;\n}\n"
  },
  {
    "path": "Chapter_11/8.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define SIZE 40\n#define QUIT \"quit\"\nchar * s_gets(char * st, int n);\t\t\t\t//读取字符串输入的函数\nchar * string_in(char * s1, char * s2);\nint main(void)\n{\n    char str1[SIZE];\n    char str2[SIZE];\n    char * ret_val;\n    \n    printf(\"Input the first string (type quit to quit):\\n\");\n    while (strcmp(s_gets(str1, SIZE), QUIT))\t\t//输入quit结束\n    {\n        printf(\"Input the second string:\\n\");\n        s_gets(str2, SIZE);\n        ret_val = string_in(str1, str2);\n        if (ret_val)\n        {\n            printf(\"Find!\\n\");\n            puts(ret_val);\n        }\n        else\n            printf(\"Can't find!\\n\");\n        printf(\"Input the first string (type quit to quit):\\n\");\n    }\n    \n    printf(\"Bye.\\n\");\n}\n\n//自定义的字符串输入函数\nchar * s_gets(char * st, int n)\n{\n    int i = 0;\n\n    if (fgets(st, n, stdin))\t//即返回值st不等于NULL\n    {\n        while (st[i] != '\\n' && st[i] != '\\0')\n            i++;\n        if (st[i] == '\\n')\n            st[i] = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return st;\n}\n\n\n//在s1中检测s2\nchar * string_in(char * s1, char * s2)\n{\n    int i;\n    \n    while (*s1)\n    {\n        i = 0;\n        while (s1[i] == s2[i])\n        {\n            i++;\n            if (!s2[i])\n                return s1;\n        }\n        s1++;\n    }\n\n    return NULL;\n}\n\n\n\n        \n"
  },
  {
    "path": "Chapter_11/9.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define SIZE 40\n#define QUIT \"quit\"\nchar * s_gets(char * st, int n);\nvoid reverse(char * st);\nint main(void)\n{\n    char str[SIZE];\n    while (strcmp(s_gets(str, SIZE), QUIT))\t\t//输入quit结束\n    {\n        reverse(str);\n        puts(str);\n    }\n    \n    return 0;\n}\n\n\n//自定义的字符串输入函数\nchar * s_gets(char * st, int n)\n{\n    int i = 0;\n    \n    printf(\"Input the first string (type quit to quit):\\n\");\n    if (fgets(st, n, stdin))\t//即返回值st不等于NULL\n    {\n        while (st[i] != '\\n' && st[i] != '\\0')\n            i++;\n        if (st[i] == '\\n')\n            st[i] = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return st;\n}\n\n//使字符串倒序的函数\nvoid reverse(char * st)\n{\n    unsigned int i;\n    char temp;\n\n    size_t length = strlen(st);\n    for (i = 0; i < length / 2; i++)\n    {\n        temp = st[length - i -1];\n        st[length - i - 1] = st[i];\n        st[i] = temp;\n    }\n    st[length] = '\\0';\n}\n\n\n\n\n\n    \n\n"
  },
  {
    "path": "Chapter_12/1.c",
    "content": "#include <stdio.h>\nint critic(void);\nint main(void)\n{\n    int units;\n\n    printf(\"How many pounds to a firkin of butter?\\n\");\n    scanf(\"%d\", &units);\n    while (units != 56)\n        units = critic();\n    printf(\"You must have looked it up!\\n\");\n\n    return 0;\n}\n\nint critic(void)\n{\n    int answer;\n\n    printf(\"No luck, my friend. Try again.\\n\");\n    scanf(\"%d\", &answer);\n\n    return answer;\n}\n"
  },
  {
    "path": "Chapter_12/2/pe12-2a.c",
    "content": "#include <stdio.h>\n#include \"pe12-2a.h\"\nint main(void)\n{\n    int mode;\n\n    printf(\"Enter 0 for metric mode, 1 for US mode: \");\n    scanf(\"%d\", &mode);\n    while (mode >= 0)\n    {\n        set_mode(mode);\n        get_info();\n        show_info();\n        printf(\"Enter 0 for metric mode, 1 for US mode\");\n        printf(\" (-1 to quit): \");\n        scanf(\"%d\", &mode);\n    }\n    printf(\"Done.\\n\");\n    \n    return 0;\n}\n"
  },
  {
    "path": "Chapter_12/2/pe12-2a.h",
    "content": "void set_mode(int mode);\nvoid get_info(void);\nvoid show_info(void);"
  },
  {
    "path": "Chapter_12/2/pe12-2b.c",
    "content": "#include <stdio.h>\nstatic int last_mode;\nstatic double distance;\nstatic double fuel;\n\nvoid set_mode(int mode)\n{\n    if (mode != 0 && mode != 1)\n    {\n        printf(\"Invalid mode specified. Mode %s used.\\n\",\n                last_mode == 0 ? \"0 (metric)\" : \"1 (US)\");\n    }\n    else\n        last_mode = mode;\n}\n\nvoid get_info(void)\n{\n        printf(\"Enter distance traveled in %s: \",\n            last_mode == 0 ? \"kilometers\" : \"miles\");\n        scanf(\"%lf\", &distance);\n        printf(\"Enter fuel consumed in %s: \",\n            last_mode == 0 ? \"liters\" : \"gallons\");\n        scanf(\"%lf\", &fuel);\n}\n\nvoid show_info(void)\n{\n    printf(\"Fuel consumption is \");\n    if (last_mode == 0)\n        printf(\"%.2f liters per 100 km.\\n\", fuel / distance * 100);\n    else\n        printf(\"%.1f miles per gallon.\\n\", distance / fuel);\n}\n\n"
  },
  {
    "path": "Chapter_12/3/pe12-3a.c",
    "content": "#include <stdio.h>\nvoid get_info(int mode);\nint main(void)\n{\n    int mode;\n\n    printf(\"Enter 0 for metric mode, 1 for US mode: \");\n    scanf(\"%d\", &mode);\n    while (mode >= 0)\n    {\n        get_info(mode);\n        printf(\"Enter 0 for metric mode, 1 for US mode\");\n        printf(\" (-1 to quit): \");\n        scanf(\"%d\", &mode);\n    }\n    printf(\"Done.\\n\");\n    \n    return 0;\n}\n"
  },
  {
    "path": "Chapter_12/3/pe12-3a.h",
    "content": "void set_mode(int mode);\nvoid get_info(void);\nvoid show_info(void);\n"
  },
  {
    "path": "Chapter_12/3/pe12-3b.c",
    "content": "#include <stdio.h>\nint set_mode(int mode);\nvoid show_info(double * distance, double * fuel, int mode);\n\nint set_mode(int mode)\n{\n    int last_mode = 0;\n    if (mode != 0 && mode != 1)\n    {\n        printf(\"Invalid mode specified. Mode %s used.\\n\",\n                last_mode == 0 ? \"0 (metric)\" : \"1 (US)\");\n    }\n    else\n        last_mode = mode;\n\n    return last_mode;\n}\n\nvoid get_info(int mode)\n{\n    double * p1, * p2;\n    double distance, fuel;\n\n    printf(\"Enter distance traveled in %s: \",\n            set_mode(mode) == 0 ? \"kilometers\" : \"miles\");\n    scanf(\"%lf\", &distance);\n    printf(\"Enter fuel consumed in %s: \",\n            set_mode(mode) == 0 ? \"liters\" : \"gallons\");\n    scanf(\"%lf\", &fuel);\n    p1 = &distance;\n    p2 = &fuel;\n    show_info(p1, p2, mode);\n}\n\nvoid show_info(double * distance, double * fuel, int mode)\n{\n    printf(\"Fuel consumption is \");\n    if (set_mode(mode) == 0)\n        printf(\"%.2f liters per 100 km.\\n\", *fuel / *distance * 100);\n    else\n        printf(\"%.1f miles per gallon.\\n\", *distance / *fuel);\n}\n"
  },
  {
    "path": "Chapter_12/4.c",
    "content": "#include <stdio.h>\n#define TIMES 20\nvoid called_count(void);\nstatic int count;\nint main(void)\n{\n    int i;\n\n    for (i = 0; i < TIMES; i++)\n        called_count();\n    printf(\"The times is %d.\\n\", count);\n}\n\nvoid called_count(void)\n{\n    count++;\n}\n"
  },
  {
    "path": "Chapter_12/5.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n#define SIZE 100\nvoid print_ar(int ar[], int n);\t\t\t//打印数组元素的函数\nint main(void)\n{\n    int i, j, temp;\n    int digit[SIZE];\n    srand((unsigned int)time(0));\n    for (i = 0; i < SIZE; i++)\n    {\n        digit[i] = rand() % 10 + 1;\n    }\n    printf(\"The original numbers:\\n\");\n    print_ar(digit, SIZE);\n    for (j = 0; j < SIZE - 1; j++)\n        for (i = j + 1; i < SIZE; i++)\n            if (digit[j] < digit[i])\n            {\n                temp = digit[i];\n                digit[i] = digit[j];\n                digit[j] = temp;\n            }\n    printf(\"\\nThe numbers in reverse order:\\n\");\n    print_ar(digit, SIZE);\n}\n\nvoid print_ar(int ar[], int n)\n{\n    int i;\n    for (i = 0; i < n; i++)\n    {\n        printf(\"%3d\", ar[i]);\n        if (i % 10 == 9)\n            printf(\"\\n\");\n    }\n}\n"
  },
  {
    "path": "Chapter_12/6.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#define SIZE 1000\n#define SIZE_N 10\n#define SIZE_S 10\nint main(void)\n{\n    int i, j;\n    int count[SIZE_N];\t\t\t//各随机数出现次数的数组\n    \n    unsigned int seed[SIZE_S] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n\n    for (j = 0; j < SIZE_S; j++)\n    {\n        for (i = 0; i < SIZE_N; i++)\n            count[i] = 0;\n        srand(seed[j]);\n        printf(\"seed = %d:\\n\", seed[j]);\n        for (i = 0; i < SIZE; i++)\n            count[rand() % 10]++;\n        for (i = 0; i < SIZE_N; i++)\n        {\n            printf(\"%d:%-5d  \", i + 1, count[i]);\n            if (i == 4)\n                printf(\"\\n\");\n        }\n        printf(\"\\n\");\n    }\n\n    return 0;\n}\n        \n"
  },
  {
    "path": "Chapter_12/7.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\nvoid roll_count(int sets, int sides, int dice);\nint main(void)\n{\n    int sets;\n    int sides, dice;\n    \n    srand((unsigned int)time(0));\n    printf(\"Enter the number of sets; enter q to stop: \");\n    while (scanf(\"%d\", &sets) == 1)\n    {\n        printf(\"How many sides and how many dice? \");\n        scanf(\"%d%d\", &sides, &dice);\n        printf(\"Here are %d sets of %d %d-sided throws.\\n\",\n                sets, dice, sides);\n        roll_count(sets, sides, dice);\n        printf(\"How many sets? Enter q to stop: \");\n    }\n    printf(\"Bye.\\n\");\n}\n\nvoid roll_count(int sets, int sides, int dice)\n{\n    int i, j;\n    int roll;\n\n    if (sides < 2)\n    {\n        printf(\"Need at least 2 sides.\\n\");\n        return;\n    }\n    if (dice < 1)\n    {\n        printf(\"Need at least 1 die.\\n\");\n        return;\n    }\n    srand((unsigned int)time(0));\n    for (i = 0; i < sets; i++)\n    {\n        roll = 0;\n        for (j = 0; j < dice; j++)\n            roll += rand() % sides + 1;\n        printf(\"%d \", roll);\n    }\n    printf(\"\\n\");\n}\n\n\n\n        \n"
  },
  {
    "path": "Chapter_12/8.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\nint * make_array(int elem, int val);\nvoid show_array(const int ar [], int n);\nint main(void)\n{\n    int * pa;\n    int size;\n    int value;\n\n    printf(\"Enter the number of elements: \");\n    while (scanf(\"%d\", &size) == 1 && size > 0)\n    {\n        printf(\"Enter the initialization value: \");\n        scanf(\"%d\", &value);\n        pa = make_array(size, value);\n        if (pa)\n        {\n            show_array(pa, size);\n            free(pa);\n        }\n        printf(\"Enter the number of elements (<1 to quit): \");\n    }\n    printf(\"Done.\\n\");\n    return 0;\n}\n\nint * make_array(int elem, int val)\n{\n    int * pt;\n    int i;\n    \n    pt = (int *)malloc(elem * sizeof(int));\n    for (i = 0; i < elem; i++)\n        pt[i] = val;\n\n    return pt;\n}\n\nvoid show_array(const int ar [], int n)\n{\n    int i;\n    \n    for (i = 0; i < n; i++)\n    {\n        printf(\"%d \", ar[i]);\n        if (i % 8 == 7)\n            printf(\"\\n\");\n    }\n    printf(\"\\n\");\n}\n"
  },
  {
    "path": "Chapter_12/9.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#define SIZE 40\nchar * * mal_ar(int n);\nint main(void)\n{\n    int words, i;\n    char * * st;\n    \n    printf(\"How many words do you wish to enter? \");\n    scanf(\"%d\", &words);\n    getchar();\t\t\t\t\t//滤掉回车\n    printf(\"Enter %d words now:\\n\", words);\n    st = mal_ar(words);\n    printf(\"Here are your words:\\n\");\n    for (i = 0; i < words; i++)\n    {\n        puts(st[i]);\n        free(st[i]);\t//释放每个指针指向的内存\n    }\n    free(st);\t\t\t//释放指针数组\n\n    return 0;\n}\n\nchar * * mal_ar(int n)\n{\n    char * * pt;\n    int i, j;\n    char ch;\n\n    //给n个指针分配动态内存空间，返回指针的指针\n    pt = (char * *)malloc(n * sizeof(char *));\n    for (i = 0; i < n; i++)\n    {\n        //给每个指针指向的地址分配内存空间\n        pt[i] = (char *)malloc(SIZE * sizeof(char));\n        //可以仅用scanf(\"%s\", pt[i]);\n        while (isspace(ch = getchar()))\t\t\t\t//处理单词之前的空格符\n            continue;\n        pt[i][0] = ch;\t\t\t\t\t\t\t\t//单词首字符\n        j = 1;\n        while (!isspace(pt[i][j] = getchar()))\n            j++;\n        pt[i][j] = '\\0';\t\t\t\t\t\t\t//将末尾的空格替换为\\0'\n    }\n\n    return pt;\n}\n"
  },
  {
    "path": "Chapter_13/1.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#define SIZE 40\nchar * s_gets(char * st, int n);\nint main(void)\n{\n    char name[SIZE];\n    char ch;\n    unsigned long count = 0;\n    FILE * fp;\n    \n    printf(\"Enter the file name: \");\n    s_gets(name, SIZE);\n    if ((fp = fopen(name, \"r\")) == NULL)\n    {\n        printf(\"Can't open %s\\n\", name);\n        exit(EXIT_FAILURE);\n    }\n    while ((ch = getc(fp)) != EOF)\n    {\n        putc(ch, stdout);\n        count++;\n    }\n    fclose(fp);\n    printf(\"File %s has %lu characters\\n\", name, count);\n\n    return 0;\n}\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_13/10.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#define LEN 41\nint main(void)\n{\n    char name[LEN];\n    char words[LEN];\n    FILE * fp;\n    long offset;\t\t\t//偏移量\n    \n    printf(\"Please input the file name:\\n\");\n    gets(name);\n    if ((fp = fopen(name, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"can't open \\\"%s\\\" file.\\n\", name);\n        exit(EXIT_FAILURE);\n    }\n    printf(\"Please input a position in the file (negative number or \"\n        \"Non numeric character to quit):\\n\");\n    while (scanf(\"%ld\", &offset) == 1 && offset > 0)\n    {\n        getchar();\n        fseek(fp, offset, SEEK_SET);\n        fscanf(fp, \"%40s\", words);\n        fprintf(stdout, \"%s\\n\", words);\n        printf(\"Please input a position in the file (negative number or\\n\"\n        \"Non numeric character to quit):\\n\");\n    }\n    fclose(fp);\n\n    return 0;\n}\n\n\n    \n"
  },
  {
    "path": "Chapter_13/11.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define SIZE 256\nint main(int argc, char * argv[])\n{\n    FILE * fp;\n    char words[SIZE];\n\n    if (argc < 3)\n    {\n        fprintf(stderr, \"Wrong number of arguments.\\n\");\n        exit(EXIT_FAILURE);\n    }\n    if ((fp = fopen(argv[2], \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\", argv[2]);\n        exit(EXIT_FAILURE);\n    }\n    while (fgets(words, SIZE, fp) != NULL)\n    {\n        if (strstr(words, argv[1]) != NULL)\n            fputs(words, stdout);\n    }\n\n    if (fclose(fp) != 0)\n        fprintf(stderr, \"Error for closing file.\\n\");\n\n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_13/12.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#define LEN 40\n#define ROW 20\n#define COL 30\nint main(void)\n{\n    FILE * fs, * fd;\t\t\t\t//fs指向源文件,fd指向目标文件\n    char name[LEN];\n    char digit[ROW][COL];\n    char result[ROW][COL + 1];\n    char ch[] = {\" .':~*= %#\"};\t\t//数字字符对应的符号\n    int i, j;\n\n    printf(\"Please enter the source file name:\\n\");\n    gets(name);\n    //打开源文件\n    if ((fs = fopen(name, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\", name);\n        exit(EXIT_FAILURE);\n    }\n    //将文件内容读入 20 * 30 的int数组中\n    //并且转化成相应符号放入 20 * 31 的字符数组中\n    for (i = 0; i < ROW; i++)\n    {\n        for (j = 0; j < COL; j++)\n        {\n            fscanf(fs, \"%d\", &digit[i][j]);\n            result[i][j] = ch[digit[i][j]];\n        }\n        result[i][j] = '\\0';\n    }\n    printf(\"Enter the destination file name:\\n\");\n    gets(name);\n    //打开目标文件\n    if ((fd = fopen(name, \"w\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\", name);\n        exit(EXIT_FAILURE);\n    }\n    //将结果打印出来并写入目标文件\n    for (i = 0; i < ROW; i++)\n    {\n        fprintf(stdout, \"%s\\n\", result[i]);\n        fprintf(fd, \"%s\\n\", result[i]);\n    }\n    //关闭文件\n    if (fclose(fs) != 0 || fclose(fd) != 0)\n        fprintf(stderr, \"Error for closing file.\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_13/14.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <math.h>\n#define LEN 40\n#define ROW 20\n#define COL 30\nint main(void)\n{\n    FILE * fs, * fd;\t\t\t\t//fs指向源文件,fd指向目标文件\n    char name[LEN];\n    char dig[ROW][COL];\n    char res[ROW][COL + 1];\n    char ch[] = {\" .':~*= %#\"};\t\t//数字字符对应的符号\n    int i, j;\n\n    printf(\"Please enter the source file name:\\n\");\n    gets(name);\n    //打开源文件\n    if ((fs = fopen(name, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\", name);\n        exit(EXIT_FAILURE);\n    }\n    //将文件内容读入 20 * 30 的int数组中\n    //并且转化成相应符号放入 20 * 31 的字符数组中\n    for (i = 0; i < ROW; i++)\n    {\n        for (j = 0; j < COL; j++)\n        {\n            fscanf(fs, \"%d\", &dig[i][j]);\n        }\n    }\n    for (i = 0; i < ROW; i++)\n    {\n        for (j = 0; j < COL; j++)\n        {\n            if ((i == 0 && j == 0) || (i == 0 && j == 29)\n                ||(i == 19 && j == 0) || (i == 19 && j == 29))\n                ;\n            else if (i == 0) {\n                if ((abs(dig[i][j] - dig[i][j - 1]) > 1) && (abs(dig[i][j] - dig[i][j + 1]) > 1) &&\n                    (abs(dig[i][j] - dig[i + 1][j]) > 1))\n                    dig[i][j] = (dig[i][j - 1] + dig[i][j + 1] + dig[i + 1][j]) / 3.0 + 0.5;\n            }\n            else if (j == 0) {\n                if ((abs(dig[i][j] - dig[i - 1][j]) > 1) && (abs(dig[i][j] - dig[i][j + 1]) > 1) &&\n                    (abs(dig[i][j] - dig[i + 1][j]) > 1))\n                    dig[i][j] = (dig[i - 1][j] + dig[i][j + 1] + dig[i + 1][j]) / 3.0 + 0.5;\n            }\n\n            else if (i == 19) {\n                if ((abs(dig[i][j] - dig[i][j - 1]) > 1) && abs((dig[i][j] - dig[i][j + 1]) > 1) &&\n                    (abs(dig[i][j] - dig[i - 1][j]) > 1))\n                    dig[i][j] = (dig[i - 1][j] + dig[i][j - 1] + dig[i][j + 1]) / 3.0 + 0.5;\n            }\n\n            else if (j == 29) {\n                if ((abs(dig[i][j] - dig[i][j - 1]) > 1) && (abs(dig[i][j] - dig[i + 1][j]) > 1) &&\n                    (abs(dig[i][j] - dig[i - 1][j]) > 1))\n                    dig[i][j] = (dig[i - 1][j] + dig[i][j - 1] + dig[i + 1][j]) / 3.0 + 0.5;\n            }\n            \n            else {\n                if ((abs(dig[i][j] - dig[i][j - 1]) > 1) && (abs(dig[i][j] - dig[i][j + 1]) > 1) &&\n                    (abs(dig[i][j] - dig[i - 1][j]) > 1) && (abs(dig[i][j] - dig[i + 1][j]) > 1))\n                    dig[i][j] = (dig[i - 1][j] + dig[i + 1][j] + dig[i][j - 1] + dig[i][j + 1]) / 4.0 + 0.5;\n            }\n        }\n    }\n\n    for (i = 0; i < ROW; i++)\n    {\n        for (j = 0; j < COL; j++)\n        {\n            res[i][j] = ch[dig[i][j]];\n        }\n        res[i][j] = '\\0';\n    }\n    printf(\"Enter the destination file name:\\n\");\n    gets(name);\n    //打开目标文件\n    if ((fd = fopen(name, \"w\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\\n\", name);\n        exit(EXIT_FAILURE);\n    }\n    //将结果打印出来并写入目标文件\n    for (i = 0; i < ROW; i++)\n    {\n        fprintf(stdout, \"%s\\n\", res[i]);\n        fprintf(fd, \"%s\\n\", res[i]);\n    }\n    //关闭文件\n    if (fclose(fs) != 0 || fclose(fd) != 0)\n        fprintf(stderr, \"Error for closing file.\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_13/2.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#define SIZE 256\nint main(int argc, char * argv[])\n{\n    FILE * fs;\t\t//原始文件指针\n    FILE * fa;\t\t//目标文件指针\n    char temp[SIZE];\n    size_t bytes;\n\n    //检查命令行参数\n    if (argc < 3)\n    {\n        fprintf(stderr, \"Usage: %s filename\\n\", argv[0]);\n        exit(EXIT_FAILURE);\n    }\n\n    //以读模式打开原始文件\n    if ((fs = fopen(argv[1], \"rb\")) == NULL)\n    {\n        fprintf(stderr, \"I couldn't open the file \\\"%s\\\"\\n\", argv[1]);\n        exit(EXIT_FAILURE);\n    }\n    \n    //以读写模式打开目标文件\n    if ((fa = fopen(argv[2], \"wb\")) == NULL)\n    {\n        fprintf(stderr, \"I couldn't open the file \\\"%s\\\"\\n\", argv[2]);\n        exit(EXIT_FAILURE);\n    }\n\n    //拷贝数据\n    {\n        while ((bytes = fread(temp, sizeof(char), SIZE, fs)) > 0)\n            fwrite(temp, sizeof(char), bytes, fa);\n    }\n\n    fclose(fs);\n    fclose(fa);\n\n    return 0;\n}\n\n\n\n"
  },
  {
    "path": "Chapter_13/3.c",
    "content": "#include <stdio.h>\n#include <ctype.h>\n#include <string.h>\n#define SIZE 40\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    FILE * fs;\t\t\t//原始文件指针\n    FILE * ft;\t\t\t//目标文件指针\n    char name_s[SIZE];\n    char name_t[SIZE];\n    char ch;\n    \n    //输入原始文件和目标文件名\n    fprintf(stdout, \"Please enter the source file name:\\n\");\n    s_gets(name_s, SIZE);\n    fprintf(stdout, \"Please enter the target file name:\\n\");\n    s_gets(name_t, SIZE);\n    \n    //打开原始文件和目标文件\n    if ((fs = fopen(name_s, \"r\")) == NULL)\n        fprintf(stderr, \"Could not open the %s\\n\", name_s);\n    if ((ft = fopen(name_t, \"w\")) == NULL)\n        fprintf(stderr, \"Could not open the %s\\n\", name_t);\n    \n    //拷贝数据\n    while ((ch = getc(fs)) != EOF)\n        putc(toupper(ch), ft);\n    \n    fclose(fs);\n    fclose(ft);\n\n    return 0;\n}\n    \n\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\t\t\t//处理多余输入\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_13/4.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\nint main(int argc, char * argv[])\n{\n    char ch;\n    int i;\n    FILE * fp;\n\n    for (i = 1; i < argc; i++)\n    {\n        printf(\"File %d:\\n\", i);\n        if ((fp = fopen(argv[i], \"r\")) == NULL)\n        {\n            fprintf(stderr, \"Could not open %s.\\n\", argv[i]);\n            exit(EXIT_FAILURE);\n        }\n        while ((ch = getc(fp)) != EOF)\n            putchar(ch);\n        printf(\"\\n\");\n    }\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_13/5.c",
    "content": "//命令行参数argv[1]代表目标文件名，后面几个为源文件名\n//该程序把多个源文件加到源文件末尾\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define BUFSIZE 4096\n#define SLEN 81\nvoid append(FILE * source, FILE * dest);\nchar * s_gets(char * st, int n);\n\nint main(int argc, char * argv[])\n{\n    FILE * fs, * fa;\t\t//fs指向源文件,fa指向目标文件\n    int i;\n    int files = 0;\n    char ch;\n\n    if (argc < 3)\n    {\n        fprintf(stderr, \"Usage: %s filename\\n\", argv[0]);\n        exit(EXIT_FAILURE);\n    }\n    if ((fa = fopen(argv[1], \"a+\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open %s.\\n\", argv[1]);\n        exit(EXIT_FAILURE);\n    }\n    if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)\n    {\n        fputs(\"Can't creat output buffer\\n\", stderr);\n        exit(EXIT_FAILURE);\n    }\n    for (i = 2; i < argc; i++)\n    {\n        if (strcmp(argv[1], argv[i]) == 0)\n        {\n            fputs(\"Can't append file to itself\\n\", stderr);\n        }\n        else if ((fs = fopen(argv[i], \"r\")) == NULL)\n        {\n            fprintf(stderr, \"Could not open %s.\\n\", argv[i]);\n        }\n        else\n        {\n            if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)\n            {\n                fputs(\"Can't creat input buffer\\n\", stderr);\n                continue;\n            }\n            append(fs, fa);\n            if (ferror(fs) != 0)\n                fprintf(stderr, \"Error in reading file %s.\\n\", argv[i]);\n            if (ferror(fa) != 0)\n                fprintf(stderr, \"Error in writing file %s.\\n\", argv[1]);\n            fclose(fs);\n            printf(\"File %s appended.\\n\", argv[i]);\n        }\n    }\n    printf(\"%s contents:\\n\", argv[1]);\n    rewind(fa);\n    while ((ch = getc(fa)) != EOF)\n        putchar(ch);\n    puts(\"Done displaying.\");\n    fclose(fa);\n\n    return 0;\n}\n\n//附加文件的函数\nvoid append(FILE * source, FILE * dest)\n{\n    size_t bytes;\n    static char temp[BUFSIZE];\n\n    while ((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0)\n        fwrite(temp, sizeof(char), bytes, dest);\n}\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t \t\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\t\t\t\t\t//处理多余输入\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_13/6.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define SIZE 40\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    FILE * fs, * fa;\t\t//fs指向源文件,fa指向目标文件\n    char ch;\n    char name_source[SIZE];\n    char name_dest[SIZE];\n    int count = 0;\n\n    //输入源文件和目标文件名\n    fprintf(stdout, \"Please enter the source file name:\\n\");\n    s_gets(name_source, SIZE);\n    fprintf(stdout, \"Please enter the destination file name:\\n\");\n    s_gets(name_dest, SIZE);\n    \n    //打开源文件和目标文件\n    if ((fs = fopen(name_source, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_source);\n        exit(EXIT_FAILURE);\n    }\n    if ((fa = fopen(name_dest, \"w\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_dest);\n        exit(EXIT_FAILURE);\n    }\n\n    //将源文件每3个字符的第1个字符输出到目标文件\n    while ((ch = getc(fs)) != EOF)\n    {\n        if (count++ %3 == 0)\n            putc(ch, fa);\n    }\n\n    //收尾工作\n    if (fclose(fs) != 0 || fclose(fa) != 0)\n        fprintf(stderr, \"Error in closing files\\n\");\n\n    return 0;\n}\n\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t \t\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\t\t//处理多余输入\n    }\n\n    return ret_val;\n}\n\n"
  },
  {
    "path": "Chapter_13/7a.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define SIZE 40\n#define SLEN 81\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    FILE * f1, * f2;\n    char st[SLEN];\n    char name_f1[SIZE];\n    char name_f2[SIZE];\n    char * ret_val1;\n    char * ret_val2;\n    int count = 0;\n\n    //依次输入两个文件的文件名\n    fprintf(stdout, \"Please enter the first file name:\\n\");\n    s_gets(name_f1, SIZE);\n    fprintf(stdout, \"Please enter the second file name:\\n\");\n    s_gets(name_f2, SIZE);\n    \n    //依次输入两个文件\n    if ((f1 = fopen(name_f1, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_f1);\n        exit(EXIT_FAILURE);\n    }\n    if ((f2 = fopen(name_f2, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_f2);\n        exit(EXIT_FAILURE);\n    }\n    \n    do\n    {\n        if ((ret_val1 = fgets(st, SLEN, f1)) != NULL)\n        {\n            fputs(st, stdout);\n\n        }\n        if ((ret_val2 = fgets(st, SLEN, f2)) != NULL)\n        {\n            fputs(st, stdout);\n        }\n    }\n    while (ret_val1 != NULL || ret_val2 != NULL);\n\n    fclose(f1);\n    fclose(f2);\n\n    return 0;\n}\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t \t\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\t\t//处理多余输入\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_13/7b.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define SIZE 40\n#define SLEN 81\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    FILE * f1, * f2;\n    char name_f1[SIZE];\n    char name_f2[SIZE];\n    char ch1, ch2;\n\n    //依次输入两个文件的文件名\n    fprintf(stdout, \"Please enter the first file name:\\n\");\n    s_gets(name_f1, SIZE);\n    fprintf(stdout, \"Please enter the second file name:\\n\");\n    s_gets(name_f2, SIZE);\n    \n    //依次输入两个文件\n    if ((f1 = fopen(name_f1, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_f1);\n        exit(EXIT_FAILURE);\n    }\n    if ((f2 = fopen(name_f2, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Could not open the %s\\n\", name_f2);\n        exit(EXIT_FAILURE);\n    }\n    \n    do\n    {\n        while ((ch1 = getc(f1)) != EOF && ch1 != '\\n')\n        {\n            putchar(ch1);\n        }\n        while ((ch2 = getc(f2)) != EOF && ch2 != '\\n')\n        {\n            putchar(ch2);\n        }\n        putchar('\\n');\n    }\n    while (ch1 != EOF || ch2 != EOF);\n\n    fclose(f1);\n    fclose(f2);\n\n    return 0;\n}\n\n//自定义读取文件名的函数\nchar * s_gets(char * st, int n)\n{\n    char * find;\n    char * ret_val;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\t \t\t//查找换行符\n        if (find)\n            *find = '\\0';\t\t\t\t\t//替换为空字符\n        else\n            while (getchar() != '\\n')\n                continue;\t\t\t\t\t//处理多余输入\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_13/8.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\nint main(int argc, char * argv[])\n{\n    int i;\n    char ch;\n    FILE * fp;\n    int count = 0;\n    \n    if (argc < 2)\n    {\n        fprintf(stderr, \"the wrong number of arguments.\");\n        exit(EXIT_FAILURE);\n    }\n\n    if (argc == 2)\n    {\n        printf(\"Input some texts:\\n\");\n        while ((ch = getchar()) != EOF)\n        {\n            if (ch == argv[1][0])\n                count++;\n        }\n        printf(\"the \\\"%s\\\" in your input: %d times.\",\n            argv[1], count);\n    }\n    \n    else\n    {\n        for (i = 2; i < argc; i++)\n        {\n            count = 0;\n            if ((fp = fopen(argv[i], \"r\")) == NULL)\n            {\n                fprintf(stderr, \"Could not open the %s.\", argv[i]);\n                continue;\n            }\n            while ((ch = getc(fp)) != EOF)\n            {\n                if (ch == argv[1][0])\n                    count++;\n            }\n            fprintf(stdout, \"the \\\"%s\\\" in %s: %d times.\\n\",\n                argv[1], argv[i], count);\n        }\n    }\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_13/9.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define MAX 41\nint main(void)\n{\n    FILE * fp;\n    char words[MAX];\n    char name[MAX];\n    int count = 0;\n\n    printf(\"Please input the file name:\\n\");\n    gets(name);\n    if ((fp = fopen(name, \"a+\")) == NULL)\n    {\n        fprintf(stderr, \"can't open \\\"%s\\\" file.\\n\", name);\n        exit(EXIT_FAILURE);\n    }\n\n    puts(\"Enter words to add to the file; press the #\");\n    puts(\"key at the beginning of a line to terminate.\");\n    while ((fscanf(stdin, \"%40s\", words) == 1) && (words[0] != '#'))\n        fprintf(fp, \"%s\\n\", words);\n    puts(\"File contents:\");\n    rewind(fp);\n    while (fscanf(fp, \"%s\", words) != EOF)\n    {\n        count++;\n        fprintf(stdout, \"%d. %s\\n\", count, words);\n    }\n    puts(\"Done!\");\n    if (fclose(fp) != 0)\n        fprintf(stderr, \"Error closing file\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "Chapter_14/1.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#define NAMELEN 10\n#define ABBLEN 4\n#define MONTHS 12\nchar * s_gets(char * st, int n);\nint get_days(char * st);\n\nstruct month {\n    char name[NAMELEN];\n    char abbrev[ABBLEN];\n    int days;\n    int monumb;\n};\n\nstruct month months[12] = {\n    {\"january\", \"jan\", 31, 1},\n    {\"february\", \"feb\", 28, 2},\n    {\"march\", \"mar\", 31, 3},\n    {\"april\", \"apr\", 30, 4},\n    {\"may\", \"may\", 31, 5},\n    {\"june\", \"jun\", 30, 6},\n    {\"july\", \"jul\", 31, 7},\n    {\"august\", \"aug\", 31, 8},\n    {\"september\", \"sep\", 30, 9},\n    {\"october\", \"oct\", 31, 10},\n    {\"november\", \"nov\", 30, 11},\n    {\"december\", \"dec\", 31, 12}\n};\n\nint main(void)\n{\n    char st[NAMELEN];\n\n    printf(\"Please input the month name (empty line to quit):\\n\");\n    while (s_gets(st, NAMELEN) != NULL && st[0] != '\\0')\n    {\n        if (get_days(st) == 0)\n            printf(\"The month name is wrong.\\n\");\n        else\n        printf(\"The days untill %s: %d\\n\", st, get_days(st));\n        printf(\"Next input (empty line to quit):\\n\");\n    }\n\n    printf(\"Bye.\\n\");\n    return 0;\n}\n\n\nint get_days(char * st)\n{\n    int total = 0;\n    int i = 0;\n    int count;\n    \n    while (st[i])\n    {\n        st[i] = tolower(st[i]);\n        i++;\n    }\n\n    for (i = 0; i < MONTHS; i++)\n    {\n        if (strcmp(st, months[i].name) == 0)\n            break;\n    }\n    if (i < MONTHS)\n    {\n        for (count = 0; count <= i; count++)\n            total += months[count].days;\n    }\n\n    return total;\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_14/10.c",
    "content": "#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#define LEN 81\n#define SIZE 4\n\nchar * s_gets(char * st, int n);\nchar get_choice(void);\nvoid ToUpper(char * st);\nvoid ToLower(char * st);\nvoid Transpos(char * st);\nvoid Dummy(char * st);\n\nint main(void)\n{\n    char st[LEN];\n    char copy[LEN];\n    char ch;\n    //函数指针数组\n    void(*fp[SIZE])(char *) = {ToUpper, ToLower, Transpos, Dummy};\n    int index;\n\n    puts(\"Enter a string (empty line to quit):\");\n    while (s_gets(st, LEN) != NULL && st[0] != '\\0')\n    {\n        while ((ch = get_choice()) != 'q')\n        {\n            switch (ch)\n            {\n            case 'a': index = 0; break;\n            case 'b': index = 1; break;\n            case 'c': index = 2; break;\n            case 'd': index = 3; break;\n            default : printf(\"Program error.\\n\");  break;\n            }\n            strcpy(copy, st);\n            (*(fp[index]))(copy);\n            puts(copy);\n        }\n        printf(\"Enter a string (empty line to quit):\\n\");\n    }\n    puts(\"Bye.\");\n\n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n\n    printf(\"To choose a function, enter its letter labal:\\n\");\n    printf(\"a). upper             b). lower\\n\");\n    printf(\"c). transposed        d). original\\n\");\n    printf(\"q). quit.\\n\");\n    while ((ch = getchar()) < 'a' || ch > 'd' && ch != 'q')\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter (a, b, c, d or q).\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n\n    return ch;\n}\n\nvoid ToUpper(char * st)\n{\n    while (*st)\n    {\n        *st = toupper(*st);\n        st++;\n    }\n}\n\nvoid ToLower(char * st)\n{\n    while (*st)\n    {\n        *st = tolower(*st);\n        st++;\n    }\n}\n\nvoid Transpos(char * st)\n{\n    while (*st)\n    {\n        if (islower(*st))\n            *st = toupper(*st);\n        else if (isupper(*st))\n            *st = tolower(*st);\n        st++;\n    }\n}\n\nvoid Dummy(char * st)\n{\n\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_14/11.c",
    "content": "#include <stdio.h>\n#include <math.h>\n#define PSIZE 4\n#define SIZE 100\n\nvoid transform(double [], double [], int, double(*)(double));\ndouble oppsite_val(double);\ndouble double_val(double);\n\nint main(void)\n{\n    int i, j;\n    double source[SIZE], target[SIZE];\n    double(*p[PSIZE])(double) = {sin, sqrt, oppsite_val, double_val};\n    \n    printf(\"The source array:\\n\");\n    for (i = 0; i <SIZE; i++)\n    {\n        source[i] = i;\n        printf(\"%7g \", source[i]);\n        if (i % 10 == 9)\n            printf(\"\\n\");\n    }\n    printf(\"\\n\");\n    for (i = 0; i < PSIZE; i++)\n    {\n        printf(\"The source array %d:\\n\", i + 1);\n        transform(source, target, SIZE, p[i]);\n        for (j = 0; j < SIZE; j++)\n        {\n            printf(\"%7.2f \", target[j]);\n            if (j % 10 == 9)\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n    }\n\n    return 0;\n}\n\nvoid transform(double source[], double target[], int size,\n    double(*pf)(double num))\n{\n    int i;\n\n    for (i = 0; i <size; i++)\n    {\n        target[i] = (*pf)(source[i]);\n    }\n}\n\n\ndouble oppsite_val(double num)\n{\n    return (-1 * num);\n}\n\ndouble double_val(double num)\n{\n    return (2 * num);\n}\n"
  },
  {
    "path": "Chapter_14/2.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#define NAMELEN 10\n#define ABBLEN 4\n#define MONTHS 12\n    \nstruct month {\n    char name[NAMELEN];\n    char abbrev[ABBLEN];\n    int days;\n    int monumb;\n};\n\nstruct month months[MONTHS] = {\n    {\"january\", \"jan\", 31, 1},\n    {\"february\", \"feb\", 28, 2},\n    {\"march\", \"mar\", 31, 3},\n    {\"april\", \"apr\", 30, 4},\n    {\"may\", \"may\", 31, 5},\n    {\"june\", \"jun\", 30, 6},\n    {\"july\", \"jul\", 31, 7},\n    {\"august\", \"aug\", 31, 8},\n    {\"september\", \"sep\", 30, 9},\n    {\"october\", \"oct\", 31, 10},\n    {\"november\", \"nov\", 30, 11},\n    {\"december\", \"dec\", 31, 12}\n};\n\nint get_res(void);\nvoid get_years(void);\nint get_months(void);\nint get_days(int);\n\nint main(void)\n{\n    int total_days;\n\n    total_days = get_res();\n    printf(\"The number of days before the specified day \");\n    printf(\"is %d\\n\", total_days);\n    return 0;\n}\n\n//得到结果的函数\nint get_res(void)\n{\n    int n_months;\n    int n_days;\n    int i;\n    int total = 0;\n    \n    get_years();\n    n_months = get_months();\n    n_days = get_days(n_months);\n\n    for (i = 0; i < n_months - 1; i++)\n    {\n        total += months[i].days;\n    }\n    total += n_days;\n\n    return total;\n}\n\n//输入并处理年数\nvoid get_years(void)\n{\n    int n_years;\n\n    printf(\"Please input a particular year: \");\n    while (scanf(\"%d\", &n_years) != 1)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input a integer: \");\n    }\n    if (n_years % 4 == 0)\n        months[1].days = 29;\n    else\n        months[1].days = 28;\n}\n\n//输入并处理月份\nint get_months(void)\n{\n    int n_months;\n\n    printf(\"Input the month: \");\n    while (scanf(\"%d\", &n_months) != 1 || (n_months < 0 || n_months > 12))\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input a integer from 1 to 12: \");\n    }\n\n    return n_months;\n}\n\n//输入并处理天数\nint get_days(int n)\n{\n    int n_days;\n\n    printf(\"Input the days: \");\n    while (scanf(\"%d\", &n_days) != 1 ||\n        (n_days < 1 || n_days > months[n - 1].days))\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input a integer from 1 to %d: \",\n            months[n - 1].days);\n    }\n\n    return n_days;\n}\n\n"
  },
  {
    "path": "Chapter_14/3.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#define MAXTITL 40\n#define MAXAUTL 40\n#define MAXBKS 100\n\nstruct book {\n    char title[MAXTITL];\n    char author[MAXAUTL];\n    float value;\n};\n\nvoid sort_by_input(const struct book library[], int n);\nvoid sort_by_title(struct book library[], int n);\nvoid sort_by_price(struct book library[], int n);\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    static struct book library[MAXBKS];\n    int count = 0;\n\n    printf(\"Please enter the book title (empty line to stop):\\n\");\n    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL\n        && library[count].title[0] != '\\0')\n    {\n        printf(\"Now enter the author.\\n\");\n        s_gets(library[count].author, MAXTITL);\n        printf(\"Now enter the value.\\n\");\n        scanf(\"%f\", &library[count++].value);\n        while (getchar() != '\\n')\n            continue;\n        if (count < MAXBKS)\n            printf(\"Enter the next title.\\n\");\n    }\n\n    if (count > 0)\n    {\n        printf(\"The list of books in order of your input:\\n\");\n        sort_by_input(library, count);\n        printf(\"The list of books in order of title:\\n\");\n        sort_by_title(library, count);\n        printf(\"The list of books in order of price:\\n\");\n        sort_by_price(library, count);\n    }\n    \n    return 0;\n}\n\nvoid sort_by_input(const struct book library[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        printf(\"%s by %s: $%.2f\\n\", library[i].title,\n            library[i].author, library[i].value);\n    printf(\"\\n\");\n}\n\nvoid sort_by_title(struct book library[], int n)\n{\n    int i, j;\n    struct book temp;\n    \n    for (i = 0; i < n - 1; i++)\n    {\n        for (j = i + 1; j < n; j++)\n            if (strcmp(library[i].title, library[j].title) > 0)\n            {\n                temp = library[i];\n                library[i] = library[j];\n                library[j] = temp;\n            }\n    }\n    for (i = 0; i < n; i++)\n        printf(\"%s by %s: $%.2f\\n\", library[i].title,\n            library[i].author, library[i].value);\n    printf(\"\\n\");\n}\n\nvoid sort_by_price(struct book library[], int n)\n{\n    int i, j;\n    struct book temp;\n    \n    for (i = 0; i < n - 1; i++)\n    {\n        for (j = i + 1; j < n; j++)\n            if (library[i].value > library[j].value)\n            {\n                temp = library[i];\n                library[i] = library[j];\n                library[j] = temp;\n            }\n    }\n    for (i = 0; i < n; i++)\n        printf(\"%s by %s: $%.2f\\n\", library[i].title,\n            library[i].author, library[i].value);\n    printf(\"\\n\");\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_14/4a.c",
    "content": "#include <stdio.h>\n#define SSN 20\n#define LEN 10\n\nstruct names {\n    char last_name[LEN];\n    char mid_name[LEN];\n    char fir_name[LEN];\n};\n\nstruct person {\n    char number[SSN];\n    struct names name;\n};\n\nvoid print_info(struct person info[], int n);\n\nint main(void)\n{\n    struct person info[5] = {\n        {\"B51414029\", {\"Jie\", \"Yu\", \"Zha\"}},\n        {\"B51414010\", {\"Xiang\", \"Xiang\", \"Pan\"}},\n        {\"B51414025\", {\"Jie\", \"Wen\", \"Jiang\"}},\n        {\"B51414031\", {\"Ming\", \"Cheng\", \"Liu\"}},\n        {\"B51414044\", {\"Qiang\", \" \", \"Sha\"}}\n    };\n\n    print_info(info, 5);\n\n    return 0;\n}\n\nvoid print_info(struct person info[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n    {\n        printf(\"%s, %s %c%c -- %s\", info[i].name.last_name,\n        info[i].name.fir_name, info[i].name.mid_name[0],\n        (info[i].name.mid_name[0] == ' ') ? ' ': '.', info[i].number);\n        printf(\"\\n\");\n    }\n}\n"
  },
  {
    "path": "Chapter_14/4b.c",
    "content": "#include <stdio.h>\n#define SSN 20\n#define LEN 10\n\nstruct names {\n    char last_name[LEN];\n    char mid_name[LEN];\n    char fir_name[LEN];\n};\n\nstruct person {\n    char number[SSN];\n    struct names name;\n};\n\nvoid print_info(struct person info);\n\nint main(void)\n{\n    int index;\n\n    struct person info[5] = {\n        {\"B51414029\", {\"Jie\", \"Yu\", \"Zha\"}},\n        {\"B51414010\", {\"Xiang\", \"Xiang\", \"Pan\"}},\n        {\"B51414025\", {\"Jie\", \"Wen\", \"Jiang\"}},\n        {\"B51414031\", {\"Ming\", \"Cheng\", \"Liu\"}},\n        {\"B51414044\", {\"Qiang\", \" \", \"Sha\"}}\n    };\n\n    for (index = 0; index < 5; index++)\n        print_info(info[index]);\n\n    return 0;\n}\n\nvoid print_info(struct person info)\n{\n    printf(\"%s, %s %c%c -- %s\", info.name.last_name,\n    info.name.fir_name, info.name.mid_name[0],\n    (info.name.mid_name[0] == ' ') ? ' ': '.', info.number);\n    printf(\"\\n\");\n    \n}\n\n"
  },
  {
    "path": "Chapter_14/5.c",
    "content": "#include <stdio.h>\n#define LEN 10\n#define NUM 3\n#define CSIZE 4\n#define N_SCORES 3\n\nstruct name {\n    char first_name[LEN];\n    char last_name[LEN];\n};\n\nstruct student {\n    struct name names;\n    float grade[NUM];\n    float average;\n};\n\nvoid get_score(struct student info[], int n);\nvoid get_aver(struct student info[], int n);\nvoid print_info(struct student info[], int n);\nvoid get_all_aver(struct student info[], int n);\n\nint main(void)\n{\n    struct student score[CSIZE] = {\n        {{\"Zha\", \"Yujie\"}, {0, 0, 0}, 0},\n        {{\"Jiang\", \"Wenjie\"}, {0, 0, 0}, 0},\n        {{\"Liu\", \"Chengming\"}, {0, 0, 0}, 0},\n        {{\"Pan\", \"Xiangxiang\"}, {0, 0, 0}, 0}\n    };\n    get_score(score, CSIZE);\n    get_aver(score, CSIZE);\n    print_info(score, CSIZE);\n    get_all_aver(score, CSIZE);\n    \n    return 0;\n}\n\nvoid get_score(struct student info[], int n)\n{\n    int i, j;\n\n    for (i = 0; i < n; i++)\n    {\n        printf(\"Input the scores of 3 courses of %s %s\\n\",\n            info[i].names.first_name, info[i].names.last_name);\n        for (j = 0; j < N_SCORES; j++)\n        {\n            scanf(\"%f\", &info[i].grade[j]);\n        }\n    }\n}\n        \nvoid get_aver(struct student info[], int n)\n{\n\n    int i, j;\n    float sum;\n\n    for (i = 0; i < n; i++)\n    {\n        for (j = 0, sum = 0.0; j < N_SCORES; j++)\n        {\n            sum += info[i].grade[j];\n        }\n        info[i].average = sum / N_SCORES;\n    }\n}\n\nvoid print_info(struct student info[], int n)\n{\n    int i, j;\n\n    printf(\"    Name%15s %s %s  average\\n\", \"score1\",\n        \"score2\", \"score3\");\n    for (i = 0; i < n; i++)\n    {\n        printf(\"%5s%-10s  \", info[i].names.first_name,\n            info[i].names.last_name);\n        for (j = 0; j < N_SCORES; j++)\n            printf(\"%6g\", info[i].grade[j]);\n        printf(\"%11.2f\\n\", info[i].average);\n    }\n}\n\nvoid get_all_aver(struct student info[], int n)\n{\n    int i;\n    float total = 0;\n\n    for (i = 0; i < n; i++)\n    {\n        total += info[i].average;\n    }\n        printf(\"The average score of all students is %.2f.\\n\",\n            total / CSIZE);\n}\n"
  },
  {
    "path": "Chapter_14/6.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#define LEN 10\n#define NLEN 20\n#define SIZE 20\n\nstruct member {\n    int num;\n    char fname[LEN];\n    char lname[LEN];\n    int play;\n    int hit;\n    int walk;\n    int rbi;\n};\n\nvoid get_initial_val(struct member *, int n);\nvoid get_data(struct member *, int n);\nvoid print_info(struct member *, int n);\n\nint main(void)\n{\n    static struct member members[SIZE];\n\n    get_initial_val(members, SIZE);\n    get_data(members, SIZE);\n    print_info(members, SIZE);\n\n    return 0;\n}\n    \nvoid get_initial_val(struct member * pst, int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++, pst++)\n    {\n        pst->num = 0;\n        strcpy(pst->fname, \"\");\n        strcpy(pst->lname, \"\");\n        pst->play = 0;\n        pst->hit = 0;\n        pst->walk = 0;\n        pst->rbi = 0;\n    }\n}\n\nvoid get_data(struct member * pst, int n)\n{\n    int number, play_times, hit_times, walk_times, rbi_num;\n    FILE * fp;\n    char name[NLEN];\n    char f_name[LEN], l_name[LEN];\n\n    printf(\"Please enter the file name: \");\n    scanf(\"%s\", name);\n    if ((fp = fopen(name, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Can't open %s\\n\", name);\n        exit(EXIT_FAILURE);\n    }\n    while (fscanf(fp, \"%d%s%s%d%d%d%d\", &number, f_name, l_name,\n        &play_times, &hit_times, &walk_times, &rbi_num) == 7)\n    {\n        pst[number].num = number;\n        strcpy(pst[number].fname, f_name);\n        strcpy(pst[number].lname, l_name);\n        pst[number].play += play_times;\n        pst[number].hit += hit_times;\n        pst[number].walk += walk_times;\n        pst[number].rbi += rbi_num;\n    }\n    fclose(fp);\n}\n\nvoid print_info(struct member * pst, int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n    {\n        if (pst[i].fname[0] != '\\0')\n        {\n            printf(\"%d %s %s %d %d %d %d\", pst[i].num, pst[i].fname, pst[i].lname,\n                    pst[i].play, pst[i].hit, pst[i].walk, pst[i].rbi);\n            printf(\"\\n\");\n        }\n        else\n            break;\n    }\n}\n\n\n    \n"
  },
  {
    "path": "Chapter_14/7.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define LEN 20\n#define MAXTITL 40\n#define MAXAUTH 40\n#define MAXBKS 10\n\nint count = 0;\t\t\t\t\t\t\t//外部变量，书的数目\n\nstruct book {\n    char title[MAXTITL];\n    char author[MAXAUTH];\n    float value;\n};\n\nchar * s_gets(char * st, int n);\t\t//输入字符串的函数\nchar get_choice(void);\t\t\t\t\t//获取用户选择\nvoid add(struct book *);\t\t\t\t//添加若干本书\nvoid change(struct book *);\t\t\t\t//替换一本书\nvoid del(struct book *);\t\t\t\t//删除一本书\nvoid print(struct book *);\t\t\t\t//打印书单\nvoid write_file(struct book *, char *);\t//将数据写入文件\n\nint main(void)\n{\n    char name[LEN];\n    struct book library[MAXBKS];\n    char ch;\n    FILE * fp;\n\n    //打开文件并读取其中内容到结构体\n    printf(\"Please input the file name: \");\n    s_gets(name, LEN);\n    if ((fp = fopen(name, \"a+b\")) == NULL)\n    {\n        fprintf(stderr, \"Can't open %s\", name);\n        exit(EXIT_FAILURE);\n    }\n    rewind(fp);\n    while (count < MAXBKS && fread(&library[count],\n        sizeof(struct book), 1, fp) == 1)\n    {\n        count++;\t\t\t\t\t\t//当前文件数\n    }\n    fclose(fp);\n    \n    //打印文件中已有数据\n    printf(\"There are %d books:\\n\", count);\n    print(library);\n\n    //让用户输入选项\n    while ((ch = get_choice()) != 'q')\n    {\n        switch (ch)\n        {\n            case 'a': add(library);\n                      break;\n            case 'c': change(library);\n                      break;\n            case 'd': del(library);\n                      break;\n            case 'p': print(library);\n                      break;\n            default: printf(\"Program error.\\n\");\n                    break;\n        }\n    }\n    //将数据输出至文件中\n    write_file(library, name);\n    \n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"Please input your choice:\\n\");\n    printf(\"a. add some books        c. change a book\\n\");\n    printf(\"d. delete a book         p. print all books\\n\");\n    printf(\"q. quit.\\n\");\n    while ((ch = getchar()) != 'a' && ch != 'c' && ch != 'd' &&\n        ch != 'p' && ch != 'q')\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right character.\\n\");\n    }\n    while (getchar() != '\\n')\n            continue;\n\n    return ch;\n}\n\nvoid add(struct book * ps)\n{\n    if (count == MAXBKS)\n        printf(\"Sorry, the list is full.\\n\");\n    else\n    {\n        printf(\"Please input the title (empty line to quit):\\n\");\n        while (count < MAXBKS && s_gets(ps[count].title, MAXTITL) != NULL\n            && ps[count].title[0] != '\\0')\n        {\n            printf(\"Input the author:\\n\");\n            s_gets(ps[count].author, MAXAUTH);\n            printf(\"Input the value:\\n\");\n            scanf(\"%f\", &ps[count].value);\n            getchar();\n            printf(\"Now, next title (empty line to quit):\\n\");\n            count++;\n        }\n    }\n}\n\nvoid change(struct book * ps)\n{\n    int i;\n    char name[MAXTITL];\n\n    if (count == 0)\n        printf(\"Sorry, the list is empty.\\n\");\n    else\n    {\n        printf(\"Please input the title of book you want to change:\\n\");\n        s_gets(name, MAXTITL);\n        for (i = 0; i < count; i++)\n        {\n            if (!strcmp(name, ps[i].title))\n            {\n                printf(\"Please input the title of new book:\\n\");\n                s_gets(ps[i].title, MAXTITL);\n                printf(\"Input the author:\\n\");\n                s_gets(ps[i].author, MAXAUTH);\n                printf(\"Input the value:\\n\");\n                scanf(\"%f\", &ps[i].value);\n                getchar();\n                break;\n            }\n        }\n        if (i == count)\n            printf(\"Sorry, we can't find the book you input.\\n\");\n    }\n    \n}\n\nvoid del(struct book * ps)\n{\n    char name[MAXTITL];\n    int i, j;\n\n    if (count == 0)\n        printf(\"Sorry, the list is empty.\\n\");\n    printf(\"Please input the title of book you want to delete:\\n\");\n    s_gets(name, MAXTITL);\n    for (i = 0; i < count; i++)\n    {\n        if (!strcmp(name, ps[i].title))\n        {\n            for (j = i + 1; j < count; j++)\n                ps[j - 1] = ps[j];\n            count--;\n            strcpy(ps[count].title, \"\");\n            strcpy(ps[count].author, \"\");\n            ps[count].value = 0;\n            break;\n        }\n    }\n    if (i == count)\n        printf(\"Sorry, we can't find the book you input.\\n\");\n}\n\nvoid print(struct book * ps)\n{\n    int i;\n\n    for (i = 0; i < count; i++)\n    {\n        printf(\"%s by %s: $%g\\n\", ps[i].title, ps[i].author,\n            ps[i].value);\n    }\n}\n\nvoid write_file(struct book * ps, char * st)\n{\n    FILE * fp;\n\n    fp = fopen(st, \"w+b\");\n    fwrite(ps, sizeof(struct book), count, fp);\n    fclose(fp);\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_14/8.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define LEN 20\n#define LEN 20\n#define MAX 12\n\nstruct seat {\n    int num;\n    int isassign;\n    char fname[LEN];\n    char lname[LEN];\n};\n\nvoid read_file(struct seat * ps);\t\t//从文件读取\nvoid initialize(struct seat * ps);\t\t//初始化结构\nchar get_choice(void);\t\t\t\t\t//用户输入选择\nvoid show_number(struct seat * ps);\t\t//空位置的个数\nvoid show_empty(struct seat * ps);\t\t//打印所有空位置\nvoid show_list(struct seat * ps);\t\t//打印所有位置信息\nvoid assign_seat(struct seat * ps);\t\t//预订一个位置\nvoid delete_seat(struct seat * ps);\t\t//删除一个位置\nvoid write_file(struct seat * ps);\t\t//数据写入文件\nchar * s_gets(char * st, int n);\t\t//输入字符串\n\nint main(void)\n{\n    char ch;\n    struct seat customer[MAX];\n    \n    initialize(customer);\n    read_file(customer);\n    \n    //用户输入选项\n    while ((ch = get_choice()) != 'f')\n    {\n        switch (ch)\n        {\n            case 'a': show_number(customer);\n                      break;\n            case 'b': show_empty(customer);\n                      break;\n            case 'c': show_list(customer);\n                      break;\n            case 'd': assign_seat(customer);\n                      break;\n            case 'e': delete_seat(customer);\n                      break;\n            default: printf(\"Program error.\\n\");\n                    break;\n        }\n    }\n    //将数据输出至文件中\n    write_file(customer);\n\n}\n    \nvoid initialize(struct seat * ps)\n{\n    int i;\n\n    for (i = 0; i < MAX; i++)\n    {\n        ps[i].num = i + 1;\n        ps[i].isassign = 0;\n        strcpy(ps[i].fname, \"\");\n        strcpy(ps[i].lname, \"\");\n    }\n}\n\nvoid read_file(struct seat * ps)\n{\n    FILE * fp;\n    \n    \n    if((fp = fopen(\"seat.txt\", \"a+b\")) == NULL)\n    {\n        fprintf(stderr, \"Can't open seat.txt.\\n\");\n        exit(EXIT_FAILURE);\n    }\n    fread(ps, sizeof(struct seat), MAX, fp);\n}\n\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"To choose a function, enter its letter label:\\n\");\n    printf(\"a) Show number of empty seats\\n\");\n    printf(\"b) Show list of empty seats\\n\");\n    printf(\"c) Show alphabetical list of seats\\n\");\n    printf(\"d) Assign a customer to a seat assignment\\n\");\n    printf(\"e) Delete a seat assignment\\n\");\n    printf(\"f) Quit\\n\");\n\n    while ((ch = getchar()) < 'a' || ch > 'f')\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n            continue;\n\n    return ch;\n}\n\nvoid show_number(struct seat * ps)\n{\n    int i;\n    int count = 0;\n\n    for(i = 0; i < MAX; i++)\n    {\n        if (ps[i].isassign == 0)\n            count++;\n    }\n    printf(\"The number of empty seats is %d\\n\", count);\n}\n\nvoid show_empty(struct seat * ps)\n{\n    int i;\n\n    printf(\"The list of empty seats:\\n\");\n    for (i = 0; i <\tMAX; i++)\n    {\n        if (!ps[i].isassign)\n            printf(\"%d \", ps[i].num);\n    }\n    printf(\"\\n\");\n}\n\nvoid show_list(struct seat * ps)\n{\n    int i;\n\n    printf(\"The list of seats:\\n\");\n    for (i = 0; i <\tMAX; i++)\n    {\n        printf(\"%d %s %s\\n\", ps[i].num, ps[i].fname, ps[i].lname);\n    }\n}\n\nvoid assign_seat(struct seat * ps)\n{\n    int number;\n    int ret_val;\n\n    printf(\"Please input the number of seat you want to book (1 to 12):\\n\");\n    while ((ret_val = scanf(\"%d\", &number)) != 1|| number < 1 || number > 12 ||\n        ps[number - 1].isassign == 1)\n    {\n        if (ret_val != 1 || number < 1 || number > 12)\n            printf(\"Sorry, please input a integer from 1 to 12.\\n\");\n        else\n            printf(\"Sorry, the seat is assigned, please reselect:\\n\");\n        while (getchar() != '\\n')\n            continue;\n    }\n    while (getchar() != '\\n')\n        continue;\n    printf(\"Now, input your firstname:\\n\");\n    s_gets(ps[number - 1].fname, LEN);\n    printf(\"Input your lastname:\\n\");\n    s_gets(ps[number - 1].lname, LEN);\n    ps[number - 1].isassign = 1;\n    printf(\"OK.\\n\");\n}\n\n\nvoid delete_seat(struct seat * ps)\n{\n    int number;\n\n    printf(\"Please input the number of seat you want to delete (1 to 12):\\n\");\n    while (scanf(\"%d\", &number) != 1 || number < 1 || number > 12 ||\n        ps[number - 1].isassign == 0)\n    {\n        if (ps[number - 1].isassign == 1)\n            printf(\"Sorry, the seat is empty, please reselect:\\n\");\n        else\n            printf(\"Sorry, please input a integer from 1 to 12.\\n\");\n        while (getchar() != '\\n')\n            continue;\n    }\n    getchar();\n    strcpy(ps[number - 1].fname, \"\");\n    strcpy(ps[number - 1].lname, \"\");\n    ps[number - 1].isassign = 0;\n    printf(\"OK.\\n\");\n}\n\nvoid write_file(struct seat * ps)\n{\n    FILE * fp;\n\n    fp = fopen(\"seat.txt\", \"w+b\");\n    fwrite(ps, sizeof(struct seat), MAX, fp);\n    fclose(fp);\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n        \n"
  },
  {
    "path": "Chapter_14/9.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define LEN 20\n#define LEN 20\n#define MAX 12\n#define MAXFL 5\n\nstruct seat {\n    int num;\n    int isassign;\n    char fname[LEN];\n    char lname[LEN];\n};\n\nvoid read_file(struct seat * ps);\t\t//从文件读取\nvoid initialize(struct seat * ps);\t\t//初始化结构\nchar get_flight(void);\t\t\t\t//用户选择航班\nchar get_choice(void);\t\t\t\t\t//用户输入选择\nvoid show_number(struct seat * ps);\t\t//空位置的个数\nvoid show_empty(struct seat * ps);\t\t//打印所有空位置\nvoid show_list(struct seat * ps);\t\t//打印所有位置信息\nvoid assign_seat(struct seat * ps);\t\t//预订一个位置\nvoid delete_seat(struct seat * ps);\t\t//删除一个位置\nvoid write_file(struct seat * ps);\t\t//数据写入文件\nchar * s_gets(char * st, int n);\t\t//输入字符串\nvoid is_confirmed(void);\t\t\t\t//确认座位分配\n\n//全局变量，值为1时表示被确认，值为0时未确认\nint isconf;\nchar flight_num[MAXFL];\n\nint main(void)\n{\n    char ch, flight;\n    struct seat customer[MAX];\n    \n    initialize(customer);\n    read_file(customer);\n    \n    //用户输入选项\n    while ((flight = get_flight()) != 'q')\n    {\n        isconf = 0;\n        while ((ch = get_choice()) != 'g')\n        {\n            switch (ch)\n            {\n                case 'a': show_number(customer);\n                          break;\n                case 'b': show_empty(customer);\n                          break;\n                case 'c': show_list(customer);\n                          break;\n                case 'd': assign_seat(customer);\n                          break;\n                case 'e': delete_seat(customer);\n                          break;\n                case 'f': is_confirmed();\n                          break;\n                default: printf(\"Program error.\\n\");\n                        break;\n            }\n        }\n    //将数据输出至文件中\n    if (isconf)\n        write_file(customer);\n    }\n    \n    \n    return 0;\n}\n    \nvoid initialize(struct seat * ps)\n{\n    int i;\n\n    for (i = 0; i < MAX; i++)\n    {\n        ps[i].num = i + 1;\n        ps[i].isassign = 0;\n        strcpy(ps[i].fname, \"\");\n        strcpy(ps[i].lname, \"\");\n    }\n}\n\nvoid read_file(struct seat * ps)\n{\n    FILE * fp;\n    \n    \n    if((fp = fopen(\"seat.txt\", \"a+b\")) == NULL)\n    {\n        fprintf(stderr, \"Can't open seat.txt.\\n\");\n        exit(EXIT_FAILURE);\n    }\n    fread(ps, sizeof(struct seat), MAX, fp);\n}\n\nchar get_flight(void)\n{\n    char ch;\n\n    printf(\"To choose a flight, enter its letter labal:\\n\");\n    printf(\"a). 102             b). 311\\n\");\n    printf(\"c). 444             d). 519\\n\");\n    printf(\"q). quit.\\n\");\n    while ((ch = getchar()) < 'a' || ch > 'd' && ch != 'q')\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter (a, b, c, d or q).\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n    if (ch == 'a')\n        strcpy(flight_num, \"102\");\n    else if (ch == 'b')\n        strcpy(flight_num, \"311\");\n    else if (ch == 'c')\n        strcpy(flight_num, \"444\");\n    else if (ch == 'd')\n        strcpy(flight_num, \"519\");\n\n    return ch;\n}\n\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"The number of the flight is %s. To choose a function, \"\n        \"enter its letter label:\\n\", flight_num);\n    printf(\"a) Show number of empty seats\\n\");\n    printf(\"b) Show list of empty seats\\n\");\n    printf(\"c) Show alphabetical list of seats\\n\");\n    printf(\"d) Assign a customer to a seat assignment\\n\");\n    printf(\"e) Delete a seat assignment\\n\");\n    printf(\"f) Confirmed the seat assignment\\n\");\n    printf(\"g) Quit\\n\");\n\n    while ((ch = getchar()) < 'a' || ch > 'g')\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n            continue;\n\n    return ch;\n}\n\nvoid show_number(struct seat * ps)\n{\n    int i;\n    int count = 0;\n\n    for(i = 0; i < MAX; i++)\n    {\n        if (ps[i].isassign == 0)\n            count++;\n    }\n    printf(\"The number of empty seats is %d\\n\", count);\n}\n\nvoid show_empty(struct seat * ps)\n{\n    int i;\n\n    printf(\"The list of empty seats in %s flight:\\n\", flight_num);\n    for (i = 0; i <\tMAX; i++)\n    {\n        if (!ps[i].isassign)\n            printf(\"%d \", ps[i].num);\n    }\n    printf(\"\\n\");\n}\n\nvoid show_list(struct seat * ps)\n{\n    int i;\n\n    printf(\"The list of seats in %s flight:\\n\", flight_num);\n    for (i = 0; i <\tMAX; i++)\n    {\n        printf(\"%d %s %s\\n\", ps[i].num, ps[i].fname, ps[i].lname);\n    }\n    printf(\"The seat assignment is not confirmed.\\n\");\n}\n\nvoid assign_seat(struct seat * ps)\n{\n    int number;\n    int ret_val;\n\n    printf(\"Please input the number of seat you want to book (1 to 12) \"\n        \"in %s flight:\\n\", flight_num);\n    while ((ret_val = scanf(\"%d\", &number)) != 1|| number < 1 || number > 12 ||\n        ps[number - 1].isassign == 1)\n    {\n        if (ret_val != 1 || number < 1 || number > 12)\n            printf(\"Sorry, please input a integer from 1 to 12.\\n\");\n        else\n            printf(\"Sorry, the seat is assigned, please reselect:\\n\");\n        while (getchar() != '\\n')\n            continue;\n    }\n    while (getchar() != '\\n')\n        continue;\n    printf(\"Now, input your firstname:\\n\");\n    s_gets(ps[number - 1].fname, LEN);\n    printf(\"Input your lastname:\\n\");\n    s_gets(ps[number - 1].lname, LEN);\n    ps[number - 1].isassign = 1;\n    printf(\"OK.\\n\");\n}\n\n\nvoid delete_seat(struct seat * ps)\n{\n    int number;\n\n    printf(\"Please input the number of seat you want to delete (1 to 12) \"\n        \"in %s flight:\\n\", flight_num);\n    while (scanf(\"%d\", &number) != 1 || number < 1 || number > 12 ||\n        ps[number - 1].isassign == 0)\n    {\n        if (ps[number - 1].isassign == 1)\n            printf(\"Sorry, the seat is empty, please reselect:\\n\");\n        else\n            printf(\"Sorry, please input a integer from 1 to 12.\\n\");\n        while (getchar() != '\\n')\n            continue;\n    }\n    getchar();\n    strcpy(ps[number - 1].fname, \"\");\n    strcpy(ps[number - 1].lname, \"\");\n    ps[number - 1].isassign = 0;\n    printf(\"OK.\\n\");\n}\n\nvoid is_confirmed(void)\n{\n    isconf = 1;\n    printf(\"The seat assignment is confirmed.\\n\");\n}\n\nvoid write_file(struct seat * ps)\n{\n    FILE * fp;\n\n    fp = fopen(\"seat.txt\", \"w+b\");\n    fwrite(ps, sizeof(struct seat), MAX, fp);\n    fclose(fp);\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_15/1.c",
    "content": "#include <stdio.h>\n#include <string.h>\nint btoi(char * st);\nint main(void)\n{\n    char * pbin = \"01001001\";\n    \n    printf(\"The decimal number: %d.\\n\", btoi(pbin));\n    \n    return 0;\n}\n\nint btoi(char * st)\n{\n    int num = 0;\n\n    while (*st)\n        num = (num << 1) + (*st++ - '0');\n\n    return num;\n}\n"
  },
  {
    "path": "Chapter_15/2.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <limits.h>\n#define SIZE 5\n\nint btoi(char * st);\t\t\t\t//二进制字符串转化为整数的函数\nchar * itob(char * st, int n);\t\t//整数转化为二进制字符串的函数\nvoid show_bstr(const char * st);\t//4位一组显示二进制字符串\n\nint main(int argc, char * argv[])\n{\n    int num1, num2;\n    char bin_st[CHAR_BIT * sizeof(int) + 1];\n\n    if (argc != 3)\n    {\n        fprintf(stderr, \"The number of arguments is wrong.\\n\");\n        exit(EXIT_FAILURE);\n    }\n    num1 = btoi(argv[1]);\n    num2 = btoi(argv[2]);\n    show_bstr(itob(bin_st, ~num1));\n    show_bstr(itob(bin_st, ~num2));\n    show_bstr(itob(bin_st, num1 & num2));\n    show_bstr(itob(bin_st, num1 | num2));\n    show_bstr(itob(bin_st, num1 ^ num2));\n\n    return 0;\n}\n\nint btoi(char * st)\n{\n    int num = 0;\n\n    while (*st)\n        num = (num << 1) + (*st++ - '0');\n\n    return num;\n}\n\nchar * itob(char * st, int n)\n{\n    int i;\n    const static int size = CHAR_BIT * sizeof(int);\n    \n    for (i = size - 1; i >= 0; i--, n >>= 1)\n        st[i] = (01 & n) + '0';\n    st[size] = '\\0';\n\n    return st;\n}\n\nvoid show_bstr(const char * st)\n{\n    int i = 0;\n\n    while (st[i])\n    {\n        putchar(st[i]);\n        if (++i % 4 == 0 && st[i])\n            putchar(' ');\n    }\n    printf(\"\\n\");\n}\n"
  },
  {
    "path": "Chapter_15/3.c",
    "content": "#include <stdio.h>\n#include <limits.h>\n#define MASK 01\n\nint count_bit_on(int num);\n\nint main(void)\n{\n    int num;\n\n    printf(\"Please input a integer:\\n\");\n    scanf(\"%d\", &num);\n    printf(\"The number of bit on is: %d\\n\",\n        count_bit_on(num));\n\n    return 0;\n}\n\nint count_bit_on(int num)\n{\n    int i;\n    int count = 0;\n    int size = CHAR_BIT * sizeof(int);\n    \n    for (i = 0; i < size; i++)\n    {\n        if (num & MASK)\n        {\n            count++;\n        }\n        num >>= 1;\n    }\n\n    return count;\n}\n"
  },
  {
    "path": "Chapter_15/4.c",
    "content": "#include <stdio.h>\n#define MASK 01\nint check_bit(int num, int bit);\nint main(void)\n{\n    int num, bit;\n\n    printf(\"Please input a integer:\\n\");\n    scanf(\"%d\", &num);\n    printf(\"Now input the position:\\n\");\n    scanf(\"%d\", &bit);\n    printf(\"The number in your position is: %d.\\n\",\n        check_bit(num, bit));\n    \n    return 0;\n}\n\nint check_bit(int num, int bit)\n{\n    int i;\n    \n    for (i = 0; i < bit; i++)\n    {\n        num >>= 1;\n    }\n    \n    return num & MASK;\n}\n"
  },
  {
    "path": "Chapter_15/5.c",
    "content": "#include <stdio.h>\n#include <limits.h>\n#define MASK 01\n#define SIZE CHAR_BIT * sizeof(unsigned int)\n\nchar * itob(char * st, unsigned int n);\nvoid show_bstr(const char * st);\nunsigned int move_left(unsigned int, int);\nint main(void)\n{\n    unsigned int num;\n    int bit;\n    char st[SIZE + 1];\n\n    printf(\"Please input a integer and the number of bits to move:\\n\");\n    scanf(\"%u%d\", &num, &bit);\n    printf(\"The original binary string:\\n\");\n    show_bstr(itob(st, num));\n    //把所有位向左移动\n    num = move_left(num, bit);\n    printf(\"The present binary string:\\n\");\n    show_bstr(itob(st, num));\n}\n\nunsigned int move_left(unsigned int n, int bit)\n{\n    int i;\n    \n    for (i = 0; i < bit; i++)\n    {\n        n = (n << 1) + ((n >> (SIZE - 1)) & MASK);\n    }\n\n    return n;\n}\n\nchar * itob(char * st, unsigned int n)\n{\n    int i;\n    \n    for (i = SIZE - 1; i >= 0; i--, n >>= 1)\n        st[i] = (01 & n) + '0';\n    st[SIZE] = '\\0';\n\n    return st;\n}\n\nvoid show_bstr(const char * st)\n{\n    int i = 0;\n\n    while (st[i])\n    {\n        putchar(st[i]);\n        if (++i % 4 == 0 && st[i])\n            putchar(' ');\n    }\n    printf(\"\\n\");\n}\n    \n"
  },
  {
    "path": "Chapter_15/6.c",
    "content": "#include <stdio.h>\n#include <string.h>\n\nstruct word {\n    unsigned int font: 8;\n    unsigned int size: 7;\n    unsigned int alignment: 2;\n    unsigned int bold: 1;\n    unsigned int italic: 1;\n};\n\nconst char * align[3] = {\"left\", \"center\", \"right\"};\nconst char * open[2] = {\"off\", \"on\"};\n\nchar get_choice(void);\nchar get_select(void);\nvoid print(struct word *);\n\nint main(void)\n{\n    struct word type = {1, 12, 0, 0, 0};\n    char choice;\n    char ch;\n    int num;\n\n    print(&type);\n    while ((choice = get_choice()) != 'q')\n    {\n        switch (choice)\n        {\n        case 'f': printf(\"Enter font ID (0 - 256): \");\n                  scanf(\"%u\", &num);\n                  getchar();\n                  type.font = num;\n                  break;\n        case 's': printf(\"Enter font ID (0 - 127): \");\n                  scanf(\"%u\", &num);\n                  getchar();\n                  type.size = num;\n                  break;\n        case 'a': printf(\"Select alignment:\\n\");\n                  printf(\"l)left   c)center   r)right\\n\");\n                  if ((ch = get_select()) == 'l')\n                      type.alignment = 0;\n                  else if (ch == 'c')\n                      type.alignment = 1;\n                  else\n                      type.alignment = 2;\n                  break;\n        case 'b': type.bold = !type.bold;\n                  break;\n        case 'i': type.italic = !type.italic;\n                  break;\n        default : break;\n        }\n        print(&type);\n    }\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"f)change font\\ts)change size\\ta)change alignment\\n\");\n    printf(\"b)toggle bold\\ti)toggle italic\\nq)quit\\n\");\n\n    while (strchr(\"fsabiq\", ch = getchar()) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n    \n    return ch;\n}\n\nchar get_select(void)\n{\n    char ch;\n\n    while (strchr(\"lcr\", ch = getchar()) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n    \n    return ch;\n}\n\nvoid print(struct word * pst)\n{\n    printf(\"\\n ID  SIZE  ALIGNMENT   B    I\\n\");\n    printf(\"%3u\", pst->font);\n    printf(\"%5u\", pst->size);\n    printf(\"%10s\", align[pst->alignment]);\n    printf(\"%7s\", open[pst->bold]);\n    printf(\"%5s\\n\\n\", open[pst->italic]);\n}\n"
  },
  {
    "path": "Chapter_15/7.c",
    "content": "#include <stdio.h>\n#include <string.h>\n\n#define ID_MASK 0xff\n//括号缺失会出错\n#define SIZE_MASK (0x7f << 8)\n#define ALIGN_MASK (0x3 << 15)\n\n#define CENTER (0x1 << 15)\n#define RIGHT (0x2 << 15)\n#define BOLD (0x1 << 17)\n#define ITALIC (0x1 << 18)\n\nconst char * align[3] = {\"left\", \"center\", \"right\"};\nconst char * open[2] = {\"off\", \"on\"};\n\nchar get_choice(void);\nchar get_select(void);\nvoid print(unsigned long);\n\nint main(void)\n{\n    unsigned long type = 0;\n    char choice;\n    char ch;\n    int num;\n\n    print(type);\n    while ((choice = get_choice()) != 'q')\n    {\n        switch (choice)\n        {\n        case 'f': printf(\"Enter font ID (0 - 256): \");\n                  scanf(\"%lu\", &num);\n                  getchar();\n                  type &= ~ID_MASK;\t\t//把表示字体ID的位清空\n                  type |= num;\t\t\t//重置字体ID\n                  break;\n        case 's': printf(\"Enter font ID (0 - 127): \");\n                  scanf(\"%lu\", &num);\n                  getchar();\n                  type &= ~SIZE_MASK;\n                  type |= (num << 8);\n                  break;\n        case 'a': type &= ~ALIGN_MASK;\n                  printf(\"Select alignment:\\n\");\n                  printf(\"l)left   c)center   r)right\\n\");\n                  if ((ch = get_select()) == 'c')\n                      type |= CENTER;\n                  else if (ch == 'r')\n                      type |= RIGHT;\n                      break;\n        case 'b': type ^= BOLD;\n                  break;\n        case 'i': type ^= ITALIC;\n                  break;\n        default : break;\n        }\n        print(type);\n    }\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n    \n    printf(\"f)change font\\ts)change size\\ta)change alignment\\n\");\n    printf(\"b)toggle bold\\ti)toggle italic\\nq)quit\\n\");\n\n    while (strchr(\"fsabiq\", ch = getchar()) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n    \n    return ch;\n}\n\nchar get_select(void)\n{\n    char ch;\n\n    while (strchr(\"lcr\", ch = getchar()) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the right letter.\\n\");\n    }\n    while (getchar() != '\\n')\n        continue;\n    \n    return ch;\n}\n\nvoid print(unsigned long num)\n{\n    printf(\"\\n ID  SIZE  ALIGNMENT   B    I\\n\");\n    printf(\"%3u\", num & ID_MASK);\n    printf(\"%5u\", (num & SIZE_MASK) >> 8);\n    printf(\"%10s\", align[(num & ALIGN_MASK) >> 15]);\n    printf(\"%7s\", open[(num & BOLD) >> 17]);\n    printf(\"%5s\\n\\n\", open[(num & ITALIC) >> 18]);\n}\n"
  },
  {
    "path": "Chapter_16/2.c",
    "content": "#include <stdio.h>\n#define HORMONIC(x, y) (1 / (1 / (x) + 1 / (y)))\n\nint main(void)\n{\n    double num1, num2;\n\n    printf(\"Please input two number ('q' to quit): \");\n    while (scanf(\"%lf%lf\", &num1, &num2) == 2)\n    {\n        printf(\"The hormonic average: %.2f\\n\", HORMONIC(num1, num2));\n        printf(\"Please input two number ('q' to quit): \");\n    }\n    \n    return 0;\n}\n"
  },
  {
    "path": "Chapter_16/3.c",
    "content": "#include <stdio.h>\n#include <math.h>\n\n#define DEG_TO_RED ((4 * atan(1)) / 180)\n\ntypedef struct polar_v {\n    double magnitude;\n    double angle;\n} Polar_V;\n\ntypedef struct rect_v {\n    double x;\n    double y;\n} Rect_V;\n\nRect_V polar_to_rect(Polar_V pv);\n\nint main(void)\n{\n    Polar_V input;\n    Rect_V result;\n\n    puts(\"Enter magnitude and angle; enter q to quit:\");\n    while (scanf(\"%lf%lf\", &input.magnitude, &input.angle) == 2)\n    {\n        result = polar_to_rect(input);\n        printf(\"x = %.2f, y = %.2f\\n\", result.x, result.y);\n        puts(\"Next input (q to quit):\");\n    }\n    puts(\"Bye.\");\n\n    return 0;\n}\n\nRect_V polar_to_rect(Polar_V pv)\n{\n    Rect_V rv;\n    rv.x = pv.magnitude * cos(pv.angle * DEG_TO_RED);\n    rv.y = pv.magnitude * sin(pv.angle * DEG_TO_RED);\n\n    return rv;\n}\n"
  },
  {
    "path": "Chapter_16/4.c",
    "content": "#include <stdio.h>\n#include <time.h>\n\nvoid delay(double time);\n\nint main(void)\n{\n    double time;\n\n    printf(\"Please input the time (in seconds) to delay\"\n        \" (q to quit):\\n\");\n    while (scanf(\"%lf\", &time) == 1)\n    {\n        delay(time);\n        printf(\"Time is out.\\n\");\n        printf(\"Your next input (q to quit):\\n\");\n    }\n\n    return 0;\n}\n\nvoid delay(double time)\n{\n    double t;\n\n    t = (double) clock() / CLOCKS_PER_SEC;\n    while ((double) clock() / CLOCKS_PER_SEC - t < time)\n        ;\n}\n"
  },
  {
    "path": "Chapter_16/5.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n#include <string.h>\n#define SIZE 10\nvoid random(const int ar[], int size, int n);\n\nint main(void)\n{\n    int ar[SIZE] = {1, 2, 3, 6, 7, 9, 10, 13, 26, 17};\n    int i;\n    int num;\n\n    srand((unsigned int) time(NULL));\n    printf(\"The original array:\\n\");\n    for (i = 0; i < SIZE; i++)\n        printf(\"%d \", ar[i]);\n    printf(\"\\nPlease input the number of integers (no more than 10)\\n\");\n    printf(\"you want to take (q to quit):\\n\");\n    while (scanf(\"%d\", &num) == 1)\n    {\n        random(ar, SIZE, num);\n        printf(\"Your next input (q to quit):\\n\");\n    }\n    puts(\"Bye.\");\n\n    return 0;\n}\n\nvoid random(const int ar[], int size, int n)\n{\n    int i;\n    int count;\n    int index;\n    int acpy[SIZE];\n\n    //创建一个数组副本，避免修改原始数组\n    memcpy(acpy, ar, SIZE * sizeof(int));\n    if (n > size)\n        printf(\"The number is too big.\\n\");\n    else\n    {\n        for (count = 0; count < n; count++)\n        {\n            index = rand() % size;\n            printf(\"%d \", acpy[index]);\n            for (i = index; i < size - 1; i++)\n            {\n                acpy[i] = acpy[i + 1];\n            }\n            size--;\n        }\n        printf(\"\\n\");\n    }\n}\n"
  },
  {
    "path": "Chapter_16/6.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define SIZE 40\n#define NUM 5\n\nstruct names {\n    char first[SIZE];\n    char last[SIZE];\n};\n\nint comp(const void * p1, const void * p2);\n\nint main(void)\n{\n    int i;\n\n    struct names staff[NUM] = {\n        {\"zha\", \"yujie\"},\n        {\"xia\", \"yufeng\"},\n        {\"bu\", \"jianqiang\"},\n        {\"xu\", \"feifan\"},\n        {\"ding\",\"bing\"}\n    };\n\n    qsort(staff, NUM, sizeof(struct names), comp);\n    for (i = 0; i < NUM; i++)\n    {\n        printf(\"%s %s\\n\", staff[i].first, staff[i].last);\n    }\n\n    return 0;\n}\n\nint comp(const void * p1, const void * p2)\n{\n    const struct names *ps1 = (const struct names *) p1;\n    const struct names *ps2 = (const struct names *) p2;\n    int res;\n    res = strcmp(ps1->first, ps2->first);\n    if (res)\n        return res;\n    else\n        return strcmp(ps1->last, ps2->last);\n}\n\n\n\n\n \n"
  },
  {
    "path": "Chapter_16/7.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdarg.h>\n#define SIZE 40\n\nvoid show_array(const double ar[], int n);\ndouble * new_d_array(int n, ...);\n\nint main()\n{\n    double * p1;\n    double * p2;\n    p1 = new_d_array(5, 1.2, 2.3, 3.4, 4.5, 5.6);\n    p2 = new_d_array(4, 100.0, 20.00, 8.08, -1890.0);\n    show_array(p1, 5);\n    show_array(p2, 4);\n    free(p1);\n    free(p2);\n    \n    return 0;\n}\n\nvoid show_array(const double ar[], int n)\n{\n    int i;\n\n    for (i = 0; i < n; i++)\n        printf(\"%g \", ar[i]);\n    printf(\"\\n\");\n}\n\ndouble * new_d_array(int n, ...)\n{\n    int i;\n    double * p = (double *) malloc(n * sizeof(double));\n\n    va_list ap;\n    va_start(ap, n);\n    for (i = 0; i < n; i++)\n        *(p + i) = va_arg(ap, double);\n    va_end(ap);\n\n    return p;\n}\n"
  },
  {
    "path": "Chapter_17/1.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#define TSIZE 45\n\ntypedef struct film {\n    char title[TSIZE];\n    int rating;\n    struct film * prev;\n    struct film * next;\n} Node;\n\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    Node * head = NULL;\n    Node * pre = NULL;\n    Node * end;\n    Node * current;\n    char input[TSIZE];\n\n    //储存信息\n    puts(\"Enter first movie title:\");\n    while (s_gets(input, TSIZE) != NULL && input[0] != '\\0')\n    {\n        current = (Node *) malloc(sizeof(Node));\n        if (head == NULL)\n            head = current;\n        else\n        {\n            pre->next = current;\n        }\n        current->prev = pre;\n        current->next = NULL;\n        strcpy(current->title, input);\n        puts(\"Enter your rating <0-10>:\");\n        scanf(\"%d\", &current->rating);\n        while (getchar() != '\\n')\n            continue;\n        puts(\"Enter next movie title (empty to stop):\");\n        pre = current;\n    }\n    end = pre;\n\n    if (head == NULL)\n    {\n        fprintf(stderr, \"No data entered.\\n\");\n        exit(1);\n    }\n    else\n        printf(\"Here is the movie list in positive order:\\n\");\n\n    //正序显示电影列表\n    current = head;\n    while (current != NULL)\n    {\n        printf(\"Movie: %s  Rating: %d\\n\", current->title,\n            current->rating);\n        current = current->next;\n    }\n\n    //逆序显示电影列表\n    printf(\"Here is the movie list in reverse order:\\n\");\n    current = end;\n    while (current != NULL)\n    {\n        printf(\"Movie: %s  Rating: %d\\n\", current->title,\n            current->rating);\n        current = current->prev;\n    }\n\n    return 0;\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while(getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_17/2movie/2.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include \"movie.h\"\n\nvoid show_list(Item item);\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    List movies;\n    Item temp;\n    \n    //初始化\n    InitializeList(&movies);\n    if (ListIsFull(&movies))\n    {\n        fprintf(stderr, \"No memory available.\\n\");\n        exit(1);\n    }\n\n    //储存数据\n    printf(\"Please input the title of movie:\\n\");\n    while (s_gets(temp.titile, TSIZE) != NULL &&\n        temp.titile[0] != '\\0')\n    {\n        printf(\"Enter your rating <0-10>:\\n\");\n        scanf(\"%d\", &temp.rating);\n        while (getchar() != '\\n')\n            continue;\n        if (AddItem(&movies, temp) == 0)\n        {\n            fprintf(stderr, \"Allocting memory error.\\n\");\n            break;\n        }\n        if (ListIsFull(&movies))\n        {\n            fprintf(stderr, \"Sorry, the list is full.\\n\");\n            break;\n        }\n        printf(\"Input the next title (Empty line to stop):\\n\");\n    }\n\n    //显示数据\n    if (ListIsEmpty(&movies))\n    {\n        fprintf(stderr, \"Sorry, the list is empty.\\n\");\n        return 0;\n    }\n    printf(\"Here is the movie list:\\n\");\n    Traverse(&movies, show_list);\n    printf(\"You entered %u movies.\\n\", ListItemCount(&movies));\n    \n    //清空数据\n    DeleteAll(&movies);\n\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nvoid show_list(Item item)\n{\n    printf(\"Movie: %s   Rating: %d\\n\", item.titile, item.rating);\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if(ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n\n\n"
  },
  {
    "path": "Chapter_17/2movie/movie.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include \"movie.h\"\n\n//初始化链表为空\nvoid InitializeList(List * plist)\n{\n    plist->head = NULL;\n    plist->end = NULL;\n}\n\n//链表为空,返回1\nint ListIsEmpty(const List * plist)\n{\n    if (plist->head == NULL)\n        return 1;\n    else\n        return 0;\n}\n\n//链表为满，返回1\nint ListIsFull(const List * plist)\n{\n    Node * pt;\n    int full;\n\n    pt = (Node *)malloc(sizeof(Node));\n    if (pt == NULL)\n        full = 1;\n    else\n        full = 0;\n    free(pt);\n\n    return full;\n}\n\n//为链表添加项，成功返回1\nint AddItem(List * plist, Item item)\n{\n    Node * pnew;\n\n    pnew = (Node *)malloc(sizeof(Node));\n    if (pnew == NULL)\n        return 0;\n    \n    pnew->next = NULL;\n    pnew->item = item;\n    \n    if (plist->head == NULL)\n        plist->head = pnew;\n    else\n        plist->end->next = pnew;\n    plist->end = pnew;\n    \n    return 1;\n}\n\n//确定链表的项数\nunsigned int ListItemCount(List * plist)\n{\n    unsigned int count = 0;\n    Node * pnode = plist->head;\n\n    while (pnode != NULL)\n    {\n        pnode = pnode->next;\n        count++;\n    }\n\n    return count;\n}\n\n//遍历链表\nvoid Traverse(const List * plist, void (*pfun)(Item item))\n{\n    Node * pnode = plist->head;\n\n    while (pnode != NULL)\n    {\n        (*pfun)(pnode->item);\n        pnode = pnode->next;\n    }\n}\n\n//清空链表\nvoid DeleteAll(List * plist)\n{\n    Node * pnode;\n\n    while (plist->head != NULL)\n    {\n        pnode = plist->head;\n        plist->head = plist->head->next;\n        free(pnode);\n    }\n}\n"
  },
  {
    "path": "Chapter_17/2movie/movie.h",
    "content": "#ifndef MOVIE_H_\n#define MOVIE_H_\n\n#define TSIZE 45\n\ntypedef struct film {\n\tchar titile[TSIZE];\n\tint rating;\n} Item;\n\ntypedef struct node {\n\tItem item;\n\tstruct node * next;\n} Node;\n\ntypedef struct list {\n\tNode * head;\n\tNode * end;\n} List;\n\n//初始化链表为空\nvoid InitializeList(List * plist);\n\n//链表为空,返回1\nint ListIsEmpty(const List * plist);\n\n//链表为满，返回1\nint ListIsFull(const List * plist);\n\n//为链表添加项，成功返回1\nint AddItem(List * plist, Item item);\n\n//确定链表的项数\nunsigned int ListItemCount(List * plist);\n\n//遍历链表\nvoid Traverse(const List * plist, void (*pfun)(Item item));\n\n//清空链表\nvoid DeleteAll(List * plist);\n\n#endif"
  },
  {
    "path": "Chapter_17/3movie/3.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include \"movie2.h\"\n\nvoid show_list(Item item);\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    List movies;\n    Item temp;\n    \n    //初始化\n    InitializeList(&movies);\n    if (ListIsFull(&movies))\n    {\n        fprintf(stderr, \"No memory available.\\n\");\n        exit(1);\n    }\n\n    //储存数据\n    printf(\"Please input the title of movie:\\n\");\n    while (s_gets(temp.titile, TSIZE) != NULL &&\n        temp.titile[0] != '\\0')\n    {\n        printf(\"Enter your rating <0-10>:\\n\");\n        scanf(\"%d\", &temp.rating);\n        while (getchar() != '\\n')\n            continue;\n        if (AddItem(&movies, temp) == 0)\n        {\n            fprintf(stderr, \"Allocting memory error.\\n\");\n            break;\n        }\n        if (ListIsFull(&movies))\n        {\n            fprintf(stderr, \"Sorry, the list is full.\\n\");\n            break;\n        }\n        printf(\"Input the next title (Empty line to stop):\\n\");\n    }\n\n    //显示数据\n    if (ListIsEmpty(&movies))\n    {\n        fprintf(stderr, \"Sorry, the list is empty.\\n\");\n        return 0;\n    }\n    printf(\"Here is the movie list:\\n\");\n    Traverse(&movies, show_list);\n    printf(\"You entered %u movies.\\n\", ListItemCount(&movies));\n    \n    //清空数据\n    DeleteAll(&movies);\n\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nvoid show_list(Item item)\n{\n    printf(\"Movie: %s   Rating: %d\\n\", item.titile, item.rating);\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if(ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_17/3movie/movie2.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"movie2.h\"\n\n//初始化链表为空\nvoid InitializeList(List * plist)\n{\n    int i;\n\n    for (i = 0; i < MAXSIZE; i++)\n    {\n        strcpy((plist->entries[i]).titile, \"\");\n        (plist->entries[i]).rating = 0;\n        plist->items = 0;\n    }\n}\n\n//链表为空,返回1\nint ListIsEmpty(const List * plist)\n{\n    if (plist->items == 0)\n        return 1;\n    else\n        return 0;\n}\n\n//链表为满，返回1\nint ListIsFull(const List * plist)\n{\n    if (plist->items == MAXSIZE)\n        return 1;\n    else\n        return 0;\n}\n\n//为链表添加项，成功返回1\nint AddItem(List * plist, Item item)\n{\n    if (plist->items == MAXSIZE)\n    {\n        fprintf(stderr, \"The list is full.\\n\");\n        return 0;\n    }\n\n    plist->entries[plist->items] = item;\n    (plist->items)++;\n\n    return 1;\n}\n\n//确定链表的项数\nunsigned int ListItemCount(List * plist)\n{\n    return plist->items;\n}\n\n//遍历链表\nvoid Traverse(const List * plist, void (*pfun)(Item item))\n{\n    int i;\n    \n    for (i = 0; i < plist->items; i++)\n    {\n        (*pfun)(plist->entries[i]);\n    }\n}\n\n//清空链表\nvoid DeleteAll(List * plist)\n{\n    int i;\n\n    for (i = 0; i < plist->items; i++)\n    {\n        strcpy((plist->entries[i]).titile, \"\");\n        (plist->entries[i]).rating = 0;\n        plist->items = 0;\n    }\n    \n}\n"
  },
  {
    "path": "Chapter_17/3movie/movie2.h",
    "content": "#ifndef MOVIE_H_\n#define MOVIE_H_\n\n#define TSIZE 45\n#define MAXSIZE 5\n\ntypedef struct film {\n\tchar titile[TSIZE];\n\tint rating;\n} Item;\n\ntypedef struct list {\n\tItem entries[MAXSIZE];\n\tint items;\n} List;\n\n//ʼΪ\nvoid InitializeList(List * plist);\n\n//Ϊ,1\nint ListIsEmpty(const List * plist);\n\n//Ϊ1\nint ListIsFull(const List * plist);\n\n//Ϊɹ1\nint AddItem(List * plist, Item item);\n\n//ȷ\nunsigned int ListItemCount(List * plist);\n\n//\nvoid Traverse(const List * plist, void (*pfun)(Item item));\n\n//\nvoid DeleteAll(List * plist);\n\n#endif"
  },
  {
    "path": "Chapter_17/4queue/mall.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n#include \"queue.h\"\n#define MIN_PER_HOUR 60\n\nint newcustomer(double x);\nItem customertime(long when);\n\nQueue line1;\nQueue line2;\nItem temp;\n\nint hours;\t\t\t\t\t//模拟的小时数\nint cust_per_hour;\t\t\t//平均每小时来的顾客数\nint wait_time1 = 0;         //摊位1从当前到空闲的时间\nint wait_time2 = 0;         //摊位2从当前到空闲的时间\nlong cycle;\t\t\t\t\t//循环次数\nlong cyclelimit;\t\t\t//循环的最大数\nlong turnaways = 0;\t\t\t//被拒的顾客数\nlong customers = 0;\t\t\t//加入队列的顾客数\nlong served = 0;\t\t\t//接受服务的顾客数\nlong sum_line = 0;\t\t\t//累计队列总长\nlong line_wait = 0;\t\t\t//累计队列等候时间\ndouble min_per_cust;\t\t//顾客到来的平均间隔时间\n\nint main(void)\n{\n    printf(\"Enter the number of simulation hours:\\n\");\n    scanf(\"%d\", &hours);\n    printf(\"Enter the average number of customers per hour:\\n\");\n    scanf(\"%d\", &cust_per_hour);\n    cyclelimit = MIN_PER_HOUR * hours;\n    min_per_cust = MIN_PER_HOUR / cust_per_hour;\n\n    InitializeQueue(&line1);\n    InitializeQueue(&line2);\n    srand((unsigned int) time(0));\n    for (cycle = 0; cycle < cyclelimit; cycle++)\n    {\n        if (newcustomer(min_per_cust))\t\t//如果一分钟内有顾客来\n        {\n            if (QueueIsFull(&line1) && QueueIsFull(&line2))\n                turnaways++;\n            else\n            {\n                customers++;\n                temp = customertime(cycle);\n                if (line1.items > line2.items)\n                    EnQueue(&line2, temp);\n                else if (line1.items < line2.items)\n                    EnQueue(&line1, temp);\n                else\n                    (rand() % 2) ? EnQueue(&line1, temp) : EnQueue(&line2, temp);\n            }\n        }\n\n        if (wait_time1 <= 0 && !QueueIsEmpty(&line1))\n        {\n            DeQueue(&line1, &temp);\n            wait_time1 = temp.processtime;\n            line_wait += cycle - temp.arrive;\n            served++;\n        }\n\n        if (wait_time2 <= 0 && !QueueIsEmpty(&line2))\n        {\n            DeQueue(&line2, &temp);\n            wait_time2 = temp.processtime;\n            line_wait += cycle - temp.arrive;\n            served++;\n        }\n\n        if (wait_time1 > 0)\n            wait_time1--;\n\n        if (wait_time2 > 0)\n            wait_time2--;\n\n        sum_line += (long) QueueItemCount(&line1)\n            + (long) QueueItemCount(&line2);\n    }\n\n    if (customers > 0)\n    {\n        printf(\"customers accepted: %ld\\n\", customers);\n        printf(\"  customers served: %ld\\n\", served);\n        printf(\"         turnaways: %ld\\n\", turnaways);\n        printf(\"average queue size: %.2f\\n\", (double) sum_line / cyclelimit);\n        printf(\" average wait time: %.2f minutes\\n\", (double) line_wait / served);\n    }\n    else\n        puts(\"No customers!\");\n    DeleteAll(&line1);\n    DeleteAll(&line2);\n    puts(\"Bye!\");\n\n    return 0;\n}\n\n//一分钟之内有顾客来，返回1\nint newcustomer(double x)\n{\n    if (x * rand() / RAND_MAX < 1)\n        return 1;\n    else\n        return 0;\n}\n\nItem customertime(long when)\n{\n    Item cust;\n\n    cust.arrive = when;\n    cust.processtime = rand() % 3 + 1;\n\n    return cust;\n}\n\n"
  },
  {
    "path": "Chapter_17/4queue/queue.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include \"queue.h\"\n\nstatic void AddItem(Node * pq, Item item);\nstatic void CopyItem(Node * pnode, Item * pitem);\n\n//接口函数\n\n//初始化队列\nvoid InitializeQueue(Queue * pq)\n{\n    pq->head = NULL;\n    pq->end = NULL;\n    pq->items = 0;\n}\n\n//确定队列为空\nint QueueIsEmpty(const Queue * pq)\n{\n    if (pq->items == 0)\n        return 1;\n    else\n        return 0;\n}\n\n//确定队列为满\nint QueueIsFull(const Queue * pq)\n{\n    if (pq->items == MAXSIZE)\n        return 1;\n    else\n        return 0;\n}\n\n//确定队列项数\nunsigned int QueueItemCount(const Queue * pq)\n{\n    return pq->items;\n}\n\n//给队列末尾添加项\nint EnQueue(Queue * pq, Item item)\n{\n    Node * pnode;\n\n    if (QueueIsFull(pq))\n    {\n        fprintf(stderr, \"The queue is full.\\n\");\n        return 0;\n    }\n\n    pnode = (Node *) malloc(sizeof(Node));\n    if (pnode == NULL)\n    {\n        fprintf(stderr, \"Can't alloc memory.\\n\");\n        return 0;\n    }\n    \n    AddItem(pnode, item);\n    pnode->next = NULL;\n    pq->items++;\n    if (pq->head == NULL)\n    {\n        pq->head = pnode;\n    }\n    else\n        pq->end->next = pnode;\n    pq->end = pnode;\n\n    return 1;\n}\n\n//从队列开头删除项\nint DeQueue(Queue * pq, Item * pitem)\n{\n    Node * pnode;\n\n    if (QueueIsEmpty(pq))\n    {\n        fprintf(stderr, \"the queue is empty.\\n\");\n        return 0;\n    }\n    \n    CopyItem(pq->head, pitem);\n    pnode = pq->head;\n    pq->head = pq->head->next;\n    free(pnode);\n    pq->items--;\n\n    if (pq->items == 0)\n        pq->end = NULL;\n\n    return 1;\n}\n\n//清空队列\nvoid DeleteAll(Queue * pq)\n{\n    Item item;\n\n    while (!QueueIsEmpty(pq))\n        DeQueue(pq, &item);\n}\n\n//局域函数\nstatic void AddItem(Node * pnode, Item item)\n{\n    pnode->item = item;\n}\n\nstatic void CopyItem(Node * pnode, Item * pitem)\n{\n    *pitem = pnode->item;\n}\n"
  },
  {
    "path": "Chapter_17/4queue/queue.h",
    "content": "#ifndef QUEUE_H_\n#define QUEUE_H_\n\n#define MAXSIZE 10\n\ntypedef struct item {\n\tlong arrive;\n\tint processtime;\n} Item;\n\ntypedef struct node {\n\tItem item;\n\tstruct node * next;\n} Node;\n\ntypedef struct queue {\n\tNode * head;\n\tNode * end;\n\tint items;\n} Queue;\n\n//初始化队列\nvoid InitializeQueue(Queue * pq);\n\n//确定队列为空\nint QueueIsEmpty(const Queue * pq);\n\n//确定队列为满\nint QueueIsFull(const Queue * pq);\n\n//确定队列项数\nunsigned int QueueItemCount(const Queue * pq);\n\n//给队列末尾添加项\nint EnQueue(Queue * pq, Item item);\n\n//从队列开头删除项\nint DeQueue(Queue * pq, Item * pitem);\n\n//清空队列\nvoid DeleteAll(Queue * pq);\n\n#endif"
  },
  {
    "path": "Chapter_17/5stack/5.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include \"stack.h\"\n\n#define SIZE 40\n\nchar * s_gets(char * st, int n);\n\nint main(void)\n{\n    Stack stack;\n    Item temp[SIZE];\n    Item ch;\n    int i;\n\n    InitializeStack(&stack);\n\n    //将用户输入数据压入栈中\n    printf(\"Enter a string:\\n\");\n    while (s_gets(temp, SIZE) != NULL && temp[0] != '\\0')\n    {\n        for (i = 0; temp[i] != '\\0'; i++)\n        {\n            if (AddItem(&stack, temp[i]) == 0)\n            {\n                break;\n            }\n        }\n        \n        if (StackIsFull(&stack))\n        {\n            fprintf(stderr, \"The stack is full.\\n\");\n            break;\n        }\n        printf(\"Your next string (empty line to stop):\\n\");\n    }\n\n    //从栈中弹出数据并输出\n    if (StackIsEmpty(&stack))\n    {\n        fprintf(stderr, \"There is no data.\\n\");\n        exit(1);\n    }\n    \n    printf(\"You input %d charcters.\\n\", StackItemCount(&stack));\n    printf(\"The string in reverse order:\\n\");\n    while (StackIsEmpty(&stack) != 1)\n    {\n        DelItem(&stack, &ch);\n        putchar(ch);\n    }\n    printf(\"\\nBye.\\n\");\n\n    return 0;\n}\n\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n\n\n"
  },
  {
    "path": "Chapter_17/5stack/stack.c",
    "content": "#include <stdio.h>\n#include \"stack.h\"\n\n//初始化栈\nvoid InitializeStack(Stack * ps)\n{\n    ps->items = 0;\n}\n\n//确定栈为空\nint StackIsEmpty(const Stack * ps)\n{\n    return (ps->items == 0) ? 1 : 0;\n}\n\n//确定栈为满\nint StackIsFull(const Stack * ps)\n{\n    return (ps->items == SMAX) ? 1 : 0;\n}\n\n//统计栈内项数\nint StackItemCount(const Stack * ps)\n{\n    return ps->items;\n}\n\n//从栈顶压入项\nint AddItem(Stack * ps, Item item)\n{\n    if (StackIsFull(ps))\n        return 0;\n    else\n    {\n        ps->item[ps->items] = item;\n        ps->items++;\n        return 1;\n    }\n}\n\n//从栈顶弹出项\nint DelItem(Stack * ps, Item * pitem)\n{\n    if (StackIsEmpty(ps))\n        return 0;\n    else\n    {\n        * pitem = ps->item[ps->items - 1];\n        ps->item[ps->items - 1] = '\\0';\n        ps->items--;\n        return 1;\n    }\n}\n"
  },
  {
    "path": "Chapter_17/5stack/stack.h",
    "content": "#ifndef STACK_H_\n#define STACK_H_\n\n#define SMAX 100\n\ntypedef char Item;\ntypedef struct stack {\n\tItem item[SMAX];\n\tint items;\n} Stack;\n\n//初始化栈\nvoid InitializeStack(Stack * ps);\n\n//确定栈为空\nint StackIsEmpty(const Stack * ps);\n\n//确定栈为满\nint StackIsFull(const Stack * ps);\n\n//统计栈内项数\nint StackItemCount(const Stack * ps);\n\n//从栈顶压入项\nint AddItem(Stack * ps, Item item);\n\n//从栈顶弹出项\nint DelItem(Stack * ps, Item * pitem);\n\n//清空项\nvoid EmptyItem(Stack * ps);\n\n#endif"
  },
  {
    "path": "Chapter_17/6.c",
    "content": "#include <stdio.h>\n#define SIZE 8\n\nint find(int ar[], int size, int n);\n\nint main(void)\n{\n    int n;\n    int array[SIZE] = {1, 3, 6, 7, 9, 10, 13, 15};\n\n    printf(\"Please input the integer you want to search:\\n\");\n    while (scanf(\"%d\", &n) == 1)\n    {\n        if (find(array, SIZE, n))\n            printf(\"Find.\\n\");\n        else\n            printf(\"Can't find.\\n\");\n        printf(\"Your next input (q to quit):\\n\");\n    }\n\n    return 0;\n}\n\nint find(int * ar, int size, int n)\n{\n    int head = 0;\n    int end = size - 1;\n    int mid;\n    \n    while (head <= end)\n    {\n        mid = (head + end) / 2;\n        if (n < ar[mid])\n            end = mid - 1;\n        else if (n > ar[mid])\n            head = mid + 1;\n        else\n            return 1;\n    }\n\n    return 0;\n}\n\n\n\n\n\n\n"
  },
  {
    "path": "Chapter_17/7word/7.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#include \"word.h\"\n#define LEN 40\n#define SIZE 40\n\nchar get_choice(void);\nchar * s_gets(char * st, int n);\nvoid count_word(Tree * pt, Item * item);\nvoid print_word(Item temp);\n\nint main(void)\n{\n    Tree words;\n    Item temp;\n    FILE * fp;\n    char ch;\n    char name[LEN];\n    char word[SIZE];\n    \n    //打开文件\n    printf(\"Please input the file name:\\n\");\n    s_gets(name, LEN);\n    if ((fp = fopen(name, \"r\")) == NULL)\n    {\n        fprintf(stderr, \"Can't open the file.\\n\");\n        exit(1);\n    }\n\n    //从文件读取单词\n    InitializeTree(&words);\n    while (fscanf(fp, \"%s\", word) == 1)\n    {\n        strcpy(temp.word, word);\n        temp.num = 1;\n        AddItem(&words, &temp);\n        if (TreeIsFull(&words))\n        {\n            fprintf(stderr, \"The tree is full.\\n\");\n            break;\n        }\n    }\n    fclose(fp);\n\n    while ((ch = get_choice()) != 'q')\n    {\n        if (ch == 'a')\n        {\n            if (TreeIsEmpty(&words))\n                printf(\"There is no word.\\n\");\n            else\n                Traverse(&words, print_word);\n        }\n        else\n            count_word(&words, &temp);\n    }\n    \n    DeleteAll(&words);\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n\n    printf(\"\\na. show word list\\n\");\n    printf(\"b. find a word\\n\");\n    printf(\"q. quit\\n\");\n    \n\n    ch = getchar();\n    while (strchr(\"abq\", tolower(ch)) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the a, b, or q.\\n\");\n        ch = getchar();\n    }\n    while (getchar() != '\\n')\n        continue;\n\n    return ch;\n}\n\nvoid print_word(Item temp)\n{\n    printf(\"word: %s\\t\\t\\tcount: %d\\n\", temp.word, temp.num);\n}\n \nvoid count_word(Tree * pt, Item * item)\n{\n    int count;\n\n    printf(\"Enter the word you want to search:\\n\");\n    s_gets(item->word, SIZE);\n    item->num = 1;\n    count = ItemCount(pt, item);\n    if (!count)\n        printf(\"Can't find the word.\\n\");\n    else\n        printf(\"count: %d\\n\", ItemCount(pt, item));\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_17/7word/word.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include \"word.h\"\n\n//局域数据类型\ntypedef struct pair {\n    Trnode * parent;\n    Trnode * child;\n} Pair;\n\n//局域函数声明\nstatic Pair SeekItem(const Tree * ptree, Item * pi);\nstatic int ToLeft(Item * p1, Item * p2);\nstatic int ToRight(Item * p1, Item * p2);\nstatic Trnode * MakeNode(Item * item);\nstatic void AddNode(Trnode * new_node, Trnode * root);\nstatic void DeleteNode(Trnode * * ptr);\nstatic void InOrder(Trnode * root, void (*pfun)(Item item));\nstatic void DeleteAllNodes(Trnode * root);\n\n//接口函数\n\n//初始化树\nvoid InitializeTree(Tree * ptree)\n{\n    ptree->root = NULL;\n    ptree->size = 0;\n}\n\n//确认树为满\nint TreeIsFull(const Tree * ptree)\n{\n    if (ptree->size == MAX)\n        return 1;\n    else\n        return 0;\n}\n\n//确认树为空\nint TreeIsEmpty(const Tree * ptree)\n{\n    if (ptree->size == 0)\n        return 1;\n    else\n        return 0;\n}\n\n//统计项数\nint TreeItemCount(const Tree * ptree)\n{\n    return ptree->size;\n}\n\n//查找项\nint InTree(const Tree * ptree, const Item * pi)\n{\n    Pair look;\n    look = SeekItem(ptree, pi);\n\n    if (look.child != NULL)\n        return 1;\n    else\n        return 0;\n}\n    \n//添加项\nint AddItem(Tree * ptree, const Item * pi)\n{\n    Pair look;\n    Trnode * new_node;\n\n    if (TreeIsFull(ptree))\n        return 0;\n    \n    look = SeekItem(ptree, pi);\n    if (look.child != NULL)\n    {\n        (look.child->item.num)++;\n        return 1;\n    }\n    else\n    {\n        new_node = MakeNode(pi);\n        if (new_node == NULL)\n        {\n            fprintf(stderr, \"Could't eate node\\n\");\n            return 0;\n        }\n        ptree->size++;\n        \n        if (ptree->root == NULL)\n            ptree->root = new_node;\n        else\n            AddNode(new_node, ptree->root);\n    }\n    \n    return 1;\n}\n\n//删除项\nint DelItem(Tree * ptree, const Item * pi)\n{\n    Pair look;\n    \n    look = SeekItem(ptree, pi);\n    if (look.child == NULL)\n        return 0;\n    \n    if (look.parent == NULL)\n        DeleteNode(&ptree->root);\n    else if (look.parent->left == look.child)\n        DeleteNode(&look.parent->left);\n    else\n        DeleteNode(&look.parent->right);\n    ptree->size--;\n\n    return 1;\n}\n\n//遍历树\nvoid Traverse(const Tree * ptree, void (*pfun)(Item item))\n{\n    if (ptree != NULL)\n        InOrder(ptree->root, pfun);\n}\n\n//清空树\nvoid DeleteAll(Tree * ptree)\n{\n    if (ptree != NULL)\n        DeleteAllNodes(ptree->root);\n    ptree->root = NULL;\n    ptree->size = 0;\n}\n\n//每一项计数\nint ItemCount(const Tree * ptree, const Item * item)\n{\n    Pair look;\n\n    look = SeekItem(ptree, item);\n    if (look.child == NULL)\n        return 0;\n    else\n        return look.child->item.num;\n}\n\n\n\n//局域函数\n//寻找项\nstatic Pair SeekItem(const Tree * ptree, const Item * pi)\n{\n    Pair look;\n    look.parent = NULL;\n    look.child = ptree->root;\n    \n    if (look.child == NULL)\n        return look;\n\n    while (look.child != NULL)\n    {\n        if (ToLeft(pi, &(look.child->item)))\n        {\n            look.parent = look.child;\n            look.child = look.child->left;\n        }\n        else if (ToRight(pi, &(look.child->item)))\n        {\n            look.parent = look.child;\n            look.child = look.child->right;\n        }\n        else\n            break;\n    }\n\n    return look;\n}\n\n//项在左边\nstatic int ToLeft(Item * p1, Item * p2)\n{\n    if (strcmp(p1->word, p2->word) < 0)\n        return 1;\n    else\n        return 0;\n}\n\n//项在右边\nstatic int ToRight(Item * p1, Item * p2)\n{\n    if (strcmp(p1->word, p2->word) > 0)\n        return 1;\n    else\n        return 0;\n}\n\nstatic Trnode * MakeNode(Item * item)\n{\n    Trnode * pnode;\n\n    pnode = (Trnode *) malloc(sizeof(Trnode));\n    if (pnode != NULL)\n    {\n        pnode->item = *item;\n        pnode->left = NULL;\n        pnode->right = NULL;\n    }\n\n    return pnode;\n}\n\nstatic void AddNode(Trnode * new_node, Trnode * root)\n{\n    if (ToLeft(&(new_node->item), &(root->item)))\n    {\n        if (root->left == NULL)\n            root->left = new_node;\n        else\n            AddNode(new_node, root->left);\n    }\n\n    else if (ToRight(&(new_node->item), &(root->item)))\n    {\n        if (root->right == NULL)\n            root->right = new_node;\n        else\n            AddNode(new_node, root->right);\n    }\n\n    else\n    {\n        fprintf(stderr, \"location error in AddNode().\\n\");\n        exit(1);\n    }\n}\n\n//ptr 是父节点指向目标节点的指针的地址\nstatic void DeleteNode(Trnode * * ptr)\n{\n    Trnode * pnode;\n\n    if ((*ptr)->left == NULL)\n    {\n        pnode = *ptr;\n        *ptr = (*ptr)->right;\n        free(pnode);\n    }\n\n    else if ((*ptr)->right == NULL)\n    {\n        pnode = *ptr;\n        *ptr = (*ptr)->left;\n        free(pnode);\n    }\n\n    else\n    {\n        for (pnode = (*ptr)->left; pnode->right != NULL; pnode = pnode->right)\n            continue;\n        pnode->right = (*ptr)->right;\n        pnode = (*ptr);\n        *ptr = (*ptr)->left;\n        free(pnode);\n    }\n}\n\nstatic void InOrder(Trnode * root, void (*pfun)(Item item))\n{\n    if (root != NULL)\n    {\n        InOrder(root->left, pfun);\n        (*pfun)(root->item);\n        InOrder(root->right, pfun);\n    }\n}\n\nstatic void DeleteAllNodes(Trnode * root)\n{\n    Trnode * pright;\n\n    if (root != NULL)\n    {\n        pright = root->right;\n        DeleteAllNodes(root->left);\n        free(root);\n        DeleteAllNodes(pright);\n    }\n}\n\n"
  },
  {
    "path": "Chapter_17/7word/word.h",
    "content": "#ifndef WORD_H_\n#define WORD_H_\n\n#define LEN 40\n#define MAX 100\n\ntypedef struct item {\n\tchar word[LEN];\n\tint num;\n} Item;\n\ntypedef struct trnode {\n\tItem item;\n\tstruct trnode * left;\n\tstruct trnode * right;\n} Trnode;\n\ntypedef struct tree {\n\tTrnode * root;\n\tint size;\n} Tree;\n\n//初始化树\nvoid InitializeTree(Tree * ptree);\n\n//确认树为满\nint TreeIsFull(const Tree * ptree);\n\n//确认树为空\nint TreeIsEmpty(const Tree * ptree);\n\n//统计项数\nint TreeItemCount(const Tree * ptree);\n\n//查找项\nint InTree(const Tree * ptree, const Item * pi);\n\n//添加项\nint AddItem(Tree * ptree, const Item * pi);\n\n//删除项\nint DelItem(Tree * ptree, const Item * pi);\n\n//遍历树\nvoid Traverse(const Tree * ptree, void (*pfun)(Item item));\n\n//清空树\nvoid DeleteAll(Tree * ptree);\n\n//对某一项计数\nint ItemCount(const Tree * ptree, const Item * item);\n\n#endif"
  },
  {
    "path": "Chapter_17/8pet/8.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#include \"pet.h\"\n\nchar get_choice(void);\nchar * s_gets(char * st, int n);\nvoid addpet(Tree * pt);\nvoid printpet(Item temp);\nvoid findpet(const Tree * pt);\nvoid droppet(Tree * pt);\nvoid showpet(const Tree * pt);\n\nint main(void)\n{\n    Tree pet;\n    char ch;\n\n    InitializeTree(&pet);\n    while ((ch = get_choice()) != 'q')\n    {\n        switch (ch)\n        {\n        case 'a': addpet(&pet);\n            break;\n        case 'l': showpet(&pet);\n            break;\n        case 'f': findpet(&pet);\n            break;\n        case 'n': printf(\"%d pets in club.\\n\",\n            TreeItemCount(&pet));\n            break;\n        case 'd': droppet(&pet);\n            break;\n        default: puts(\"Swithing error\");\n        }\n        printf(\"\\n\");\n    }\n    \n    DeleteAll(&pet);\n    printf(\"Bye.\\n\");\n\n    return 0;\n}\n\nchar get_choice(void)\n{\n    char ch;\n\n    puts(\"Nerfville Pet Club Membership Program\");\n    puts(\"Enter the letter corresponding to your choice:\");\n    puts(\"a) add a pet        l) show list of pets\");\n    puts(\"n) number of pets   f) find pets\");\n    puts(\"d) delete a pet     q) quit\");\n    \n\n    ch = getchar();\n    while (strchr(\"alnfdq\", tolower(ch)) == NULL)\n    {\n        while (getchar() != '\\n')\n            continue;\n        printf(\"Please input the a, b, or q.\\n\");\n        ch = getchar();\n    }\n    while (getchar() != '\\n')\n        continue;\n\n    return ch;\n}\n\nvoid showpet(const Tree * pt)\n{\n    if (TreeIsEmpty(pt))\n        printf(\"No pet in club.\\n\");\n    else\n        Traverse(pt, printpet);\n}\n\nvoid findpet(const Tree * pt)\n{\n    Item temp;\n    \n    if (TreeIsEmpty(pt))\n        puts(\"No pet in the club.\\n\");\n    else\n    {\n        printf(\"Enter the name of pet:\\n\");\n        s_gets(temp.name, LEN);\n        FindItem(pt, &temp);\n    }\n}\n    \nvoid addpet(Tree * pt)\n{\n    Item temp;\n    \n    if (TreeIsFull(pt))\n        puts(\"No room in the club.\\n\");\n    else\n    {\n        printf(\"Enter the name of pet:\\n\");\n        s_gets(temp.name, LEN);\n        printf(\"Enter the kind of pet:\\n\");\n        s_gets(temp.kind[0], LEN);\n        temp.num = 1;\n        AddItem(pt, &temp);\n    }\n}\n\nvoid droppet(Tree * pt)\n{\n    Item temp;\n\n    if (TreeIsEmpty(pt))\n    {\n        puts(\"No entries.\\n\");\n        return;\n    }\n    \n    printf(\"Enter the name of pet:\\n\");\n    s_gets(temp.name, LEN);\n    if (DelItem(pt, &temp) == 0)\n    {\n        printf(\"Can't find the pet.\\n\");\n    }\n}\n\nvoid printpet(Item temp)\n{\n    int i;\n\n    printf(\"name: %s\\t\\tkind: \", temp.name);\n    for (i = 0; i < temp.num; i++)\n        printf(\"%s \", temp.kind[i]);\n    printf(\"\\n\");\n}\n\nchar * s_gets(char * st, int n)\n{\n    char * ret_val;\n    char * find;\n\n    ret_val = fgets(st, n, stdin);\n    if (ret_val)\n    {\n        find = strchr(st, '\\n');\n        if (find)\n            *find = '\\0';\n        else\n            while (getchar() != '\\n')\n                continue;\n    }\n\n    return ret_val;\n}\n"
  },
  {
    "path": "Chapter_17/8pet/pet.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include \"pet.h\"\n\n//局域数据类型\ntypedef struct pair {\n    Trnode * parent;\n    Trnode * child;\n} Pair;\n\n//内部链接，文件作用域\nstatic int count = 0;\n\n//局域函数声明\nstatic Pair SeekItem(const Tree * ptree, const Item * pi);\nstatic int ToLeft(const Item * p1, const Item * p2);\nstatic int ToRight(const Item * p1, const Item * p2);\nstatic Trnode * MakeNode(const Item * item);\nstatic void AddNode(Trnode * new_node, Trnode * root);\nstatic void DeleteNode(Trnode * * ptr);\nstatic void InOrder(Trnode * root, void (*pfun)(Item item));\nstatic void DeleteAllNodes(Trnode * root);\nstatic void CountItem(Trnode * root);\n\n//接口函数\n\n//初始化树\nvoid InitializeTree(Tree * ptree)\n{\n    ptree->root = NULL;\n    ptree->size = 0;\n}\n\n//确认树为满\nint TreeIsFull(const Tree * ptree)\n{\n    if (ptree->size == SIZE)\n        return 1;\n    else\n        return 0;\n}\n\n//确认树为空\nint TreeIsEmpty(const Tree * ptree)\n{\n    if (ptree->size == 0)\n        return 1;\n    else\n        return 0;\n}\n\n//统计项数\nint TreeItemCount(const Tree * ptree)\n{\n    count = 0;\n\n    CountItem(ptree->root);\n\n    return count;\n}\n\n//查找项\nvoid FindItem(const Tree * ptree, const Item * pi)\n{\n    Pair look;\n    int i;\n\n    look = SeekItem(ptree, pi);\n    if (look.child != NULL)\n    {\n        printf(\"name: %s\\t\\tkind: \", look.child->item.name);\n        for (i = 0; i < look.child->item.num; i++)\n            printf(\"%s \", look.child->item.kind[i]);\n        printf(\"\\n\");\n    }\n    else\n        printf(\"Can't find the pet.\\n\");\n\n}\n    \n//添加项\nint AddItem(Tree * ptree, const Item * pi)\n{\n    Pair look;\n    Trnode * new_node;\n\n    if (TreeIsFull(ptree))\n        return 0;\n    \n    look = SeekItem(ptree, pi);\n    if (look.child != NULL)\n    {\n        strcpy(look.child->item.kind[look.child->item.num], pi->kind[0]);\n        (look.child->item.num)++;\n        return 1;\n    }\n    else\n    {\n        new_node = MakeNode(pi);\n        if (new_node == NULL)\n        {\n            fprintf(stderr, \"Could't eate node\\n\");\n            return 0;\n        }\n        ptree->size++;\n        \n        if (ptree->root == NULL)\n            ptree->root = new_node;\n        else\n            AddNode(new_node, ptree->root);\n    }\n    \n    return 1;\n}\n\n//删除项\nint DelItem(Tree * ptree, const Item * pi)\n{\n    Pair look;\n    \n    look = SeekItem(ptree, pi);\n    if (look.child == NULL)\n        return 0;\n    \n    if (look.parent == NULL)\n        DeleteNode(&ptree->root);\n    else if (look.parent->left == look.child)\n        DeleteNode(&look.parent->left);\n    else\n        DeleteNode(&look.parent->right);\n    ptree->size--;\n\n    return 1;\n}\n\n//遍历树\nvoid Traverse(const Tree * ptree, void (*pfun)(Item item))\n{\n    if (ptree != NULL)\n        InOrder(ptree->root, pfun);\n}\n\n//清空树\nvoid DeleteAll(Tree * ptree)\n{\n    if (ptree != NULL)\n        DeleteAllNodes(ptree->root);\n    ptree->root = NULL;\n    ptree->size = 0;\n}\n\n//局域函数\n//寻找项\nstatic Pair SeekItem(const Tree * ptree, const Item * pi)\n{\n    Pair look;\n    look.parent = NULL;\n    look.child = ptree->root;\n    \n    if (look.child == NULL)\n        return look;\n\n    while (look.child != NULL)\n    {\n        if (ToLeft(pi, &(look.child->item)))\n        {\n            look.parent = look.child;\n            look.child = look.child->left;\n        }\n        else if (ToRight(pi, &(look.child->item)))\n        {\n            look.parent = look.child;\n            look.child = look.child->right;\n        }\n        else\n            break;\n    }\n\n    return look;\n}\n\n//项在左边\nstatic int ToLeft(const Item * p1, const Item * p2)\n{\n    if (strcmp(p1->name, p2->name) < 0)\n        return 1;\n    else\n        return 0;\n}\n\n//项在右边\nstatic int ToRight(const Item * p1, const Item * p2)\n{\n    if (strcmp(p1->name, p2->name) > 0)\n        return 1;\n    else\n        return 0;\n}\n\nstatic Trnode * MakeNode(const Item * item)\n{\n    Trnode * pnode;\n\n    pnode = (Trnode *) malloc(sizeof(Trnode));\n    if (pnode != NULL)\n    {\n        pnode->item = *item;\n        pnode->left = NULL;\n        pnode->right = NULL;\n    }\n\n    return pnode;\n}\n\nstatic void AddNode(Trnode * new_node, Trnode * root)\n{\n    if (ToLeft(&(new_node->item), &(root->item)))\n    {\n        if (root->left == NULL)\n            root->left = new_node;\n        else\n            AddNode(new_node, root->left);\n    }\n\n    else if (ToRight(&(new_node->item), &(root->item)))\n    {\n        if (root->right == NULL)\n            root->right = new_node;\n        else\n            AddNode(new_node, root->right);\n    }\n\n    else\n    {\n        fprintf(stderr, \"location error in AddNode().\\n\");\n        exit(1);\n    }\n}\n\n//ptr 是父节点指向目标节点的指针的地址\nstatic void DeleteNode(Trnode * * ptr)\n{\n    Trnode * pnode;\n\n    if ((*ptr)->left == NULL)\n    {\n        pnode = *ptr;\n        *ptr = (*ptr)->right;\n        free(pnode);\n    }\n\n    else if ((*ptr)->right == NULL)\n    {\n        pnode = *ptr;\n        *ptr = (*ptr)->left;\n        free(pnode);\n    }\n\n    else\n    {\n        for (pnode = (*ptr)->left; pnode->right != NULL; pnode = pnode->right)\n            continue;\n        pnode->right = (*ptr)->right;\n        pnode = (*ptr);\n        *ptr = (*ptr)->left;\n        free(pnode);\n    }\n}\n\nstatic void InOrder(Trnode * root, void (*pfun)(Item item))\n{\n    if (root != NULL)\n    {\n        InOrder(root->left, pfun);\n        (*pfun)(root->item);\n        InOrder(root->right, pfun);\n    }\n}\n\nstatic void DeleteAllNodes(Trnode * root)\n{\n    Trnode * pright;\n\n    if (root != NULL)\n    {\n        pright = root->right;\n        DeleteAllNodes(root->left);\n        free(root);\n        DeleteAllNodes(pright);\n    }\n}\n\nstatic void CountItem(Trnode * root)\n{\n    if (root != NULL)\n    {\n        CountItem(root->left);\n        count += root->item.num;\n        CountItem(root->right);\n    }\n}\n"
  },
  {
    "path": "Chapter_17/8pet/pet.h",
    "content": "#ifndef WORD_H_\n#define WORD_H_\n\n#define MAX 10\n#define LEN 40\n#define SIZE 100\n\ntypedef struct item {\n\tchar name[LEN];\n\tchar kind[MAX][LEN];\n\tint num;\n} Item;\n\ntypedef struct trnode {\n\tItem item;\n\tstruct trnode * left;\n\tstruct trnode * right;\n} Trnode;\n\ntypedef struct tree {\n\tTrnode * root;\n\tint size;\n} Tree;\n\n//初始化树\nvoid InitializeTree(Tree * ptree);\n\n//确认树为满\nint TreeIsFull(const Tree * ptree);\n\n//确认树为空\nint TreeIsEmpty(const Tree * ptree);\n\n//统计项数\nint TreeItemCount(const Tree * ptree);\n\n//查找项\nvoid FindItem(const Tree * ptree, const Item * item);\n\n//添加项\nint AddItem(Tree * ptree, const Item * pi);\n\n//删除项\nint DelItem(Tree * ptree, const Item * pi);\n\n//遍历树\nvoid Traverse(const Tree * ptree, void (*pfun)(Item item));\n\n//清空树\nvoid DeleteAll(Tree * ptree);\n\n#endif"
  },
  {
    "path": "README.md",
    "content": "# C-Primer-Plus\n\nC Primer Plus第六版中文版习题答案\n\n本答案为初学编程时记录，在语法、注释规范，代码风格，解题思路上可能并非最优，所以欢迎大家一起共建。\n\n如果代码有错误或待优化之处，可以提出 [Issues](https://github.com/zhayujie/C-Primer-Plus/issues) 一起讨论，也可以发起 [PR](https://github.com/zhayujie/C-Primer-Plus/pulls) 提交你的代码。"
  }
]