标签:des blog color os io ar for art div
1. 4个数字 24点判断
double Calc(double a, double b, string oper) { double result = 0; const char *p = oper.c_str(); if (p != NULL) { switch (*p) { case ‘+‘: result = a + b; break; case ‘-‘: result = a - b; break; case ‘*‘: result = a * b; break; case ‘/‘: result = a / b; break; default: result = 0; break; } } return result; } int main(int argc,char* argv[]) { #if 0 int value; value=cal(); printf("the result is:%d\n",value); int src[]={8, 8, 8, 1}; printf(" %d \n",calc(src)); //getchar(); #endif //----------------------------------- double nums[] = { 8, 8, 8, 1 }; string x = "", y = "", z = ""; double a = 0, b = 0, c = 0, d = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { x = i == 0 ? "+" : i == 1 ? "-" : i == 2 ? "*" : "/"; y = j == 0 ? "+" : j == 1 ? "-" : j == 2 ? "*" : "/"; z = k == 0 ? "+" : k == 1 ? "-" : k == 2 ? "*" : "/"; for (int ia = 0; ia < 4; ia++) { a = nums[ia]; for (int ib = 0; ib < 4; ib++) { if (ia == ib) { continue; } b = nums[ib]; for (int ic = 0; ic < 4; ic++) { if (ic == ia || ic == ib) { continue; } c = nums[ic]; d = nums[6 - ia - ib - ic]; if (abs(Calc(Calc(a, b, x), Calc(c, d, z), y) - (double)24) < 0.01) { printf("(%f %s %f) %s ( %f %s %f) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d); break; } if (abs(Calc(Calc(Calc(a, b, x), c, y), d, z) - (double)24) < 0.01) { printf("((%f %s %f)%s %f)%s %f = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d); break; } if (abs(Calc(Calc(a, Calc(b, c, y), x), d, z) - (double)24) < 0.01) { printf("(%f %s (%f %s %f)) %s %f = 24 \n",a, x.c_str(), b, y.c_str(), c, z.c_str(), d); break; } if (abs(Calc(a, Calc(b, Calc(c, d, z), y), x) - (double)24) < 0.01) { printf("%f %s (%f %s (%f %s %f)) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d); break; //System.Console.WriteLine(Math.Abs(Calc(a, Calc(b, Calc(c, d, z), y), x))); } if (abs(Calc(a, Calc(Calc(b, c, y), d, z), x) - (double)24) < 0.01) { printf("%f %s ((%f %s %f)%s %f) = 24 \n", a, x.c_str(), b, y.c_str(), c, z.c_str(), d); break; } } } } } } } getchar(); return 0; }
1. HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "Box2D/Box2D.h" #include "MenuLayer.h" #include "SimpleAudioEngine.h" class HelloWorld : public cocos2d::CCLayer { public: // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone virtual bool init(); // there‘s no ‘id‘ in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback virtual void menuStartCallback(CCObject* pSender); // implement the "static node()" method manually LAYER_NODE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
3. helloworldscene.cpp
#include "HelloWorldScene.h" #include "MenuLayer.h" using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // ‘scene‘ is an autorelease object scene = CCScene::node(); CC_BREAK_IF(! scene); // ‘layer‘ is an autorelease object HelloWorld *layer = HelloWorld::node(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// CCSize mysize = CCDirector::sharedDirector()->getWinSize(); MenuLayerMainMenu *MyMenu = new MenuLayerMainMenu(); this->addChild(MyMenu); bRet = true; } while (0); return bRet; } void HelloWorld::menuStartCallback(CCObject* pSender) { CCLog("1234"); }
4.menuLayer.h
#ifndef __MENULAYER_H__ #define __MENULAYER_H__ #include "cocos2d.h" using namespace cocos2d; class MenuLayerMainMenu : public CCLayer { public: MenuLayerMainMenu(); ~MenuLayerMainMenu() { } public: virtual void menuCallbackEnable(CCObject* pSender); }; #endif
5. menuLayer.cpp
#include "cocos2d.h" #include "MenuLayer.h" #include "HelloWorldScene.h" #include "GameScene.h" MenuLayerMainMenu::MenuLayerMainMenu() { CCSize size = CCDirector::sharedDirector()->getWinSize(); // Font Item CCMenuItemFont *itemFont = CCMenuItemFont::itemWithString("Start Game", this, menu_selector(MenuLayerMainMenu::menuCallbackEnable) ); itemFont->setFontSizeObj(20); itemFont->setFontName("Marker Felt"); itemFont->setColor( ccc3(200,200,255) ); itemFont->setPosition(ccp(size.width/2,size.height/2)); CCMenu* menu = CCMenu::menuWithItems(itemFont, NULL); addChild(menu); menu->setPosition(CCPointZero); } void MenuLayerMainMenu::menuCallbackEnable(CCObject* pSender) { CCLog("1234"); CCScene *s=GameScene::scene(); CCDirector::sharedDirector()->replaceScene(s); }
6. poker.h
#ifndef _POKER_H_ #define _POKER_H_ #include "cocos2d.h" using namespace cocos2d; class PokerSprite:public CCSprite { public: PokerSprite(); static PokerSprite* createPokerSprite(int number,float pointx,float pointy,int width,int height); void setPokerRect(const CCRect& rect); CCRect& GetPokerRect(); int getNumber(); void setNumber(int number); // LAYER_NODE_FUNC(HelloWorld); private: int m_number; CCLabelTTF* m_lablenumber; CCLayerColor* m_layercolor; CCRect PokerRect; void initPokerSprite(int number,float pointx,float pointy,int width,int height); }; #endif // !_SQUARE_SPRITE_H_
7. poker.cpp
#include "Poker.h" PokerSprite::PokerSprite() :m_number(0) ,m_lablenumber(NULL) ,m_layercolor(NULL) { } PokerSprite* PokerSprite::createPokerSprite(int number,float pointx,float pointy,int width,int height) { PokerSprite *pSprite = new PokerSprite(); if (pSprite && pSprite->init()) { pSprite->autorelease(); pSprite->initPokerSprite( number, pointx, pointy, width, height); return pSprite; } CC_SAFE_DELETE(pSprite); return NULL; } int PokerSprite::getNumber() { return m_number; } void PokerSprite::setNumber(int number) { m_number=number; m_lablenumber->setString(""); m_layercolor->setColor(ccc3(200,190,180)); if (m_number>0) { CCString Poker ; Poker.initWithFormat("%i",m_number); m_lablenumber->setString(Poker.getCString()); } } void PokerSprite::setPokerRect(const CCRect& rect) { PokerRect = rect; } CCRect& PokerSprite::GetPokerRect() { return PokerRect; } void PokerSprite::initPokerSprite(int number,float pointx,float pointy,int width,int height) { m_number=number; m_layercolor=CCLayerColor::layerWithColor(ccc4(200,190,180,255),width,height); m_layercolor->setPosition(ccp(pointx,pointy)); this->addChild(m_layercolor); //创建字体 m_lablenumber=CCLabelTTF::labelWithString("","arial.ttf",50); m_lablenumber->setColor(ccc3(0,0,0)); m_lablenumber->setPosition(ccp(m_layercolor->getContentSize().width/2,m_layercolor->getContentSize().height/2)); m_layercolor->addChild(m_lablenumber); setNumber(number); CCRect PokerRect = CCRect(pointx, pointy, width, height); setPokerRect(PokerRect); }
8. GameScene.h
#ifndef __GAME_SCENCE_H__ #define __GAME_SCENCE_H__ #include "cocos2d.h" #include "Poker.h" class GameScene : public cocos2d::CCLayer { public: virtual bool init(); virtual void registerWithTouchDispatcher(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); static cocos2d::CCScene* scene(); public: virtual void menuCallbackSUBMIT(CCObject* pSender); virtual void menuCallbackNEXT(CCObject* pSender); virtual void menuCallbackPLUS(CCObject* pSender); virtual void menuCallbackDEC(CCObject* pSender); virtual void menuCallbackMULTIPLY(CCObject* pSender); virtual void menuCallbackDIVIDE(CCObject* pSender); virtual void menuCallbackLBracket(CCObject* pSender); virtual void menuCallbackRBracket(CCObject* pSender); virtual void menuCallbackDEL(CCObject* pSender); PokerSprite* m_PokerSquares[4]; LAYER_NODE_FUNC(GameScene); public: int PokerNum[4]; }; #endif
9. GameScene.cpp
#include "cocos2d.h" #include "GameScene.h" #include "Poker.h" #define LABEL_IN_PUT 1 int add_fun(int x,int y){return x+y;} int sub_fun(int x,int y){return x-y;} int mul_fun(int x,int y){return x*y;} int div_fun(int x,int y){return x/y;} int (*func[])(int ,int)={add_fun,sub_fun,mul_fun,div_fun}; int num,curch; char chtbl[]="+-*/()="; char corch[]="+-*/()=0123456789"; char * test = NULL; int getid() { int i; if(*test>=‘0‘&& *test<=‘9‘) { for(num=0;*test>=‘0‘&&*test<=‘9‘;test++) num=10*num+(*test-‘0‘); return -1; } else { for(i=0;chtbl[i];i++) if(chtbl[i]==*test) break; if(i<=5) test++; return i; } } int cal() { int x1,x2,x3,op1,op2,i; i=getid(); if(i==4) x1=cal(); else x1=num; op1=getid(); if(op1>=5) return x1; i=getid(); if(i==4) x2=cal(); else x2=num; op2=getid(); while(op2!=6 && op2!= 5) { i=getid(); if(i==4) x3=cal(); else x3=num; if((op1/2==0)&&(op2/2==1)) x2=(*func[op2])(x2,x3); else { x1=(*func[op1])(x1,x2); x2=x3; op1=op2; } op2=getid(); } return (*func[op1])(x1,x2); } bool GameScene::init() { bool ret=false; do { CC_BREAK_IF(!CCLayer::init()); /* 设置允许触屏事件 */ this->setIsTouchEnabled(true); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); int SquareSize=(winSize.width-10*5)/4; CCLayerColor* layerColorBG= CCLayerColor::layerWithColor(ccc4(180,170,160,255)); this->addChild(layerColorBG,0); for (int x = 0; x < 4; x++) { int randNum = rand() % 13; randNum++; //PokerNum[x] = randNum; PokerSprite* pSquareSprite=PokerSprite::createPokerSprite(randNum,(winSize.width - SquareSize*4)/2 + (x) * (SquareSize),winSize.height - SquareSize ,SquareSize,SquareSize); m_PokerSquares[x] = pSquareSprite; this->addChild(pSquareSprite); } /* 下一题按钮 */ CCMenuItemFont *itemNEXT = CCMenuItemFont::itemWithString("NEXT", this, menu_selector(GameScene::menuCallbackNEXT) ); itemNEXT->setFontSizeObj(25); itemNEXT->setFontName("Marker Felt"); itemNEXT->setColor( ccc3(0,0,0) ); itemNEXT->setPosition(ccp(winSize.width / 2 + 100,winSize.height/5)); /* 提交按钮 */ CCMenuItemFont *itemSUBMIT = CCMenuItemFont::itemWithString("SUBMIT", this, menu_selector(GameScene::menuCallbackSUBMIT) ); itemSUBMIT->setFontSizeObj(25); itemSUBMIT->setFontName("Marker Felt"); itemSUBMIT->setColor( ccc3(0,0,0) ); itemSUBMIT->setPosition(ccp(winSize.width / 2 + 200,winSize.height/5)); /* + - * / ( ) 按钮 */ CCMenuItemFont *itemPLUS = CCMenuItemFont::itemWithString(" + ", this, menu_selector(GameScene::menuCallbackPLUS) ); itemPLUS->setColor( ccc3(0,0,0) ); itemPLUS->setPosition(ccp(20,winSize.height/5)); CCMenuItemFont *itemDEC = CCMenuItemFont::itemWithString(" - ", this, menu_selector(GameScene::menuCallbackDEC) ); itemDEC->setColor( ccc3(0,0,0) ); itemDEC->setPosition(ccp(70,winSize.height/5)); CCMenuItemFont *itemMULTIPLY = CCMenuItemFont::itemWithString(" * ", this, menu_selector(GameScene::menuCallbackMULTIPLY) ); itemMULTIPLY->setColor( ccc3(0,0,0) ); itemMULTIPLY->setPosition(ccp(120,winSize.height/5)); CCMenuItemFont *itemDIVIDE = CCMenuItemFont::itemWithString(" / ", this, menu_selector(GameScene::menuCallbackDIVIDE) ); itemDIVIDE->setColor( ccc3(0,0,0) ); itemDIVIDE->setPosition(ccp(170,winSize.height/5)); CCMenuItemFont *itemLBracket = CCMenuItemFont::itemWithString(" ( ", this, menu_selector(GameScene::menuCallbackLBracket) ); itemLBracket->setColor( ccc3(0,0,0) ); itemLBracket->setPosition(ccp(220,winSize.height/5)); CCMenuItemFont *itemRBracket = CCMenuItemFont::itemWithString(" ) ", this, menu_selector(GameScene::menuCallbackRBracket) ); itemRBracket->setColor( ccc3(0,0,0) ); itemRBracket->setPosition(ccp(270,winSize.height/5)); CCMenuItemFont *itemDEL = CCMenuItemFont::itemWithString(" Clean ", this, menu_selector(GameScene::menuCallbackDEL) ); itemDEL->setFontSizeObj(20); itemDEL->setColor( ccc3(0,0,0) ); itemDEL->setPosition(ccp(winSize.width - 30, winSize.height/2)); CCMenu* menu = CCMenu::menuWithItems(itemNEXT,itemSUBMIT,itemPLUS,itemDEC,itemMULTIPLY,itemDIVIDE,itemLBracket,itemRBracket,itemDEL, NULL); this->addChild(menu); menu->setPosition(CCPointZero); /* 静态文本框 */ CCLabelTTF* label = CCLabelTTF::labelWithString("", "Arial", 30); addChild(label, 1, LABEL_IN_PUT); label->setPosition( ccp(winSize.width/2, winSize.height/2) ); ret=true; }while(0); return ret; } CCScene* GameScene::scene() { CCScene *scene=NULL; do { scene=CCScene::node(); CC_BREAK_IF(!scene); GameScene *my=GameScene::node();//需要定义CREATE_FUNC(MyLayer)宏 CC_BREAK_IF(!my); scene->addChild(my); }while(0); return scene; } void GameScene::menuCallbackDEL(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); label->setString(""); } void GameScene::menuCallbackNEXT(CCObject* pSender) { for (int x = 0; x < 4; x++) { int randNum = rand() % 13; randNum++; //PokerNum[x] = randNum; m_PokerSquares[x]->setNumber(randNum); } CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); label->setString(""); } void GameScene::menuCallbackSUBMIT(CCObject* pSender) { /* 计算值 */ CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘=‘; *(TempString + size + 1 ) = ‘\0‘; test = TempString; int value = cal(); CCLog("%d",value); } /* +按钮回调函数 */ void GameScene::menuCallbackPLUS(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘+‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } /* -按钮回调函数 */ void GameScene::menuCallbackDEC(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘-‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } /* *按钮回调函数 */ void GameScene::menuCallbackMULTIPLY(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘*‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } /* /按钮回调函数 */ void GameScene::menuCallbackDIVIDE(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘/‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } /* (按钮回调函数 */ void GameScene::menuCallbackLBracket(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘(‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } /* )按钮回调函数 */ void GameScene::menuCallbackRBracket(CCObject* pSender) { CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 2); TempString = strcpy(TempString, StringALready); *(TempString + size ) = ‘)‘; *(TempString + size + 1 ) = ‘\0‘; label->setString(TempString); } bool GameScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { //CCLog("Began"); return true; } void itoa(int i,char*string) { int power,j; j=i; for(power=1;j>=10;j/=10) power*=10; for(;power>0;power/=10) { *string++=‘0‘+i/power; i%=power; } *string=‘\0‘; } void GameScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) { /* 获取POKER的数字 并添加到文本框 */ CCPoint touchPoint=convertTouchToNodeSpace(pTouch); for (int x = 0; x < 4; x++) { CCRect temp = m_PokerSquares[x]->getTextureRect(); if(m_PokerSquares[x]->boundingBox().CCRectContainsPoint(m_PokerSquares[x]->GetPokerRect(),touchPoint)) { char * Temp = (char*)malloc(10); int num = m_PokerSquares[x]->getNumber(); itoa(num, Temp); CCLog(Temp); CCLabelTTF* label = (CCLabelTTF*)getChildByTag(LABEL_IN_PUT); const char * StringALready = label->getString(); int size = strlen(StringALready); char * TempString = (char *)malloc(size + 3); TempString = strcpy(TempString, StringALready); if (num >0 && num <= 9) { *(TempString + size ) = ‘0‘ + num; *(TempString + size + 1 ) = ‘\0‘; } else { *(TempString + size ) = ‘0‘ + (num / 10); *(TempString + size + 1 ) = ‘0‘ + (num % 10); *(TempString + size + 2 ) = ‘\0‘; } label->setString(TempString); } } // CCLog("%f %f",touchPoint.x, touchPoint.y); } void GameScene::registerWithTouchDispatcher() { /* 注册触屏事件 */ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); }
标签:des blog color os io ar for art div
原文地址:http://www.cnblogs.com/skiplow/p/3958193.html