#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;}
#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
- #include指令要点
- include指令
- include指令
- #include指令
- include 指令
- #include指令
- include指令
- include指令
- include指令
- include指令与include行为
- jsp指令的include指令
- JSP的include指令
- weblogic中的include指令
- 页面包含指令:include
- Jsp中的include指令
- JSP include指令
- IIS 服务器端include指令
- JSP page、include指令
- 静与动:傅里叶变换
- 4点意见,从今天在京东和亚马逊的购物体验谈用户体验设计
- 【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作
- 通向架构师的道路(第七天)之漫谈使用ThreadLocal改进你的层次的划分
- 精通电子邮件的八个核心方法
- #include指令要点
- mysql中显示当前使用的数据库名称
- 通向架构师的道路(第八天)之weblogic与apache的整合与调优
- Linux Cache 机制
- 定义serialVersionUID的作用与意义整理
- 从MySQL Bug#67718浅谈B+树索引的分裂优化
- Java应用crash的可能原因-java_errorpid.log
- 一致性hash算法在memcache集群中的应用
- 转载--matlabcell中元素的删除