HDU 2203 strstr()函数应用

本文介绍了一种特殊的字符串匹配方法,通过扩展原始字符串并使用strstr函数来判断一个字符串是否为另一个字符串的子串。此方法适用于需要高效查找子串的场景。

http://acm.hdu.edu.cn/showproblem.php?pid=2203

/*

char* strstr(const char* a,constchar* b)  在a中寻找第一次出现b的位置

如果出现,返回位置,如果不出现,返回空指针;

利用这一特点,我们将s1字符串长度加长一部分(s2的长度),加长的内容为s1的前strlen(s2)个字符;

最后用函数判断s2是否在s1中出现;

over~~

*/


	#include <iostream>
	#include <cstdio>
	#include <cstring>
	#include <algorithm>
	#include <cmath>
	#include <string>
	#include <iomanip>
	#define maxn 100005
	using namespace std;
	char s1[2*maxn],s2[maxn];
	int main(int argc, char *argv[])
	{
		while(scanf("%s%s",&s1,&s2)!=EOF)
		{
			int len1 = strlen(s1),len2 = strlen(s2);
			if(len2>len1)
			{
				puts("no");
				continue;
			}
			for(int i = len1,j = 0; i < len1+len2; i++)
			{
				s1[i] = s1[j++];
			}
			
			len1 = len1+len2;
			
			if(strstr(s1,s2)!=NULL)
			{
				puts("yes");
			}
			else
			{
				puts("no");
			}
		}
		return 0;
	}


static void parse_gprmc(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPRMC"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 状态检查 (A=有效, V=无效) pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF && *(p + pos) == 'V') { gps->fixmode = 0; // 无效定位 return; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->ewhemi = *(p + pos); } // 完整GPGGA解析 static void parse_gpgga(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPGGA"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) gps->ewhemi = *(p + pos); // 定位质量 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->fixmode = nmea_str2num((const uint8_t *)(p + pos), NULL); // 卫星数 pos = nmea_comma_pos((const uint8_t *)p, 7); if (pos != 0xFF) gps->posslnum = nmea_str2num((const uint8_t *)(p + pos), NULL); // 海拔高度 pos = nmea_comma_pos((const uint8_t *)p, 9); if (pos != 0xFF) gps->altitude = nmea_str2num((const uint8_t *)(p + pos), &dx) / 10.0f; } // 主解析函数 void gps_parse_nmea(nmea_msg *gps, const char *buf) { // 校验和验证 const char *end = strchr(buf, '*'); if (end) { uint8_t calc_check = nmea_str2num(buf); uint8_t recv_check = strtoul(end + 1, NULL, 16); if (calc_check != recv_check) { return; // 校验失败 } } if (strstr(buf, "GPRMC")) parse_gprmc(gps, buf); else if (strstr(buf, "GPGGA")) parse_gpgga(gps, buf); } // 显示数据 void gps_show_data(const nmea_msg *gps) { if (gps->fixmode == 0) { printf("No valid GPS fix\n"); return; } printf("Longitude: %.6f° %c\n", gps->longitude, gps->ewhemi); printf("Latitude: %.6f° %c\n", gps->latitude, gps->nshemi); printf("Altitude: %.1f meters\n", gps->altitude); printf("UTC Time: %02d:%02d:%02d\n", gps->utc.hour, gps->utc.min, gps->utc.sec); printf("Satellites: %d\n", gps->posslnum); printf("Fix mode: %s\n", gps->fixmode == 1 ? "GPS" : gps->fixmode == 2 ? "DGPS" : "Unknown"); } 修过后该处显示以下错误如何修改[OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c: In function 'gps_parse_nmea': [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:43: warning: pointer targets in passing argument 1 of 'nmea_str2num' differ in signedness [-Wpointer-sign] [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: expected 'const uint8_t * {aka const unsigned char *}' but argument is of type 'const char *' [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:30: error: too few arguments to function 'nmea_str2num' [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: declared here [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] you can check build log in D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus\build.log [OHOS ERROR] command: "D:\hi3861\hi3861_hdu_iot_application\src\prebuilts\build-tools\win64-x64\bin\ninja.exe -w dupbuild=warn -C D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: D:\hi3861\hi3861_hdu_iot_application\src scons: *** [src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf] Error -1
最新发布
07-07
[OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c: In function 'gps_init': [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:115:5: error: unknown type name 'hi_uart_attribute'; did you mean '__has_attribute'? [OHOS ERROR] hi_uart_attribute config = { [OHOS ERROR] ^~~~~~~~~~~~~~~~~ [OHOS ERROR] __has_attribute [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:116:9: error: field name not in record or union initializer [OHOS ERROR] .baud_rate = 9600, // NEO-6M默认波特率 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:116:9: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:9: error: field name not in record or union initializer [OHOS ERROR] .data_bits = HI_UART_DATA_BIT_8, // 8位数据位 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:9: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:22: error: 'HI_UART_DATA_BIT_8' undeclared (first use in this function); did you mean 'HI_ERR_DMA_BUSY'? [OHOS ERROR] .data_bits = HI_UART_DATA_BIT_8, // 8位数据位 [OHOS ERROR] ^~~~~~~~~~~~~~~~~~ [OHOS ERROR] HI_ERR_DMA_BUSY [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:22: note: each undeclared identifier is reported only once for each function it appears in [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:22: warning: excess elements in scalar initializer [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:117:22: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:118:9: error: field name not in record or union initializer [OHOS ERROR] .stop_bits = HI_UART_STOP_BIT_1, // 1位停止位 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:118:9: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:118:22: error: 'HI_UART_STOP_BIT_1' undeclared (first use in this function); did you mean 'HI_UART_DATA_BIT_8'? [OHOS ERROR] .stop_bits = HI_UART_STOP_BIT_1, // 1位停止位 [OHOS ERROR] ^~~~~~~~~~~~~~~~~~ [OHOS ERROR] HI_UART_DATA_BIT_8 [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:118:22: warning: excess elements in scalar initializer [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:118:22: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:119:9: error: field name not in record or union initializer [OHOS ERROR] .parity = HI_UART_PARITY_NONE, // 无校验 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:119:9: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:119:19: error: 'HI_UART_PARITY_NONE' undeclared (first use in this function); did you mean 'HI_UART_DATA_BIT_8'? [OHOS ERROR] .parity = HI_UART_PARITY_NONE, // 无校验 [OHOS ERROR] ^~~~~~~~~~~~~~~~~~~ [OHOS ERROR] HI_UART_DATA_BIT_8 [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:119:19: warning: excess elements in scalar initializer [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:119:19: note: (near initialization for 'config') [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:120:9: error: field name not in record or union initializer [OHOS ERROR] .fifo_enable = HI_TRUE // 启用FIFO [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:120:9: note: (near initialization for 'config') [OHOS ERROR] In file included from ../../../device/hisilicon/hispark_pegasus/sdk_liteos/include/hi_io.h:26:0, [OHOS ERROR] from ../../../vendor/pzkj/pz_hi3861/common/bsp/include/bsp_gps.h:6, [OHOS ERROR] from ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:1: [OHOS ERROR] ../../../device/hisilicon/hispark_pegasus/sdk_liteos/include/hi_types_base.h:284:26: warning: excess elements in scalar initializer [OHOS ERROR] #define HI_TRUE 1 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:120:24: note: in expansion of macro 'HI_TRUE' [OHOS ERROR] .fifo_enable = HI_TRUE // 启用FIFO [OHOS ERROR] ^~~~~~~ [OHOS ERROR] ../../../device/hisilicon/hispark_pegasus/sdk_liteos/include/hi_types_base.h:284:26: note: (near initialization for 'config') [OHOS ERROR] #define HI_TRUE 1 [OHOS ERROR] ^ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:120:24: note: in expansion of macro 'HI_TRUE' [OHOS ERROR] .fifo_enable = HI_TRUE // 启用FIFO [OHOS ERROR] ^~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:124:18: warning: implicit declaration of function 'hi_uart_init'; did you mean 'uart0_init'? [-Wimplicit-function-declaration] [OHOS ERROR] hi_u32 ret = hi_uart_init(HI_UART_IDX_2, &config); [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] uart0_init [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:124:31: error: 'HI_UART_IDX_2' undeclared (first use in this function); did you mean 'HI_GPIO_IDX_2'? [OHOS ERROR] hi_u32 ret = hi_uart_init(HI_UART_IDX_2, &config); [OHOS ERROR] ^~~~~~~~~~~~~ [OHOS ERROR] HI_GPIO_IDX_2 [OHOS ERROR] you can check build log in D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus\build.log [OHOS ERROR] command: "D:\hi3861\hi3861_hdu_iot_application\src\prebuilts\build-tools\win64-x64\bin\ninja.exe -w dupbuild=warn -C D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: D:\hi3861\hi3861_hdu_iot_application\src scons: *** [src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf] Error -1请根据该错误对VSCODE以下代码进行修改#ifndef BSP__GPS_H__ #define BSP__GPS_H__ #include "cmsis_os2.h" #include "hi_io.h" #include "hi_gpio.h" #include <stdio.h> #include <string.h> #include "bsp_uart.h" #include "hi_errno.h" // GPS 数据结构定义 typedef struct { float longitude; // 经度 (度) float latitude; // 纬度 (度) char nshemi; // 北纬/南纬 (N/S) char ewhemi; // 东经/西经 (E/W) struct { uint8_t hour; // UTC 小时 uint8_t min; // UTC 分钟 uint8_t sec; // UTC 秒 } utc; uint8_t fixmode; // 定位模式 uint8_t posslnum; // 参与定位的卫星数 float altitude; // 海拔高度 (米) } nmea_msg; // 定义UART2引脚配置 #define GPS_UART_IDX HI_UART_IDX_2 #define GPS_BAUDRATE 9600 // NEO-6M默认波特率 // 函数声明 void gps_init(void); void gps_parse_nmea(nmea_msg *gps, const char *buf); void gps_show_data(const nmea_msg *gps); #endif#include "bsp_gps.h" #include <stdlib.h> #include "bsp_uart.h" // 添加必要的UART头文件 // 查找逗号位置 // 计算m的n次方 static uint32_t NMEA_Pow(uint8_t m, uint8_t n) { uint32_t result = 1; while (n--) result *= m; return result; } // 查找逗号位置(修改为接受const指针) uint8_t nmea_comma_pos(const uint8_t *buf, uint8_t cx) { const uint8_t *p = buf; while (cx) { if (*buf == '*' || *buf < ' ' || *buf > 'z') return 0xFF; // 错误终止符 if (*buf == ',') cx--; buf++; } return buf - p; } // 字符串转数字(修改为接受const指针) int nmea_str2num(const uint8_t *buf, uint8_t *dx) { const uint8_t *p = buf; uint32_t ires = 0, fres = 0; uint8_t ilen = 0, flen = 0, i; uint8_t mask = 0; int res; while (1) { if (*p == '-') { mask |= 0x02; p++; } // 负数标记 if (*p == ',' || *p == '*') break; // 结束符 if (*p == '.') { mask |= 0x01; p++; } // 小数点 else if (*p > '9' || *p < '0') { // 非法字符 ilen = 0; flen = 0; break; } if (mask & 0x01) flen++; else ilen++; p++; } if (mask & 0x02) buf++; // 跳过负号 for (i = 0; i < ilen; i++) // 整数部分 ires += NMEA_Pow(10, ilen - 1 - i) * (buf[i] - '0'); if (flen > 5) flen = 5; // 最多5位小数 if (dx) *dx = flen; // 安全处理空指针 for (i = 0; i < flen; i++) // 小数部分 fres += NMEA_Pow(10, flen - 1 - i) * (buf[ilen + 1 + i] - '0'); res = ires * NMEA_Pow(10, flen) + fres; if (mask & 0x02) res = -res; return res; } // 解析 GPRMC 语句 static void parse_gprmc(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPRMC"); if (!p) return; // 显式类型转换解决警告(函数参数已改为const uint8_t*) uint8_t pos = nmea_comma_pos((const uint8_t *)p, 1); // UTC 时间 if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), NULL); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } pos = nmea_comma_pos((const uint8_t *)p, 3); // 纬度 if (pos != 0xFF) { float lat = nmea_str2num((const uint8_t *)(p + pos), NULL) / 100000.0f; gps->latitude = (int)(lat / 100) + (lat - (int)(lat / 100) * 100) / 60.0f; } pos = nmea_comma_pos((const uint8_t *)p, 4); // 纬度半球 if (pos != 0xFF) gps->nshemi = *(p + pos); pos = nmea_comma_pos((const uint8_t *)p, 5); // 经度 if (pos != 0xFF) { float lon = nmea_str2num((const uint8_t *)(p + pos), NULL) / 100000.0f; gps->longitude = (int)(lon / 100) + (lon - (int)(lon / 100) * 100) / 60.0f; } pos = nmea_comma_pos((const uint8_t *)p, 6); // 经度半球 if (pos != 0xFF) gps->ewhemi = *(p + pos); } // 主解析函数 void gps_parse_nmea(nmea_msg *gps, const char *buf) { if (strstr(buf, "$GPRMC")) parse_gprmc(gps, buf); // 修正:移除多余空格 // 可扩展其他语句解析 (如GPGGA, GPGSA等) } void gps_init(void) { // 初始化GPIO子系统 (必须首先调用) hi_gpio_init(); // ===== 配置TX引脚 (GPIO11 - 输出) ===== hi_io_set_pull(HI_IO_NAME_GPIO_11, HI_IO_PULL_UP); // 上拉模式 hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD); // UART TX功能 hi_gpio_set_dir(HI_IO_NAME_GPIO_11, HI_GPIO_DIR_OUT); // 输出方向 // ===== 配置RX引脚 (GPIO12 - 输入) ===== hi_io_set_pull(HI_IO_NAME_GPIO_12, HI_IO_PULL_NONE); // 浮空模式(推荐) hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD); // UART RX功能 hi_gpio_set_dir(HI_IO_NAME_GPIO_12, HI_GPIO_DIR_IN); // 输入方向 // ===== UART参数配置 ===== hi_uart_attribute config = { .baud_rate = 9600, // NEO-6M默认波特率 .data_bits = HI_UART_DATA_BIT_8, // 8位数据位 .stop_bits = HI_UART_STOP_BIT_1, // 1位停止位 .parity = HI_UART_PARITY_NONE, // 无校验 .fifo_enable = HI_TRUE // 启用FIFO }; // 初始化UART2 hi_u32 ret = hi_uart_init(HI_UART_IDX_2, &config); if (ret != HI_ERR_SUCCESS) { printf("GPS UART init failed! Error: 0x%X\n", ret); } } // 显示GPS数据 void gps_show_data(const nmea_msg *gps) { printf("Longitude: %.5f%c\n", gps->longitude, gps->ewhemi); printf("Latitude: %.5f%c\n", gps->latitude, gps->nshemi); printf("UTC Time: %02d:%02d:%02d\n", gps->utc.hour, gps->utc.min, gps->utc.sec); printf("Satellites: %d\n", gps->posslnum); printf("Altitude: %.1fm\n", gps->altitude); }
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值