#include指令要点

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"
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;
}


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

原来的代码binaryTree.cpp如下:


#include<iostream>
#include "find.cpp"
#include "res.cpp"
using namespace std;
#define N 201
int 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 201
int 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;
}



#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <time.h> #define QUECTEL_TIME_MAX_LEN 128 int main(int argc, char *argv[]) { int connectfd = -1; int connfd = -1; char buff[QUECTEL_TIME_MAX_LEN]; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int n; /*1.socket*/ connectfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(connectfd < 0) { printf("create socket error :%s %d",strerror(errno), errno); exit(0); } memset(&server_addr, 0, sizeof(struct sockaddr_in)); memset(&client_addr, 0, sizeof(struct sockaddr_in)); client_addr.sin_family=AF_INET;//指定IPV4协议族 client_addr.sin_addr.s_addr=htonl(INADDR_ANY);//可以使用本地任何IP去连接 client_addr.sin_port=htons(9981);//本地端口号使用为9981 /*2.bind*/ if(bind(connectfd, (struct sockaddr *)(&client_addr), sizeof(client_addr )) == -1) { printf("bind error:%s %d",strerror(errno), errno); exit(0); } server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr= inet_addr("10.0.2.15"); server_addr.sin_port=htons(9980); /*3.connect*/ if(connect(connectfd, (struct sockaddr *)(&server_addr),sizeof(server_addr)) == -1) { printf("conect error:%s %d",strerror(errno), errno); exit(0); } while( (n = read(connectfd, buff, QUECTEL_TIME_MAX_LEN))) { buff[n] = 0; printf("%s\r\n",buff); } if(n < 0) { printf("recv error"); } close(connectfd); return 0; }
03-12
### 如何使用Arduino IRsend库进行红外信号发送 为了利用Arduino Uno板通过`IRsend`库来发送红外信号,需先了解该过程涉及的主要组件以及具体操作流程。 #### 准备工作 确保已安装了支持红外通信的`IRremote`库[^3]。此库不仅包含了用于接收红外信号的功能,还提供了发送红外命令所需的工具——即`IRsend`类。通常情况下,默认设置会指定数字引脚3作为红外发射管连接的位置;不过,在实际应用中可以根据需求调整这一设定。 #### 发送红外信号的基础代码结构 下面展示了一个简单的例子,说明怎样配置并调用`IRsend`对象以发出特定编码形式的数据包给目标设备: ```cpp #include <IRremote.h> const uint16_t kIrLed = 3; // 定义用来驱动红外LED的GPIO编号 IRsend irsend(kIrLed); void setup() { // 初始化串口监视器以便调试输出 Serial.begin(9600); } void loop() { if (Serial.available()) { char inputChar = Serial.read(); switch(inputChar){ case 'a': sendNECCode(0x2FD8778F); // 假设这是空调遥控的一个按键对应的十六进制码值 break; default: ; } } delay(50); // 防止连续读取错误字符 } // 自定义函数:向外部设备传输NEC协议格式下的指令集 void sendNECCode(unsigned long data) { irsend.sendNEC(data, 32); // 参数二表示地址位宽 } ``` 这段程序展示了如何创建一个可以响应来自计算机终端输入字母'a'的动作触发事件,并据此执行一次针对某品牌空调产品的控制动作模拟。这里采用的是NEC标准编码方案之一,适用于许多家用电器产品线中的远程控制系统设计。 #### 关键要点解析 - **初始化阶段**:引入必要的头文件后声明全局变量`kIrLed`指明物理层面上与红外发光二极管相连的具体端口号。接着实例化名为`irsend`的对象负责处理后续所有的数据帧构建任务。 - **主循环逻辑**:监听是否有新的ASCII字符到达缓冲区,一旦检测到匹配项就立即激活相应的子例程完成预设的操作序列。 - **封装好的辅助方法**:最后定义了一套独立的小型服务接口供内部调用者按需选用不同类型的脉冲宽度组合模式(如RC5/6、Sony等),从而满足更广泛的应用场景要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值