Вводим текст, нажимаем "ESC" для окончания ввода.
Ввод текста в динамический массив, разбивка текста на слова
Сообщений 1 страница 3 из 3
Поделиться220-04-2011 10:21:45
#include "stdafx.h" #include <iostream> #include <conio.h> #include <iomanip> #include <time.h> #include <string.h> char* Function_Text_Input(char *pTextUser, int *i_SIZE); char** Function_Split_Into_Words(char **pTextWordSplit, int i_SIZE, char *pTextUser); int Function_Number_Of_Words(char *pTextUserCPY); int _tmain(int argc, _TCHAR* argv[]) { int i_SIZE = 10; int iCurIndexText = 0; char *pTextUser = new char[i_SIZE];//Starting size array for text pTextUser = Function_Text_Input(pTextUser, &i_SIZE);//Entering text std::cout<<pTextUser<<std::endl;//Display the original text. /* *Reating a copy of the array with the text for word count and the allocation of memory for them */ int iTextLeng = 0; iTextLeng = (int)strlen(pTextUser)+1; char *pTextUserCPY = new char[iTextLeng]; strcpy_s(pTextUserCPY, iTextLeng, pTextUser); i_SIZE = Function_Number_Of_Words(pTextUserCPY); char **pTextWordSplit = new char*[i_SIZE];//Create an array with the words pTextWordSplit = Function_Split_Into_Words(pTextWordSplit, i_SIZE, pTextUser); delete[] pTextUserCPY; /* *Display an array of individual words */ for(register int i = 0; i < i_SIZE; ++i) { std::cout<<pTextWordSplit[i]<<std::endl; } for(register int i = 0; i < i_SIZE; ++i) { delete[] pTextWordSplit[i]; } delete[] pTextWordSplit; delete[] pTextUser; _getch(); return 0; } ////////////////////////////////////////////////////////////////////// /* *Write text and save it in a dynamic array */ ////////////////////////////////////////////////////////////////////// char* Function_Text_Input(char *pTextUser, int *i_SIZE) { int i_STEP = *i_SIZE/2, iCurIndex = 0, iConsoleSaze = 75; char *pTemp;//A pointer to a memory copy with increasing array of text int *pNewLine = new int;//Allocate memory for variable inserts a new row *pNewLine = 0; char cInSymbol; while(true)//Cycle text entry { cInSymbol = _getch(); system("cls"); if(27 == cInSymbol)//Chek kay to exit { pTextUser[iCurIndex] = 0; system("cls"); break; } if(13 == cInSymbol)//Processing Keys { pTextUser[iCurIndex++] = '\n'; *pNewLine = 0; } else if(8 == cInSymbol && 0 != iCurIndex) { pTextUser[--iCurIndex] = 0; if(0 != *pNewLine) { --*pNewLine; } } else { pTextUser[iCurIndex++] = cInSymbol; ++*pNewLine; } /* *Increasing the array for the text to step increment */ if(iCurIndex == *i_SIZE) { pTemp = new char[*i_SIZE + i_STEP]; memcpy_s(pTemp, sizeof(char)*(*i_SIZE + i_STEP), pTextUser, sizeof(char) * *i_SIZE); delete[] pTextUser; pTextUser = pTemp; *i_SIZE += i_STEP; i_STEP = *i_SIZE * 2; } if(*pNewLine == iConsoleSaze)//Checking the boundaries of the input { pTextUser[iCurIndex++] = '\n'; *pNewLine = 0; } pTextUser[iCurIndex] = 0; std::cout<< pTextUser; } *i_SIZE = iCurIndex; delete pNewLine; return pTextUser; } ////////////////////////////////////////////////////////////////////// /* *Function divides the text into words */ ////////////////////////////////////////////////////////////////////// char** Function_Split_Into_Words(char **pTextWordSplit, int i_SIZE, char *pTextUser) { char *pWordUserText; char *pNextWordUserText = NULL; pWordUserText = strtok_s(pTextUser, " .,\n", &pNextWordUserText); int iIndex = 0; while(pWordUserText != NULL || iIndex < i_SIZE) { int iLeng = 0; iLeng = (int)strlen(pWordUserText)+1; pTextWordSplit[iIndex] = new char[iLeng]; strcpy_s(pTextWordSplit[iIndex++], iLeng, pWordUserText); if(pWordUserText != NULL) { pWordUserText = strtok_s(NULL, " .,\n", &pNextWordUserText); } } return pTextWordSplit; } ////////////////////////////////////////////////////////////////////// /* *Function divides the text into words */ ////////////////////////////////////////////////////////////////////// int Function_Number_Of_Words(char *pTextUserCPY) { int iCoutnWord = 0; char *pWordUserText; char *pNextWordUserText = NULL; pWordUserText = strtok_s(pTextUserCPY, " .,\n", &pNextWordUserText); while(pWordUserText != NULL) { if(pWordUserText != NULL) { pWordUserText = strtok_s(NULL, " .,\n", &pNextWordUserText); ++iCoutnWord; } } return iCoutnWord; }
Поделиться314-07-2023 22:00:34
take257BettBettBugsNoraMinnVIIIremaJoseRollIstaRondFiskSeviHardEverRajnTallTescDulcWelc
NeapChamLeatColiShinAutrAgenNaivErleMaliAntoMicrStepFortPureCaroAntiTimoGlisKorrAccaNata
SafeOreaMeduOmsaGaliNighNighJellMPEGNeroBriaLeatAureRossMornJanyHenrAkutshinAlexSelaAtik
WareGranJohnGeorSounJeanJeanSviaBettUnomERINCarlEdoucottXVIIWindgranEdmoZoneGeraFearRond
ZoneRondVirtZoneGlasBarbRyszMiyoCompCarlClauNinnExchAlexPuccRobeEdgaSylvDisnBernOscaRoll
IronSimmZoneStatXVIICallKolngaraPCIeEtliBeatStieCineBookSonyHayaOlmeDesiTwinReggplacBrad
pokeARAGLoveGeorUSMLJazzValiDigiEducArctHautFnscWindJeweWindAdobConnHyunChouVeryEnzoNels
PavlHellAmorAmorDrohSubhJohnParaExtrXVIIXVIIHertPaulHansnetwMickAnatGiorDonnLIRALighZver
WondLaurWindPoweBackPuccTaylToonClasWiddexerAMADAdriMimiBookKellAIAGTerrTubuPixiForeAndr
WishSacrCarlStepheigDownUnfiPCIePCIePCIeIUCNVaniTracAngePampPeteAnchLEAPXVIIBeauMuddMPEG
tuchkasupdaMary