#include指令要点

来源:互联网 发布:中行外汇宝软件下载 编辑:程序博客网 时间:2024/05/10 20:57

1.使用#include 指令避免重复声明

一段代码longest.c,代码内容忽略不看

longest.c

#include <stdio.h>#include <stdlib.h>#define MAX_LEN 1001 /* Buffer size for longest line */#include <string.h> /*** Reads lines of input from the standard input and prints the longest line that** was found to the standard output. It is assumed that no line will exceed** 1000 characters.*/int main( void ){freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);char input[ MAX_LEN ];int len;char longest[ MAX_LEN ];int longest_len;/*** Initialize length of the longest line found so far.*/longest_len = -1;/*** Read input lines, one by one.*/while( gets( input ) != NULL ){/*** Get length of this line. If it is longer than the previous** longest line, save this line.*/len = strlen( input );if( len > longest_len ){longest_len = len;strcpy( longest, input );}}/*** If we saved any line at all from the input, print it now.*/if( longest_len >= 0 )puts( longest );return EXIT_SUCCESS;}

可以改变成下面两个源文件util.cpp和longest.c.

util.cpp

#include <stdio.h>

#include <stdlib.h>#define MAX_LEN 1001 /* Buffer size for longest line */#include <string.h> 

longest.c


/*** Reads lines of input from the standard input and prints the longest line that** was found to the standard output. It is assumed that no line will exceed** 1000 characters.*/#include "util.cpp"intmain( void ){freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);char input[ MAX_LEN ];int len;char longest[ MAX_LEN ];int longest_len;/*** Initialize length of the longest line found so far.*/longest_len = -1;/*** Read input lines, one by one.*/while( gets( input ) != NULL ){/*** Get length of this line. If it is longer than the previous** longest line, save this line.*/len = strlen( input );if( len > longest_len ){longest_len = len;strcpy( longest, input );}}/*** If we saved any line at all from the input, print it now.*/if( longest_len >= 0 )puts( longest );return EXIT_SUCCESS;}


2.在#include文件中放置函数原型

原来的代码binaryTree.cpp如下:


#include<iostream>#include "find.cpp"#include "res.cpp"using namespace std;#define N 201int n,m,pre[N];bool useif[N],map[N][N];bool find(int x);int res();int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    int i,j,num,t;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(map,0,sizeof(map));        for(i=1;i<=n;i++)        {        scanf("%d",&num);        for(j=1;j<=num;j++)        {scanf("%d",&t);map[i][t]=1;}        }        int ans=res();        cout<<ans<<endl;    }        return 0;}bool find(int x){int m=0;    int k;    for(k=1;k<=m;k++)    {        if(!useif[k]&&map[x][k])        {            useif[k]=1;            if(pre[k]==-1||find(pre[k]))            {                pre[k]=x;                return 1;            }                    }    }        return 0;}int res(){    int count=0;    memset(pre,-1,sizeof(pre));    for(int i=1;i<=n;i++)    {        memset(useif,0,sizeof(useif));        if(find(i))        count ++;    }        return count;}

此时,把一个源文件转换为三个文件

find.cpp

bool find(int x){int m=0;    int k;    //for(k=1;k<=m;k++)//    {//        if(!useif[k]&&map[x][k])//        {//            useif[k]=1;//            if(pre[k]==-1||find(pre[k]))//            {//                pre[k]=x;//                return 1;//            }//            //        }//    }        return 0;}

res.cpp


bool find(int x){int m=0;    int k;    //for(k=1;k<=m;k++)//    {//        if(!useif[k]&&map[x][k])//        {//            useif[k]=1;//            if(pre[k]==-1||find(pre[k]))//            {//                pre[k]=x;//                return 1;//            }//            //        }//    }        return 0;}


binaryTree.cpp


#include<iostream>#include "find.cpp"#include "res.cpp"using namespace std;#define N 201int n,m,pre[N];bool useif[N],map[N][N];int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    int i,j,num,t;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(map,0,sizeof(map));        for(i=1;i<=n;i++)        {        scanf("%d",&num);        for(j=1;j<=num;j++)        {scanf("%d",&t);map[i][t]=1;}        }        int ans=res();        cout<<ans<<endl;    }        return 0;}



0 0
原创粉丝点击