"C + + Academy" 0729-speech recognition/const keyword/string application/memory allocation and processing massive amounts of data

Source: Internet
Author: User
Tags drawtext sapi sprintf string find

Speech recognition

Er.xml

<?xml version= "1.0" encoding= "Utf-8"? ><grammar langid= "804" >  <DEFINE>    <id name= "CMD" Val= "/>    </DEFINE>  <rule name=" COMMAND "id=" CMD "toplevel=" ACTIVE ">    <L>      <P> Explorer </P>      <P> open Penguin </P>      <P> close Penguin </P>      <P> shut down </P>      <P> Restart </P>      <P> notepad </P>      <P> calculator </P>      <P> drawing board </P>     <P> Tan Sheng </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; Speech Recognition Program Interface ccomptr<isprecogrammar>m_cpcmdgramma; Recognition Syntax ccomptr<isprecognizer>m_cprecoengine; Speech recognition engine int WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,pstr szcmdline,int icmdshow) {HWND hwnd; M SG 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 ("Preach wisdom Podcast C + + Academy speech Recognition Tutorial"), Ws_overlappedwindow, Cw_usedefault, Cw_usede FAULT, Cw_usedefault, cw_usedefault, NULL, NULL, HINSTANCE, NULL); ShowWindow (hwnd,icmdshow);  UpdateWindow (HWND); while (GetMessage (&msg,null,0,0)) {translatemessage (&msg); DispatchMessage (&AMP;MSG); } return Msg.wparam; } LRESULT CALLBACK WndProc (HWND hwnd,uint message,wparam wparam,lparam LPARAM) {HDC hdc; Paintstruct PS; Switch (message) {case WM_CREATE: {//Initialize COM port:: CoInitializeEx (null,coinit_apartmentthreaded);//create recognition engine COM instance for shared type HRESULT hr=m_cprecoengine.cocreateinstance (Clsid_spsharedrecognizer); Creating a recognition contextInterface if (SUCCEEDED (HR)) {Hr=m_cprecoengine->createrecocontext (&m_cprecoctxt);} else MessageBox (Hwnd,text (" Error1 "), TEXT (" error "), S_OK); Set the recognition message so that the computer listens for voice messages at all times if (SUCCEEDED (HR)) {hr=m_cprecoctxt->setnotifywindowmessage (hwnd,wm_recoevent,0,0);} else MessageBox (Hwnd,text ("Error2"), TEXT ("error"), S_OK); Set events we are interested in 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); Create grammar rules 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; At the beginning of the recognition, the activation syntax entersLine Identification 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; Gets the recognition result 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 ("explorer", LpszText2)!=null) {System ("c:\\"); DrawText (Hdc,text ("explorer"), -1,&rect,dt_singleline| dt_center|         Dt_vcenter); } if (Strstr ("Open Penguin", lpszText2)!=null) {System ("\" C:\\Program FileS\\tencent\\qq\\qqprotect\\bin\\qqprotect.exe\ ""); DrawText (Hdc,text ("Open Penguin"), -1,&rect,dt_singleline| dt_center|         Dt_vcenter);  } if (Strstr ("Close Penguin", lpszText2)! = NULL) {System ("taskkill/f/im QQ.exe"); DrawText (HDC, TEXT ("Close Penguin"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter);  } if (Strstr ("Shutdown", lpszText2)! = NULL) {System ("shutdown-s-T 1200"); DrawText (HDC, TEXT ("Shut Down"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter);  } if (Strstr ("Restart", lpszText2)! = NULL) {System ("shutdown-r-T 1200"); DrawText (HDC, TEXT ("Restart"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter);  } if (Strstr ("notepad", lpszText2)! = NULL) {System ("Notepad"); DrawText (HDC, TEXT ("Notepad"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter);  } if (Strstr ("Calculator", lpszText2)! = NULL) {System ("calc"); DrawText (HDC, TEXT ("Calculator"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter);  } if (Strstr ("drawing board", lpszText2) = NULL) {System ("MSPaint"); DrawText (hdc, TEXT ("drawing board"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter); } if (Strstr ("Tan Sheng", lpszText2)! = NULL) {DrawText (hdc, TEXT ("This is a wretched man"),-1, &rect, Dt_singleline | Dt_center |  Dt_vcenter); }}}}} return TRUE; } Case Wm_paint:hdc=beginpaint (HWND,&AMP;PS); EndPaint (HWND,&AMP;PS); return 0; Case Wm_destroy:postquitmessage (0); return 0; } return DefWindowProc (Hwnd,message,wparam,lparam); }

const keyword


Const int *p; int const *P;


Const * address can not be modified


int *const p;


*const data pointed to cannot be modified

#include <stdio.h>void main01 () {int num = 10;//const int data;//data = 20;const int data = 20;//const modified variable, only when initialized Operation GetChar (); Const on the left, the address itself can not be modified, you can change the pointer point. Can only read cannot write void main02 () {int num = 10;const int data = 20;//const int *p;//equivalent to int const *p;int CONST *P;P = #printf ("%d\n", *p) ;p = &data;printf ("%d\n", *p);//*p = 30;//getchar ();} Const on the right, the data pointed to cannot be modified void main () {int num = 10;const int data = 20;int *const p = #//only at initialization time, specify a unique point to//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 ( );}

String application

#define _CRT_SECURE_NO_WARNINGS//close security check #include<stdio.h> #include <stdlib.h> #include <string.h># Include <windows.h>void main1 () {//char str[100] = "for/l%i in (1,1,5) does calc";//char *p = "for/l%i in (1,1,5) d O Calc ";//str[0] ="; STR is an array that stores strings for each character//*p = ';p is a pointer to store the address of the string char str[100] = {0};//printf ("for/l%%i in (1,1,%d) does%s", 5, "Calc"); int n Um;char op[30] = {0};scanf ("%d%s", &num, op), sprintf (str, "for/l%%i in (1,1,%d) do%s", num, op); system (str); syste M ("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};//storage instruction sprintf (str, "Color%x%x", I,0XF-I);//Print command System (str);//discoloration Sleep (500);}} void Main4 () {char str[100] = "I have $1000"; int num;sscanf (str, "I have%d", &num);p rintf ("%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);//String addition System (STR3);} void Main7 () {//printf ("%.6s", "1234.ABCD"), char str[100] = "notepad8888"; char newstr[100] = ""; sprintf (Newstr, "%-10.7s ", str);//10 width, + right alignment within width range,-left alignment,. 7 digs 7 characters from left to right printf (" [%s] ", NEWSTR), System (NEWSTR); System (" Pause ");} int Mystrlen (char* str) {int i = 0;for (char*p = str; *p! = ' + '; p++) {i++;} Return i;//implements string statistics}int Mystrlena (char* str) {char *p = Str;int i = 0; Aaa:if (*p!= ') {i++; p++; goto AAA;} return i;} int Mystrlenb (char *str) {if (*str== ') {return 0;} Else{return 1 + mystrlenb (++STR);}} void Main8 () {char str[100] = "China is good";p rintf ("%d", sizeof (str));//array buffer length printf ("\n%d", Mystrlen (str));// From the beginning of the first character to the \n%d character printf ("Mystrlena"),///from the first character to the \ n character printf ("\n%d", Mystrlenb (str));//From the beginning of the first character to the GetChar character ();} void Main9 () {Char StR1[100] = "My name is YinCheng"; char str2[30] = "Chen", char *p = strstr (str1, str2), if (p = = NULL) {printf ("not Found");} else{printf ("Find%p,%c,%s", p, *p, p);} GetChar ();}


memory allocation and processing of massive amounts of data
#include <stdio.h> #include <stdlib.h>//m, N generates an array, m row n column void main () {int *p = (int*) malloc (sizeof (int) * 40); /one-dimensional array for (int *px = p,i=0; px < p + N; px++,i++)//{*px = i;//Assignment printf ("%d,%p\n", *px, px),//Pointer loop}//int b[5][8];p rintf ( "\n\n\n"); Int (*PP) [8] = (int (*) [8]) p;//pointer type determines how access is made to (int i = 0; i < 5; i++) {for (int j = 0; J < 8; J + +) {//printf ("%5d", Pp[i][j]);//Print Data 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]);//print element printf ("%5d", * (* (* (PPP + i) + j) + K));} printf ("\ n");} printf ("\n\n\n");} System ("Pause");}


#define _CRT_SECURE_NO_WARNINGS//close security check #include<stdio.h> #include <stdlib.h> #include <string.h> Char **pp=null;//stores the address of an array of pointers//13180807 *4/1024/1024 50Mvoid initdatatomem (char *path) {pp = (char * *) malloc (sizeof (CH AR *) * 13180807);//allocation pointer array file *PF = fopen (path, "R"), if (PF = = NULL) {printf ("fail");} else{for (int i = 0; i < 13180807; i++) {char str[275] = {0};//reads the buffer of the string fgets (str, 275, PF);//reads the string int strlength from the file row by line = strlen (str) + 1;//Gets the string length to allocate char *PX = (char *) malloc (sizeof (char) *strlength);//Allocate Memory strcpy (px, str);//Copy String px[ Strlength-1] = ' + ';//Set the last character to ' pp[i ' = px;//stores the first address of the string to the pointer array}}printf ("Load Memory ok\n");} void Findstr (char *searchstr) {for (int i = 0; i < 13180807; i++) {//pp[i];//is a pointer char *ptemp = STRSTR (Pp[i], searchstr) ;//Traverse all pointers array address, string find if (ptemp! = NULL) {printf ("\n%s", Pp[i]);//print string}}}int getfilesize (char *path) {file *pf;//file pointer pf = fopen (Path, "R");//Read mode open if (PF = = NULL) {return-1;//stands for failed}else{fseek (PF, 0, seek_end);//to end of file int num = Ftell (PF);//The number of bytes fclose (PF) at the beginning of the file to the current position;//closing the file return num;}}  int Getn (char *path) {file *pf;//files pointer pf = fopen (path, "R");//Read mode open if (PF = = NULL) {return-1;//represents get failed}else{int i = 0;while (!feof (PF))//whether to the end of the file {char str[275];fgets (str, 275, PF);//read one row i++;//count rows}fclose (PF);//close file return i;}} void Main () {/*char *path = "c:\\users\\yincheng01\\desktop\\old\\dangdangwang.txt"; int num = getfilesize (path);p rintf ("%d bytes,%fk,%fm", Num, num/1024.0, num/1024.0/1024.0);p rintf ("\ n%d lines", GETN (Path)); */char *path = "C:\\users\\yinchen G01\\desktop\\old\\dangdangwang.txt "; initdatatomem (path); while (1) {char searchstr[100] = {0};scanf ("%s ", Searchstr) ; findstr (SEARCHSTR);} System ("Pause");}


Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!

"C + + Academy" 0729-speech recognition/const keyword/string application/memory allocation and processing massive amounts of data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.