User:ColorfulGalaxy/Programs:修订间差异

此后如竟没有炬火,我便是唯一的光。
ColorfulGalaxy留言 | 贡献
无编辑摘要
ColorfulGalaxy留言 | 贡献
Minor fix
第1行: 第1行:
This adaptable C++ code converts [[聊天字母|ASCII-based alphabet]] to ampersand-encoded [[PUA]].  
This adaptable C++ code converts [[聊天字母|ASCII-based alphabet]] to ampersand-encoded [[PUA]].  
There may be bugs.
There may be bugs. [http://tio.run/##7Vjdb9s2EH/XX8G4WGAjDiyK@kTiAEEfigIbVrTYU@AHhaIdoTLlSnK8rsjfnh15lE05tus2yfawJUDo@/wdj3c8OnyxOOdFKmePj28yMc2lIB8@/v7u4/Vvzptc8mKZCXLJ6ybLy6sup8rlDFjGSBT5FP7UguRTZzQi7@Vi2RAhs5rkkkixKkDLWco6n0mREX6XVoSP3QsyGuVKdUuUf7mJJ2sh@bIUS@HksgFBYVmhgBRCzpo7h5eyBg3QKsT9jReEk/E3x9U/w50LeSXhaETDfwmYeRrYR55PcfE65r6/w9yPUBggGaBlgJYBM0K0DAJcQlwiBezHzlkQBT4bun8KN@G4RLikYA5rGGoyNFxDGWGSDJUDl2mnEQJHBjhC4AiBIwSONHDor1OtvCB@eKudBS76Zrco9FCISH5ohALJSC8sRso3UQVIJtqSTZHyFHDs6h0ztPONootLbMzRG0PzBKNiAKy3iuEkPprQI85YAyemuMxWp3rxKFLcpBrJxHAzXJgRBihELkMqMDsOMZEMvbeRY1VTqC7FxMAZHiAzblpkk2SBS4xC45zh0TPDTQ9tOXIeLkaj609v378nTUk@/HFN@r@Ke1EMrE6fVel/u9NN3XtPNKCmQpXuFweGTj/GXNV0opeQhXZUQZz8HLDV6RZE/AKb0kH6HbfYzklkdXqMvBgbPg4P@9Y@k2462A/uOFkXlzb3jzLXwN5@YRDH3aioNvE7VQ2drv2YNOCNGeNNBbOB7IBkqOrhIcPtrrnxji5@V6X3wu5iLuT/Xfy6wHBHuHyrixUvRRHOCTORXDMnXB9JHBAuXvduZIR4h7vJc7v4n0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yEYWuZBtsSgE9DX0YcZbUY92oAce/MOorRiGQhw8RK/blBU1HCLtQjbpHA45PiVk28OPhbz3ZCwc22of1gLC@iyrrSSirqu6G6NmUtKxHnnq1bDKCumOlXVb/6F8bO@LUrj8bEa983O1MIyXz5aiVdtzsQBhWxySZBJiJieKoJTbxh11lYo5UMrGFbH51nAl5ro4tIMZ20MBBbiK3uOUIU2xlIf@KISO0/i4Y@xUVEzS43qMXnPQgpOLdEK60UNOvwSQArguAT2c58Nup09f1i9vYKN4NV5abWYnyjaAneyULbvrbrwO2nUFeRhD/4h2yMMnuEfoAfT89yzNQN1X1SPPoPpcfO7qC3olmiZbitu59rzQNWd4k6bNoe51mXzaZmtoDEHabUWyerlZQbM2cHh6wG6r@nMhl7o5atFuakk86I2np78A Try it online!]
 
<pre class="mw-collapsible">#define PROGRAM
<pre class="mw-collapsible">#define PROGRAM
#include <cstdio>
#include <cstdio>
第7行: 第7行:
#define elif else if
#define elif else if
// Input ends in newline
// Input ends in newline
char c=0; //input
unsigned char c=0; //input
char iq[8]; //input queue
unsigned char iq[8]; //input queue
int iql=0; //input queue length
int iql=0; //input queue length
const int lev[256]={
const int lev[256]={
第56行: 第56行:
  if(iql==0){
  if(iql==0){
   ;
   ;
  } elif(iq[0]==-30 && iq[1]==-121 && iq[2]==-89){
  } elif(iq[0]==226 && iq[1]==135 && iq[2]==167){
   if(iq[4]==96){
   if(iq[4]==96){
   if(gra[iq[3]]<129){
   if(gra[iq[3]]<129){
第68行: 第68行:
   }
   }
   printf("%c%c%d;",38,35,lev[iq[3]]-16);
   printf("%c%c%d;",38,35,lev[iq[3]]-16);
  }
} elif(iq[0]==94){
  if(iq[2]==96){
  if(gra[iq[1]]<129){
    throw("e004 space and punctuation can not be capitalized");
  }
  printf("%c%c%d;",38,35,gra[iq[1]]-16);
  }
  if(iql==2){
  if(lev[iq[1]]<129){
    throw("e005 space and punctuation can not be capitalized");
  }
  printf("%c%c%d;",38,35,lev[iq[1]]-16);
   }
   }
  } elif(iq[0]==126){
  } elif(iq[0]==126){
   if(iq[2]==96){
   if(iq[2]==96){
   if(gra[iq[1]]<129){
   if(gra[iq[1]]<129){
     throw("e004 space and punctuation can not be made middle case");
     throw("e006 space and punctuation can not be made middle case");
   }
   }
   printf("%c%c%d;",38,35,gra[iq[1]]+16);
   printf("%c%c%d;",38,35,gra[iq[1]]+16);
第78行: 第91行:
   if(iql==2){
   if(iql==2){
   if(lev[iq[1]]<129){
   if(lev[iq[1]]<129){
     throw("e005 space and punctuation can not be made middle case");
     throw("e007 space and punctuation can not be made middle case");
   }
   }
   printf("%c%c%d;",38,35,lev[iq[1]]+16);
   printf("%c%c%d;",38,35,lev[iq[1]]+16);
第90行: 第103行:
  } elif(iq[1]==96){
  } elif(iq[1]==96){
   if(gra[iq[0]]<10){
   if(gra[iq[0]]<10){
   throw("e006 unidentified character");
   throw("e010 unidentified character");
   } elif(gra[iq[0]]<127){
   } elif(gra[iq[0]]<127){
   printf("%s",iq);
   printf("%s",iq);
第98行: 第111行:
  } elif(iql==1){
  } elif(iql==1){
   if(lev[iq[0]]<10){
   if(lev[iq[0]]<10){
   throw("e007 unidentified character");
   throw("e011 unidentified character");
   } elif(lev[iq[0]]<127){
   } elif(lev[iq[0]]<127){
   printf("%s",iq);
   printf("%s",iq);
第105行: 第118行:
   }  
   }  
  } else {
  } else {
   throw("e00f unknown error");
   throw("e03f unknown error");
  }
  }
  clear(c1);
  clear(c1);
第112行: 第125行:
int main() {
int main() {
  try{
  try{
   while(c!=10&&c!=13){
   while(c!=255){
   c=getchar();
   c=getchar();
   if(c==10){
   if(c==10){
第121行: 第134行:
   } elif(iql==0){
   } elif(iql==0){
     push(c);
     push(c);
   } elif(c==-30){
   } elif(c==226){
     print(c); //Capital sign
     print(c); //Capital sign
   } elif(iql==1&&c==-121){
   } elif(iql==1&&c==135){
     push(c);
     push(c);
   } elif(iql==2&&c==-89){
   } elif(iql==2&&c==167){
     push(c);
     push(c);
   } elif(31<c&&c<126&&c!=96){
   } elif(31<c&&c<126&&c!=96){
     if(iq[0]==-30 && iq[1]==-121 && iq[2]==-89 && iql==3){
     if(iq[0]==226 && iq[1]==135 && iq[2]==167 && iql==3){
    push(c);
    } elif(iq[0]==94 && iql==1){
     push(c);
     push(c);
     } elif(iq[0]==126 && iql==1){
     } elif(iq[0]==126 && iql==1){
第141行: 第156行:
   } elif(c==96){
   } elif(c==96){
     push(c);
     push(c);
  } elif(c==94){
    print(c);
   } elif(c==126){
   } elif(c==126){
     print(c);
     print(c);

2024年12月20日 (五) 22:26的版本

This adaptable C++ code converts ASCII-based alphabet to ampersand-encoded PUA. There may be bugs. Try it online!

#define PROGRAM
#include <cstdio>
#include <cstring>
#define elif else if
// Input ends in newline
unsigned char c=0; //input
unsigned char iq[8]; //input queue
int iql=0; //input queue length
const int lev[256]={
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //16
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //32
000040,000041,000042,000000, 000044,000000,000000,000047, 000050,000051,000052,000053, 000054,000055,000056,000057, //48
+57543,0xe09c,0xe097,0xe09a, 0xe066,0xe067,0xe096,0xe06a, 0xe099,+57503,000072,000073, 000074,000075,000076,000077, //64
000000,0xe093,0xe06b,+57550, 0xe03b,0xe092,0xe069,+57546, 0xe03e,+57547,+57538,+57544, 0xe095,+57549,0xe03f,+57542, //80
+57537,+57545,+57540,+57548, 0xe098,+57539,0xe09b,+57536, +57502,0xe094,+57541,000000, 000000,000000,000000,000002, //96
000003,0xe06f,0xe021,0xe06c, 0xe061,0xe091,0xe06d,0xe063, 0xe065,0xe09d,0xe035,0xe054, 0xe060,0xe033,0xe039,0xe090, //112
0xe032,0xe036,0xe03d,0xe03c, 0xe062,0xe06e,0xe068,0xe034, 0xe037,0xe038,0xe03a,000000, 000000,000000,000007
};//ASCII to PUA (Level)
const int gra[256]={
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //16
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //32
000000,0xe020,000000,000000, +57637,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //48
000000,000000,000000,000000, 000000,+57590,+57636,000000, +57589,000000,000000,000000, 000000,000000,000000,000000, //64
000000,000000,+57598,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, +57634,000000,+57595,+57597, //80
+57585,+57587,+57586,000000, 000000,000000,000000,+57639, 000000,+57593,000000,000000, 000000,000000,000000,000000, //96
000000,+57594,000000,000000, 000000,000000,000000,+57632, 000000,000000,000000,+57588, 000000,+57591,+57640,000000, //112
+57635,+57599,+57584,+57551, 000000,000000,+57633,+57592, +57596,+57638
};//ASCII to PUA (Grave)
const int cen[256]={
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //16
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //32
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,0xe00c,000000, //48
0xe00a,0xe001,0xe002,0xe003, 0xe004,0xe005,0xe006,0xe007, 0xe008,0xe009,000000,000000, 000000,000000,000000,000000, //64
000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, 000000,000000,000000,000000, //80
000000,000000,000000,000000, 0xe00b
};

void push(char c1){
 if(iql>sizeof(iq)){
  throw("e001 queue overflow");
 }
 iq[iql]=c1;
 iql+=1;
}

void clear(char c1=0){
 memset(iq,0,sizeof(iq));
 iq[0]=c1;
 iql=!!c1;
}

void print(char c1=0){
 if(iql==0){
  ;
 } elif(iq[0]==226 && iq[1]==135 && iq[2]==167){
  if(iq[4]==96){
   if(gra[iq[3]]<129){
    throw("e002 space and punctuation can not be capitalized");
   }
   printf("%c%c%d;",38,35,gra[iq[3]]-16);
  }
  if(iql==4){
   if(lev[iq[3]]<129){
    throw("e003 space and punctuation can not be capitalized");
   }
   printf("%c%c%d;",38,35,lev[iq[3]]-16);
  }
 } elif(iq[0]==94){
  if(iq[2]==96){
   if(gra[iq[1]]<129){
    throw("e004 space and punctuation can not be capitalized");
   }
   printf("%c%c%d;",38,35,gra[iq[1]]-16);
  }
  if(iql==2){
   if(lev[iq[1]]<129){
    throw("e005 space and punctuation can not be capitalized");
   }
   printf("%c%c%d;",38,35,lev[iq[1]]-16);
  }
 } elif(iq[0]==126){
  if(iq[2]==96){
   if(gra[iq[1]]<129){
    throw("e006 space and punctuation can not be made middle case");
   }
   printf("%c%c%d;",38,35,gra[iq[1]]+16);
  }
  if(iql==2){
   if(lev[iq[1]]<129){
    throw("e007 space and punctuation can not be made middle case");
   }
   printf("%c%c%d;",38,35,lev[iq[1]]+16);
  }
 } elif(iq[0]==37){
  if(iq[2]==37){
   printf("%c%c%d;",38,35,cen[iq[1]]);
  } else {
   printf("%s",iq);
  }
 } elif(iq[1]==96){
  if(gra[iq[0]]<10){
   throw("e010 unidentified character");
  } elif(gra[iq[0]]<127){
   printf("%s",iq);
  } else {
   printf("%c%c%d;",38,35,gra[iq[0]]);
  }
 } elif(iql==1){
  if(lev[iq[0]]<10){
   throw("e011 unidentified character");
  } elif(lev[iq[0]]<127){
   printf("%s",iq);
  } else {
   printf("%c%c%d;",38,35,lev[iq[0]]);
  } 
 } else {
  throw("e03f unknown error");
 }
 clear(c1);
}

int main() {
 try{
  while(c!=255){
   c=getchar();
   if(c==10){
    print();
    puts("");
   } elif(c==13){
    ;
   } elif(iql==0){
    push(c);
   } elif(c==226){
    print(c); //Capital sign
   } elif(iql==1&&c==135){
    push(c);
   } elif(iql==2&&c==167){
    push(c);
   } elif(31<c&&c<126&&c!=96){
    if(iq[0]==226 && iq[1]==135 && iq[2]==167 && iql==3){
     push(c);
    } elif(iq[0]==94 && iql==1){
     push(c);
    } elif(iq[0]==126 && iql==1){
     push(c);
    } elif(iql>1&&iq[iql-1]==96){
     print(c);
    } elif(31<iq[iql-1]&&iq[iql-1]<126){
     print(c);
    } else {
     push(c);
    }
   } elif(c==96){
    push(c);
   } elif(c==94){
    print(c);
   } elif(c==126){
    print(c);
   } else {
    push(c);
   }
  }
  print();
 }catch(const char message[]){
  fputs(message,stderr); //Swap the two arguments if not working
 }
 return 0;
}