(5) 外部变量、头文件、多个文件
 
//File:     extvar.h
#ifndef _EXTVAR_H
#define  _EXTVAR_H

#ifdef  __cplusplus
extern "C" {
#endif

#define MAXLINE 1000
int Max = 0;
char LongestLine[MAXLINE] = "";

#ifdef  __cplusplus
}
#endif

#endif  /* _EXTVAR_H */
 
//getLine.c
#include<stdio.h>
int getLine(char tmpStr[]){
        int ch, i = 0;
        while((ch = getchar()) != EOF && ch != '\n'){
                tmpStr[i++] = ch;
        }
        tmpStr[i] = '\0';
        return i;
}
 
//copy.c
void copy(char target[], const char source[]){
        int i;
        for(i = 0; source[i] != '\0'; i++){
                target[i] = source[i];
        }
        target[i] = '\0';
}
 
#include <stdio.h>
#include <stdlib.h>
#include "extvar.h"
/*
注意:程序中,MAXLINE,Max,LongestLine定义在extvar.h中
*/

int main(int argc, char** argv) {
        char tmpStr[MAXLINE];
        int length;
        while((length = getLine(tmpStr)) > 0){//调用getLine函数
                if(length > Max){
                        Max =    length;
                        copy(LongestLine,tmpStr);//调用copy函数
                }
        }
        printf("The Max Length is: %d\n", Max);
        printf("%s\n", LongestLine);
        return (EXIT_SUCCESS);
}

(4)字符数组:求最长的行
 
#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
/*
字符串拷贝
*/

void copy(char target[], const char source[]){//从source拷贝到target字符数组
        int i = 0;
        while(source[i] != '\0')
                target[i] = source[i++];//i++ 自增,向后挪
        target[i] = '\0';//在target字符串的末尾加上'\0'
}
/*
获取最长行,并返回其长度
*/

int getLongestLine(char longestLine[]){//用字符数组做参数,实质上传地址
        char tmpLine[MAX];
        char ch;
        int maxLength, i;

        maxLength = i = 0;
        while((ch = getchar()) != EOF){
                if(ch != '\n') tmpLine[i++] = ch;    
                else {
                        tmpLine[i] = '\0';
                        if(maxLength < i){//是否出现了新的最长行
                                maxLength = i;
                                copy(longestLine, tmpLine);//copy的定义在前,调用在后
                        }
                        i = 0;//让tmpLine行重新回到行首
                }
        }
        return maxLength;
}

int main(int args, char** argv){
        char longestLine[MAX];
        printf("Please input several lines end with Ctrl+D:\n");
        int length = getLongestLine(longestLine);//数组名作参数,传地址
        printf("length = %d, line = %s\n", length, longestLine);
        return EXIT_SUCCESS;
}
 

(3)用数组统计每个'0'~'9'之间每个字符出现的个数
 
/*
用数组统计每个'0'~'9'之间每个字符出现的个数
* 而不是用10个变量来记忆
*/

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
        int ch, i, nwhite, nother;
        char ndigit[10];

        nwhite = nother = 0;
        for(i = 0; i < 10; i++)
                ndigit[i] = 0;

        while((ch = getchar()) != EOF){
                if(ch <= '9' && ch >= '0') //判断ch是否满足'0'<ch<'9'
                        ndigit[ch-'0']++;    //正好可以定位到ch[x]的下标
                else if(ch == ' ' || ch == '\t' || ch == '\n')
                        nwhite++;
                else
                        nother++;
        }

        for(i = 0; i < 10; i++)
                printf("%d's num is: %d\n", i, ndigit[i]);

        printf("number of white is: %d\n", nwhite);
        printf("number of other is: %d\n", nother);
        
        return EXIT_SUCCESS;
}
 

(2) 字符输入输出及分析统计
 
#include <stdio.h>
#include <stdlib.h>
#define OUT 0
#define IN    1
/*
计数空格、tab、行、字的数目
*/

void count(){
        long nch;        
        int nblanks, ntabs, nlines, nwords, ch;     //声明
        int state;
        nch = 0;
        nblanks = ntabs = nlines = nwords = 0;                        //连续赋初值,从右向左
        state = OUT;
        
        printf("\nWorld Counting");
        printf("\nPlease input a paragraph with several lines end with Ctrl+D:\n");

        while((ch = getchar()) != EOF){
                ++nch;
                if(ch == ' ') nblanks++;
                else if(ch == '\t') ntabs++;
                else if(ch == '\n') nlines++;
                //统计字
                if(ch == ' ' || ch == '\t' || ch == '\n') // ||或,从左到右
                        state = OUT;
                else if(state != IN){
                        state = IN;
                        ++nwords;
                }
        }
        printf("\nnch =%ld, nblanks = %d, ntabs = %d, nlines = %d, nwords = %d\n",
                        nch, nblanks, ntabs, nlines, nwords);//%ld表示长×××
}
/*
获取输入,去掉多余的空格,然后输出
*/

void removeBlank21(){
        int ch, nblank;
        nblank = 0;
        while((ch = getchar()) != EOF){
                if(ch != ' ') {
                        putchar(ch);
                        nblank = 0;
                }else if(nblank == 0) {
                        putchar(ch);
                        nblank++;
                }
        }
}
/*
每行打印一个word
*/

void wordPerLine(){
        printf("\nPlease input several strings end with Ctrl+d\n");
        int ch;
        while((ch = getchar()) != EOF){
                if(ch == ' ' || ch == '\t')
                        putchar('\n');
                else putchar(ch);
        }
}

int main(int argc, char** argv) {
        //count();
        //removeBlank21();
        //wordPerLine();
        printf("\n%d\t%d\n",'A','\n');    //打印字符'A'(65)和'\n'(10)的ASCII码
        return (EXIT_SUCCESS);
}
 
 

(1) 温度对应表
 
#include <stdio.h>
#include <stdlib.h>

#define LOWER     0                 //符号常量
#define UPPER     300
#define STEP        20
int main(int argc, char** argv) {
        int ftemp;
        float ctemp;
/*                                             //注释
        int lower, upper, step;
        lower = 0;
        upper = 300;
        step = 20;
*/

        printf("================================\n");
        ftemp = LOWER;
        while(ftemp <= UPPER){                            // /除符号,注意整除与float除
                ctemp = 5 * (ftemp - 32) / 9.0;//int 转换为 float
                printf("%3d\t%5.2f\n", ftemp, ctemp); //%3d宽度为3,右对齐
                ftemp = ftemp + STEP;
        }
        printf("================================\n");
        for(ftemp = LOWER; ftemp <= UPPER; ftemp += STEP){
                ctemp = 5 * (ftemp - 32) / 9.0;         //
                printf("%3d\t%6.1f\n", ftemp, ctemp); //%6.1f宽度为6,小数点后一位有效数字
        }
        printf("================================\n");
        for(ftemp = LOWER; ftemp <= UPPER; ftemp += STEP)
                printf("%5d %6.2f\n", ftemp, (5.0/9)*(ftemp-32));//表达式也可以错printf参数
         printf("================================\n");
        return (EXIT_SUCCESS);    
}