1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
//#define UNICODE
//#define _UNICODE
#include <Windows.h> //for MessageBox(), GetTickCount() and GlobalAlloc()
#include <tchar.h>
#include <String.h> //for strncpy(), strcpy(), strcat(), etc.
#include <cstdio> //for sprintf()
enum // Exercise
{ // =======================================
NUMBER = 2000000, // 1)Create a 2MB string of dashes
LINE_LENGTH = 90, // 2)Change every 7th dash to a "P"
NUM_PS = NUMBER/7+1, // 3)replace every "P" with a "PU" (hehehe)
PU_EXT_LENGTH = NUMBER+NUM_PS, // 4)replace every dash with an "8"
NUM_FULL_LINES = PU_EXT_LENGTH/LINE_LENGTH, // 5)Put in a CrLf every 90 characters
MAX_MEM = PU_EXT_LENGTH+NUM_FULL_LINES*2 // 6)Output last 4K to Message Box
};
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArg, int nCmdShow)
{
TCHAR szMsg[64],szTmp[16]; //for message box
int i=0,iCtr=0,j; //iterators/counters
TCHAR* s1=NULL; //pointers to null terminated
TCHAR* s2=NULL; //character array bufers
DWORD tick=GetTickCount(); //Get Initial Tick Count Number
s1=(TCHAR*)GlobalAlloc(GPTR,MAX_MEM*sizeof(TCHAR)); //Allocate two buffers big enough to
s2=(TCHAR*)GlobalAlloc(GPTR,MAX_MEM*sizeof(TCHAR)); //hold the original NUMBER of chars
//plus substitution of PUs for Ps and
for(i=0; i<NUMBER; i++) //CrLfs after each LINE_LENGTH chunk.
s1[i]=_T('-');
// 1) Create 2MB string of dashes putting a 'P' every
for(i=0; i<NUMBER; i++, iCtr++) // seventh char;
{
if(iCtr==7)
{
s1[i]=_T('P');
iCtr=0;
}
}
iCtr=0; // 3) Substitute 'PUs' for 'Ps' This is
for(i=0; i<NUMBER; i++) // tricky! Note the buffer needs to
{ // grow. See John's (hehehe) above!
if(_tcsncmp(s1+i,_T("P"),1)==0)
{
_tcscpy(s2+iCtr,_T("PU"));
iCtr+=2;
}
else
{
s2[iCtr]=s1[i];
iCtr++;
}
}
for(i=0; i<PU_EXT_LENGTH; i++) // 4) Replace every '-' with an 8;
{
if(s2[i]==_T('-'))
s2[i]=56; //56 is '8'
}
i=0, j=0, iCtr=0; // 5)Put in a CrLf every 90 characters
while(i<PU_EXT_LENGTH)
{
s1[j]=s2[i];
i++, j++, iCtr++;
if(iCtr==LINE_LENGTH)
{
s1[j]=13, j++;
s1[j]=10, j++;
iCtr=0;
}
}
s1[j]=0, s2[0]=0;
_tcsncpy(s2,&s1[j]-4001,4000); // 6) Output last (right most) 4 K to
s2[4000]=0; // MessageBox().
tick=GetTickCount()-tick;
_tcscpy(szMsg,_T("Here's Your String John In ")); //Let me clue you in on something.
_stprintf(szTmp,_T("%u"),(unsigned)tick); //You'll get real tired of this
_tcscat(szMsg,szTmp); //sprintf(), strcpy(), strcat()
_tcscat(szMsg,_T(" ticks!")); //stuff real fast. It'll wear you
MessageBox(0,s2,szMsg,MB_OK); //right into the ground!
GlobalFree(s1), GlobalFree(s2);
return 0;
}
|