33,311
社区成员
发帖
与我相关
我的任务
分享
#include <process.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <queue>
#include <time.h>
#include <stdarg.h>
#include <math.h>
using namespace std;
static const char* qType[] = {"C/C++","JAVA","PHP"};
struct question{
int type;
int score;
};
typedef queue<question> questions;
questions unsolved;
class Coder{
public:
char name[256];
int score;
int exp[3];
questions bookmark;
Coder(const char* name):score(0){
strcpy_s(this->name,256,name);
::memset(exp,0,sizeof(exp));
}
void LogOnCSDN();
bool solve(question& q);
void study(question& q);
void say(char* format,...);
void info(char* format,...);
void update();
void poolingWater(){say("%s is pooling water....",name);Sleep(3000);};
};
void Coder::say(char* format,...){
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);
printf("[%s]: %s\n",this->name,s);
};
void Coder::info(char* format,...){
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);
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.",name,qType[q.type]);
info(words);
for(int i = 0;i<5;i++){
::Sleep(1000);
printf(".");
}
printf("\n");
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::LogOnCSDN(){
say("Hi!I'm a new fish on CSDN!My name is %s.\n",name);
while(true){
update();
::Sleep(1000);
if(!unsolved.empty()){
info("%s is trying to solve a problem.\n",name);
question q = ::unsolved.front();
unsolved.pop();
if(solve(q)){
say("I just resolved one %s problem!I must reward myself a piece of honey!",qType[q.type]);
this->score+=q.score;
say("umm,umm,umm,It's yummy!");
Sleep(100);
}else{
say("Oh my bee!I can\'t solve this problem!I must add it to my bookmark!");
bookmark.push(q);
}
::Sleep(500);
}else if(!bookmark.empty()){
//learning
question q = bookmark.front();
this->study(q);
bookmark.pop();
}else{
this->poolingWater();
}
}
}
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(3000);
}
::_endthread();
}
int _tmain(int argc, _TCHAR* argv[])
{
time_t t;
::time(&t);
::srand((UINT)t);
::_beginthread(generateQuestion,0,0);
Coder teddyBear("Teddy Bear");
teddyBear.LogOnCSDN();
return 0;
}