Skip to content →

Tag: C++

Bug in Waspmote API v0.15

I found a bug in Waspmote API v0.15 when I was trying to set XBee link keys. The API failed to set the key as specified in the code. The bug lies in function gen_data()” in lines 4355-4403. Details below. However, it was corrected in v0.18.

/*
 Function: Generates the API frame to send to the XBee module
 Parameters:
 	data : The string that contains part ofthe API frame
 	param : The param to set
 Returns: Nothing
 Values: Stores in 'command' variable the API frame to send to the XBee module
*/
void WaspXBeeCore::gen_data(const char* data, uint8_t* param)
{
	uint8_t inc=0;
	uint8_t inc2=0;
		
	clearCommand();
	it=0;
	while(data[it] != '\0') {
		inc++;
		it++;
	}
	inc/=2;
	
	while(inc211) 
	{
		for(it=0;it<8;it++)
		{
			command[inc-9+it]=param[it];
		}
	}
	else if(inc==11)
	{
		for(it=0;it<3;it++)
		{
			command[inc-4+it]=param[it];
		}
	}
	else if(inc==10)
	{
		for(it=0;it<2;it++)
		{
			command[inc-3+it]=param[it];
		}
	}
	else command[inc-2]=param[0];
}

The function in v0.18 is as follows:

/*
 Function: Generates the API frame to send to the XBee module
 Parameters:
 	data : The string that contains part of the API frame
 	param : The param to set
 Returns: Nothing
 Values: Stores in 'command' variable the API frame to send to the XBee module
*/
void WaspXBeeCore::gen_data(const char* data, uint8_t* param)
{
    uint8_t    inc=0;
    uint8_t inc2=0;
		
    clearCommand();
    it=0;
    while(data[it] != '\0') {
        inc++;
        it++;
    }
    inc/=2;
	
    while(inc2

Leave a Comment

一步一脚印

咱虽然笨,一步一步慢慢来还不行么。

用正则表达式改进了昨天的计算器,用来限制用户输入。

regExp.setPattern("^\\d*.?\\d+[\\+\\-\\*\\/]\\d*.?\\d+$");
lineEdit->setValidator(new QRegExpValidator(regExp,this));

因为这个匹配模式是字符串,而字符串中”是转义字符,所以’d’放到字符串中就应该写成’\d’。类似的,如果要匹配一个”,则应该写成’\\’。

另外今天还写了个记事本,实现了最基本的打开,修改,保存和修改字体功能。Qt
Designer用的不太熟,于是手工添加控件,那叫一个麻烦。基本控件大致了解了,对文件的操作还不熟。中间由于把保存文件的对话框写成了打开文件的对话框而导致一直不能保存文件,原来这两个还不一样,虽然长得很像,汗。

QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), ".", tr("Text files (*.txt)"));
QString filename = QFileDialog::getSaveFileName(this, tr("Save text file"), ".", tr("Text files(*.txt)"));

对Signal-Slot机制的了解基本到了可以用的程度,但是对于中间的参数传递过程还一无所知。明天继续。

=============================心情分割线=============================

最近一直比较抑郁,算来差不多郁闷了将近二十年了。当然中间也会有间歇性的亢奋,但总体来说太微不足道了。以前总是对过去恋恋不舍,对未来不知所措,对当下却不能把握。总是过于重视别人的看法,过多的考虑别人的感受。从来不知道自己需要什么,现在回首看看,自己竟然不认识自己。

于是我打算跟自己一刀两断,我从今天开始就是我而且只是我,不再在乎他人的评价,不去关心我不愿关心的人和事,只做自己想做的事情。锲而不舍,勇往直前,纵然头破血流也在所不惜。不是做回自己,因为以前的我没有自己。

这几天一直听李宗盛,把他的四首歌翻来覆去的听。他那沙哑的声音切割者我的心,想哭却无法从干枯的眼睛中挤出半滴泪水。真爱难寻,我会继续等待,孤傲的常驻于此。直到你出现在我面前,I’ll love as if I’ve never been in love before.

思维之混乱,阅者了然。

Leave a Comment

千里之行

用Qt写计算器,花了我一个晚上终于写出来一个能用的了,能赶上大二学Java时写计算器的速度了。

用Qt Designer设计界面比较方便,用熟之后效率会更高。把界面设计好之后可以通过继承的方式给自己的类添加逻辑。如果设计的界面保存为mycaculator.ui的话,uic将会自动将这个XML文件转换成ui_mycaculator.h,其中的namespace为Ui。

#include"ui_mycaculator.h"
class Caculator : public QMainWindow, public Ui::MyCaculator

另外,Qt对字符串的处理简直跟Java一样了:

QString s1 = expStr.split(QRegExp("[+-*/]"))[0]; 
QString s2 = expStr.split(QRegExp("[+-*/]"))[1];
double d1 = s1.toDouble();
double d2 =s2.toDouble();
resStr = QVariant(d1+d2).toString();
expStr.append(resStr);

总算是迈出了第一步,再接再厉。

更新:上面的正则表达式不对,因为’+’, ‘-‘和’*’在正则表达式中有特殊意义。应该为如下:

QString s1 = expStr.split(QRegExp("[\\+\\-\\*/]"))[0];
QString s2 =expStr.split(QRegExp("[\\+\\-\\*/]"))[1];
double d1 = s1.toDouble();
double d2 = s2.toDouble();
resStr = QVariant(d1+d2).toString();
expStr.append(resStr);
Leave a Comment

立志

毕业在即,前途未卜。不是没有动力,是实在不知道自己该干什么好。前几天连续看了几天的Android开发,从昨天开始又开始看Qt,自己已经迷茫的手足无措了。

但是不管做什么,只要能做好,就不会有任何问题。重要的不是做什么,而是我愿意花多少精力来做这件事情。

所以从今天起,给自己一个目标,然后矢志不渝的坚持下去。不再给自己任何借口,不要心存侥幸,不要有一丝恐慌。踏踏实实,一心一意的奋斗,不管前路如何艰险,我只顾走好眼前一步。

从今开始,用所有精力去完成一件事情:学习Qt。具体规划表:

25/10/2010–07/11/2010参考C++ GUI Programming with Qt 4
学习Qt,顺便复习C++语言。理解Qt各种类的使用方法,每天必须保证至少200行的代码量,中途不准间断。
07/11/2010–21/11/2010第一个Qt项目的详细规划,包括详细设计,严格按照软件工程方法做好前期工作。
21/11/2010–17/12/2010第一个Qt项目的编码和测试,以开源方式发布。严格按照计划进行,宁可考试挂科也不能放弃执行自己的项目。
20/12/2010–17/01/2011对上一个项目进行总结,给自己充电,阅读理解An
Introduction to Design Patterns in C++ with Qt 4。同时启动第二个Qt项目,做好详细规划。
18/01/2011–10/02/2011第二个Qt项目的编码和测试,以开源方式发布。严格按照计划进行,宁可不做毕业设计也不能放弃执行自己的项目。

铁了心去干一件事情,拿出破釜沉舟的勇气来,就像两年前一样。

Leave a Comment

Qt学习:简介和信号与槽机制

以前以为Qt比较麻烦,今天看了一下,发现它其实不比Java难。

首先Qt有非常详细的文档。这个对于开发者来说是十分重要的,纵览现在的流行开发平台,不管是Java,还是微软的平台,都有为开发人员提供详实的文档和指南。如果没有文档支持,开发过程会相当痛苦。这一点我深有体会,前段时间用libpcap写点儿小程序,这个库的所有文档就是一页man page,我甚至不知道某个函数的参数到底是什么类型。

其次,Qt的跨平台性能不输Java。Qt现在支持大多数桌面平台,Windows,Linux和Unix,Mac,手机平台上面的应用也已经成熟。开发者不用更改一行代码,就可以使自己的应用程序运行在每一个操作系统中。

还有,Qt被Nokia收购之后推出了配套的IDE,Qt Creator。这个IDE强大且美观,包含方便的图形界面设计工具,图形化调试工具,手机界面模拟器以及多样化的版本控制工具支持。

总体来说,Qt的易用性是不错的,易于上手,易于开发;但是至于性能,包括速度和安全性,我目前还没有一个清晰地认识。但我相信Nokia里面的那帮人会不断想方设法的提升Qt性能的,毕竟Nokia现在手里的王牌也只有这个了。

Qt之中提供了方便的消息传递机制:信号和槽机制(Signals and Slots),这也是Qt的核心机制,用于对象之间的通信。他的格式如下:

connect(Object1, SIGNAL(signal), Object2, SLOT(slot));

当Object1的信号被触发时,Object2的相应槽就会给以相应。当然,上面只是信号与槽的一种连接方式,其他方式还可能是:
一个信号与另外一个信号相连,即一个信号触发另外一个信号:

connect(Object1, SIGNAL(signal1), Object2,
SIGNAL(signal2));

一个信号与多个槽相连:

connect(Object1, SIGNAL(signal), Object2, SLOT(slot1));
connect(Object1, SIGNAL(signal), Object3, SLOT(slot2));

同样的,一个槽也可以响应多个信号:

connect(Object1, SIGNAL(signal1), Object2, SLOT(slot));
connect(Object3, SIGNAL(signal3), Object2, SLOT(slot));

简单的例子:

#include
#include

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton b("Quit");
    b.show();
    QObject::connect(&b, SIGNAL(clicked()), &app, SLOT(quit()));
    return app.exec();
}
Leave a Comment

Technical Details

We define a BPF filter as “icmp and dst host <bouncer_ip>”, which filters out all none-ICMP packets and packets not destined to the bouncer. The we compile this filter and set the filter to the capture device.

When the capture device captures a packet, a process_pkt function is called to process the packet. And that is where we validate the packets and then, if the packet is valid, update
the packet and then send it out, or write the packet to a dump file.

The process of validating the packets are as follows:

  1. Validate checksum of IP header.
  2. Validate TTL of IP.
  3. Validate IP source address
  4. Validate ICMP header checksum. 
  5. Validate ICMP type and code. 

Since we write the packet to a dump file in a separate function, so a pointer to the dump file handler is passed to the processing function when the process_pkt function is called. The same method is used to pass server IP address and test mode flag.

We keep a linked list of all the ICMP echo requests. When a ICMP echo reply is received, we go through the linked list to find out where
the original echo request comes from.

The code is as follows.

bouncer.h:

/* Global definitions for the port bouncer
 * Packet headers and so on
 */

#define _BSD_SOURCE 1

#include 
#include 
#include 
#include 
#include 

/* PCAP declarations*/
#include 

/* Standard networking declaration */
#include 
#include 
#include 

/*
 * The following system include files should provide you with the 
 * necessary declarations for Ethernet, IP, and TCP headers
 */

#include 
#include 
#include 
#include 

/* Add any otherdeclarations you may need here... */

#define MAX_PACKET_SIZE 65535

/* Ethernet addresses are 6 bytes */
//#define ETHER_ADDR_LEN	6

/* Ethernet header */
struct sniff_ethernet {
    u_char ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */
    u_char ether_shost[ETHER_ADDR_LEN]; /* Source host address */
    u_short ether_type; /* IP? ARP? RARP? etc */
};

/* IP header */
struct sniff_ip {
    u_char ip_vhl; /*version << 4 | header length >> 2 */
    u_char ip_tos; /* type of service */
    u_short ip_len; /* total length */
    u_short ip_id; /* identification */
    u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000		/* reserved fragment flag */
#define IP_DF 0x4000		/* dont fragment flag */
#define IP_MF 0x2000		/* more fragments flag */
#define IP_OFFMASK 0x1fff	/* mask for fragmenting bits */
    u_char ip_ttl; /*time to live */
    u_char ip_p; /* protocol */
    u_short ip_sum; /* checksum */
    struct in_addr ip_src, ip_dst; /* source and dest address */
};
#define IP_HL(ip)		(((ip)->ip_vhl) & 0x0f)
#define IP_V(ip)		(((ip)->ip_vhl) >> 4)

/* ICMP header */
struct sniff_icmp {
    u_char icmp_type;
#define ICMP_ECHO 0x8
#define ICMP_REPLY 0x0
    u_char icmp_code;
    u_int16_t icmp_sum;
    u_int16_t icmp_id;
u_int16_t icmp_sequence;
};

#define SIZE_ETHERNET 14
#define SIZE_ICMP 8

#define IP_QUAD(ip)  (ip)>>24,((ip)&0x00ff0000)>>16,((ip)&0x0000ff00)>>8,((ip)&0x000000ff)

struct request{
    struct request* next;
    struct sniff_ip* ip;
    struct sniff_icmp* icmp;
};

process_pkt.c:

#include "bouncer.h"

/* CRC
 * Adopted from http://www.netfor2.com/ipsum.htm
 */
typedef unsigned short u16;
typedef unsigned long u32;

u16 ip_sum_calc(u16 len_ip_header, u16 buff[]) {
    u16 word16;
    u32 sum =0;
    u16 i;

    // make 16 bit words out of every two adjacent 8 bit words in the packet
    // and add them up
    for (i = 0; i < len_ip_header; i = i + 2) {
        word16 = ((buff[i] << 8)&0xFF00)+(buff[i + 1]&0xFF);
        sum = sum + (u32) word16;
    }

    // take only 16 bits out of the 32 bit sum and add up the carries
    while (sum >> 16)
        sum = (sum & 0xFFFF)+(sum >> 16);

    // one'scomplement the result
    sum = ~sum;

    return ((u16) sum);
}

void process_pkt(u_char *args, struct pcap_pkthdr *header,
        u_char *packet) {
    if(header->caplen < header->len){
        fprintf(stderr, "ERROR: Packet data not captured completely\n");
        return;
    }
    /* Main function to perform the bouncing */
    u_char testmode = *args;
    char *serv_addr = (char *) (args + 1);
    u_int32_t dst_addr =inet_addr(serv_addr);
    //pcap_dumper_t *dumper = NULL;
    u_char *dumper = NULL;
    memcpy(&dumper, args + 2 + strlen(serv_addr), sizeof (pcap_dumper_t *));
    struct request ** prequests = NULL;
    memcpy(&prequests, args + 2 + strlen(serv_addr) + sizeof (pcap_dumper_t *), sizeof (struct request**));
    //struct request *requests = *prequests;

    /* Typecasting packet*/
    struct sniff_ethernet *ethernet; /* The ethernet header */
struct sniff_ip *ip; /* The IP header */
    struct sniff_icmp *icmp; /* The ICMP header */
    char *padding; /* Packet padding */
    u_int size_ip;
    ethernet = (struct sniff_ethernet*) (packet);
    ip = (struct sniff_ip*) (packet + SIZE_ETHERNET);
    size_ip = IP_HL(ip)*4;
    u_int32_t srcip = ntohl(ip->ip_src.s_addr);
    if (size_ip < 20 || size_ip > header->len - SIZE_ETHERNET) {
        fprintf(stderr, "ERROR: Invalid IP header length: %ubytes\n", size_ip);
        if (testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }
    icmp = (struct sniff_icmp*) (packet + SIZE_ETHERNET + size_ip);
    padding = (u_char *) (packet + SIZE_ETHERNET + size_ip + SIZE_ICMP);

    /* Validate the packet */
    /* Validate IP header */
    /* Check IP version */
    u_char v = ip->ip_vhl>> 4;
    if(v != 4){
        fprintf(stderr, "ERROR: Wrong IP verison %d\n", v);
        return;
    }
    /* Validate CRC */
    u16 ipbuf[size_ip];
    u16 ipsum = ntohs(ip->ip_sum);
    int i;
    for (i = 0; i < size_ip; i++) {
        if (i == 10 || i == 11)
            ipbuf[i] = 0x00;
        else
            ipbuf[i] = *((u_char *) (packet + SIZE_ETHERNET + i));
    }
    if (ipsum != ip_sum_calc(size_ip, ipbuf)) {
fprintf(stderr, "ERROR: ip checksum mismatch. Dropping packet from %u.%u.%u.%u\n",
                IP_QUAD(srcip));
        if (testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }
    /* Validate TTL */
    if (ip->ip_ttl <= 0) {
        fprintf(stderr, "ERROR: ip TTL expired. Dropping packet from %u.%u.%u.%u\n",
IP_QUAD(srcip));
        if (testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }
    /* Validate IP source address */
    if (srcip == 0x00000000 || srcip >= 0xE0000000
            || (srcip & 0x000000FF) == 0x000000FF || (srcip & 0x000000FF) == 0x00000000) {
        fprintf(stderr, "ERROR: ip source address invalid. Dropping packetfrom %u.%u.%u.%u\n",
                IP_QUAD(srcip));
        if (testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }

    /* Validate ICMP header */
    /* Validate ICMP type and code */
    if (!(icmp->icmp_code == 0 && (icmp->icmp_type == 0 || icmp->icmp_type == 8))) {
        fprintf(stderr, "ERROR: icmp type orcode unsupported. Dropping packet from %u.%u.%u.%u\n",
                IP_QUAD(srcip));
        if (testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }
    /* Validate CRC */
    int plen = header->len - SIZE_ETHERNET - size_ip - 8;
    u16 icmpbuf[8 + plen];
    u16 icmpsum = ntohs(icmp->icmp_sum);
    for (i = 0; i < 8;i++) {
        if (i == 2 || i == 3)
            icmpbuf[i] = 0x00;
        else
            icmpbuf[i] = *((u_char *) (packet + SIZE_ETHERNET + size_ip + i));
    }
    for (i = 0; i < plen; i++) {
        icmpbuf[i + 8] = *((u_char *) (padding + i));
    }
    if (icmpsum != ip_sum_calc(8 + plen, icmpbuf)) {
        fprintf(stderr, "ERROR: icmp checksum mismatch. Dropping packet from %u.%u.%u.%u\n",
                IP_QUAD(srcip));
        if(testmode == 1) {
            header->caplen = 0;
            header->len = 0;
            pcap_dump(dumper, header, packet);
        }
        return;
    }

    /* Update the packet */
    if (icmp->icmp_type == 0) {
        /* ICMP echo reply */
        ip->ip_src.s_addr = ip->ip_dst.s_addr;
        /* Search the linked list for client address */
        if (*prequests == NULL) {
            fprintf(stderr, "ERROR: process_pkt: nullrequest linked list\n");
            return;
        } else {
            /* Find where the request comes from */
            struct request *r;
            for (r = *prequests; r != NULL; r = r->next) {
                if (r->icmp->icmp_id == icmp->icmp_id && r->icmp->icmp_sequence == icmp->icmp_sequence) {
                    ip->ip_dst.s_addr = r->ip->ip_src.s_addr;
                    break;
                }
}
            if (r == NULL) {
                fprintf(stderr, "ERROR: no match echo requests in stack\n");
                return;
            }
            /* Remove the request from the linked list */
            struct request *rr = *prequests;
            if (rr == r) {
                free(rr->icmp);
                free(rr->ip);
                *prequests = r->next;
                free(r);
            } else {
                while (rr!= r)
                    rr = rr->next;
                free(rr->icmp);
                free(rr->ip);
                rr->next = r->next;
                free(r);
            }
        }
    } else {
        /* ICMP echo request */
        /* Add new request to linked list */
        struct request *r;
        r = malloc(sizeof (struct request));
        if (r == NULL) {
            perror("ERROR: process_pkt:malloc");
            return;
        }
        struct sniff_ip *ipt = malloc(sizeof (struct sniff_ip));
        if (ipt == NULL) {
            perror("ERROR: process_pkt: malloc");
            return;
        }
        memcpy(ipt, ip, sizeof (struct sniff_ip));
        r->ip = ipt;
        struct sniff_icmp* icmpt = malloc(sizeof (struct sniff_icmp));
        if (icmpt == NULL) {
            perror("ERROR: process_pkt: malloc");
return;
        }
        memcpy(icmpt, icmp, sizeof (struct sniff_icmp));
        r->icmp = icmpt;
        r->next = *prequests;
        *prequests = r;
        /* Update destination address */
        ip->ip_src.s_addr = ip->ip_dst.s_addr;
        ip->ip_dst.s_addr = dst_addr;
    }
    /* Recaculate CRC */
    for (i = 0; i < size_ip; i++) {
        if (i == 10 || i == 11)
            ipbuf[i] = 0x00;
        else
ipbuf[i] = *((u_char *) (packet + SIZE_ETHERNET + i));
    }
    ip->ip_sum = htons(ip_sum_calc(size_ip, ipbuf));
    if (testmode == 0) {
        pcap_t *handle = NULL;
        char errbuf[PCAP_ERRBUF_SIZE], *device = "tap0";
        memset(errbuf, 0, PCAP_ERRBUF_SIZE);
        if ((handle = pcap_open_live(device, MAX_PACKET_SIZE, 1, 512, errbuf)) == NULL) {
            fprintf(stderr, "ERROR: %s\n", errbuf);
            exit(1);
        }
        /*Send the packet to network */
        if (pcap_sendpacket(handle, packet, header->len) != 0) {
            perror("ERROR: process_pkt: pcap_sendpacket");
            exit(1);
        }
        pcap_close(handle);
        fprintf(stderr, "Bouncer: packet sent\n");
    } else {
        /* Or put it back on stdout */
        int i = 0;
        for (i = 0; i < ETHER_ADDR_LEN; i++)
            ethernet->ether_shost[i] = 1;
        if(icmp->icmp_type == 0) {
            for (i = 0; i < ETHER_ADDR_LEN; i++)
                ethernet->ether_dhost[i] = 2;
        } else if (icmp->icmp_type == 8) {
            for (i = 0; i < ETHER_ADDR_LEN; i++)
                ethernet->ether_dhost[i] = 3;
        }
        pcap_dump(dumper, header, packet);

////////////////////////////////////////////////////////////////////////
/*
        pcap_t *handle = NULL;
        char errbuf[PCAP_ERRBUF_SIZE], *device = "tap0";
        memset(errbuf, 0, PCAP_ERRBUF_SIZE);
        if ((handle = pcap_open_live(device, MAX_PACKETS_NO, 1, 512, errbuf)) == NULL) {
            fprintf(stderr, "ERROR: %s\n", errbuf);
            exit(1);
        }
        if (pcap_sendpacket(handle, packet, header->len) != 0) {
perror("ERROR: process_pkt: pcap_sendpacket");
            exit(1);
        }
        pcap_close(handle);
        fprintf(stderr, "Packet sent\n");
*/
        ////////////////////////////////////////////////////////////////////////

    }
    return;
}
Leave a Comment

Collage

Collage is a little game that let users to rearrange pictures according to the given order. The main logic is as follows:

void canvasFrame::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CClientDC dc(this);
    xx = yy = -1;

    //判断鼠标点击的位置
    if(point.x > 0 && point.x < 100)
    {

if(point.y > 0 && point.y < 100)
        {
            xx = 0;
            yy = 0;
        }
        if(point.y > 100 && point.y < 200)
        {
            xx = 1;
            yy = 0;
        }
        if(point.y > 200 && point.y < 300)
        {
            xx = 2;
            yy = 0;
        }
    }
    if(point.x > 100 && point.x < 200)
    {
        if(point.y >
0 && point.y < 100)
        {
            xx = 0;
            yy = 1;
        }
        if(point.y > 100 && point.y < 200)
        {
            xx = 1;
            yy = 1;
        }
        if(point.y > 200 && point.y < 300)
        {
            xx = 2;
            yy = 1;
        }
    }
    if(point.x > 200 && point.x < 300)
    {
        if(point.y > 0 && point.y <
100)
        {
            xx = 0;
            yy = 2;
        }
        if(point.y > 100 && point.y < 200)
        {
            xx = 1;
            yy = 2;
        }
        if(point.y > 200 && point.y < 300)
        {
            xx = 2;
            yy = 2;
        }
    }

    int xxx, yyy;
    if(xx != -1 && yy != -1)
    {
        for(int i = 0; i < 3; ++i)
        {

for(int j = 0; j < 3; ++j)
            {
                if(cur[i][j] == 9)
                {
                    xxx = i;
                    yyy = j;
                }
            }
        }
    }

    if(abs(xxx - xx) + abs(yyy - yy) == 1)
    {
        //dc.TextOut(400, 400 ,"hello!");
        //int t = cur[xxx][yyy];
        cur[xxx][yyy] = cur[xx][yy];
        cur[xx][yy] = 9;

    //交换图片位置
        mdc->SelectObject(b);
        dc.BitBlt(a[xx * 3 + yy].x, a[xx * 3 + yy].y, 100, 100, mdc, 0, 0, SRCCOPY);

        switch(cur[xxx][yyy])
        {
            case 1: mdc->SelectObject(p1);break;
            case 2: mdc->SelectObject(p2);break;
            case 3: mdc->SelectObject(p3);break;
            case 4: mdc->SelectObject(p4);break;
            case 5: mdc->SelectObject(p5);break;

    case 6: mdc->SelectObject(p6);break;
            case 7: mdc->SelectObject(p7);break;
            case 8: mdc->SelectObject(p8);break;
        }
        dc.BitBlt(a[xxx * 3 + yyy].x, a[xxx * 3 + yyy].y, 100, 100, mdc, 0, 0, SRCCOPY);

        int f = 0;
        for(int i = 0; i < 3; ++i)
        {
            int flag = 0;
            for(int j = 0; j < 3; ++j)
            {
                if(cur[i][j] != 3 * i + j + 1)

    {
                    flag = 1;
                    break;
                }
            }
            if(flag == 1)
            {
                f = 1;
                break;
            }
        }
        if(f == 0)
        {
            strcpy(result,"恭喜你,你赢了!");
            dc.TextOut(100 , 350 ,result);
        }
    }
    CFrameWnd::OnLButtonDown(nFlags,
    point);
}

 

Leave a Comment

C++ STL Notes: String Replacement

Macro

You are asked to do some replacements on a string. Can you work it out?

Input and Output

 

Each test case will begin with a string in a single line, and followed by an integer N which means the number of replacements. The next N lines will give you two strings s1, s2, you should replace all the s1 into s2. For more details, see the
sample below.

Sample Input

 

abc
3
a b
bb a
c d
aaabaaaa
1
aa a

Sample Output

 

ad
aabaa

Note:
For the first sample:
After the first replacement, the string will be bbc.
After the second replacement, the string will be ac.
At last, the string will be ad.

My Code:

#include 
#include 
#include 

using namespace std;

void myreplace(string & strBig, const string & strsrc, const string &strdst)
{
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos)
{
strBig.replace(pos, srclen, strdst);
pos += dstlen;
}
}

int main(int argc, char** argv)
{
string s;
while(cin>>s)
{
int n;
string s1,s2;
cin>>n;
while(n–)
{
cin>>s1>>s2;
myreplace(s,s1,s2);
}
cout<

Leave a Comment