【C/C++学院】0729-语音识别/Const关键字/字符串应用/内存分配以及处理海量数据

简介: <div style="top:0px"></div> <h1>语音识别</h1> <p>er.xml</p> <pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?><GRAMMAR LANGID="804"> <DEFINE> <ID NAM


语音识别

er.xml

<?xml version="1.0" encoding="utf-8"?>
<GRAMMAR LANGID="804">
  <DEFINE>
    <ID NAME="CMD" VAL="10"/>
    </DEFINE>
  <RULE NAME="COMMAND" ID="CMD" TOPLEVEL="ACTIVE">
    <L>
      <P>资源管理器</P>
      <P>打开企鹅</P>
      <P>关闭企鹅</P>
      <P>关机</P>
      <P>重启</P>
      <P>记事本</P>
      <P>计算器</P>
      <P>画图板</P>
     <P>谭胜</P>
      </L>
    </RULE>
  </GRAMMAR>

yuyin.cpp

#include <windows.h>
#include <atlstr.h>
#include <sphelper.h>
#include <sapi.h>
#include<comutil.h>
#include<string.h>
#include<stdlib.h>

#pragma comment(lib,"sapi.lib")
#pragma comment(lib, "comsupp.lib") 

#define GID_CMD_GR 333333
#define WM_RECOEVENT WM_USER+1

 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

 char 	 szAppName[] = "TsinghuaYincheng";
 BOOL b_initSR;
 BOOL b_Cmd_Grammar;
 CComPtr<ISpRecoContext>m_cpRecoCtxt;  //语音识别程序接口
 CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
 CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎


 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
 {
	 HWND        hwnd;
	 MSG         msg;
	 WNDCLASS    wndclass;

	 wndclass.cbClsExtra          =0;
	 wndclass.cbWndExtra          =0;
	 wndclass.hbrBackground       =(HBRUSH)GetStockObject(WHITE_BRUSH);
	 wndclass.hCursor             =LoadCursor(NULL,IDC_ARROW);
	 wndclass.hIcon               =LoadIcon(NULL,IDI_APPLICATION);
	 wndclass.hInstance           =hInstance;
	 wndclass.lpfnWndProc         =WndProc;
	 wndclass.lpszClassName       =szAppName;
	 wndclass.lpszMenuName        =NULL;
	 wndclass.style               =CS_HREDRAW|CS_VREDRAW;

	 if(!RegisterClass(&wndclass))
	 {
		 MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
		 return 0;
	 }

	 hwnd=CreateWindow(szAppName,
		               TEXT("传智播客C/C++学院语音识别教程"),
					   WS_OVERLAPPEDWINDOW,
					   CW_USEDEFAULT,
					   CW_USEDEFAULT,
					   CW_USEDEFAULT,
					   CW_USEDEFAULT,
					   NULL,
					   NULL,
					   hInstance,
					   NULL);

	 ShowWindow(hwnd,iCmdShow);
	 UpdateWindow(hwnd);
	 
	 while(GetMessage(&msg,NULL,0,0))
	 {
		 TranslateMessage(&msg);
		 DispatchMessage(&msg);
	 }
	 return msg.wParam;
 }

 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
 {
	 HDC           hdc;
	 PAINTSTRUCT   ps;

	 switch(message)
	 {
	 case WM_CREATE:
		 {
			 //初始化COM端口
			 ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
			 //创建识别引擎COM实例为共享型
			 HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
			 //创建识别上下文接口
			 if(SUCCEEDED(hr))
			 {
				 hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
			 }
			 else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
			 //设置识别消息,使计算机时刻监听语音消息
			 if(SUCCEEDED(hr))
			 {
				 hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
			 }
			 else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
			 //设置我们感兴趣的事件
			 if(SUCCEEDED(hr))
			 {
				 ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
				 hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
			 }
			 else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
			 //创建语法规则
			 b_Cmd_Grammar=TRUE;
			 if(FAILED(hr))
			 {
				 MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
			 }
			 hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
			 WCHAR wszXMLFile[20]=L"yuyinliebiao.xml";
			 MultiByteToWideChar(CP_ACP,0,(LPCSTR)"yuyinliebiao.xml",-1,wszXMLFile,256);
			 hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
			 if(FAILED(hr))
			 {
				 MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
			 }
			 b_initSR=TRUE;
			 //在开始识别时,激活语法进行识别
		     hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
	    	 return 0;
		 }
	 case WM_RECOEVENT:
		 {
			 RECT rect;
             GetClientRect(hwnd,&rect);
             hdc=GetDC(hwnd);
			 USES_CONVERSION;
			 CSpEvent event;
			 while(event.GetFrom(m_cpRecoCtxt)==S_OK)
			 {
			     switch(event.eEventId)
			     {
			     case SPEI_RECOGNITION:
    				 {
            			 static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
		            	 CSpDynamicString dstrText;
			    		 //取得识别结果
				    	 if(FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
					     {
						     dstrText=wszUnrecognized;
    					 }
        	    		 BSTR SRout;
	        	    	 dstrText.CopyToBSTR(&SRout);
						 char* lpszText2 = _com_util::ConvertBSTRToString(SRout);

			    		 if(b_Cmd_Grammar)
				    	 {
							 if (strstr("资源管理器",lpszText2)!=NULL)
	    				     {    
								
								  system("C:\\");
								
				        		  DrawText(hdc,TEXT("资源管理器"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
					         }
							  if (strstr("打开企鹅",lpszText2)!=NULL)
	    				     {    
								
								  system("\"C:\\Program Files\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe\"");
				        		  DrawText(hdc,TEXT("打开企鹅"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
					         }
							  if (strstr("关闭企鹅", lpszText2) != NULL)
							  {
								  system("taskkill /f /im QQ.exe");
								  DrawText(hdc, TEXT("关闭企鹅"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("关机", lpszText2) != NULL)
							  {
								  system("shutdown -s -t 1200");
								  DrawText(hdc, TEXT("关机"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("重启", lpszText2) != NULL)
							  {
								  system("shutdown -r -t 1200");
								  DrawText(hdc, TEXT("重启"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("记事本", lpszText2) != NULL)
							  {
								  system("notepad");
								  DrawText(hdc, TEXT("记事本"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("计算器", lpszText2) != NULL)
							  {
								  system("calc");
								  DrawText(hdc, TEXT("计算器"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("画图板", lpszText2) != NULL)
							  {
								  system("mspaint");
								  DrawText(hdc, TEXT("画图板"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }
							  if (strstr("谭胜", lpszText2) != NULL)
							  {

								  DrawText(hdc, TEXT("这是一个猥琐男"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
							  }


						
						
    					 }    
        			 }
	    		 }
			 }
			 return TRUE;
		 }
	 case WM_PAINT:
		 hdc=BeginPaint(hwnd,&ps);
		 EndPaint(hwnd,&ps);
		 return 0;
	 case WM_DESTROY:
		 PostQuitMessage(0);
		 return 0;
	 }
	 return DefWindowProc(hwnd,message,wParam,lParam);
 }

Const关键字


Const int *p; int const *p;


const *地址不可以修改


int *const p;


*const指向的数据不可修改

#include <stdio.h>

void main01()
{
	int num = 10;
	//const int data;
	//data = 20;
	const int data = 20;//const 修饰的变量,只能在初始化的时候进行赋值操作
	getchar();
}

//const在*左侧,地址本身不可以修改,可以改变指针指向。只能读不能写
void main02()
{
	int num = 10;
	const int data = 20;
	//const int *p;//等价于int const *p;
	int const *p;
	p = #
	printf("%d\n", *p);
	p = &data;
	printf("%d\n", *p);
	//*p = 30;//

	getchar();
}
//const在*右侧,指向的数据不可修改
void main()
{
	int num = 10;
	const int data = 20;
	int *const p = #//只能在初始化的时候,指定唯一的指向

	//int *const p;
	//p = #
	printf("%d\n", *p);
	//p = &data;
	*p = 30;
	printf("%d\n", *p);
	getchar();
}

void main04()
{
	int num = 10;
	const int data = 20;
	const int *const p = #
	printf("%d\n", *p);
	//*p = 30;
	//p = &data;

	getchar();
}

字符串应用

#define  _CRT_SECURE_NO_WARNINGS //关闭安全检查
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <Windows.h>

void main1()
{
	//char str[100] = "for /l %i in (1,1,5) do calc";
	//char *p = "for /l %i in (1,1,5) do calc";
	//str[0] = ' '; str是数组,存储的是字符串每一个字符
	//*p = ' ';p是一个指针,存储字符串的地址

	char str[100] = { 0 };
	//printf("for /l %%i in (1,1,%d) do %s",5,"calc");
	int num;
	char op[30] = { 0 };
	scanf("%d%s", &num, op);
	sprintf(str, "for /l %%i in (1,1,%d) do %s", num, op);
	system(str);

	system("pause");
}

void main2()
{
	char str[100] = { 0 };
	char op[30] = { 0 };
	scanf("%s", op);
	sprintf(str, "taskkill /f /im %s", op);
	system(str);

	system("pause");
}

void main3()
{
	system("tasklist");
	for (int i = 0x0; i <= 0xf; i++)
	{
		char str[30] = { 0 };//存储指令
		sprintf(str, "color %x%x", i,0xf-i);//打印指令
		system(str);//变色
		Sleep(500);
	}
}

void  main4()
{
	char str[100] = "我有1000元";
	int num;
	sscanf(str, "我有%d元", &num);
	printf("%d", num);

	system("pause");
}

void main5()
{
	//printf("%.6s\n", "1234.abcd");
	char str[100] = "notepad8888";
	char newstr[100] = "";
	sprintf(newstr, "%.s", str);
	system(newstr);

	system("pause");
}

void  main6()
{
	char str1[100] = "note";
	char str2[100] = "pad";
	//system(str1 + str2);
	char str3[100] = { 0 };
	sprintf(str3, "%s%s", str1, str2);//字符串相加
	system(str3);
}

void main7()
{
	//printf("%.6s", "1234.abcd");
	char str[100] = "notepad8888";
	char newstr[100] = "";
	sprintf(newstr, "%-10.7s", str);//10宽度,+宽度范围内右边对齐,-左边对齐,.7从左往右挖去7个字符
	printf("[%s]", newstr);
	system(newstr);

	system("pause");
}

int mystrlen(char* str)
{
	int i = 0;
	for (char*p = str; *p != '\0'; p++)
	{
		i++;
	}
	return i;//实现字符串统计
}

int mystrlenA(char* str)
{
	char *p = str;
	int i = 0;
AAA: if (*p!='\0')
{
		 i++;
		 p++;
		 goto AAA;
}
	 return i;
}

int mystrlenB(char *str)
{
	if (*str=='\0')
	{
		return 0;
	}
	else
	{
		return 1 + mystrlenB(++str);
	}
}

void  main8()
{
	char str[100] = "china is good";
	printf("%d", sizeof(str));//求数组缓冲区长度
	printf("\n%d", mystrlen(str));//从开头第一个字符到\0字符
	printf("\n%d", mystrlenA(str));//从开头第一个字符到\0字符
	printf("\n%d", mystrlenB(str));//从开头第一个字符到\0字符
	getchar();
}

void main9()
{
	char str1[100] = "my name is yincheng";
	char str2[30] = "chen";
	char *p = strstr(str1, str2);
	if (p == NULL)
	{
		printf("没有找到");
	}
	else
	{
		printf("找到%p,%c, %s", p, *p, p);
	}
	getchar();
}


内存分配以及处理海量数据

#include<stdio.h>
#include<stdlib.h>

// m, n生成一个数组,m行 n列
void main()
{
	int *p =(int*) malloc(sizeof(int)* 40);//一维数组
	for (int *px = p,i=0; px < p + 40; px++,i++)//
	{
		*px = i;//赋值
		printf("%d,%p\n", *px, px);//指针循环
	}
	//int b[5][8];
	printf("\n\n\n");
	int(*pp)[8] = (int(*)[8]) p;//指针类型决定了访问的方式
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			//printf("%5d", pp[i][j]);//打印数据
			printf("%5d", *(*(pp + i) + j));// pp[i][j]
		}
		printf("\n");
	}
	//a [4][2][5]
	printf("\n\n\n");
	int(*ppp)[2][5] = (int(*)[2][5])p;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			for (int k = 0; k < 5; k++)
			{
				//printf("%5d", ppp[i][j][k]);//打印元素
				printf("%5d", *(*(*(ppp + i) + j) + k));
			}
			printf("\n");
		}
		printf("\n\n\n");
	}

	system("pause");
}


#define  _CRT_SECURE_NO_WARNINGS //关闭安全检查
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char **pp=NULL;//存储指针数组的地址

//13180807 *4  /1024/1024  50M

void initdatatomem(char *path)
{
    pp  = (char **)malloc(sizeof(char *)* 13180807);//分配指针数组
	FILE *pf = fopen(path, "r");
	if (pf == NULL)
	{
		printf("fail");
	}
	else
	{		
		for (int i = 0; i < 13180807; i++)
		{
			char str[275] = {0};//读取字符串的缓冲区
			fgets(str, 275, pf);//从文件中逐行读取字符串
			int  strlength = strlen(str) + 1;//获取要分配的字符串长度
			char *px =(char *) malloc(sizeof(char)*strlength);//分配内存
			strcpy(px, str);//拷贝字符串
			px[strlength - 1] = '\0';//设定最后一个字符为'\0'
			pp[i] = px;//存储字符串的首地址到指针数组
		}
	}
	printf("载入内存OK\n");
}

void  findstr(char *searchstr)
{
	for (int i = 0; i < 13180807; i++)
	{
		//pp[i];//是一个指针
		char *ptemp = strstr(pp[i], searchstr);//遍历所有的指针数组的地址,字符串查找
		if (ptemp != NULL)
		{
			printf("\n%s", pp[i]);//打印字符串
		}
	}
}

int getfilesize(char *path)
{
	FILE *pf;//文件指针
	pf = fopen(path, "r");//读取的模式打开
	if (pf == NULL)
	{
		return -1;//代表获取失败
	}
	else
	{
		fseek(pf, 0, SEEK_END);//到文件末尾
		int num = ftell(pf);//文件开头到当前位置有多少个字节
		fclose(pf);//关闭文件
		return num;
	}
}

int getn(char *path)
{
	FILE *pf;//文件指针
	pf = fopen(path, "r");//读取的模式打开
	if (pf == NULL)
	{
		return -1;//代表获取失败
	}
	else
	{
		int i = 0;
		while (!feof(pf))//是否到文件末尾
		{
			char str[275];
			fgets(str, 275, pf);//读取一行
			i++;//统计行数
		}
		fclose(pf);//关闭文件
		return i;
	}
}

void main()
{
	/*char *path = "C:\\Users\\yincheng01\\Desktop\\old\\dangdangwang.txt";
	int num = getfilesize(path);
	printf("%d字节,%fK,%fM", num, num / 1024.0, num / 1024.0 / 1024.0);
	printf("\n有%d行", getn(path));*/
	char *path = "C:\\Users\\yincheng01\\Desktop\\old\\dangdangwang.txt";
	initdatatomem(path);
	while (1)
	{
		char searchstr[100] = { 0 };
		scanf("%s", searchstr);
		findstr(searchstr);
	}
	system("pause");
}


相关实践学习
达摩院智能语音交互 - 声纹识别技术
声纹识别是基于每个发音人的发音器官构造不同,识别当前发音人的身份。按照任务具体分为两种: 声纹辨认:从说话人集合中判别出测试语音所属的说话人,为多选一的问题 声纹确认:判断测试语音是否由目标说话人所说,是二选一的问题(是或者不是) 按照应用具体分为两种: 文本相关:要求使用者重复指定的话语,通常包含与训练信息相同的文本(精度较高,适合当前应用模式) 文本无关:对使用者发音内容和语言没有要求,受信道环境影响比较大,精度不高 本课程主要介绍声纹识别的原型技术、系统架构及应用案例等。 讲师介绍: 郑斯奇,达摩院算法专家,毕业于美国哈佛大学,研究方向包括声纹识别、性别、年龄、语种识别等。致力于推动端侧声纹与个性化技术的研究和大规模应用。
目录
相关文章
|
11天前
|
搜索推荐 编译器 C语言
【C++核心】特殊的元素集合-数组与字符串详解
这篇文章详细讲解了C++中数组和字符串的基本概念、操作和应用,包括一维数组、二维数组的定义和使用,以及C风格字符串和C++字符串类的对比。
41 4
|
11天前
|
程序员 编译器 C++
【C++核心】C++内存分区模型分析
这篇文章详细解释了C++程序执行时内存的四个区域:代码区、全局区、栈区和堆区,以及如何在这些区域中分配和释放内存。
27 2
|
6天前
|
安全 C++
超级好用的C++实用库之环形内存池
超级好用的C++实用库之环形内存池
22 5
|
6天前
|
C++
HTML+JavaScript构建一个将C/C++定义的ANSI字符串转换为MASM32定义的DWUniCode字符串的工具
HTML+JavaScript构建一个将C/C++定义的ANSI字符串转换为MASM32定义的DWUniCode字符串的工具
|
6天前
|
C++
超级好用的C++实用库之动态内存池
超级好用的C++实用库之动态内存池
15 0
|
2月前
|
数据采集 Rust 安全
Rust在网络爬虫中的应用与实践:探索内存安全与并发处理的奥秘
【8月更文挑战第31天】网络爬虫是自动化程序,用于从互联网抓取数据。随着互联网的发展,构建高效、安全的爬虫成为热点。Rust语言凭借内存安全和高性能特点,在此领域展现出巨大潜力。本文探讨Rust如何通过所有权、借用及生命周期机制保障内存安全;利用`async/await`模型和`tokio`运行时处理并发请求;借助WebAssembly技术处理动态内容;并使用`reqwest`和`js-sys`库解析CSS和JavaScript,确保代码的安全性和可维护性。未来,Rust将在网络爬虫领域扮演更重要角色。
59 1
|
2月前
|
JavaScript 前端开发 Java
JavaScript内存泄露大揭秘!你的应用为何频频“爆内存”?点击解锁救星秘籍!
【8月更文挑战第23天】在Web前端开发中,JavaScript是构建动态网页的关键技术。然而,随着应用复杂度增加,内存管理变得至关重要。本文探讨了JavaScript中常见的内存泄露原因,包括意外的全局变量、不当使用的闭包、未清除的定时器、未清理的DOM元素引用及第三方库引发的内存泄露。通过了解这些问题并采取相应措施,开发者可以有效避免内存泄露,提高应用性能。
33 1
|
22天前
|
监控 算法 数据可视化
深入解析Android应用开发中的高效内存管理策略在移动应用开发领域,Android平台因其开放性和灵活性备受开发者青睐。然而,随之而来的是内存管理的复杂性,这对开发者提出了更高的要求。高效的内存管理不仅能够提升应用的性能,还能有效避免因内存泄漏导致的应用崩溃。本文将探讨Android应用开发中的内存管理问题,并提供一系列实用的优化策略,帮助开发者打造更稳定、更高效的应用。
在Android开发中,内存管理是一个绕不开的话题。良好的内存管理机制不仅可以提高应用的运行效率,还能有效预防内存泄漏和过度消耗,从而延长电池寿命并提升用户体验。本文从Android内存管理的基本原理出发,详细讨论了几种常见的内存管理技巧,包括内存泄漏的检测与修复、内存分配与回收的优化方法,以及如何通过合理的编程习惯减少内存开销。通过对这些内容的阐述,旨在为Android开发者提供一套系统化的内存优化指南,助力开发出更加流畅稳定的应用。
43 0
|
1月前
|
存储 C++
C++(五)String 字符串类
本文档详细介绍了C++中的`string`类,包括定义、初始化、字符串比较及数值与字符串之间的转换方法。`string`类简化了字符串处理,提供了丰富的功能如字符串查找、比较、拼接和替换等。文档通过示例代码展示了如何使用这些功能,并介绍了如何将数值转换为字符串以及反之亦然的方法。此外,还展示了如何使用`string`数组存储和遍历多个字符串。

热门文章

最新文章

下一篇
无影云桌面