[DEMO] 年份判断

#include 
#include 
#include 
#include 



std::string a = "^2024~2026";   //>=2024 and <=2026
std::string b = "=2024";        //=2024
std::string c = "=2024^2027";   //=2024 and >=2027

std::vector > f_list;

using namespace std::placeholders;

bool isGE(int target,int standard){
  return target >= standard;
}

bool isLE(int target,int standard){
  return target <= standard;
}

bool isEQ(int target,int standard){
  return target == standard;
}

bool test(int i ,int l){
  printf("%d,%d\n",i,l);
  return true;
}

void parse(std::string s){
  const char* ptr = s.c_str();
  int len = s.length();
  for(int i = 0; i <= len ;i++){
    char e = ptr[i];
    if(e=='^'|| e=='=' || e=='~'){
        //read 4 chars
        if(i+4>=len){
          printf("out of scope");
        }
        if(ptr[i+1]>='2'&& ptr[i+1]<'9'
           && ptr[i+2]>='0'&& ptr[i+2]<'9'
           && ptr[i+3]>='0'&& ptr[i+3]<'9'
           && ptr[i+4]>='0'&& ptr[i+4]<'9'
        ){
           int v = (ptr[i+1]-'0')*1000 +
                        (ptr[i+2]-'0')*100 +
                         (ptr[i+3]-'0')*10 +
                          (ptr[i+4]-'0');
           if(e == '^'){
             printf(" >= %d\n",v);
             f_list.emplace_back(std::bind(isGE,_1,v));
           }else if(e == '='){
             printf(" == %d\n",v);
             f_list.emplace_back(std::bind(isEQ,_1,v));
           }else if(e == '~'){
             printf(" <= %d\n",v);
             f_list.emplace_back(std::bind(isLE,_1,v));
           }
        }
    }
  }
}

int main(){
  parse(a);
  bool ret = true;
  printf("%d\n",ret);
  for(auto e:f_list){
    printf("%d\n",ret);
    ret = ret && e(2023);
  }
}

你可能感兴趣的:(DEMO,c++,开发语言)