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

来源:互联网 发布:广州春季网络教育 编辑:程序博客网 时间:2024/06/09 18:11


语音识别

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  50Mvoid 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");}


2 0