C/C++ 程序小例

  • 时间:
  • 来源:互联网
  • 文章标签:

1、FTP连接的函数

View Code
void CFtpClientDemoDlg::OnBnClickedConnect()
{
    FILETIME fileTime;
    CString str,szFile,szFtpInfo;
    HINTERNET hFind;

    UpdateData(TRUE);

    BYTE b1,b2,b3,b4;
    m_ServerAddr.GetAddress(b1,b2,b3,b4);

    CString strServerAddr;
    strServerAddr.Format(_T("%d.%d.%d.%d"),b1,b2,b3,b4);
    HINTERNET hInetSession=InternetOpen(strServerAddr,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    
    szFtpInfo="正在连接";
    szFtpInfo+=strServerAddr;
    szFtpInfo+="服务器";

    TRACE(szFtpInfo);

    HINTERNET hFtpConn=InternetConnect(hInetSession,strServerAddr,m_nFtpPort,
        m_strUserID,m_strPwd,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
    if(!hFtpConn)
    {
        szFtpInfo=strServerAddr;
        szFtpInfo+="服务器未接上";
        InternetCloseHandle(hInetSession);
        ::Sleep(10);

        MessageBox(szFtpInfo);

        return ;
    }
    szFtpInfo=strServerAddr;
    szFtpInfo+=_T("服务器已连上\n用户:");
    szFtpInfo+=m_strUserID;
    szFtpInfo+=_T("登录成功");
    MessageBox(szFtpInfo);
}

2、打印文件的对话框

View Code
void CPrintDemoDlg::OnBnClickedPrint()
{
    //
    UpdateData(TRUE);
    AnalyBuyInfo(m_strBuyInfo);

    CPrintDialog printDlg(false);
    if(printDlg.DoModal()==IDOK)
    {
        CDC printcd;
        printcd.Attach(printDlg.GetPrinterDC());

        DOCINFO pdoc;
        pdoc.cbSize=sizeof(pdoc);
        pdoc.lpszDocName=_T("pdoc");
        pdoc.lpszDatatype=NULL;
        pdoc.fwType=NULL;
        pdoc.lpszOutput=NULL;
        if(printcd.StartDoc(&pdoc)>=0)
        {
            LOGFONT logfont;
            memset(&logfont,0,sizeof(LOGFONT));
            logfont.lfHeight=75;

            CFont font;
            CFont *oldfont=NULL;
            if(font.CreateFontIndirect(&logfont))
                oldfont=(CFont*)printcd.SelectObject(&font);
            /*for(int i=1;i<3;i++)
            {
                CString stt;
                stt.Format(_T("第%d页  test print file  %d"),i,i);

                printcd.StartPage();
                printcd.TextOut(0,100,stt);
                printcd.EndPage();
            }*/
            //开始打印
            printcd.StartPage();
            //买家信息
            printcd.TextOut(800,200,  m_strBuyName);

            printcd.TextOut(1200,200,m_strBuyMb);
            printcd.TextOut(2000,200,m_strBuyTel);
            printcd.TextOut(200,400,m_strBuyAddr);
            printcd.TextOut(2800,400,m_strBuyPost);
            
            //卖家信息
            printcd.TextOut(800,600,m_strSellName);
            printcd.TextOut(1200,600,m_strSellMb);
            printcd.TextOut(2000,600,m_strSellTel);
            printcd.TextOut(200,800,m_strSellAddr);
            printcd.TextOut(2800,800,m_strSellPost);

            printcd.EndPage();



            font.DeleteObject();
            if(oldfont!=NULL)  printcd.SelectObject(oldfont);
            printcd.EndDoc();
        }
        printcd.DeleteDC();
    }
}

3、动态函数执行

View Code
#include <stdio.h>
#include <stdlib.h>

#define LEN sizeof(struct Student)
struct Student
{
    long num;
    float score;
    struct Student *next;
};
int n;
struct Student *creat(void)
{
    struct Student *head;
    Student *p1,*p2;
    n=0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%ld %f",&p1->num,&p2->score);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else p2->next=p1;
            p2=p1;
        p1=(struct Student *)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return(head);
}
int main()
{
    struct Student *pt;
    pt=creat();
    printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);
    return 0;
}

4、动态拼接字符串

View Code
#include <iostream>


using namespace std;

#define    EndBuff           100000
#define    MBTYPE             101
#define    CHANNELTYPE        102

char sqlbuff[1024] = "";
char sqlbyte[1024] = "";


typedef struct 
{
    int nKey;
    char* pValue;
}nKeyBuff;

nKeyBuff buff[] =
{
    {100,"1000"},
    {200,"2000"},
    {300,"3000"},
    {400,"4000"},
    {EndBuff,""}
};

nKeyBuff buffs[] =
{
    {10,"%08d"},
    {MBTYPE,"%012s"},
    {CHANNELTYPE,"%13s"},

    {EndBuff,""}
};

typedef struct 
{
    char* pKey;
    char* pValue;
}szKeyBuff;

szKeyBuff BuffChar[] =
{
    {"100","%08s"},
    {"200","%013s"},
    {"300","%8s"},
    {NULL,""}
};

void SetNKeyValue(int nKey,char* pValue)
{
    nKeyBuff* pBuff = buffs;
    for (int i = 0;buffs[i].nKey < EndBuff;i++)
    {
        if (buffs[i].nKey == nKey)
        {
            sprintf(sqlbuff+strlen(sqlbuff),buffs[i].pValue,pValue);
            cout<<sqlbuff<<endl;
        }
    }
}

void SetSZKeyValue(char* pKey,char* pValue)
{
    szKeyBuff* pBuff = BuffChar;
    for (int i = 0;pBuff[i].pKey != NULL;i++)
    {
        if (strcmp(pBuff[i].pKey,pKey) == 0)
        {
            sprintf(sqlbyte+strlen(sqlbyte),pBuff[i].pValue,pValue);
            cout<<sqlbyte<<endl;
        }
    }
}
int _tmain(int argc, _TCHAR* argv[])
{

    char *p = "%s select ";

    int i = 0;

    /*
    nKeyBuff* pBuff = buff;

    for (int i = 0;pBuff[i].nKey < EndBuff;i++)
    {
        cout<<"Key="<<pBuff[i].nKey<<"   Vaule="<<pBuff[i].pValue<<endl;
    }*/
    
    /*
    SetNKeyValue(MBTYPE,"asd");
    SetNKeyValue(CHANNELTYPE,"ffffffffffffaaaaaaaaaaaaaaaaaaaaaaaaffffffffff");
    SetNKeyValue(MBTYPE,"123");
    */

    /*
    SetSZKeyValue("100","acv");
    SetSZKeyValue("300","aaaaaaaaaaaaa");
    

    szKeyBuff* pzBuff = BuffChar;
    for (int i = 0;pzBuff[i].pKey != NULL;i++)
    {
        cout<<"Key="<<pzBuff[i].pKey<<"   Vaule="<<pzBuff[i].pValue<<endl;
    }
    */

    char szBuff[1024];
    memset(szBuff,0,1024);
    sprintf(szBuff,"%08d",8+92);
    cout<<szBuff<<endl;


    getchar();
    return 0;
}

5、随机生成一个6位数

View Code
//生成6位随机数
int beCreateRandom()
{
    int wRet = 0;
    srand((int)time(0));
    wRet = 1 + (int)(10000000.0 * rand() / (RAND_MAX + 1.0));
    return wRet;
}

 

转载于:https://www.cnblogs.com/aulin/archive/2012/10/25/2740093.html

本文链接http://www.taodudu.cc/news/show-647831.html