[ C / C++ ] — начинающим: задаем вопросы (архивная - 2015)

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by _Great_, 26 May 2007.

Thread Status:
Not open for further replies.
  1. bad_boy

    bad_boy Elder - Старейшина

    Joined:
    30 Dec 2004
    Messages:
    189
    Likes Received:
    33
    Reputations:
    3
    zifanchuck, С++ не хранит, вроде, названия объектов. Нужно реализовывать функцию вроде
    Code:
    if(strcmp("Image1",funcname) == 0) { return (void*)Image1;}
    if(strcmp("Image2",funcname) == 0) { return (void*)Image2;}
    и т.д.
    
     
  2. bad_boy

    bad_boy Elder - Старейшина

    Joined:
    30 Dec 2004
    Messages:
    189
    Likes Received:
    33
    Reputations:
    3
    masterlights, не проверял
    Code:
    int a,b,i,min_razn,razn;
    cin >> a;
    min_razn = a;	
    for(i=0; i<10; i++) 
    {
    	cin >> b;
    	razn = abs(a-b);
    	if(min_razn < razn)
    	{
    		min_razn = razn;
    	}
    	a = b;
    }
    cout << min_razn;
    
    
    //2
    
    void main()
    {
    	char string[256]
    		,gl[]="EeUuOoAaIiYy"
    		,*p=0
    		,temp[256];
    	int i
    	   ,b=0;
    	
    	cout<<"String:\n";
    	cin.get(string,256)
    
    	cout<<"Slova:\n";
    	
    	p=temp;
    	while(*string;;string++)
    	{
    	   if(*(string+1) == ' ' || *(string+1) == (char)0)
    	   {
    			switch(*string)
    			{
    			case 'E':
    			case 'e':
    			case 'U':
    			case 'u':
    			case 'O':
    			case 'o':
    			case 'I':
    			case 'i':
    			case 'Y':
    			case 'y':
    			    *p=(char)0x00;
    				cout<<temp<<endl;
    				string++;				
    				p = temp;
    				break;
    			}			
    	   }
    	   *p = *string;
    	   p++;	   
    	}	
    	system("pause");
    }
     
  3. SEWERN

    SEWERN Elder - Старейшина

    Joined:
    9 Jan 2009
    Messages:
    33
    Likes Received:
    44
    Reputations:
    26
    так то оно так)) но почему не получается в этом цикле ?
    Code:
    do 
    	{int EndNumb = 0;
    		EndNumb = n % 10; n = n / 10;
    		if (EndNumb <=9)
    		{y = EndNumb;}
    		else {
    			y = y * 10 +n;}
    		cout << "End: \n" << y << "\n";
    	    getch();}
    	while (n!=0);
     
  4. masterlights

    masterlights New Member

    Joined:
    20 Oct 2011
    Messages:
    41
    Likes Received:
    0
    Reputations:
    0
    не работает(
     
  5. bad_boy

    bad_boy Elder - Старейшина

    Joined:
    30 Dec 2004
    Messages:
    189
    Likes Received:
    33
    Reputations:
    3
    masterlights, бери академ, что ещё можно сказать
     
  6. masterlights

    masterlights New Member

    Joined:
    20 Oct 2011
    Messages:
    41
    Likes Received:
    0
    Reputations:
    0
    Как сделать так, что бы выполнялось данное уравнение

    char* qwe[3]; - правильно ли, что использую char* ?

    qwe[0]="12345";
    for(i=0;i<3;i++)
    qwe="0"+qwe; - как надо правильно реализовать на С++?

    например:
    qwe[0]=12345, после выполнения уравнения получилось так: qwe[0]=012345;
     
  7. Ins3t

    Ins3t Харьковчанин

    Joined:
    18 Jul 2009
    Messages:
    942
    Likes Received:
    428
    Reputations:
    139
    Code:
    std::vector<std::string> container;
    container.push_back("12345");
    container.push_back("67890");
    container.push_back("87776");
    
    for(auto it = container.begin(); it != container.end(); ++it)
    	(*it) = "0" + (*it);
     
  8. XaKeR-SpAm

    XaKeR-SpAm New Member

    Joined:
    18 Jun 2009
    Messages:
    17
    Likes Received:
    2
    Reputations:
    0
    как проверять строку на содержание заданного слова?
    надо ввести слово, а потом проверить есть ли в в строке это слово, если нету то удалить строку если есть вывести её
     
  9. cel1697i845

    cel1697i845 Elder - Старейшина

    Joined:
    22 Nov 2008
    Messages:
    616
    Likes Received:
    383
    Reputations:
    80
    Добрый вечер, пишу используя технологию CUDA (создал в MS VS 2010 в разделе NVIDIA, проект CUDA 4.0), так вот возник вопрос как организовать чтение/запись данных в файл (естественно это не в __global__, а в int main-e).
    P.S. google.com пользовался не помог, или я просто не то искал.
    Заранее благодарен за любую помощь, в том числе и косвенную(в какую сторону копать).
     
  10. Chrome~

    Chrome~ Elder - Старейшина

    Joined:
    13 Dec 2008
    Messages:
    939
    Likes Received:
    162
    Reputations:
    27
    Code:
    if (strstr(строка_которую_ищем, строка_в_которой_ищем) != NULL)
      // подстрока найдена
    else
      // подстрока не найдена
     
  11. XaKeR-SpAm

    XaKeR-SpAm New Member

    Joined:
    18 Jun 2009
    Messages:
    17
    Likes Received:
    2
    Reputations:
    0
    так вроде только 1 букву?
     
    #6891 XaKeR-SpAm, 9 Dec 2011
    Last edited: 10 Dec 2011
  12. tim-oleksii

    tim-oleksii Member

    Joined:
    14 Mar 2011
    Messages:
    199
    Likes Received:
    10
    Reputations:
    0
    std::string lString = "World, hello";
    int lnIndex = lString.find( "hello" );

    if ( lnIndex != std::string::npos ) std::cout << "Gotcha" << std::endl;
     
  13. Chrome~

    Chrome~ Elder - Старейшина

    Joined:
    13 Dec 2008
    Messages:
    939
    Likes Received:
    162
    Reputations:
    27
    Что "только 1 букву"?
     
  14. Mello^_^

    Mello^_^ New Member

    Joined:
    4 Dec 2011
    Messages:
    8
    Likes Received:
    0
    Reputations:
    0
    Помогите откомпилировать код программы FTP-клиент.

    #include <winsock2.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>

    int init_sock() {
    int len;
    sockaddr_in address;
    int result;
    int s;
    s = socket(AF_INET, SOCK_STREAM,0);
    address.sin_family = AF_INET; /
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(21);
    len = sizeof(address);
    result = connect(s, (sockaddr *)&address, len);
    if (result == -1) {
    perror("oops: client");
    return -1;
    }
    return s;
    }

    nt readServ(int s) {
    int rc;
    fd_set fdr;
    FD_ZERO(&fdr);
    FD_SET(s,&fdr);
    timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    do {
    char buff[512] ={' '};
    recv(s,&buff,512,0);
    cout << buff;
    rc = select(s+1,&fdr,NULL,NULL,&timeout);
    } while(rc);
    return 2;
    }

    int main() {
    int s;
    s = init_sock();
    readServ(s);

    close(s);
    return 0;
    }

    int init_data() {
    send(s,"PASV\r\n",strlen("PASV\r\n"),0);
    char buff[128];
    recv(s,buff,128,0);
    cout << buff;

    int a,b;
    char *tmp_char;
    tmp_char = strtok(buff,"(");
    tmp_char = strtok(NULL,"(");
    tmp_char = strtok(tmp_char, ")");
    int c,d,e,f;
    sscanf(tmp_char, "%d,%d,%d,%d,%d,%d",&c,&d,&e,&f,&a,&b);
    int len;
    sockaddr_in address;
    int result;

    int port = a*256 + b;

    ds = socket(AF_INET, SOCK_STREAM,0);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr(addr);
    address.sin_port = htons(port);
    len = sizeof(address);
    result = connect(ds, (sockaddr *)&address, len);
    if (result == -1) {
    perror("oops: client");
    return -1;
    }
    return 0;
    }






    nt login()
    {
    cout << "Введите имя: "; char name[64]; cin >> name;
    char str[512];
    sprintf(str,"USER %s\r\n",name);
    send(s,str,strlen(str),0);
    readServ();
    cout << "Введите пароль: "; char pass[64]; cin >> pass;
    sprintf(str,"PASS %s\r\n",pass);
    send(s,str,strlen(str),0);
    readServ();
    return 0;
    }

    nt get(char *file) {
    char str[512];
    sprintf(str,"RETR %s\r\n",file);
    send(s,str,strlen(str),0);

    /* получение размера файла */
    char size[512];
    recv(s,size,512,0);
    cout << size;

    char *tmp_size;
    tmp_size = strtok(size,"(");
    tmp_size = strtok(NULL,"(");
    tmp_size = strtok(tmp_size, ")");
    tmp_size = strtok(tmp_size, " ");

    int file_size;
    sscanf(tmp_size,"%d",&file_size);

    FILE *f;
    f = fopen(file, "wb");
    int read = 0;
    do
    {
    char buff[2048];
    int readed = recv(ds,buff,sizeof(buff),0);
    fwrite(buff,1,readed,f);
    read += readed;
    } while (read < file_size);
    fclose(f);
    cout << "Готово. Ожидание ответа сервера...\n";
    return 0;
    }
    P.S. При компиляция выдает ошибку Unable to open include file , причом все нужные директории в папке include у меня есть, и так во всех созданых мною прогах, я уже и Borland С++ другие скачивал, не знаю в чом проблема..
     
  15. ubuntu

    ubuntu New Member

    Joined:
    28 Dec 2009
    Messages:
    35
    Likes Received:
    0
    Reputations:
    0
    нажимаем Esc и у нас выходит оконное сообщениие с текстом "Поменять местами Да и Нет?", и если нажмаем Да, то эти кнопки Да и Нет меняются метами)

    как такое сделать на с++ win api?
     
  16. geforse

    geforse Elder - Старейшина

    Joined:
    2 Mar 2008
    Messages:
    623
    Likes Received:
    290
    Reputations:
    1
    Дайте кто нибудь atlconv.h (может в старых версиях MVC он есть)
     
  17. mrazota

    mrazota New Member

    Joined:
    8 Dec 2011
    Messages:
    20
    Likes Received:
    1
    Reputations:
    0
    Поиск библиотек/классов"
    Здесь осуществляется поиск библиотек/классов

    // This is a part of the Active Template Library.
    // Copyright (C) 1996-1997 Microsoft Corporation
    // All rights reserved.
    //
    // This source code is only intended as a supplement to the
    // Active Template Library Reference and related
    // electronic documentation provided with the library.
    // See these sources for detailed information regarding the
    // Active Template Library product.

    #ifndef __ATLCONV_H__
    #define __ATLCONV_H__

    #ifndef __cplusplus
    #error ATL requires C++ compilation (use a .cpp suffix)
    #endif

    #ifndef _INC_MALLOC
    #include <malloc.h>
    #endif // _INC_MALLOC

    #pragma pack(push,8)

    namespace ATL
    {
    namespace _ATL_SAFE_ALLOCA_IMPL
    {
    // Following code is to avoid alloca causing a stack overflow.
    // It is intended for use from the _ATL_SAFE_ALLOCA macros
    // or Conversion macros.
    __declspec(selectany) DWORD _Atlosplatform = 0;
    inline BOOL _AtlGetVersionEx()
    {
    OSVERSIONINFO osi;
    memset(&osi, 0, sizeof(OSVERSIONINFO));
    osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osi);
    _Atlosplatform = osi.dwPlatformId;
    return TRUE;
    }

    // From VC7 CRT sources.
    #define _ATL_MIN_STACK_REQ_WIN9X 0x11000
    #define _ATL_MIN_STACK_REQ_WINNT 0x2000

    /***
    * void _resetstkoflw(void) - Recovers from Stack Overflow
    *
    * Purpose:
    * Sets the guard page to its position before the stack overflow.
    *
    * Exit:
    * Returns nonzero on success, zero on failure
    *
    *******************************************************************************/

    inline int _Atlresetstkoflw(void)
    {
    static BOOL bTemp = _AtlGetVersionEx();

    LPBYTE pStack, pGuard, pStackBase, pMaxGuard, pMinGuard;
    MEMORY_BASIC_INFORMATION mbi;
    SYSTEM_INFO si;
    DWORD PageSize;
    DWORD flNewProtect;
    DWORD flOldProtect;

    // Use _alloca() to get the current stack pointer

    pStack = (LPBYTE)_alloca(1);

    // Find the base of the stack.

    if (VirtualQuery(pStack, &mbi, sizeof mbi) == 0)
    return 0;
    pStackBase = (LPBYTE)mbi.AllocationBase;

    // Find the page just below where the stack pointer currently points.
    // This is the highest potential guard page.

    GetSystemInfo(&si);
    PageSize = si.dwPageSize;

    pMaxGuard = (LPBYTE) (((DWORD_PTR)pStack & ~(DWORD_PTR)(PageSize - 1))
    - PageSize);

    // If the potential guard page is too close to the start of the stack
    // region, abandon the reset effort for lack of space. Win9x has a
    // larger reserved stack requirement.

    pMinGuard = pStackBase + ((_Atlosplatform == VER_PLATFORM_WIN32_WINDOWS)
    ? _ATL_MIN_STACK_REQ_WIN9X
    : _ATL_MIN_STACK_REQ_WINNT);

    if (pMaxGuard < pMinGuard)
    return 0;

    // On a non-Win9x system, do nothing if a guard page is already present,
    // else set up the guard page to the bottom of the committed range.
    // For Win9x, just set guard page below the current stack page.

    if (_Atlosplatform != VER_PLATFORM_WIN32_WINDOWS) {

    // Find first block of committed memory in the stack region

    pGuard = pStackBase;
    do {
    if (VirtualQuery(pGuard, &mbi, sizeof mbi) == 0)
    return 0;
    pGuard = pGuard + mbi.RegionSize;
    } while ((mbi.State & MEM_COMMIT) == 0);
    pGuard = (LPBYTE)mbi.BaseAddress;

    // If first committed block is already marked as a guard page,
    // there is nothing that needs to be done, so return success.

    if (mbi.Protect & PAGE_GUARD)
    return 1;

    // Fail if the first committed block is above the highest potential
    // guard page. Should never happen.

    if (pMaxGuard < pGuard)
    return 0;

    VirtualAlloc(pGuard, PageSize, MEM_COMMIT, PAGE_READWRITE);
    }
    else {
    pGuard = pMaxGuard;
    }

    // Enable the new guard page.

    flNewProtect = _Atlosplatform == VER_PLATFORM_WIN32_WINDOWS
    ? PAGE_NOACCESS
    : PAGE_READWRITE | PAGE_GUARD;

    return VirtualProtect(pGuard, PageSize, flNewProtect, &flOldProtect);
    }

    #pragma warning(push)
    #pragma warning(disable: 4068) // turn off unknown pragma warning so prefast pragmas won't show
    #pragma prefast(push)
    #pragma prefast(suppress:515, "Atlresetstkoflw is the same as resetstkoflw")

    // Verifies if sufficient space is available on the stack.
    inline bool _AtlVerifyStackAvailable(SIZE_T Size)
    {
    bool bStackAvailable = true;

    __try
    {
    PVOID p = _alloca(Size);
    p;
    }
    __except ((EXCEPTION_STACK_OVERFLOW == GetExceptionCode()) ?
    EXCEPTION_EXECUTE_HANDLER :
    EXCEPTION_CONTINUE_SEARCH)
    {
    bStackAvailable = false;
    _Atlresetstkoflw();
    }
    return bStackAvailable;
    }

    #pragma prefast(pop)
    #pragma warning(pop)

    // Helper Classes to manage heap buffers for _ATL_SAFE_ALLOCA

    // Default allocator used by ATL
    class _CCRTAllocator
    {
    public :
    static void * Allocate(SIZE_T nRequestedSize)
    {
    return malloc(nRequestedSize);
    }
    static void Free(void* p)
    {
    free(p);
    }
    };

    template < class Allocator>
    class CAtlSafeAllocBufferManager
    {
    private :
    struct CAtlSafeAllocBufferNode
    {
    CAtlSafeAllocBufferNode* m_pNext;
    void* GetData()
    {
    return (this + 1);
    }
    };

    CAtlSafeAllocBufferNode* m_pHead;
    public :

    CAtlSafeAllocBufferManager() : m_pHead(NULL) {};
    void* Allocate(SIZE_T nRequestedSize)
    {
    CAtlSafeAllocBufferNode *p = (CAtlSafeAllocBufferNode*)Allocator::Allocate(nRequestedSize + sizeof(CAtlSafeAllocBufferNode));
    if (p == NULL)
    return NULL;

    // Add buffer to the list
    p->m_pNext = m_pHead;
    m_pHead = p;

    return p->GetData();
    }
    ~CAtlSafeAllocBufferManager()
    {
    // Walk the list and free the buffers
    while (m_pHead != NULL)
    {
    CAtlSafeAllocBufferNode* p = m_pHead;
    m_pHead = m_pHead->m_pNext;
    Allocator::Free(p);
    }
    }
    };

    // Use one of the following macros before using _ATL_SAFE_ALLOCA
    // EX version allows specifying a different heap allocator
    #define USES_ATL_SAFE_ALLOCA_EX(x) ATL::_ATL_SAFE_ALLOCA_IMPL::CAtlSafeAllocBufferManager<x> _AtlSafeAllocaManager

    #ifndef USES_ATL_SAFE_ALLOCA
    #define USES_ATL_SAFE_ALLOCA USES_ATL_SAFE_ALLOCA_EX(ATL::_ATL_SAFE_ALLOCA_IMPL::_CCRTAllocator)
    #endif

    // nRequestedSize - requested size in bytes
    // nThreshold - size in bytes beyond which memory is allocated from the heap.

    // Defining _ATL_SAFE_ALLOCA_ALWAYS_ALLOCATE_THRESHOLD_SIZE always allocates the size specified
    // for threshold if the stack space is available irrespective of requested size.
    // This available for testing purposes. It will help determine the max stack usage due to _alloca's

    #ifdef _ATL_SAFE_ALLOCA_ALWAYS_ALLOCATE_THRESHOLD_SIZE
    #define _ATL_SAFE_ALLOCA(nRequestedSize, nThreshold) \
    (((nRequestedSize) <= (nThreshold) && ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nThreshold) ) ? \
    _alloca(nThreshold) : \
    ((ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nThreshold)) ? _alloca(nThreshold) : 0), \
    _AtlSafeAllocaManager.Allocate(nRequestedSize))
    #else
    #define _ATL_SAFE_ALLOCA(nRequestedSize, nThreshold) \
    (((nRequestedSize) <= (nThreshold) && ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nRequestedSize) ) ? \
    _alloca(nRequestedSize) : \
    _AtlSafeAllocaManager.Allocate(nRequestedSize))
    #endif

    // Use 1024 bytes as the default threshold in ATL
    #ifndef _ATL_SAFE_ALLOCA_DEF_THRESHOLD
    #define _ATL_SAFE_ALLOCA_DEF_THRESHOLD 1024
    #endif

    } // namespace _ATL_SAFE_ALLOCA_IMPL

    } // namespace ATL

    // Make sure MFC's afxconv.h hasn't already been loaded to do this
    #ifndef USES_CONVERSION

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    #ifndef _DEBUG
    #define USES_CONVERSION int _convert; _convert
    #else
    #define USES_CONVERSION int _convert = 0
    #endif

    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #ifndef _DEBUG
    #define USES_CONVERSION_EX int _convert_ex; _convert_ex; UINT _acp_ex = CP_ACP; _acp_ex; LPCWSTR _lpw_ex; _lpw_ex; LPCSTR _lpa_ex; _lpa_ex; USES_ATL_SAFE_ALLOCA
    #else
    #define USES_CONVERSION_EX int _convert_ex = 0; _convert_ex; UINT _acp_ex = CP_ACP; _acp_ex; LPCWSTR _lpw_ex = NULL; _lpw_ex; LPCSTR _lpa_ex = NULL; _lpa_ex; USES_ATL_SAFE_ALLOCA
    #endif


    /////////////////////////////////////////////////////////////////////////////
    // Global UNICODE<>ANSI translation helpers
    LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars);
    LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars);

    #ifndef ATLA2WHELPER
    #define ATLA2WHELPER AtlA2WHelper
    #define ATLW2AHELPER AtlW2AHelper
    #endif

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    #define A2W(lpa) (\
    ((LPCSTR)lpa == NULL) ? NULL : (\
    _convert = (lstrlenA(lpa)+1),\
    ATLA2WHELPER((LPWSTR) alloca(_convert*2), (LPCSTR)lpa, _convert)))

    #define W2A(lpw) (\
    ((LPCWSTR)lpw == NULL) ? NULL : (\
    _convert = (lstrlenW(lpw)+1)*2,\
    ATLW2AHELPER((LPSTR) alloca(_convert), lpw, _convert)))

    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    // The call to _alloca will not cause stack overflow if _AtlVerifyStackAvailable returns TRUE.
    #define A2W_EX(lpa, nThreshold) (\
    ((_lpa_ex = lpa) == NULL) ? NULL : (\
    _convert_ex = (lstrlenA(_lpa_ex)+1),\
    ATLA2WHELPER( \
    (LPWSTR)_ATL_SAFE_ALLOCA(_convert_ex * sizeof(WCHAR), nThreshold), \
    _lpa_ex, \
    _convert_ex)))

    #define W2A_EX(lpw, nThreshold) (\
    ((_lpw_ex = lpw) == NULL) ? NULL : (\
    _convert_ex = (lstrlenW(_lpw_ex)+1) * sizeof(WCHAR),\
    ATLW2AHELPER( \
    (LPSTR)_ATL_SAFE_ALLOCA(_convert_ex, nThreshold), \
    _lpw_ex, \
    _convert_ex)))
    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
    #define W2CA(lpw) ((LPCSTR)W2A(lpw))

    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #define A2CW_EX(lpa, nChar) ((LPCWSTR)A2W_EX(lpa, nChar))
    #define W2CA_EX(lpw, nChar) ((LPCSTR)W2A_EX(lpw, nChar))

    #if defined(_UNICODE)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
    inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpyW(dest, src); }

    inline LPCOLESTR T2COLE_EX(LPCTSTR lp, UINT) { return lp; }
    inline LPCTSTR OLE2CT_EX(LPCOLESTR lp, UINT) { return lp; }
    inline LPOLESTR T2OLE_EX(LPTSTR lp, UINT) { return lp; }
    inline LPTSTR OLE2T_EX(LPOLESTR lp, UINT) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
    inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
    inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
    inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #elif defined(OLE2ANSI)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline int ocslen(LPCOLESTR x) { return lstrlen(x); }
    inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpy(dest, src); }

    inline LPCOLESTR T2COLE_EX(LPCTSTR lp, UINT) { return lp; }
    inline LPCTSTR OLE2CT_EX(LPCOLESTR lp, UINT) { return lp; }
    inline LPOLESTR T2OLE_EX(LPTSTR lp, UINT) { return lp; }
    inline LPTSTR OLE2T_EX(LPOLESTR lp, UINT) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
    inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
    inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
    inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #else
    inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
    //lstrcpyW doesn't work on Win95, so we do this
    inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src)
    {return (LPOLESTR) memcpy(dest, src, (lstrlenW(src)+1)*sizeof(WCHAR));}
    //CharNextW doesn't work on Win95 so we use this

    #define T2COLE_EX(lpa, nChar) A2CW_EX(lpa, nChar)
    #define T2OLE_EX(lpa, nChar) A2W_EX(lpa, nChar)
    #define OLE2CT_EX(lpo, nChar) W2CA_EX(lpo, nChar)
    #define OLE2T_EX(lpo, nChar) W2A_EX(lpo, nChar)

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    #define T2COLE(lpa) A2CW(lpa)
    #define T2OLE(lpa) A2W(lpa)
    #define OLE2CT(lpo) W2CA(lpo)
    #define OLE2T(lpo) W2A(lpo)
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #endif

    #ifdef OLE2ANSI
    inline LPOLESTR A2OLE_EX(LPSTR lp, UINT) { return lp;}
    inline LPSTR OLE2A_EX(LPOLESTR lp, UINT) { return lp;}
    #define W2OLE_EX W2A_EX
    #define OLE2W_EX A2W_EX
    inline LPCOLESTR A2COLE_EX(LPCSTR lp, UINT) { return lp;}
    inline LPCSTR OLE2CA_EX(LPCOLESTR lp, UINT) { return lp;}
    #define W2COLE_EX W2CA_EX
    #define OLE2CW_EX A2CW_EX

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline LPOLESTR A2OLE(LPSTR lp) { return lp;}
    inline LPSTR OLE2A(LPOLESTR lp) { return lp;}
    #define W2OLE W2A
    #define OLE2W A2W
    inline LPCOLESTR A2COLE(LPCSTR lp) { return lp;}
    inline LPCSTR OLE2CA(LPCOLESTR lp) { return lp;}
    #define W2COLE W2CA
    #define OLE2CW A2CW
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #else
    inline LPOLESTR W2OLE_EX(LPWSTR lp, UINT) { return lp; }
    inline LPWSTR OLE2W_EX(LPOLESTR lp, UINT) { return lp; }
    #define A2OLE_EX A2W_EX
    #define OLE2A_EX W2A_EX
    inline LPCOLESTR W2COLE_EX(LPCWSTR lp, UINT) { return lp; }
    inline LPCWSTR OLE2CW_EX(LPCOLESTR lp, UINT) { return lp; }
    #define A2COLE_EX A2CW_EX
    #define OLE2CA_EX W2CA_EX

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline LPOLESTR W2OLE(LPWSTR lp) { return lp; }
    inline LPWSTR OLE2W(LPOLESTR lp) { return lp; }
    #define A2OLE A2W
    #define OLE2A W2A
    inline LPCOLESTR W2COLE(LPCWSTR lp) { return lp; }
    inline LPCWSTR OLE2CW(LPCOLESTR lp) { return lp; }
    #define A2COLE A2CW
    #define OLE2CA W2CA
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #endif

    #ifdef _UNICODE

    #define T2A_EX W2A_EX
    #define A2T_EX A2W_EX
    inline LPWSTR T2W_EX(LPTSTR lp, UINT) { return lp; }
    inline LPTSTR W2T_EX(LPWSTR lp, UINT) { return lp; }
    #define T2CA_EX W2CA_EX
    #define A2CT_EX A2CW_EX
    inline LPCWSTR T2CW_EX(LPCTSTR lp, UINT) { return lp; }
    inline LPCTSTR W2CT_EX(LPCWSTR lp, UINT) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    #define T2A W2A
    #define A2T A2W
    inline LPWSTR T2W(LPTSTR lp) { return lp; }
    inline LPTSTR W2T(LPWSTR lp) { return lp; }
    #define T2CA W2CA
    #define A2CT A2CW
    inline LPCWSTR T2CW(LPCTSTR lp) { return lp; }
    inline LPCTSTR W2CT(LPCWSTR lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #else
    #define T2W_EX A2W_EX
    #define W2T_EX W2A_EX
    inline LPSTR T2A_EX(LPTSTR lp, UINT) { return lp; }
    inline LPTSTR A2T_EX(LPSTR lp, UINT) { return lp; }
    #define T2CW_EX A2CW_EX
    #define W2CT_EX W2CA_EX
    inline LPCSTR T2CA_EX(LPCTSTR lp, UINT) { return lp; }
    inline LPCTSTR A2CT_EX(LPCSTR lp, UINT) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    #define T2W A2W
    #define W2T W2A
    inline LPSTR T2A(LPTSTR lp) { return lp; }
    inline LPTSTR A2T(LPSTR lp) { return lp; }
    #define T2CW A2CW
    #define W2CT W2CA
    inline LPCSTR T2CA(LPCTSTR lp) { return lp; }
    inline LPCTSTR A2CT(LPCSTR lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #endif

    inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
    {
    if (lp == NULL || nLen == 0)
    return NULL;
    USES_CONVERSION_EX;
    BSTR str = NULL;
    int nConvertedLen = MultiByteToWideChar(_acp_ex, 0, lp,
    nLen, NULL, NULL);
    int nAllocLen = nConvertedLen;
    if (nLen == -1)
    nAllocLen -= 1; // Don't allocate terminating '\0'
    str = ::SysAllocStringLen(NULL, nAllocLen);
    if (str != NULL)
    {
    int nResult;
    nResult = MultiByteToWideChar(_acp_ex, 0, lp, nLen, str, nConvertedLen);
    if(nResult != nConvertedLen)
    {
    SysFreeString(str);
    return NULL;
    }
    }
    return str;
    }

    inline BSTR OLE2BSTR(LPCOLESTR lp) {return ::SysAllocString(lp);}
    #if defined(_UNICODE)
    // in these cases the default (TCHAR) is the same as OLECHAR

    inline BSTR T2BSTR_EX(LPCTSTR lp) {return ::SysAllocString(lp);}
    inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
    inline BSTR W2BSTR_EX(LPCWSTR lp) {return ::SysAllocString(lp);}

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
    inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
    inline BSTR W2BSTR(LPCWSTR lp) {return W2BSTR_EX(lp); }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #elif defined(OLE2ANSI)
    // in these cases the default (TCHAR) is the same as OLECHAR

    inline BSTR T2BSTR_EX(LPCTSTR lp) {return ::SysAllocString(lp);}
    inline BSTR A2BSTR_EX(LPCSTR lp) {return ::SysAllocString(lp);}
    inline BSTR W2BSTR_EX(LPCWSTR lp) {USES_CONVERSION_EX; return ::SysAllocString(W2COLE_EX(lp));}

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
    inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
    inline BSTR W2BSTR(LPCWSTR lp) {USES_CONVERSION; return ::SysAllocString(W2COLE(lp));}
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #else

    inline BSTR T2BSTR_EX(LPCTSTR lp) {return A2WBSTR(lp);}
    inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
    inline BSTR W2BSTR_EX(LPCWSTR lp) {return ::SysAllocString(lp);}

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
    inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
    inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
    inline BSTR W2BSTR(LPCWSTR lp) {return W2BSTR_EX(lp); }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #endif

    #if defined(_WINGDI_) && !defined(NOGDI)
    /////////////////////////////////////////////////////////////////////////////
    // Global UNICODE<>ANSI translation helpers
    LPDEVMODEW AtlDevModeA2W(LPDEVMODEW lpDevModeW, LPDEVMODEA lpDevModeA);
    LPDEVMODEA AtlDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW);
    LPTEXTMETRICW AtlTextMetricA2W(LPTEXTMETRICW lptmW, LPTEXTMETRICA pltmA);
    LPTEXTMETRICA AtlTextMetricW2A(LPTEXTMETRICA lptmA, LPTEXTMETRICW pltmW);

    #ifndef ATLDEVMODEA2W
    #define ATLDEVMODEA2W AtlDevModeA2W
    #define ATLDEVMODEW2A AtlDevModeW2A
    #define ATLTEXTMETRICA2W AtlTextMetricA2W
    #define ATLTEXTMETRICW2A AtlTextMetricW2A
    #endif

    // Requires USES_CONVERSION_EX or USES_ATL_SAFE_ALLOCA macro before using the _EX versions of the macros
    #define DEVMODEW2A_EX(lpw)\
    ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)_ATL_SAFE_ALLOCA(sizeof(DEVMODEA)+lpw->dmDriverExtra, _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lpw))
    #define DEVMODEA2W_EX(lpa)\
    ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)_ATL_SAFE_ALLOCA(sizeof(DEVMODEW)+lpa->dmDriverExtra, _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lpa))
    #define TEXTMETRICW2A_EX(lptmw)\
    ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)_ATL_SAFE_ALLOCA(sizeof(TEXTMETRICA), _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lptmw))
    #define TEXTMETRICA2W_EX(lptma)\
    ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)_ATL_SAFE_ALLOCA(sizeof(TEXTMETRICW), _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lptma))

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    #define DEVMODEW2A(lpw)\
    ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)alloca(sizeof(DEVMODEA)+lpw->dmDriverExtra), lpw))
    #define DEVMODEA2W(lpa)\
    ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)alloca(sizeof(DEVMODEW)+lpa->dmDriverExtra), lpa))
    #define TEXTMETRICW2A(lptmw)\
    ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)alloca(sizeof(TEXTMETRICA)), lptmw))
    #define TEXTMETRICA2W(lptma)\
    ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)alloca(sizeof(TEXTMETRICW)), lptma))

    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #ifdef OLE2ANSI
    #define DEVMODEOLE DEVMODEA
    #define LPDEVMODEOLE LPDEVMODEA
    #define TEXTMETRICOLE TEXTMETRICA
    #define LPTEXTMETRICOLE LPTEXTMETRICA
    #else
    #define DEVMODEOLE DEVMODEW
    #define LPDEVMODEOLE LPDEVMODEW
    #define TEXTMETRICOLE TEXTMETRICW
    #define LPTEXTMETRICOLE LPTEXTMETRICW
    #endif

    #if defined(_UNICODE)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline LPDEVMODEW DEVMODEOLE2T_EX(LPDEVMODEOLE lp) { return lp; }
    inline LPDEVMODEOLE DEVMODET2OLE_EX(LPDEVMODEW lp) { return lp; }
    inline LPTEXTMETRICW TEXTMETRICOLE2T_EX(LPTEXTMETRICOLE lp) { return lp; }
    inline LPTEXTMETRICOLE TEXTMETRICT2OLE_EX(LPTEXTMETRICW lp) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    inline LPDEVMODEW DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
    inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODEW lp) { return lp; }
    inline LPTEXTMETRICW TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
    inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRICW lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #elif defined(OLE2ANSI)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline LPDEVMODE DEVMODEOLE2T_EX(LPDEVMODEOLE lp) { return lp; }
    inline LPDEVMODEOLE DEVMODET2OLE_EX(LPDEVMODE lp) { return lp; }
    inline LPTEXTMETRIC TEXTMETRICOLE2T_EX(LPTEXTMETRICOLE lp) { return lp; }
    inline LPTEXTMETRICOLE TEXTMETRICT2OLE_EX(LPTEXTMETRIC lp) { return lp; }

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    inline LPDEVMODE DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
    inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODE lp) { return lp; }
    inline LPTEXTMETRIC TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
    inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRIC lp) { return lp; }
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #else
    #define DEVMODEOLE2T_EX(lpo) DEVMODEW2A_EX(lpo)
    #define DEVMODET2OLE_EX(lpa) DEVMODEA2W_EX(lpa)
    #define TEXTMETRICOLE2T_EX(lptmw) TEXTMETRICW2A_EX(lptmw)
    #define TEXTMETRICT2OLE_EX(lptma) TEXTMETRICA2W_EX(lptma)

    #ifndef _ATL_EX_CONVERSION_MACROS_ONLY

    #define DEVMODEOLE2T(lpo) DEVMODEW2A(lpo)
    #define DEVMODET2OLE(lpa) DEVMODEA2W(lpa)
    #define TEXTMETRICOLE2T(lptmw) TEXTMETRICW2A(lptmw)
    #define TEXTMETRICT2OLE(lptma) TEXTMETRICA2W(lptma)
    #endif // _ATL_EX_CONVERSION_MACROS_ONLY

    #endif

    #endif //_WINGDI_

    #else //!USES_CONVERSION

    // if USES_CONVERSION already defined (i.e. MFC_VER < 4.21 )
    // flip this switch to avoid atlconv.cpp
    #define _ATL_NO_CONVERSIONS

    #endif //!USES_CONVERSION

    // Define these even if MFC already included
    #if defined(_UNICODE)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNextW(lp);}
    #elif defined(OLE2ANSI)
    // in these cases the default (TCHAR) is the same as OLECHAR
    inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNext(lp);}
    #else
    inline LPOLESTR CharNextO(LPCOLESTR lp) {return (LPOLESTR)(lp+1);}
    #endif

    #pragma pack(pop)

    #endif // __ATLCONV_H__

    /////////////////////////////////////////////////////////////////////////////


    Странно тут как-то теги работают.. Если все что написала обернуть тегом CODE сообщение пропадает в ничто..
     
    #6897 mrazota, 13 Dec 2011
    Last edited: 13 Dec 2011
  18. Dethrone

    Dethrone New Member

    Joined:
    2 Dec 2011
    Messages:
    3
    Likes Received:
    1
    Reputations:
    0
    добрый день, обьясните пожалуйста, почему у меня не работает конструктор, буду очень признателен.


    // peregruz.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    class cont
    {
    cont();
    cont(int x){X=new int(x);}
    cont(const cont&);
    ~cont (){delete X; X=0;}
    int GetX()const{return *X;}
    void SetX(int x){*X=x;}
    const cont operator++(int x);
    const cont & operator++();
    cont & operator =( const cont&);
    private:
    int *X;
    };
    cont::cont()
    {
    X=new int(5);
    }
    cont::cont(const cont &rhs)
    {
    X=new int;
    *X=rhs.GetX();
    }
    const cont cont::eek:perator ++(int x)
    {
    cont C;
    C=*this;
    ++(*X);
    return C;
    }
    const cont & cont::eek:perator ++()
    {
    ++(*X);
    return *this;
    }
    cont & cont::eek:perator =(const cont & rhs)
    {
    if(this==&rhs)
    return *this;
    *X=rhs.GetX();
    return *this;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    cont x, y(9);
    x++;
    ++y;
    cout<<x.GetX()<<"\n";
    cout<<y.GetX()<<"\n";
    x=y;
    cout<<x.GetX()<<"\n";
    cout<<y.GetX()<<"\n";
    system("pause");
    return 0;
    }
     
    1 person likes this.
  19. Patronik

    Patronik Member

    Joined:
    16 Mar 2009
    Messages:
    111
    Likes Received:
    12
    Reputations:
    0
    Обясните начинающему C++ программисту. Єсть пример кода
    Code:
    /* strtok example */
     #include <stdio.h> 
    #include <string.h> 
    int main () { 
    char str[] ="- This, a sample string."; 
    char * pch; 
    printf ("Splitting string \"%s\" into tokens:\n",str); 
    pch = strtok (str," ,.-"); 
    while (pch != NULL) { 
    printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); 
    } 
    return 0; 
    }
    
    Он демонстрирует работу функции strtok. В описании интефейса функции написано так:
    Code:
    char * strtok ( char * str, const char * delimiters );
    И так вопрос 1, почему в описании функции первым параметром должен быть указатель типа char а в примере туда подставляеться просто переменная типа char?
    Вопрос 2, почему внутри вызова printf для вывода значения токена в стандратный вывод не используеться оператор * вместе с pch а просто pch? В книге читал что обращение к указателю без оператора разыменовывания вернет просто адрес а не значение по адресу.
     
  20. Chrome~

    Chrome~ Elder - Старейшина

    Joined:
    13 Dec 2008
    Messages:
    939
    Likes Received:
    162
    Reputations:
    27
    У нас есть:
    Code:
    char str[];
    // str - указатель на первый символ строки (char *)
    // &str[0] - тот самый указатель на первый символ строки (char *)
    // str[0] - первый символ (char)
    Чтобы вывести строчку в printf() мы должны записаь соответствующий формат ("%s") и передать указатель на первый символ строки, которую нужно вывести.
     
    1 person likes this.
Loading...
Thread Status:
Not open for further replies.