zoukankan      html  css  js  c++  java
  • C-C和指针作业题(第一章)

    在Linux下输入EOF使用Ctrl+D, Windows下输入EOF使用Ctrl+Z

    编写一个程序,从标准输入读取几行输入,每行输入都要打印到标准输出上,前面要加上行号,在编写这个程序时要试图让程序能够处理的输入行的长度没有限制。

    #include <stdio.h>
    
    int main(int argc, char **argv) {
        int ch, row_num = 1;
        unsigned char state = 1;
    while ((ch = getchar()) != EOF) { if (state == 1) { printf("%d", row_num); row_num++; state = 0; } putchar(ch); if (ch == ' ') { state = 1; } }return 0; }

    编写一个程序,一行行的读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,可以假定所有的输入行均不超过1000个字符

    顺便练习一下多文件编译

    文件func.h

    #ifndef _FUNC_H
    #define _FUNC_H 1
    int int_max(int a, int b);
    int int_min(int a, int b);
    #endif

    文件func.c

    #include "func.h"
    
    int int_max(int a, int b) {
        return a > b ? a : b;
    }
    
    int int_min(int a, int b) {                                                                                                                       
        return a > b ? b : a;
    }

    文件main.c

    #include <stdio.h>
    #include <string.h>
    #include "func.h"
    
    #define MAX_LENGTH 1000
    
    int main(int argc, char **argv) {
        char max_str[MAX_LENGTH];
        char scanf_str[MAX_LENGTH];
        short max_len = 0;
        while (gets(scanf_str) != NULL) {
            if (strlen(scanf_str) > max_len && max_len < MAX_LENGTH) {
                max_len = int_min(strlen(scanf_str), MAX_LENGTH);
                strcpy(max_str, scanf_str);
            }
        }
        if (max_len > 0) {
            printf("%s
    ", max_str);
        }
        return 0;
    }

    编译:

      gcc -c -o func.o func.c

      gcc -c -o main.o main.c

    连接:

      gcc -o main main.o func.o

    运行:

      ./main

  • 相关阅读:
    java获取web项目下文件夹的路径方法
    The method setCharacterEncoding(String) is undefined for the type HttpServletResponse
    java获取windows和linux下本机ip通用方法
    mysql慢查询日志查找与分析
    struts1 action之间的跳转
    jquery的tap会执行2次的替换办法
    Win7下如何安装切换jdk7和jdk8
    elasticdump
    python hive.py
    Hdfs数据备份
  • 原文地址:https://www.cnblogs.com/JohnABC/p/3530814.html
Copyright © 2011-2022 走看看