zoukankan      html  css  js  c++  java
  • 多年后从头敲打第一个C程序

      1/*
      2**这个程序从标准输入中读取行并在标准输出中打印这些输入行,
      3**每个输入行的后面一行是该行内容的一部分
      4**
      5**输入的第1行是一串列标号,串的最后一个以负数结尾。
      6**这些列标号成对出现,说明需要打印的输入行的列的范围
      7**例如。0 3 10 12 -1 表示第0列到第3列,第10列到第12列的内容将被打印。
      8*/

      9#include <stdio.h>
     10#include <stdlib.h>
     11#include <string.h>
     12#define MAX_COLS 20 /* 所能处理的最大列号*/
     13#define MAX_INPUT 1000 /*每个输入行的最大长度*/
     14
     15int read_column_numbers(int columns[], int mex);
     16void rearrange(char *output, char const *input,  int n_coulumns, int const columns[]);
     17  
     18int main(void)  
     19{
     20     int  n_columns;  /*进行处理的列标号*/
     21     int  columns[MAX_COLS]; /*需要处理的列数*/
     22     char input[MAX_INPUT]; /*容纳输入行的数组*/
     23     char output[MAX_INPUT]; /*容纳输出行的数组*/
     24 
     25     /*
     26     **读取该串列标号
     27     */

     28     n_columns = read_column_numbers(columns, MAX_COLS);
     29 
     30     /*读取、处理和打印剩余的输入行*/
     31     while(gets(input) != NULL)
     32     {
     33          printf("Original input : %s\n", input);
     34          rearrange(output, input, n_columns, columns);
     35          printf("Rearranged line : %s\n", output);
     36     }

     37 
     38     return EXIT_SUCCESS;
     39}

     40
     41/*读取列标号,如果超出规定范围则不予理会*/
     42int read_column_numbers(int columns[], int max)
     43{
     44     int num = 0;
     45     int ch;
     46 
     47     /*取得列标号,如果所读取的数小于0则停止*/
     48     while(num < max && scanf("%d"&columns[num]) == 1
     49          && columns[num] >= 0)
     50      {
     51           num += 1;
     52      }

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

     60 
     61     /*丢弃该行中包含最后一个数字的那部分内容*/
     62     while( (ch = getchar()) != EOF && ch != '\n')
     63          ;
     64  
     65     return num;
     66}

     67
     68/*处理输入行,将指定列的字符连接在一起,输出行以NULL结尾*/
     69void rearrange(char *output, char const *input, 
     70 int n_columns, int const columns[])
     71{
     72     int col; /*columns数组的下标*/
     73     int output_col; /*输出列计数器*/
     74     int len; /*输入行的长度*/
     75 
     76     len = strlen(input);
     77     output_col = 0;
     78 
     79     /*处理每对列标号*/
     80     for(col = 0; col < n_columns; col +=2)
     81     {
     82          int nchars = columns[col + 1- columns[col] + 1;
     83  
     84          /*如果输入行结束或输出行数已满,就结束任务。*/
     85          if(columns[col] >= len || output_col == MAX_INPUT -1)
     86               break;
     87   
     88          /*如果输出行数数组空间不够,只复制可以容纳的数据*/
     89          if(output_col + nchars > MAX_INPUT - 1)
     90          {
     91               nchars = MAX_INPUT - output_col - 1;
     92          }

     93  
     94          /*复制相关的数据*/
     95          strncpy(output + output_col, input + columns[col], nchars);
     96          output_col += nchars;
     97     }

     98     output[output_col] = '\0';
     99}

    100
  • 相关阅读:
    python,os操作文件,文件路径(上一级目录)
    python屏幕的交互(读取输出信息)input,raw_input的区别
    Shell script 传参数处理(默认变量)
    python 2.4 的字符串转时间(日期减法取间隔时间)
    java的hashcode(结合hashset讲解)
    μCOS-II系统之事件(event)的使用规则及Semaphore的相互排斥量使用方法
    HDU 1079 Calendar Game (博弈论-sg)
    flume MemoryChannel 源代码解析
    MySQL查询时强制区分大写和小写
    蓝桥杯——真题训练之李白打酒
  • 原文地址:https://www.cnblogs.com/xuyuan77/p/1108025.html
Copyright © 2011-2022 走看看