zoukankan      html  css  js  c++  java
  • 现代方法第15章第三节的程序

    #include <string.h>
    #include <stdio.h>
    
    #ifndef LINE_H
    #define LINE_H
    void clear_line(void);
    void add_word(const char *word);
    int space_remaining(void);
    void write_line(void);
    void flush_line(void);
    #endif
    
    
    
    #ifndef WORD_H
    #define WORD_H
    void read_word(char *word,int len);
    #endif
    
    #define MAX_WORD_LEN 20
    #define MAX_LINE_LEN 60
    char line[MAX_LINE_LEN+1];
    int line_len = 0;
    int num_words = 0;
    
    main()
    {
    char word[MAX_WORD_LEN+2];
    int word_len;
    
    clear_line();
    for (;;){
        read_word(word,MAX_WORD_LEN+1);
        word_len = strlen(word);
        if (word_len == 0){
                  flush_line();
                  return 0;
        }    
        if (word_len > MAX_WORD_LEN)
          word[MAX_WORD_LEN] = '*';
        if (word_len + 1 > space_remaining()) {
          write_line();
          clear_line();
        }
        add_word(word);
    
    
    }
    }
    
    
    ///////////line.c////////////////////////////////////////////////////////////////i
    void clear_line(void){
        line[0] = '';
        line_len = 0;
        num_words = 0;
    
    }
    
    void add_word(const char *word)
    {
        if (num_words > 0) {
            line[line_len] = ' ';
            line[line_len+1] = '';
            line_len++;
        }
        strcat(line,word);
        line_len += strlen(word);
        num_words++;
        
    }
    int space_remaining(void){
        return MAX_LINE_LEN - line_len;
    }
    void write_line(void)
    {
        int extra_spaces,spaces_to_insert,i,j;
        extra_spaces = MAX_LINE_LEN-line_len;
        for(i=0;i<line_len;i++){
          if(line[i] != ' ')
            putchar(line[i]);
          else {
              spaces_to_insert = extra_spaces / (num_words - 1);
              for (j = 1; j<=spaces_to_insert +1; j++)
                 putchar(' ');
              extra_spaces -= spaces_to_insert;
              num_words--;
          }
    }
    putchar ('
    ');    
    }
    void flush_line(void)
    {
        if (line_len>0)
          puts(line);
    }
    
    
    /////word.c/////////////////////////////////////////////////////////////
    int read_char(void)
    {
        int ch = getchar ();
        
        if (ch == '
    ' || ch == '	')
          return ' ';
        return ch;
    }
    
    void read_word(char *word, int len)
    {
        int ch,pos = 0;
        while ((ch = read_char()) == ' ')
            ;
        
        while (ch != ' ' && ch != EOF) {
            if (pos<len)
              word[pos++] = ch;
            ch = read_char();
        }
        
        word[pos] = '';
    }
    View Code
  • 相关阅读:
    C#中静态与非静态方法比较
    Hibernate学习之路-- -映射 继承关系(subclass , joined-subclass,union-subclass )
    网络协议概述:物理层、连接层、网络层、传输层、应用层详解
    phpstorm xdebug配置
    eclipse修改内存大小
    Session机制详解
    java把html标签字符转普通字符(反转换成html标签)(摘抄)
    JAVA调用WCF
    RabbitMQ入门与使用篇
    大话程序猿眼里的高并发
  • 原文地址:https://www.cnblogs.com/saolv/p/7614807.html
Copyright © 2011-2022 走看看