33,311
社区成员
发帖
与我相关
我的任务
分享
#include <process.h>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <queue>
#include <map>
#include <time.h>
#include <stdarg.h>
#include <math.h>
using namespace std;
const char* qType[] = {"C/C++","JAVA","PHP"};
struct question{
int type;
int score;
};
typedef queue<question> questions;
questions unsolved;
typedef map<const char*,char*> WordsTemplate;
class Coder{
public:
char name[256];
int score;
int exp[3];
questions bookmark;
WordsTemplate lines;
char **chips;
Coder(const char* name,char **chips,WordsTemplate lines):score(0){
this->chips=chips;
this->lines = lines;
strcpy_s(this->name,256,name);
::memset(exp,0,sizeof(exp));
}
void LogOnCSDN();
protected:
bool solve(question& q);
void study(question& q);
void say(char* format,...);
void info(char* format,...);
void update();
void poolingWater(){info("%s is pooling water....\n",name);Sleep(1500);};
void thread();
void parseCommand(int ip,string& output,int index=0);
bool executeCommand(int &ip,void* p);
};
void printSystemTime(){
//print system time;
static bool lock = false;
while(lock){
::Sleep(1);
}
lock = true;
time_t t;
::time(&t);
tm _tm;
::localtime_s(&_tm,&t);
char timechar[256];
::asctime_s(timechar,256,&_tm);
timechar[strlen(timechar)-6] = '\0';
printf("[%s]",timechar);
lock = false;
}
void Coder::say(char* format,...){
if(format==NULL) return;
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);
printSystemTime();
printf("%s: %s\n",this->name,s);
};
void Coder::info(char* format,...){
if(format==NULL) return;
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);
printSystemTime();
printf("[CSDN]: %s",s);
};
void Coder::update(){
char words[512];
::sprintf_s(words,512,"Now %s\'s score is %d.unsolved/bookmarks:(%d/%d).\n\n",name,score,unsolved.size(),bookmark.size());
info(words);
}
void Coder::study(question& q){
char words[512];
::sprintf_s(words,512,"%s is learning %s.....\n",name,qType[q.type]);
info(words);
::Sleep(5000);
exp[q.type]+=q.score/5;
::sprintf_s(words,512,"%s got %d exp on %s.(%d)\n\n",name,q.score/5,qType[q.type],exp[q.type]);
info(words);
}
bool Coder::solve(question& q){
//spend sometime to check the promblem is soluble
Sleep(500);
//raise one percent per 50 point
double successRate = exp[q.type]/5000.0;
//make it high but not too high
if(successRate>0.95) successRate = 0.95;
if(rand()>(int)(((double)RAND_MAX)*successRate)){
return false;
}
int solveTime = (255-exp[q.type])*10;
if(solveTime<=500) solveTime= 500;
::Sleep(solveTime);
return true;
}
void Coder::parseCommand(int ip,string& output,int index){
output.clear();
int stringIndex = 0;
char* pStart = chips[ip];
while(*pStart!='\0'){
if(stringIndex==index){
output.push_back(*pStart);
}else if(stringIndex>index){
break;
}
pStart++;
if(*pStart==' '){
stringIndex++;
}
}
output.push_back('\0');
}
bool Coder::executeCommand(int &ip,void*p){
string command;
this->parseCommand(ip,command);
if(::strcmp(command.c_str(),"checkOR")==0){
update();
::Sleep(1000);
this->parseCommand(ip,command,1);
int times = ::atoi(command.c_str());
int checkORIp = ip+1;
for(int checkORIp = ip+1;checkORIp<=ip+times;){
bool result = this->executeCommand(checkORIp,NULL);
if(result) {
ip = ip+times+1;
return true;
}
}
return false;
}else if(::strcmp(command.c_str(),"goto")==0){
this->parseCommand(ip,command,1);
ip = ::atoi(command.c_str());
}else if(::strcmp(command.c_str(),"solve")==0){
ip++;
static bool lock = false;
while(lock){
::Sleep(1);
}
lock = true;
if(!unsolved.empty()){
info("%s is trying to solve a problem.\n",name);
question q = ::unsolved.front();
unsolved.pop();
lock = false;
if(solve(q)){
say(lines["solve_success"],qType[q.type]);
this->score+=q.score;
say(lines["celebrate"]);
Sleep(100);
}else{
say(lines["solve_failure"]);
bookmark.push(q);
}
::Sleep(500);
return true;
}
lock = false;
return false;
}else if(::strcmp(command.c_str(),"study")==0){
ip++;
if(!bookmark.empty()){
//learning
question q = bookmark.front();
this->study(q);
bookmark.pop();
return true;
}
return false;
}else if(::strcmp(command.c_str(),"poolingWater")==0){
ip++;
this->poolingWater();
return true;
}
return true;
}
void Coder::thread(){
int ip = 0;
while(::strcmp(chips[ip],"end")!=0){
this->executeCommand(ip,NULL);
}
::_endthread();
}
void Coder::LogOnCSDN(){
say(lines["opening"],name);
union {
void (*thread)(void *);
void (Coder::*Coderthread)();
} proc;
proc.Coderthread = &Coder::thread;
::_beginthread(proc.thread,0,this);
}
void generateQuestion(void* p){
time_t t;
::time(&t);
::srand((UINT)t);
while(true){
question q = {rand()%5,(rand()%10+1)*10};
if(q.type<3){
::unsolved.push(q);
}
Sleep(1500);
}
}
void main()
{
//::_beginthread(generateQuestion,0,0);
WordsTemplate bearWords;
bearWords["opening"] = "Hi!I'm a new fish on CSDN!My name is %s.\n";
bearWords["solve_success"] = "I just resolved one %s problem!I must reward myself a piece of honey!";
bearWords["solve_failure"] = "Oh my bee!I can't solve this problem!I must add it to my bookmark!";
bearWords["celebrate"] = "umm,umm,umm,It's yummy!";
char* bearChips[] = {"checkOR 3","solve","study","poolingWater","goto 0","end"};
Coder teddyBear("<<Teddy Bear>>",bearChips,bearWords);
WordsTemplate frogWords;
frogWords["opening"] = "Hi ho! %s here!\n";
frogWords["solve_success"] = "Greeeeeeat!I just resolved one %s problem.Time to have flies!";
frogWords["solve_failure"] = "hmmmmm,it's not easy to being green.Let's have a study!";
char* frogChips[] = {"checkOR 3","study","solve","poolingWater","goto 0","end"};
Coder kermitFrog("<<Kermit the Frog>>",frogChips,frogWords);
teddyBear.LogOnCSDN();
kermitFrog.LogOnCSDN();
generateQuestion(NULL);
}