第一章 快速上手

1.7 问题

1.易于阅读,也易于后期的程序维护。

2.如果要用,只需要声明一份拷贝,用不着在很多地方复制避免了维护这些代码时出现错误的可能性。

3.当代码中一个常量用很多次时,就可 以宏定义,这样做,当程序需要改动这个常量时就只需要改动一处,易于维护,减少错误。

4.

#include <stdio.h>

void main(void)
{
    int a = 10;
    char *str = "i love program!";
    float b = 12.33;

    printf("%d %s %g\n", a, str, b);
}

5.

#include <stdio.h>


void main(void)
{
    int quatity;
    int price;
    int i = 0;
    
    char department[10] = {0};

    scanf("%ld,%d,%s", &quatity, &price, &department[i++]);

    printf("%d %d %s\n", quatity, price, department);

}

6.当程序员需要的时候,他们可以加入下标检查,在下标已经知道并且是正确的(例如,早已经检查过。),在这利并没有必要再去花费开销检查一遍,但是真正的原因时他们忽略的原因是下标可以用指针表达式表示。

7.会出现比实际要求拷贝的字符要多的情况。

1.8 编程练习

1.

#include <stdio.h>

void main(void)
{
    printf("hello world!\n");
}

2.这个程序我理解两层意思,所以写了两段代码

#include <stdio.h>
#include <string.h>

#define NUM 100

void fucReadOut(void)
{
    char *pStr;
    char TStr[NUM][NUM]={'\0'};
    char str[NUM] = {'\0'};

    int i = 0;
    int sum = 0;

    while ((pStr = gets(str)) != NULL)
    {
        strcpy(TStr[i], pStr);
        i++;
        sum = i;
        memset(str, '\0', NUM);
    }

    for (i=0; i<sum; i++)
    {
        printf("%d---%s\n", i+1,TStr[i]);
    }
}

void main(void)
{
    fucReadOut();
}

#include <stdio.h>
#include <string.h>

#define NUM 100

void fucReadOut(void)
{
    char *pStr;
    char str[NUM] = {'\0'};

    int i = 0;
    int sum = 0;

    while ((pStr = gets(str)) != NULL)
    {
        i++;
        printf("%d---%s\n", i,pStr);
        memset(str, '\0', NUM);
    }
}

void main(void)
{
    fucReadOut();
}

3.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int c;
    char sum = -1;

    while ((c = getchar()) != EOF)
    {
        putchar(c);
        sum += c;
    }

    printf("sum = %d\n", sum);

    return EXIT_SUCCESS;
}

4.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LEN 1001

int main(void)
{
    int len;
    int longest_len;
 
    char input[MAX_LEN];
    char longest[MAX_LEN];

    longest_len = -1;

    while (gets(input) != NULL)
    {
        len = strlen(input);

        if (len > longest_len)
        {
            longest_len = len;

            strcpy(longest, input);
        }
    }

    if (longest_len >= 0)
    {
        puts(longest);
    }

    return EXIT_SUCCESS;
}

5.书中的rearrage.c代码目前对我来说还很难理解,不过今天有所突破,这几天没更新了,我想明天就可以更新了!(2014-09-30)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_COLS 20    /*所能处理的最大列号*/
#define MAX_INPUT 1000        /*每个输入行的最大长度*/

int read_column_numbers(int columns[],  int max);
void rearrange(char *output, char const *input, int n_columns, int const columns[]);

int main(void)
{
    int n_columns;        /*进行处理的列标号*/
    int columns[MAX_COLS];        /*需要处理的列数*/

    char input[MAX_INPUT];        /*容纳输入行的数组*/
    char output[MAX_INPUT];        /*容纳输出行的数组*/

    /*
    **读取该串列标号
    */
    n_columns = read_column_numbers(columns, MAX_COLS);

    /*
    **读取、处理和打印剩余的输入行。
    */
    while (gets(input) != NULL)
    {
        printf("Original input:%s\n", input);
        rearrange(output, input, n_columns, columns);
        printf("Rearranged line:%s\n", output);
    }

    return EXIT_SUCCESS;
}

/*
**读取列标号,超出范围则不予理会
*/
int read_column_numbers(int columns[], int max)
{
    int num = 0;
    int ch;

    /*
    **取得列标号,如果所取的数小于0则停止
    */
    while ((num < max) && (scanf("%d", &columns[num]) == 1) && (columns[num] >= 0))
    {
        num += 1;
    }

    /*
    **确认已经读取的标号为偶数个,因为他们是以对儿的形式出现的
    */
    if (num % 2 != 0)
    {
        puts("Last column number is not paired.");
        exit(EXIT_FAILURE);
    }

    /*
    **丢弃该行中包含最后一个数字的那部分内容
    */
    while (((ch = getchar()) != EOF) && (ch != '\n'))
    {
        ;
    }

    return num;
}

/*
**处理输入行,将指定列的字符连接在一起,输出行以NUL结尾
*/
void rearrange(char *output, char const *input, int n_columns, int const columns[])
{
    int col;        /*columns数组下标*/
    int output_col;        /*输出列计数器*/
    int len;        /*输入行的长度*/

    len = strlen(input);
    output_col = 0;

    /*
    **处理每对列标号
    */
    for (col = 0; col < n_columns; col += 2)
    {
        int nchars = columns[col + 1] - columns[col] + 1;

        /*
        **如果输入行结束或者输出行数组已满,就结束任务
        */
        if ((columns[col] >= len) || (output_col == MAX_INPUT - 1))
        {
             break;
        }

        /*
        **如果输出行数据空间不够,只复制可容纳的数据
        */
        if (output_col + nchars > MAX_INPUT - 1)
        {
            nchars = MAX_INPUT - output_col - 1;
        }

        /*
        **复制相关数据
        */
        strncpy(output + output_col, input + columns[col], nchars);

        output_col += nchars;
    }

    output[output_col] = '\0';
}

上面的程序来自原书,此题我实在不理解题目的意图,如有大神理解,请赐予解答!

6.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_COLS 20    /*所能处理的最大列号*/
#define MAX_INPUT 1000        /*每个输入行的最大长度*/

int read_column_numbers(int columns[],  int max);
void rearrange(char *output, char const *input, int n_columns, int const columns[]);

int main(void)
{
    int n_columns;        /*进行处理的列标号*/
    int columns[MAX_COLS];        /*需要处理的列数*/

    char input[MAX_INPUT];        /*容纳输入行的数组*/
    char output[MAX_INPUT];        /*容纳输出行的数组*/

    /*
    **读取该串列标号
    */
    n_columns = read_column_numbers(columns, MAX_COLS);

    /*
    **读取、处理和打印剩余的输入行。
    */
    while (gets(input) != NULL)
    {
        printf("Original input:%s\n", input);
        rearrange(output, input, n_columns, columns);
        printf("Rearranged line:%s\n", output);
    }

    return EXIT_SUCCESS;
}

/*
**读取列标号,超出范围则不予理会
*/
int read_column_numbers(int columns[], int max)
{
    int num = 0;
    int ch;

    /*
    **取得列标号,如果所取的数小于0则停止
    */
    while ((num < max) && (scanf("%d", &columns[num]) == 1) && (columns[num] >= 0))
    {
        num += 1;
    }

    /*
    **确认已经读取的标号为偶数个,因为他们是以对儿的形式出现的
    */
#if 0
    if (num % 2 != 0)
    {
        puts("Last column number is not paired.");
        exit(EXIT_FAILURE);
    }
#endif

    /*
    **丢弃该行中包含最后一个数字的那部分内容
    */
    while (((ch = getchar()) != EOF) && (ch != '\n'))
    {
        ;
    }

    return num;
}

/*
**处理输入行,将指定列的字符连接在一起,输出行以NUL结尾
*/
void rearrange(char *output, char const *input, int n_columns, int const columns[])
{
    int col;        /*columns数组下标*/
    int output_col;        /*输出列计数器*/
    int len;        /*输入行的长度*/
/*new*/    int nchars;

    len = strlen(input);
    output_col = 0;

    /*
    **处理每对列标号
    */
    for (col = 0; col < n_columns; col += 2)
    {
     
#if 0  
      int nchars = columns[col + 1] - columns[col] + 1;
#endif

        /*
        **如果输入行结束或者输出行数组已满,就结束任务
        */
        if ((columns[col] >= len) || (output_col == MAX_INPUT - 1))
        {
             break;
        }

        /*
        **计算多少个字符需要复制
        */
/*new*/ 
        if (col + 1 < n_columns)
        {
             nchars = columns[col + 1] - columns[col] + 1;
        }
        else
        {
             nchars = len - columns[col + 1];
        }
/*new*/ 

        /*
        **如果输出行数据空间不够,只复制可容纳的数据
        */
        if (output_col + nchars > MAX_INPUT - 1)
        {
            nchars = MAX_INPUT - output_col - 1;
        }

        /*
        **复制相关数据
        */
        strncpy(output + output_col, input + columns[col], nchars);

        output_col += nchars;
    }

    output[output_col] = '\0';
}

关于判断奇数或者偶数的那一块儿代码,技巧太过巧妙,我目前都没有理解透彻!

                                                                                                                  第一章 完!

转载于:https://my.oschina.net/jiecaogou/blog/322559

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值