// DEMO
void chinese_demo() {
	int n = 3;
	int m[] = {3, 5, 7};
	int r[] = {2, 2, 1};
	for (int i = 0; i < n; i++)
		printf("x = %d (mod %d)\n", r[i], m[i]);
	printf("Solution x = %lld\n", chinese(n, m, r));
	printf("NC Solution x = %lld\n\n", non_chinese(n, m, r));
	
	int n2  = 3;
	int m2[] = {3, 11, 13};
	int r2[] = {2,  8, 11};
	for (int i = 0; i < n2; i++)
		printf("x = %d (mod %d)\n", r2[i], m2[i]);
	printf("Solution x = %lld\n\n", chinese(n2, m2, r2));
	
	int n3  = 5;
	int m3[] = {4, 8, 3, 11, 13};
	int r3[] = {2, 4, 2,  8, 11};
	for (int i = 0; i < n3; i++)
		printf("x = %d (mod %d)\n", r3[i], m3[i]);
	printf("NC Solution x = %lld\n\n", non_chinese(n3, m3, r3));
	
	int n4  = 5;
	int m4[] = {4, 24, 21, 11, 44};
	int r4[] = {2, 14,  5,  4, 26};
	for (int i = 0; i < n4; i++)
		printf("x = %d (mod %d)\n", r4[i], m4[i]);
	printf("NC Solution x = %lld\n", non_chinese(n4, m4, r4));
}
Exemple #2
0
LanguageId::LanguageId( std::string code )
{
    // To lower case, replace '_' with '-'.
    boost::algorithm::to_lower( code );
    boost::replace_all( code, "_", "-" );

    // Check if it is valid Chinese language ID.
    std::regex chinese( "zh(s|t|-ch|-hk|-tw)" );
    if ( std::regex_match( code, chinese ))
    {
        m_code = std::move( code );
        return;
    }
    
    // Codes more than 2 characters are treated as unknown.
    // (keep the m_code empty).
    if ( code.length() != 2 ) { return; }

    // Codes can only be alphabets
    if ( ! std::all_of( code.begin(), code.end(), isalpha ))
    {
        return;
    }

    // Then store the code.
    m_code = std::move( code );
}
Exemple #3
0
int testTrDlg::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QDialog::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: english(); break;
        case 1: chinese(); break;
        case 2: ILoveMyFamily(); break;
        case 3: testTr(); break;
        }
        _id -= 4;
    }
    return _id;
}
Exemple #4
0
LRESULT CMainDlg::OnBnClickedOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	m_richEditFormat.SetWindowText(_T(""));
	m_richEditText.SetWindowText(_T(""));
	m_richEditHTML.SetWindowText(_T(""));
	m_richEditRaw.SetWindowText(_T(""));
	m_pictureBox.SetHBitmap(NULL);
	if (::OpenClipboard(NULL))
	{
		UINT uFormat = 0;
		char format[256] = {0};
		UINT htmlFormat = ::RegisterClipboardFormat(_T("HTML Format"));
		while(uFormat = ::EnumClipboardFormats(uFormat))
		{
			CString formatText;
			formatText.Format(_T("%d:%s\n"),uFormat,GetClipFormatName(uFormat,htmlFormat));
			m_richEditFormat.AppendText(formatText);
			if (htmlFormat == uFormat)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HANDLE handle = ::GetClipboardData(uFormat);
					CHAR* data = (CHAR*)GlobalLock(handle);

					CString unicodeData = ConvertUtf8(data);
					m_richEditHTML.AppendText(unicodeData);
					GlobalUnlock(handle);
				}
			}
			else if (uFormat == CF_UNICODETEXT)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HANDLE handle = ::GetClipboardData(uFormat);
					TCHAR* data = (TCHAR*)GlobalLock(handle);
					m_richEditText.AppendText(data);
					GlobalUnlock(handle);

				}
			}
			else if (uFormat == CF_BITMAP)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HBITMAP hBitmap = (HBITMAP)GetClipboardData(uFormat);
					m_pictureBox.SetHBitmap(hBitmap);
				}
			}
		}
		int selectFormat = GetDlgItemInt(IDC_EDITFormat);
		if (selectFormat > 0)
		{
			if (::IsClipboardFormatAvailable(selectFormat))
			{
				HGLOBAL hMem = ::GetClipboardData(selectFormat);
				if (IsDlgButtonChecked(IDC_CHECKBinary))
				{
					LPVOID pvdata = GlobalLock(hMem);
					DWORD uDataSize = GlobalSize(hMem);
					BYTE* pbyData  = new BYTE [ uDataSize ];
					// Copy the data to the newly-allocated memory.
					CopyMemory ( pbyData, pvdata, uDataSize );
					UINT        uOffset;
					UINT        uBytesInThisLine;
					UINT        uLineOffset;

					CString     sLine (_T(' '), 64);            // preallocate enough space for a line
					CString     sWork;
					for ( uOffset = 0; uOffset < uDataSize; uOffset += 8 )
					{
						// How many bytes will be in the next line?  Max of 8.
						uBytesInThisLine = uDataSize - uOffset;
						if ( uBytesInThisLine > 8 )
						{
							uBytesInThisLine = 8;
						}
						// First part of the line - the starting offset.
						sLine.Format ( _T("%08X  "), uOffset );
						// Now loop through the data and add on the hex value of each byte.
						for ( uLineOffset = 0; uLineOffset < uBytesInThisLine; uLineOffset++ )
						{
							sWork.Format ( _T("%02X "), pbyData[uOffset + uLineOffset] );
							sLine += sWork;
							if ( 3 == uLineOffset || 7 == uLineOffset )
								sLine += ' ';
						}

						// If there were less than 8 bytes in this line, pad the line with
						// spaces so the ASCII representation will be in the right column.
						if ( uBytesInThisLine < 8 )
						{
							sLine += CString(_T(' '), 1 + 3 * (8 - uBytesInThisLine) );

							if ( uBytesInThisLine < 4 )
								sLine += _T(' ');
						}

						// Add on the ASCII representation
						for ( uLineOffset = 0; uLineOffset < uBytesInThisLine; uLineOffset++ )
						{
							// If the next byte isn't printable, show a period instead.
							if ( isprint ( pbyData[uOffset + uLineOffset] ))
								sLine += (TCHAR) pbyData[uOffset + uLineOffset];
							else if (uLineOffset+1 < uBytesInThisLine)
							{
								BYTE b[3];
								b[0] = pbyData[uOffset + uLineOffset];
								b[1] = pbyData[uOffset + uLineOffset+1];
								b[2] = 0;
								CString chinese(b);
								uLineOffset++;
								sLine += chinese;
							}
							else
								sLine += _T('.');
						}
						sLine += _T('\n');
						m_richEditRaw.AppendText(sLine);
					}
				}
				else if (selectFormat == CF_UNICODETEXT)
				{
					m_richEditRaw.AppendText((TCHAR*)GlobalLock(hMem));
				}
				else
				{
					char* data = (char*)GlobalLock(hMem);
					CString unicodeData(data);
					if (IsDlgButtonChecked(IDC_CHECKUnicode))
					{
						unicodeData= (TCHAR*)data;//ConvertUtf8(data);
					}
					m_richEditRaw.AppendText(unicodeData);
				}
				
				GlobalUnlock(hMem);
			}
		}

		
		CloseClipboard();
	}
	return 0;
}
Exemple #5
0
int chinese(mpz_t ret, mpz_t lcm, mpz_t *a, mpz_t *m, int items)
{
    mpz_t sum, gcd, u, v, s, t, temp1, temp2;
    int i, rval = 1;

#if 0
    if (items >= 128) {
        int first = items/2;
        mpz_t ca[2], cm[2];

        for (i = 0; i < 2; i++)
        {
            mpz_init(ca[i]);
            mpz_init(cm[i]);
        }
        rval = chinese(ca[0], cm[0], a, m, first);
        if (rval == 1)
            rval = chinese(ca[1], cm[1], a+first, m+first, items-first);
        if (rval == 1)
            rval = chinese(ret, lcm, ca, cm, 2);
        for (i = 0; i < 2; i++)
        {
            mpz_clear(ca[i]);
            mpz_clear(cm[i]);
        }
        return rval;
    }
#else
#define CRTN 8
    if (items >= 64) {
        int start = 0, step = items/CRTN;
        mpz_t ca[CRTN], cm[CRTN];

        for (i = 0; i < CRTN; i++)
        {
            mpz_init(ca[i]);
            mpz_init(cm[i]);
        }
        for (i = 0; rval && i < CRTN; i++) {
            int citems = (i==CRTN-1) ? items-(CRTN-1)*step : step;
            rval = chinese(ca[i], cm[i], a+i*step, m+i*step, citems);
        }
        if (rval) rval = chinese(ret, lcm, ca, cm, CRTN);
        for (i = 0; i < CRTN; i++)
        {
            mpz_clear(ca[i]);
            mpz_clear(cm[i]);
        }
        return rval;
    }
#endif

    mpz_init(temp1);
    mpz_init(temp2);
    mpz_init(sum);
    mpz_init(gcd);
    mpz_init(s);
    mpz_init(t);
    mpz_init(u);
    mpz_init(v);

    mpz_set(lcm, m[0]);
    mpz_mod(sum, a[0], m[0]);
    for (i = 1; i < items; i++) {
        mpz_gcdext(gcd, u, v, lcm, m[i]);
        mpz_divexact(s, m[i], gcd);
        mpz_divexact(t, lcm, gcd);
        if (mpz_cmp_ui(gcd,1) != 0) {
            mpz_mod(temp1, sum, gcd);
            mpz_mod(temp2, a[i], gcd);
            if (mpz_cmp(temp1, temp2) != 0) {
                rval = 0;
                break;
            }
        }
        if (mpz_sgn(s) < 0) mpz_neg(s,s);
        if (mpz_sgn(t) < 0) mpz_neg(t,t);
        mpz_mul(lcm, lcm, s);
        if (mpz_sgn(u) < 0) mpz_add(u, u, lcm);
        if (mpz_sgn(v) < 0) mpz_add(v, v, lcm);
        mpz_mul(temp1, v, s);
        mpz_mul(v, temp1, sum);
        mpz_mul(temp1, u, t);
        mpz_mul(u, temp1, a[i]);
        mpz_add(temp1, v, u);
        mpz_mod(sum, temp1, lcm);
    }
    mpz_set(ret, sum);
    mpz_clear(sum);
    mpz_clear(gcd);
    mpz_clear(s);
    mpz_clear(t);
    mpz_clear(u);
    mpz_clear(v);
    mpz_clear(temp1);
    mpz_clear(temp2);
    return rval;
}