2013年5月31日金曜日

覆面算の実行結果:4連足し算


0+0+30+50=80みたく


 唯一解
               zero
               zero
             thirty
              fifty
             eighty

               3245
               3245
             176410
              86810
             269710


 唯一解
               zero
               zero
              forty
              fifty
             ninety

               3759
               3759
              69582
              64682
             141782

p=0 q=5 r=27 s=37 から再開します

 唯一解
               zero
                ten
              forty
              forty
             ninety

               7986
                291
              46823
              46823
             101923


 唯一解
                one
               nine
             twenty
              fifty
             eighty

                984
               8584
             364832
              75732
             450132


 唯一解
                two
           fourteen
            fifteen
             twenty
           fiftyone

                236
           16072994
            1812994
             239425
           18125649


 唯一解
              three
              three
              three
             eleven
             twenty

              73544
              73544
              73544
             494046
             714678


 唯一解
               five
               five
               nine
             eleven
             thirty

               4027
               4027
               5057
             797275
             810386


 唯一解
               five
               five
             thirty
              forty
             eighty

               3059
               3059
             860782
              34782
             901682


 唯一解
               five
               five
             thirty
              fifty
             ninety

               9371
               9371
             523458
              93958
             636158

p=5 q=6 r=28 s=35 から再開します

 唯一解
                six
              eight
              eight
              eight
             thirty

                842
              64391
              64391
              64391
             194015


 唯一解
              seven
              seven
              seven
               nine
             thirty

              49793
              49793
              49793
               3239
             152618


 唯一解
              seven
                ten
                ten
           fourteen
           fortyone

              78082
                482
                482
           19564882
           19643928




 唯一解
             eleven
             eleven
            fifteen
           nineteen
           fiftysix

             151618
             151618
            9790118
           87810118
           97903472

友達のジェイ君にarmcmm落とせんよって言われたのでDLテスト

[[armcmmが入っている zip]]

ジェイ君落としてみて。うまくいくかなあ

2013年5月26日日曜日

覆面算の実行結果のほんの一部




 唯一解
            apple
             able
           comedy

            93378
             9478
           102856


 唯一解
            apple
             able
           convey

            96637
             9237
           105874


 唯一解
            apple
            about
           behalf

            82276
            81594
           163870


 唯一解
            apple
            about
           butter

            92264
            91083
           183347


 唯一解
            apple
           accent
           casual

            84472
           899215
           983687


 唯一解
            apple
           accent
           change

            68893
           677320
           746213


 唯一解
            apple
           accent
           clothe

            52218
           566830
           619048


 唯一解
            apple
           accent
           clover

            47709
           455982
           503691


 唯一解
            apple
           accept
           betray

            87713
           844372
           932085


 唯一解
            apple
           accept
           branch

            85567
           822754
           908321


 唯一解
            apple
           accept
           cancel

            87741
           899173
           986914


 唯一解
            apple
           accept
           climax

            53314
           566438
           619752


 唯一解
            apple
           accept
           colony

            53309
           566938
           620247


 唯一解
            apple
           accept
           column

            53308
           566839
           620147


 唯一解
            apple
              add
            break

            59924
              533
            60457


 唯一解
            apple
              add
            clerk

            59904
              577
            60481


 唯一解
            apple
              add
            trust

            79915
              733
            80648


 唯一解
            apple
           advise
           canvas

            37706
           398126
           435832

修正後の辞書$./list6 eng2.dic eng2.dic(ENTER)


ability
able
about
accent
accept
across
add
admit
advice
advise
affair
after
again
age
ago
agree
air
all
almost
also
always
among
anger
angry
ant
any
ape
apple
apply
april
arm
art
as
assist
atlas
auto
avoid
away
baby
back
bad
bag
band
bang
bar
bath
battle
beach
beam
beat
beauty
because
become
bed
bee
beef
beer
before
begin
behalf
belief
believe
bell
belong
belt
bench
bend
best
bet
betray
better
between
bible
bicycle
big
bill
bio
birth
bite
black
blame
blank
bless
block
blood
bloom
blue
board
boat
body
boil
bold
bolt
bomb
bond
bone
bonus
book
bore
born
boss
both
bottle
bottom
bound
bow
bowl
box
boy
brain
brake
branch
brave
bread
break
breakfast
bribe
bride
bridge
brief
bright
bring
brisk
britain
british
broad
broadcast
bronze
brown
brush
bubble
bucket
budget
build
building
bulldog
bulldozer
bump
burden
burn
burst
bus
bush
business
busy
but
butter
butterfly
button
buy
buzz
by
cab
cabin
cable
cafeteria
cage
calculate
calendar
call
calm
camel
camera
camp
campus
can
canal
cancel
cancer
candle
candy
cannot
canvas
cap
capacity
cape
capital
captain
capture
car
card
care
careful
carnation
carnival
carol
carpenter
carpet
carrot
carry
cart
carve
case
cash
cast
casual
cat
catch
cattle
cause
cave
celebrate
cell
cellar
cent
center
central
century
ceremony
certain
chain
chair
chairman
champion
chance
change
chaos
chapel
chapter
character
charge
charm
charming
chart
chat
cheap
check
cheek
cheer
cheerful
cheese
chemical
chemistry
cherry
chess
chicken
child
childhood
childish
chimney
china
chinese
chocolate
choice
choose
chop
chorus
christ
christian
christmas
churchcigar
cinema
circle
circuit
circus
citizen
city
civic
civil
claim
clam
clap
clash
class
classic
clay
clean
clear
clerk
clever
client
cliff
climate
climax
climb
clip
clock
close
closet
cloth
clothe
clothes
cloud
cloudy
clover
clown
club
clue
coach
coal
coast
cock
cocoa
code
coffee
coin
coke
cold
collapse
collect
collection
colon
colony
color
column
comb
combat
combination
come
comedy
comfort
comma
command
comment
commercial
commission
committee
commoncommunicate
communism
community
compact
company
compare
compass
compete
compile
complain
complex
compliment
component
compromise
compute
concentrate
concept
concern
concert
conclude
conclusion
concrete
condition
conduct
conductor
conference
confess
confidence
confirm
conflict
conform
confront
confuse
confusion
congratulate
congratulation
congress
connect
connection
conquer
conscience
conscious
consent
consequence
conserve
consider
consideration
consist
consistent
console
constant
construct
construction
consult
consume
consumption
contact
contain
contemporary
contest
context
continent
continue
contract
contradict
contrary
contrast
contribute
contribution
control
convene
convenience
convenient
convention
conversation
converse
convert
convey
convict
conviction
convince
cook
cool
cooperate
cope
copper
copy
cord
day
dog
down
for
forth
four
from
in
into
land
little
make
money
more
not
off
on
oneself
out
reason
recommend
root
round
run
semicolon
send
sub
take
to
tree
trust
try
up
upon
way
with
without

覆面算の辞書eng2.dicが重複してたので重複をなくすプログラムlist6.c


$ gcc -o list6 list6.c

$ ./list6 eng2.dic eng2.dic

argv[1]=ロードするファイル

argv[2]=セーブするファイル

先ほどアップしたeng2.dicはa〜cまでの途中で
zまで入れたらどうなるんだろう

重複するのでlist6でシェイプアップ&アルファベットにソート




#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX  200
#define EEOF 0xff

struct line{
   char text[MAX];
   long  count;
   struct line *next;
};

struct line *stt,*start,*last,*gcdata,*info,*gc2;
struct line *shojun(struct line *);
struct line *node_create();


main(argc,argv)
int argc;
char *argv[];
{
   char p,fname[40];
   FILE *fp,*fs;
   long pos=0,gyo=1;

   printf("MAX=%d \n",MAX);

   if(argc<3){
      printf("No File.\n\n");
      printf("./list6 [Load File] [Save File]\n");
      printf("./list6 abc.txt     abc.txt\n");
      exit(1);
   }


   fp=fopen(argv[1],"r");
   if(fp==NULL){
      printf("cannot open file %s \n",argv[1]);
      exit(1);
   }
   printf("file name %s \n",argv[1]);

   gcdata=start=gc2=node_create();
   while( (p=getc(fp)) != EEOF){
      if(p=='\n'){
         gcdata->text[pos]='\0';
         gcdata->count=1;
         gcdata->next =node_create();
         gc2          =gcdata;
         gcdata       =gcdata->next;
         pos=0;
      }
      else{
         if(pos>=MAX) break;
         gcdata->text[pos++]=p;
      }
   }
   gc2->next=NULL;;

   fclose(fp);
   stt=start;

   info=start;
   while(info){
      start     =info->next;
      info->next=NULL;
      stt       =shojun(info);
      info      =start;
   }

   fs=fopen(argv[2],"w");
   if(fs==NULL){
      printf("cannot open file %s \n",argv[2]);
      exit(1);
   }
   printf("file name %s \n",argv[2]);

   info=stt;
   /*   info=info->next;*/
   while(info){
      fprintf(fs,"%s\n",info->text);
      info=info->next;
   }
   fclose(fs);

}


struct line *node_create()
{
   struct line *from;
   int size;

   size=sizeof(struct line);
   from=(struct line *)malloc(size);
   if(!from){
      printf("out of memory \n");
      exit(1);
   }
   from->next=NULL;

   return from;
}

struct line *shojun(temp)
struct line *temp;
{
   struct line *old;
   struct line *now;

   now=stt;
   old=NULL;

   if(!last){
      temp->next=NULL;
      last      =temp;
      return temp;
   }
   while(now){
      if(strcmp(now->text,temp->text) == 0){
         now->count++;
         return stt;
      }
      if(strcmp(now->text,temp->text) < 0){
         old=now;
         now=now->next;
      }
      else{
         if(old){
            old ->next=temp;
            temp->next=now;
            return stt;
         }
         else{
            temp->next=now;
            return temp;
         }
      }
   }
   last->next=temp;
   temp->next=NULL;
   last      =temp;
   return stt;
}


fuku8.cに必要な辞書eng2.dic


apple
car
cat
dog
money
more
run
send
tree
try
ability
able
about
accent
accept
not
any
come
add
sub
admit
advise
advice
affair
after
again
all
age
ago
agree
angry
air
make
almost
also
always
among
anger
ant
ape
apply
april
arm
art
as
assist
atlas
auto
avoid
on
baby
back
bad
bag
band
bang
bar
into
bath
battle
beach
beam
beauty
because
become
bed
bee
beef
beer
beat
before
begin
behalf
belief
believe
bell
belt
bench
belong
bend
best
bet
betray
better
between
bible
bicycle
big
four
bill
bio
birth
bite
black
blame
blank
bless
block
blood
bloom
blue
board
boat
body
boil
bold
bolt
bomb
bond
bone
book
bonus
bore
born
boss
both
bottle
bottom
bound
bow
bowl
box
boy
brain
branch
brake
brave
bread
break
breakfast
bribe
bride
bridge
brief
bright
bring
brisk
britain
british
broad
broadcast
bronze
brown
brush
bubble
bucket
budget
build
building
bulldog
bulldozer
bump
burden
burn
burst
bus
bush
business
busy
but
little
butter
butterfly
button
buy
buzz
by
chance
day
land
oneself
reason
way
cab
cabin
cable
cafeteria
cage
calendar
call
for
forth
on
upon
out
up
calm
camel
camera
camp
campus
can
canal
cancel
candle
candy
cannot
root
with
without
canvas
cap
capacity
cape
capital
captain
capture
car
card
care
careful
carol
carnation
carnival
carol
carpenter
carpet
carrot
carry
away
off
on
out
in
cart
carve
case
cash
cast
casual
cat
catch
cattle
cause
cave
celebrate
cell
cellar
cent
century
center
cancer
central
certain
ceremony
chain
chair
chairman
champion
chance
change
chaos
chapel
chapter
character
charge
charm
charming
chart
chat
cheap
check
cheek
cheer
cheerful
cheese
chemical
chemistry
cherry
chess
chicken
child
childhood
childish
chimney
china
chinese
chocolate
choice
choose
chop
chorus
christ
christian
christmas
churchcigar
cinema
circle
circuit
circus
citizen
city
civic
civil
claim
clam
clap
clash
class
classic
clay
clean
clear
clerk
clever
client
cliff
climate
climax
climb
clip
clock
close
closet
cloth
clothe
clothes
cloud
cloudy
clover
clown
club
clue
coach
coal
coast
cock
cocoa
code
coffee
coin
coke
cold
collapse
collect
collection
colon
semicolon
colony
color
column
comb
combat
combination
come
about
across
by
from
to
round
up
down
comedy
comfort
comma
command
recommend
comment
commercial
commission
committee
commoncommunicate
communism
community
compact
company
compare
compass
compete
compile
complain
compete
complex
compliment
component
compromise
compute
calculate
concentrate
concept
concern
concert
conclude
conclusion
concrete
condition
conduct
conductor
conference
confess
confidence
trust
confirm
conflict
conform
confront
confuse
confusion
congratulate
congratulation
congress
connect
connection
conquer
conscience
conscious
consent
consequence
conserve
consider
consideration
take
consist
consistent
console
constant
construct
construction
consult
consume
consumption
contact
contain
contemporary
contest
context
continent
continue
contract
contradict
contrary
contrast
contribute
contribution
control
convene
convenience
convenient
convention
conversation
converse
convert
convey
convict
conviction
convince
cook
cool
cooperate
cope
copy
copper
cord

覆面算、辞書を作って唯一解を量産するfuku8.c


ライブラリ libncurses5-dev が必要

$ sudo apt-get install libncurses5-dev

だったかな

$ gcc -o fuku8 fuku8.c
$ ./fuku8

で実行。計算が長いので、qボタンかESCボタンで中断

$ ./fuku8

で中断したところから再開。辞書ファイルeng2.dicが必要。今からブログにかくよ

実行結果は、fuku8a.txtを見ること

計算が最後まで終わり、再び最初からはじめたい場合は

$ rm fuku8a.try
($ rm fuku8a.txt)





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <termios.h>  /* ライブラリ libncurses5-dev が必要 */
#include <term.h>
enum {FALSE,TRUE};
#define N 10
#define MAX 128
#define EEOF 0xff

/* 中断データは終わったら削除すること */
/* qボタン、ESCボタンを押すと中断、実行ファイルで再開 */

char  txt[20]="fuku8a.txt"; /* セーブするファイル名 */
char ttry[20]="fuku8a.try"; /* 中断データを入れるファイル名 */
char dict[20]="eng2.dic";

/*
覆面算

参考資料:

技術評論社 奥村晴彦 著
C言語による最新アルゴリズム事典 2330円+税
P238〜239より引用・改変

*/

struct line{
   unsigned char text[128];
   long  count;
   struct line *next;
};

struct line *stt,*start,*last,*gcdata,*info,*gc2;
struct line *node_create();

static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard();
void close_keyboard();
int kbhit();
int readch();

int imax,jmax,solution,ch;
int word[ N][128],digit[ 256];
int word1[N][128],digit1[256];
int low[256],ok[256];
FILE *fp;

void found();
void found1();
void try(int sum);
void input(int i,unsigned char buffer[128]);

void found(){
   int i,j,c;

   ++solution;
   if(solution>1) return;
   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j]=word[i][j];
         if(0x41<=c&&c<=0x5a) c+=0x20;
         if(c) digit1[c]=digit[c];
         else  digit1[c]=' ';
      }
   }
}

void found1(){
   int i,j,c;

  fp=fopen(txt,"a");
  if(fp==NULL){
     printf("fail\n");
     exit(1);
   }

   printf("\33[2J\33[0;0H");
   printf("\n 唯一解 \n");
   fprintf(fp,"\n 唯一解 \n");
   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j];
         if(c){
            printf("%c",c);
            fprintf(fp,"%c",c);
         }
         else{
            printf(" ");
            fprintf(fp," ");
         }
      }
      printf("\n");
      fprintf(fp,"\n");
   }
   printf("\n");
   fprintf(fp,"\n");

   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j];
         if(0x41<=c&&c<=0x5a) c+=0x20;
         if(c){
            printf("%d",digit1[c]);
            fprintf(fp,"%d",digit1[c]);
         }
         else{
            printf(" ");
            fprintf(fp," ");
         }
      }
      printf("\n");
      fprintf(fp,"\n");
   }
   printf("\n");
   fprintf(fp,"\n");
   fclose(fp);
}

void try(int sum){
   static int i=0,j=0,carry;
   int c,d;

   c=word[i][j];
   if(0x41<=c&&c<=0x5a) c+=0x20;
   if(i<imax){
      i++;
      if((d=digit[c])<0){
         for(d=low[c];d<=9;d++){
            if(ok[d]){
               digit[c]=d; ok[d]=FALSE;
               try(sum+d); ok[d]=TRUE;
            }
         }
         digit[c]=-1;
      }
      else try(sum+d);
      i--;
   }
   else{
      j++; i=0; d=sum%10; carry=sum/10;
      if(digit[c]==d){
         if(j<=jmax) try(carry);
         else if(carry==0) found();
      }
      else if(digit[c]<0&&ok[d]&&d>=low[c]){
          digit[c]=d; ok[d]=FALSE;
          if(j<=jmax) try(carry);
          else if(carry==0) found();
          digit[c]=-1; ok[d]=TRUE;
      }
      j--; i=imax;
   }
}

main(){
   int i,j,k,c,p,q,r,s,t,u,z;
   static unsigned char buffer[128];
   int pos,gyo;
   char cp;

   fp=fopen(dict,"r");
   if(fp==NULL){
      printf("cannot open file %s \n",dict);
      exit(1);
   }
   printf("file name %s \n",dict);

   gyo=1;pos=0;
   gcdata=start=gc2=node_create();
   while( (cp=getc(fp)) != EEOF){
      if(cp=='\n'){
         gcdata->text[pos]='\0';
         gcdata->count=1;
         gcdata->next =node_create();
         gc2          =gcdata;
         gcdata       =gcdata->next;
         pos=0;gyo++;
      }
      else{
         if(pos>=MAX) break;
         gcdata->text[pos++]=cp;
      }
   }
   gc2->next=NULL;;
   gyo--;
   fclose(fp);

   init_keyboard();
   printf("\33[2J\33[0;0H");
   jmax=0;
   imax=3;
   printf("計算式の行数は?: %d",imax); imax--;

   fp=fopen(ttry,"r");
   if(fp==NULL){
      printf("中断データはありません\n");
      fp=fopen(txt,"a");
      if(fp==NULL){
         printf("fail\n");
         exit(1);
      }
       printf(   "辞書の行数は %d\n",gyo);
      fprintf(fp,"辞書の行数は %d\n",gyo);
      fclose(fp);
   }
   else{
      fscanf(fp,"%d,%d,%d",&p,&q,&r);
      r++;
      fclose(fp);

      fp=fopen(txt,"a");
      if(fp==NULL){
         printf("fail\n");
         exit(1);
      }
       printf(   "p=%d q=%d r=%d から再開します\n",p,q,r);
      fprintf(fp,"p=%d q=%d r=%d から再開します\n",p,q,r);
      fclose(fp);
      goto SAIKAI;
   }


   for(p=0;p<gyo;p++){
      for(q=p;q<gyo;q++){
      for(r=0;r<gyo;r++){
SAIKAI:
            z++;
            if(z%256==0){
               printf("\33[0;0Hp=%d q=%d r=%d       \n",p,q,r);
               z=0;
            }
            for(j=0;j<N;j++)
               for(i=0;i<128;i++) word[j][i]=0;
            for(i=0;i<256;i++) digit[i]=0;
            for(i=0;i<256;i++) low[i]=0;
            for(i=0;i<256;i++) ok[i]=0;
            i=0;info=start;
            while(i<p&&info){ info=info->next;i++;}
            input(0,info->text);
            i=0;info=start;
            while(i<q&&info){ info=info->next;i++;}
            input(1,info->text);
            i=0;info=start;
            while(i<r&&info){ info=info->next;i++;}
            input(2,info->text);

            for(i=0;i<=9;i++) ok[i]=TRUE;
            solution=0; try(0);
            if(solution==1) found1();
            if(kbhit()){
               ch = readch();
               if(ch=='q'||ch==0x1b){
                  fp=fopen(ttry,"w");
                  if(fp==NULL){
                     printf("fail\n");
                     exit(1);
                   }
                   printf(   "p=%d q=%d r=%d で中断します\n",p,q,r);
                   fprintf(fp,"%d,%d,%d\n",p,q,r);
                   fclose(fp);
                   close_keyboard();
                   exit(0);
                }
            }
         }
      }
   }

   fp=fopen(ttry,"w");
   if(fp==NULL){
      printf("fail\n");
      exit(1);
   }
   printf(   "p=%d q=%d r=%d で終了しました\n",p,q,r);
   fprintf(fp,"%d,%d,%d\n",p,q,r);
   fclose(fp);

   close_keyboard();
}


void input(int i,unsigned char buffer[128]){
   int k,j,c;

     c=buffer[0];
     if(0x41<=c&&c<=0x5a) c+=0x20;
     low[c]=1;
      k=strlen((char *)buffer)-1;
      if(k>jmax) jmax=k;
      for(j=0;j<=k;j++){
         c=word[i][j]=buffer[k-j];
         if(0x41<=c&&c<=0x5a) c+=0x20;
         if(isalpha(c)) digit[c]=-1;
         else if(isdigit(c)) digit[c]=c-'0';
      }
}



void init_keyboard()
{
    tcgetattr(0, &initial_settings);
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 0;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &initial_settings);
}

void close_keyboard()
{
    tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
    char ch;
    int nread;

    if(peek_character != -1)
        return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0, &ch, 1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if(nread == 1) {
        peek_character = ch;
        return 1;
    }
    return 0;
}


int readch()
{
    char ch;

    if(peek_character != -1) {
        ch = peek_character;
        peek_character = -1;
        return ch;
    }
    read(0, &ch, 1);
    return ch;
}


struct line *node_create()
{
   struct line *from;
   int size;

   size=sizeof(struct line);
   from=(struct line *)malloc(size);
   if(!from){
      printf("out of memory \n");
      exit(1);
   }
   from->next=NULL;

   return from;
}

覆面算fuku7.cの実行結果


p=0 q=7 r=89 から再開します

 唯一解
                six
              seven
              seven
             twenty

                650
              68782
              68782
             138214


 唯一解
                ten
                ten
              forty
              sixty

                850
                850
              29786
              31486

覆面算のプログラムfuku7.c


ライブラリ libncurses5-dev が必要

$ sudo apt-get install libncurses5-dev

だったかな

$ gcc -o fuku7 fuku7.c
$ ./fuku7

で実行。計算が長いので、qボタンかESCボタンで中断

$ ./fuku7

で中断したところから再開

実行結果は、fuku7.txtを見ること

計算が最後まで終わり、再び最初からはじめたい場合は

$ rm fuku7.try
($ rm fuku7.txt)



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <termios.h>  /* ライブラリ libncurses5-dev が必要 */
#include <term.h>
enum {FALSE,TRUE};
#define N 10
#define T42 111   /* 20〜111までいろいろ値を変えてみよう */

/* 中断データは終わったら削除すること */
/* qボタン、ESCボタンを押すと中断、実行ファイルで再開 */

char  txt[20]="fuku7.txt"; /* セーブするファイル名 */
char ttry[20]="fuku7.try"; /* 中断データを入れるファイル名 */


/*
覆面算

参考資料:

技術評論社 奥村晴彦 著
C言語による最新アルゴリズム事典 2330円+税
P238〜239より引用・改変

*/
static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard();
void close_keyboard();
int kbhit();
int readch();

int imax,jmax,solution,ch;
int word[ N][128],digit[ 256];
int word1[N][128],digit1[256];
int low[256],ok[256];
unsigned char nn[128][128];
FILE *fp;

void found();
void found1();
void try(int sum);
void input(int i,unsigned char buffer[128]);

void found(){
   int i,j,c;

   ++solution;
   if(solution>1) return;
   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j]=word[i][j];
         if(c) digit1[c]=digit[c];
         else  digit1[c]=' ';
      }
   }
}

void found1(){
   int i,j,c;

  fp=fopen(txt,"a");
  if(fp==NULL){
     printf("fail\n");
     exit(1);
   }

   printf("\33[2J\33[0;0H");
   printf("\n 唯一解 \n");
   fprintf(fp,"\n 唯一解 \n");
   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j];
         if(c){
            printf("%c",c);
            fprintf(fp,"%c",c);
         }
         else{
            printf(" ");
            fprintf(fp," ");
         }
      }
      printf("\n");
      fprintf(fp,"\n");
   }
   printf("\n");
   fprintf(fp,"\n");

   for(i=0;i<=imax;i++){
      for(j=jmax;j>=0;j--){
         c=word1[i][j];
         if(c){
            printf("%d",digit1[c]);
            fprintf(fp,"%d",digit1[c]);
         }
         else{
            printf(" ");
            fprintf(fp," ");
         }
      }
      printf("\n");
      fprintf(fp,"\n");
   }
   printf("\n");
   fprintf(fp,"\n");
   fclose(fp);
}

void try(int sum){
   static int i=0,j=0,carry;
   int c,d;

   c=word[i][j];
   if(i<imax){
      i++;
      if((d=digit[c])<0){
         for(d=low[c];d<=9;d++){
            if(ok[d]){
               digit[c]=d; ok[d]=FALSE;
               try(sum+d); ok[d]=TRUE;
            }
         }
         digit[c]=-1;
      }
      else try(sum+d);
      i--;
   }
   else{
      j++; i=0; d=sum%10; carry=sum/10;
      if(digit[c]==d){
         if(j<=jmax) try(carry);
         else if(carry==0) found();
      }
      else if(digit[c]<0&&ok[d]&&d>=low[c]){
          digit[c]=d; ok[d]=FALSE;
          if(j<=jmax) try(carry);
          else if(carry==0) found();
          digit[c]=-1; ok[d]=TRUE;
      }
      j--; i=imax;
   }
}

main(){
   int i,j,k,c,p,q,r,s,t,u,z;
   static unsigned char buffer[128];

   strcpy(nn[0],"zero");
   strcpy(nn[1],"one");
   strcpy(nn[2],"two");
   strcpy(nn[3],"three");
   strcpy(nn[4],"four");
   strcpy(nn[5],"five");
   strcpy(nn[6],"six");
   strcpy(nn[7],"seven");
   strcpy(nn[8],"eight");
   strcpy(nn[9],"nine");
   strcpy(nn[10],"ten");
   strcpy(nn[11],"eleven");
   strcpy(nn[12],"twelve");
   strcpy(nn[13],"thirteen");
   strcpy(nn[14],"fourteen");
   strcpy(nn[15],"fifteen");
   strcpy(nn[16],"sixteen");
   strcpy(nn[17],"seventeen");
   strcpy(nn[18],"eighteen");
   strcpy(nn[19],"nineteen");
   strcpy(nn[20],"twenty");
   strcpy(nn[21],"twentyone");
   strcpy(nn[22],"twentytwo");
   strcpy(nn[23],"twentythree");
   strcpy(nn[24],"twentyfour");
   strcpy(nn[25],"twentyfive");
   strcpy(nn[26],"twentysix");
   strcpy(nn[27],"twentyseven");
   strcpy(nn[28],"twentyeight");
   strcpy(nn[29],"twentynine");
   strcpy(nn[30],"thirty");
   strcpy(nn[31],"thirtyone");
   strcpy(nn[32],"thirtytwo");
   strcpy(nn[33],"thirtythree");
   strcpy(nn[34],"thirtyfour");
   strcpy(nn[35],"thirtyfive");
   strcpy(nn[36],"thirtysix");
   strcpy(nn[37],"thirtyseven");
   strcpy(nn[38],"thirtyeight");
   strcpy(nn[39],"thirtynine");
   strcpy(nn[40],"forty");
   strcpy(nn[41],"fortyone");
   strcpy(nn[42],"fortytwo");
   strcpy(nn[43],"fortythree");
   strcpy(nn[44],"fortyfour");
   strcpy(nn[45],"fortyfive");
   strcpy(nn[46],"fortysix");
   strcpy(nn[47],"fortyseven");
   strcpy(nn[48],"fortyeight");
   strcpy(nn[49],"fortynine");
   strcpy(nn[50],"fifty");
   strcpy(nn[51],"fiftyone");
   strcpy(nn[52],"fiftytwo");
   strcpy(nn[53],"fiftythree");
   strcpy(nn[54],"fiftyfour");
   strcpy(nn[55],"fiftyfive");
   strcpy(nn[56],"fiftysix");
   strcpy(nn[57],"fiftyseven");
   strcpy(nn[58],"fiftyeight");
   strcpy(nn[59],"fiftynine");
   strcpy(nn[60],"sixty");
   strcpy(nn[61],"sixtyone");
   strcpy(nn[62],"sixtytwo");
   strcpy(nn[63],"sixtythree");
   strcpy(nn[64],"sixtyfour");
   strcpy(nn[65],"sixtyfive");
   strcpy(nn[66],"sixtysix");
   strcpy(nn[67],"sixtyseven");
   strcpy(nn[68],"sixtyeight");
   strcpy(nn[69],"sixtynine");
   strcpy(nn[70],"seventy");
   strcpy(nn[71],"seventyone");
   strcpy(nn[72],"seventytwo");
   strcpy(nn[73],"seventythree");
   strcpy(nn[74],"seventyfour");
   strcpy(nn[75],"seventyfive");
   strcpy(nn[76],"seventysix");
   strcpy(nn[77],"seventyseven");
   strcpy(nn[78],"seventyeight");
   strcpy(nn[79],"seventynine");
   strcpy(nn[80],"eighty");
   strcpy(nn[81],"eightyone");
   strcpy(nn[82],"eightytwo");
   strcpy(nn[83],"eightythree");
   strcpy(nn[84],"eightyfour");
   strcpy(nn[85],"eightyfive");
   strcpy(nn[86],"eightysix");
   strcpy(nn[87],"eightyseven");
   strcpy(nn[88],"eightyeight");
   strcpy(nn[89],"eightynine");
   strcpy(nn[90],"ninety");
   strcpy(nn[91],"ninetyone");
   strcpy(nn[92],"ninetytwo");
   strcpy(nn[93],"ninetythree");
   strcpy(nn[94],"ninetyfour");
   strcpy(nn[95],"ninetyfive");
   strcpy(nn[96],"ninetysix");
   strcpy(nn[97],"ninetyseven");
   strcpy(nn[98],"ninetyeight");
   strcpy(nn[99],"ninetynine");
   strcpy(nn[100],"onehundred");
   strcpy(nn[101],"onehundredandone");
   strcpy(nn[102],"onehundredandtwo");
   strcpy(nn[103],"onehundredandthree");
   strcpy(nn[104],"onehundredandfour");
   strcpy(nn[105],"onehundredandfive");
   strcpy(nn[106],"onehundredandsix");
   strcpy(nn[107],"onehundredandseven");
   strcpy(nn[108],"onehundredandeight");
   strcpy(nn[109],"onehundredandnine");
   strcpy(nn[110],"onehundredandten");
   strcpy(nn[111],"onehundredandeleven");

   init_keyboard();
   printf("\33[2J\33[0;0H");
   jmax=0;
   imax=4;
   printf("行数は?: %d",imax); imax--;

   fp=fopen(ttry,"r");
   if(fp==NULL){
      printf("中断データはありません\n");
      fp=fopen(txt,"a");
      if(fp==NULL){
         printf("fail\n");
         exit(1);
      }
      /*fprintf(fp,"中断データはありません\n");*/
      fclose(fp);
   }
   else{
      fscanf(fp,"%d,%d,%d",&p,&q,&r);
      r++;
      fclose(fp);

      fp=fopen(txt,"a");
      if(fp==NULL){
         printf("fail\n");
         exit(1);
      }
       printf(   "p=%d q=%d r=%d から再開します\n",p,q,r);
      fprintf(fp,"p=%d q=%d r=%d から再開します\n",p,q,r);
      fclose(fp);
      goto SAIKAI;
   }


   jmax=0;
   imax=4;
   printf("行数は?: %d",imax); imax--;
   for(p=0;p<=T42;p++){
      for(q=p;q<=T42;q++){
      for(r=q;r<=T42;r++){
SAIKAI:
         if(p+q+r<=111){
            z++;
            if(z%256==0) printf("\33[0;0Hp=%d q=%d r=%d       \n",p,q,r);
            for(j=0;j<N;j++)
               for(i=0;i<128;i++) word[j][i]=0;
            for(i=0;i<256;i++) digit[i]=0;
            for(i=0;i<256;i++) low[i]=0;
            for(i=0;i<256;i++) ok[i]=0;
            input(0,nn[p]);
            input(1,nn[q]);
            input(2,nn[r]);
            input(3,nn[p+q+r]);

            for(i=0;i<=9;i++) ok[i]=TRUE;
            solution=0; try(0);
            if(solution==1) found1();
            if(kbhit()){
               ch = readch();
               if(ch=='q'||ch==0x1b){
                  fp=fopen(ttry,"w");
                  if(fp==NULL){
                     printf("fail\n");
                     exit(1);
                   }
                   printf(   "p=%d q=%d r=%d で中断します\n",p,q,r);
                   fprintf(fp,"%d,%d,%d\n",p,q,r);
                   fclose(fp);
                   close_keyboard();
                   exit(0);
                }
            }
         }
      }}
   }

   fp=fopen(ttry,"w");
   if(fp==NULL){
      printf("fail\n");
      exit(1);
   }
   printf(   "p=%d q=%d r=%d で終了しました\n",p,q,r);
   fprintf(fp,"%d,%d,%d\n",p,q,r);
   fclose(fp);

   close_keyboard();
}


void input(int i,unsigned char buffer[128]){
   int k,j,c;

     low[buffer[0]]=1;
      k=strlen((char *)buffer)-1;
      if(k>jmax) jmax=k;
      for(j=0;j<=k;j++){
         c=word[i][j]=buffer[k-j];
         if(isalpha(c)) digit[c]=-1;
         else if(isdigit(c)) digit[c]=c-'0';
      }
}



void init_keyboard()
{
    tcgetattr(0, &initial_settings);
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 0;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &initial_settings);
}

void close_keyboard()
{
    tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
    char ch;
    int nread;

    if(peek_character != -1)
        return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0, &ch, 1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if(nread == 1) {
        peek_character = ch;
        return 1;
    }
    return 0;
}


int readch()
{
    char ch;

    if(peek_character != -1) {
        ch = peek_character;
        peek_character = -1;
        return ch;
    }
    read(0, &ch, 1);
    return ch;
}  

2013年5月23日木曜日

逆ポーランドcalc5c.cの実行結果


(q) is quit, (h) is help, (k) is 関数一覧;
Calc: hex(245)
245@hex@

hex(245.000000)=f5

(q) is quit, (h) is help, (k) is 関数一覧;
Calc: oct(237)
237@oct@

oct(237)=355

(q) is quit, (h) is help, (k) is 関数一覧;
Calc: bin(213)
213@bin@

bin(213)=1101 0101

(q) is quit, (h) is help, (k) is 関数一覧;
Calc: cos(60*do)
60@do@*cos@

cos(60*do)=0.500000000

(q) is quit, (h) is help, (k) is 関数一覧;
Calc: sin(pi/3)
pi@3@/sin@

sin(pi/3)=0.866025404

(q) is quit, (h) is help, (k) is 関数一覧;
Calc: 16*arctan(.2)-4*arctan(1/239)
16@.2@arctan@*4@1@239@/arctan@*-

16*arctan(.2)-4*arctan(1/239)=3.141592654

(q) is quit, (h) is help, (k) is 関数一覧;
Calc:

逆ポーランド表現のパーサとその演算calc5c.c


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/*
逆ポーランド表現のパーサと
その演算をするプログラム

参考資料:

技術評論社 河西 朝雄 著
C言語による はじめてのアルゴリズム入門
2718円+税
P252〜254より引用

技術評論社 奥村晴彦 著
C言語による最新アルゴリズム事典 2330円+税
P72〜73より引用・改変

*/


int ch,ii,jj,bi;
char p[100],polish[100],bit[100];

void readch();
void expression();
void term();
void factor();
int  cmpch0( char *pp);
int  cmpch(  char *pp);
void writech(char *pp);
void m1(int i);
void t1(int i,char *p);

double v[50];
int sp1,m2flg;
int m,flg,mflg,dflg,xflg;
double dd,ms;

main(){
   int i,j;
   char po;

START:
   ii=0;jj=0;sp1=0;bi=0;
   for(i=0;i<100;i++) p[i]=polish[i]=bit[i]=0;
   printf("\n(q) is quit, (h) is help, (k) is 関数一覧;\nCalc: ");
   scanf("%s",p);

   if(p[0]=='q'||p[0]=='Q') exit(1);
   if((p[0]=='h'||p[0]=='H')&&p[1]==0){
      printf("\nCalc Program  Ver.5c (2013.05.23)\n");
      printf("Example:\n");
      printf("   Calc: 3*(1+2+3+4)/2\n");
      printf("   Calc: 2*(3*(567+433)/300+15)+17\n");
      printf("   Calc: 776&773 また 776|773 また 234#234 また 345<3\n");
      printf("   Calc: -0.2*((-1.5/-.2))\n");
      printf("(+)足し算(-)引き算(*)掛け算(/)割り算(%%)余り\n");
      printf("(<) 1<3と入力すると 1<<3=8左シフト\n");
      printf("(>)64>3と入力すると64>>3=8右シフト\n");
      printf("(&)ビット積(|)ビット和(#)ビット排他,内部では{^}\n");
      printf("(^)2^3で2の3乗 (q)quit (h)help (k)関数一覧\n");
      printf("CTRL+C で強制終了します\n");
      goto START;
   }
   if((p[0]=='k'||p[0]=='K')&&p[1]==0){
      printf("\nCalc Program  Ver.5c (2013.05.23)\n");
      printf("関数一覧:\n");
      printf("三角関数: cos sin tan atan=arctan acos=arccos\n");
      printf("   asin=arcsin sinh cosh tanh atanh=arctanh\n");
      printf("   acosh=arccosh asinh=arcsinh\n");
      printf("対数関数等: exp log=ln log10 sqrt cbrt=sqrt3\n");
      printf("丸め・反転: ceil floor abs neg\n");
      printf("進数: bin oct hex  0xbf58という風に0xをつけると16進\n");
      printf("定数: pi=3.1415926 ei=ネイピア数 do=pi/180\n");
      printf("三角関数で30度で使いたい時は cos(30*do)\n");
      printf("(q)quit (h)help (k)関数一覧\n");
      printf("CTRL+C で強制終了します\n");
      goto START;
   }

  do{
      readch();
      expression();
   } while(ch);

   p[ii]='\0';
   polish[jj]='\0';
   puts(polish);

   sp1=0;flg=0;mflg=0;dflg=0;xflg=0;dd=1.0;
   m2flg=0;
   for(ii=0;ii<jj;ii++){
      po=polish[ii];
      /*putchar(po);*/
      if(po=='m') mflg=1;
      else if(!cmpch("pi")){      v[++sp1]=M_PI;        m1(2); }
      else if(!cmpch("do")){      v[++sp1]=M_PI/180.0;  m1(2); }
      else if(!cmpch("ei")){      v[++sp1]=M_E;         m1(2); }
      else if(!cmpch("cos")){     v[sp1]=cos(v[sp1]);   m1(3); }
      else if(!cmpch("sin")){     v[sp1]=sin(v[sp1]);   m1(3); }
      else if(!cmpch("tan")){     v[sp1]=tan(v[sp1]);   m1(3); }
      else if(!cmpch("atan")){    v[sp1]=atan(v[sp1]);  m1(4); }
      else if(!cmpch("asin")){    v[sp1]=asin(v[sp1]);  m1(4); }
      else if(!cmpch("acos")){    v[sp1]=acos(v[sp1]);  m1(4); }
      else if(!cmpch("arctan")){  v[sp1]=atan(v[sp1]);  m1(6); }
      else if(!cmpch("arcsin")){  v[sp1]=asin(v[sp1]);  m1(6); }
      else if(!cmpch("arccos")){  v[sp1]=acos(v[sp1]);  m1(6); }
      else if(!cmpch("tanh")){    v[sp1]=tanh(v[sp1]);  m1(4); }
      else if(!cmpch("sinh")){    v[sp1]=sinh(v[sp1]);  m1(4); }
      else if(!cmpch("cosh")){    v[sp1]=cosh(v[sp1]);  m1(4); }
      else if(!cmpch("acosh")){   v[sp1]=acosh(v[sp1]); m1(5); }
      else if(!cmpch("asinh")){   v[sp1]=asinh(v[sp1]); m1(5); }
      else if(!cmpch("atanh")){   v[sp1]=atanh(v[sp1]); m1(5); }
      else if(!cmpch("arctanh")){ v[sp1]=atanh(v[sp1]); m1(7); }
      else if(!cmpch("arcsinh")){ v[sp1]=asinh(v[sp1]); m1(7); }
      else if(!cmpch("arccosh")){ v[sp1]=acosh(v[sp1]); m1(7); }
      else if(!cmpch("exp")){     v[sp1]=exp(v[sp1]);   m1(3); }
      else if(!cmpch("log")){     v[sp1]=log(v[sp1]);   m1(3); }
      else if(!cmpch("ln")){      v[sp1]=log(v[sp1]);   m1(2); }
      else if(!cmpch("log10")){   v[sp1]=log10(v[sp1]); m1(5); }
      else if(!cmpch("sqrt")){    v[sp1]=sqrt(v[sp1]);  m1(4); }
      else if(!cmpch("cbrt")){    v[sp1]=cbrt(v[sp1]);  m1(4); }
      else if(!cmpch("sqrt3")){   v[sp1]=cbrt(v[sp1]);  m1(5); }
      else if(!cmpch("ceil")){    v[sp1]=ceil(v[sp1]);  m1(4); }
      else if(!cmpch("floor")){   v[sp1]=floor(v[sp1]); m1(5); }
      else if(!cmpch("neg")){     v[sp1]=~(int)v[sp1]; m1(3); }
      else if(!cmpch("abs")){ if(v[sp1]<0){v[sp1]=-v[sp1];} m1(3); }
      else if(!cmpch("bin")){
         m=(int)v[sp1];
         if(m) printf("\nbin(%d)=",m);
         m2flg=0;
         if(m<0){ m=-m; m2flg=1; }
         bi=0;
         while(m){
            bit[bi++]=m%2;
            m/=2;
            if(m2flg==1){ m2flg=2; m^=0x40000000; }
         }
         for(i=bi-1;i>=0;i--){
             printf("%d",bit[i]);
             if(i%4==0) printf(" ");
         }
         printf("\n");
         m1(3);
         m2flg=3;
      }
      else if(!cmpch("oct")){
         m=(int)v[sp1];
         if(m) printf("\noct(%d)=",m);
         m2flg=0;
         if(m<0){ m=-m; m2flg=1; }
         bi=0;
         while(m){
            bit[bi++]=m%8;
            m/=8;
            if(m2flg==1){ m2flg=2; m^=0x10000000; }
         }
         for(i=bi-1;i>=0;i--){
             printf("%d",bit[i]);
             if(i%4==0) printf(" ");
         }
         printf("\n");
         m1(3);
         m2flg=3;
      }
      else if(!cmpch("hex")){
         m=(int)v[sp1];
         ms=v[sp1]-m;
         if(v[sp1]!=0.0) printf("\nhex(%f)=",v[sp1]);
         m2flg=0;
         if(m<0){ m=-m; m2flg=1; }
         bi=0;
         while(m){
            if(isdigit(m%16+'0')) bit[bi++]=m%16+'0';
            else                  bit[bi++]=m%16+'a'-10;
            m/=16;
            if(m2flg==1){ m2flg=2; m^=0x08000000; }
         }
         for(i=bi-1;i>=0;i--){
             printf("%c",bit[i]);
             if(i==4) printf(" ");
         }
         if(ms!=0.0){
             printf(".");
             for(i=0;i<8;i++){
                ms*=16;
                m=(int)ms;
                if(isdigit(m+'0')) j=m+'0';
                else               j=m+'a'-10;
                ms-=m;
                printf("%c",j);
                if(i==3) printf(" ");
             }
         }
         printf("\n");
         m1(3);
         m2flg=3;
      }
      else if(isxdigit(po)||po=='x'||po=='.'){
          if(po=='.') dflg=1;
         if(po=='x'){ xflg=1; v[sp1]=0.0;}
         if(dflg==0&&xflg==0){
            if(flg==0){
               if(isdigit(po)){
                  flg=1;
                  v[++sp1]=po-'0';
               }
            }
            else{
               if(isdigit(po)) v[sp1]=v[sp1]*10+po-'0';
            }
         }
         else if(dflg==1&&xflg==0){
            if(flg==0){
               flg=1;
               v[++sp1]=0.0;
            }
            else{
               if(isdigit(po)){
                  dd/=10.0;
                  v[sp1]=v[sp1]+dd*(po-'0');
               }
            }
         }
         else if(dflg==0&&xflg==1){
            if(flg==1){
               if(isdigit(po)){
                  flg=2;
                  v[sp1]=po-'0';
               }
               else if('a'<=po&&po<='f'){
                  flg=2;
                  v[sp1]=po-'a'+10;
               }
               else if('A'<=po&&po<='F'){
                  flg=2;
                  v[sp1]=po-'A'+10;
               }
            }
            else{
               if(isdigit(po)) v[sp1]=v[sp1]*16+po-'0';
               else if('a'<=po&&po<='f') v[sp1]=v[sp1]*16+po-'a'+10;
               else if('A'<=po&&po<='F') v[sp1]=v[sp1]*16+po-'A'+10;
            }
         }
         else if(dflg==1&&xflg==1){
            if(flg==0){
               /*flg=1;
               v[++sp1]=0.0;*/
            }
            else{
               if(isdigit(po)){
                  dd/=16.0;
                  v[sp1]=v[sp1]+dd*(po-'0');
               }
               else if('a'<=po&&po<='f'){
                  dd/=16.0;
                  v[sp1]=v[sp1]+dd*(po-'a'+10);
               }
               else if('A'<=po&&po<='F'){
                  dd/=16.0;
                  v[sp1]=v[sp1]+dd*(po-'A'+10);
               }
            }
         }
      }
      else if(po=='@'){
          /*printf("m=%d 1=%f 2=%f 3=%f\n",mflg,v[sp1-1],v[sp1],v[sp1+1]);*/
          if(mflg==1) v[sp1]=-v[sp1];
          flg=0;mflg=0;dflg=0;xflg=0;dd=1.0;
      }
      else if(ispunct(po)){
         flg=0;mflg=0;dflg=0;xflg=0;dd=1.0;
         switch(po){
            case '+': v[sp1-1]+=v[sp1]; break;
            case '-': v[sp1-1]-=v[sp1]; break;
            case '*': v[sp1-1]*=v[sp1]; break;
            case '/': v[sp1-1]/=v[sp1]; break;
            case '^': v[sp1-1]=pow(v[sp1-1],v[sp1]); break;
            case '%': m=(int)v[sp1-1]%(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
            case '<': m=(int)v[sp1-1]<<(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
            case '>': m=(int)v[sp1-1]>>(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
            case '&': m=(int)v[sp1-1]&(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
            case '|': m=(int)v[sp1-1]|(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
            case '#': m=(int)v[sp1-1]^(int)v[sp1];
                       v[sp1-1]=(double)m;           break;
         }
         sp1--;
      }
   }
   if(m2flg==0) printf("\n%s=%9.9f\n",p,v[1]);
   goto START;
}

void readch(){
   ch=p[ii++];
}

void backch(){
   ch=p[ii];
}

void expression(){
   term();
   while(1){
      if(ch=='+'){      readch(); term(); polish[jj++]='+'; }
      else if(ch=='-'){ readch(); term(); polish[jj++]='-'; }
      else break;
   }
}

void term(){
   factor();
   while(1){
      if(ch=='*'){       readch(); factor(); polish[jj++]='*'; }
      else if(ch=='/'){ readch(); factor(); polish[jj++]='/'; }
      else if(ch=='^'){ readch(); factor(); polish[jj++]='^'; }
      else if(ch=='%'){ readch(); factor(); polish[jj++]='%'; }
      else if(ch=='<'){ readch(); factor(); polish[jj++]='<'; }
      else if(ch=='>'){ readch(); factor(); polish[jj++]='>'; }
      else if(ch=='&'){ readch(); factor(); polish[jj++]='&'; }
      else if(ch=='|'){ readch(); factor(); polish[jj++]='|'; }
      else if(ch=='#'){ readch(); factor(); polish[jj++]='#'; }
      else break;
   }
}

void factor(){
   int mflg=0,dflg=0,pflg=0,xflg=0,m2;

  /* polish[jj+1]='\0';
   printf("222 %s %c\n",polish,ch);*/
   /*printf("228 %d\n",cmpch0("cos"));*/
   if(ch=='('){
      while(ch!=')'&&ch!='\0'){
         readch();
         expression();
      }
      if(ch==')'){
          readch();
      }
      else printf("error2 %d\n",ii);
   }
   else if(isalnum(ch)||(ch=='-'&&mflg==0)||(ch=='+'&&pflg==0)||(ch=='.'&&dflg==0)){
      while(isalnum(ch)||(ch=='-'&&mflg==0)||(ch=='+'&&pflg==0)||(ch=='.'&&dflg==0) && ch!='\0'){
         mflg=1;pflg=1;
         if(ch=='.') dflg=1;
         if(ch=='-') polish[jj++]='m';
         else if(ch=='+'){}
         else if(!cmpch0("cos")){       t1(3,"cos");  }
         else if(!cmpch0("sin")){       t1(3,"sin");  }
         else if(!cmpch0("tan")){       t1(3,"tan");  }
         else if(!cmpch0("atan")){      t1(4,"atan");  }
         else if(!cmpch0("acos")){      t1(4,"acos");  }
         else if(!cmpch0("asin")){      t1(4,"asin");  }
         else if(!cmpch0("arctan")){    t1(6,"arctan");  }
         else if(!cmpch0("arcsin")){    t1(6,"arcsin");  }
         else if(!cmpch0("arccos")){    t1(6,"arccos");  }
         else if(!cmpch0("sinh")){      t1(4,"sinh");  }
         else if(!cmpch0("cosh")){      t1(4,"cosh");  }
         else if(!cmpch0("tanh")){      t1(4,"tanh");  }
         else if(!cmpch0("atanh")){     t1(5,"atanh");  }
         else if(!cmpch0("acosh")){     t1(5,"acosh");  }
         else if(!cmpch0("asinh")){     t1(5,"asinh");  }
         else if(!cmpch0("arctanh")){   t1(7,"arctanh");  }
         else if(!cmpch0("arccosh")){   t1(7,"arccosh");  }
         else if(!cmpch0("arcsinh")){   t1(7,"arcsinh");  }
         else if(!cmpch0("exp")){       t1(3,"exp");  }
         else if(!cmpch0("log")){       t1(3,"log");  }
         else if(!cmpch0("ln")){        t1(2,"ln");  }
         else if(!cmpch0("log10")){     t1(5,"log10");  }
         else if(!cmpch0("sqrt")){      t1(4,"sqrt");  }
         else if(!cmpch0("cbrt")){      t1(4,"cbrt");  }
         else if(!cmpch0("sqrt3")){     t1(5,"sqrt3");  }
         else if(!cmpch0("ceil")){      t1(4,"ceil");  }
         else if(!cmpch0("floor")){     t1(5,"floor");  }
         else if(!cmpch0("bin")){       t1(3,"bin");  }
         else if(!cmpch0("oct")){       t1(3,"oct");  }
         else if(!cmpch0("hex")){       t1(3,"hex");  }
         else if(!cmpch0("abs")){       t1(3,"abs");  }
         else if(!cmpch0("neg")){       t1(3,"neg");  }
         else if(!cmpch0("pi")){ ii+=1; writech("pi"); }
         else if(!cmpch0("do")){ ii+=1; writech("do"); }
         else if(!cmpch0("ei")){ ii+=1; writech("ei"); }
         else polish[jj++]=ch;
         readch();
      }
      polish[jj++]='@';
   }
   else printf("error1 %d\n",ii);
}

int  cmpch0(char *pp){
   int i=0,j=0;

     /* printf("261 %c %c\n",p[ii+i-1],*(pp+i));*/
   while(*(pp+i)){
      if(p[ii+i-1]!=*(pp+i)) return 1;
      /*printf("263 %c %c\n",p[ii+i-1],*(pp+i));*/
      i++;
   }
   if(ispunct(p[ii+i-1])){  /*printf("268 %d\n",ii);*/ return 0;}
   else if(!p[ii+i-1]){  /*printf("269 %d\n",ii);*/ return 0;}
   else{  /*printf("270 %d\n",ii);*/ return 1;}
}

int  cmpch(char *pp){
   int i=0,j=0;

   while(*(pp+i)){
      if(polish[ii+i]!=*(pp+i)) return 1;
      /*putchar(polish[ii+i+1]);*/
      /*printf("276 %c %c\n",polish[ii+i],*(pp+i));*/
      i++;
   }
   if(ispunct(polish[ii+i])){ /*printf("279 %d\n",ii); */return 0;}
   else if(polish[ii+i]=='\0'){ return 0;}
   else{ return 1;}
}


void writech(char *pp){
   int i=0;
   while(*(pp+i)){
      polish[jj++]=*(pp+i);
      /*printf("1 %c",*(pp+i));*/
      i++;
   }
}

void m1(int i){
   if(mflg) v[sp1]=-v[sp1];
   flg=0;mflg=0;dflg=0;xflg=0;dd=1.0;
   ii+=i;
}

void t1(int i,char *p){
   int m2;
   ii+=i;
   m2=0;
   if(polish[jj-1]=='m'){ m2=1; jj--;}
   readch();
   expression();
   if(m2) writech("m");
   writech(p);
}

2013年5月20日月曜日

実行ファイルをobjdumpする


前回、armcmmで130520B.cをコンパイルして
実行ファイルを作りました

最初の画面でj(ジェイ)ボタンを1回押して
画面を切り替えます。

うまくいかなかったらjボタンを4〜5回押して
実行ファイルのリストの画面にします

カーソルを4行目の130520Bに合わせ
o(オー)ボタンを押します

 130520B            Fキーで検索:                      lキーでヘルプ
 行番号:ファイル名:年/ 月/日(曜) 時:分:秒 サイズ タイトル
0003: 130520A    2013/May/20(Mon)14:22:47 9559
0004: 130520B    2013/May/20(Mon)15:42:30 9561
0005: 8q1        2013/May/18(Sat)09:29:47 9760
0006: ango       2013/May/10(Fri)12:21:05 14297
0007: armcmm     2013/May/20(Mon)15:00:04 80408
0008: armcmm-130510b   2013/May/10(Fri)14:06:37 80416
0009: armcmm-130510c     ファイルがありません   0
0010: calc1      2013/May/19(Sun)12:55:37 15210
0011: calc2      2013/May/19(Sun)13:32:48 15210
0012: calc3      2013/May/19(Sun)19:17:31 19306
0013: kisi1      2013/May/18(Sat)09:38:01 9840
0014: lucas10    2013/May/16(Thu)17:31:25 19517

するとこんなのが出て、
何かキーを押します


objdump -d 130520B > 130520B.txt
何かキーを押してください


するとTXTの画面になり、
130520B.txtにカーソルを合わせ
eボタンを押します




 130520B.txt        Fキーで検索:                      lキーでヘルプ
 行番号:ファイル名:年/ 月/日(曜) 時:分:秒 サイズ タイトル
0002: 1       .txt 2013/May/16(Thu)17:26:43 773 from A=2 to B=650
0003: 130520B .txt 2013/May/20(Mon)16:01:02 5206 特にタイトルはなし
0004: 2       .txt 2013/May/18(Sat)11:08:17 1070 from A=2 to B=650
0005: abc     .txt 2013/May/10(Fri)06:01:21 80 This is a pen.
0006: gauss   .txt 2013/Apr/30(Tue)15:09:21 34 3
0007: hyojun  .txt 2013/Apr/24(Wed)13:31:31 45 10
0008: luc11b  .txt 2013/May/17(Fri)15:30:39 3578 from A=2 to B=650
0009: luc12a  .txt 2013/May/12(Sun)21:50:54 3166 from A=2 to B=180
0010: luc12b  .txt 2013/May/13(Mon)15:59:56 9526 from A=0 to B=2
0011: luc12c  .txt 2013/May/13(Mon)16:17:18 1326 from A=2 to B=130
0012: luc12d  .txt 2013/May/13(Mon)20:55:32 6329 from A=2 to B=130
0013: lucas6a .txt 2013/May/14(Tue)15:03:50 3406 from A=2 to B=130


すると、objdumpの吐いたTXTが閲覧できます
画面を閉じるときはCTRL+Xで閉じます


CTRL+xで終了:[縦1,横21]
CTRL+lでhelp:
00001:000:⍗
00002:040:130520B:     file format elf32-littlearm⍗
00003:000:⍗
00004:000:⍗
00005:029:Disassembly of section .init:⍗
00006:000:⍗
00007:017:000082cc <_init>:⍗
00008:053:    82cc: e52de004  push {lr}  ; (str lr, [sp, #-4]!)⍗
00009:040:    82d0: e24dd004  sub sp, sp, #4 ; 0x4⍗
00010:045:    82d4: eb000020  bl 835c <call_gmon_start>⍗
00011:040:    82d8: e28dd004  add sp, sp, #4 ; 0x4⍗
00012:028:    82dc: e8bd8000  pop {pc}⍗  
ファイル名:130520B.txt




c言語を編集してみる

前回、armcmmを立ち上げ、


 armcmm.c           Fキーで検索:                      lキーでヘルプ
 行番号:ファイル名:年/ 月/日(曜) 時:分:秒 サイズ タイトル
0002: 000hina .c 2013/May/20(Mon)14:21:38 84 ひな型Hello
0003: 130520A .c 2013/May/20(Mon)14:23:01 84 ひな型Hello
0004: 8q1     .c 2013/May/18(Sat)09:29:39 1044 #include <stdio.h>
0005: ango    .c 2013/May/08(Wed)15:37:45 2608 #include <stdio.h>
0006: armcmm  .c 2013/May/10(Fri)14:06:24 90979 エディタ作成(自
0007: armcmm-130510b.c 2013/May/10(Fri)14:06:24 90979 エディタ作成(自
0008: armcmm-130510c.c 2013/May/10(Fri)21:09:50 90979 エディタ作成(自
0009: calc1   .c 2013/May/19(Sun)12:55:26 2577 #include <stdio.h>
0010: calc2   .c 2013/May/19(Sun)13:32:35 2957 #include <stdio.h>
0011: calc3   .c 2013/May/19(Sun)19:17:24 4181 #include <stdio.h>
0012: kisi1   .c 2013/May/18(Sat)09:37:56 1061 #include <stdio.h>
0013: lucas10 .c 2013/May/16(Thu)17:30:44 11865 #include <stdio.h>

この画面で、2行目の「000hina.c」にカーソルを合わせ、
Cボタンを押します。Cボタンはファイルをコピーします

0002: 000hina .c 2013/May/20(Mon)14:21:38 84 ひな型Hello
0003: 130520A .c 2013/May/20(Mon)14:23:01 84 ひな型Hello
0004: 130520B .c 2013/May/20(Mon)15:33:17 84 ひな型Hello
0005: 8q1     .c 2013/May/18(Sat)09:29:39 1044 #include <stdio.h>
0006: ango    .c 2013/May/08(Wed)15:37:45 2608 #include <stdio.h>
0007: armcmm  .c 2013/May/10(Fri)14:06:24 90979 エディタ作成(自

すると、新たに「130520B.c」というファイルがコピーされました

今日の日付が2013年5月20日だからです
明日作成したなら130521A.cになります

「130520A.c」は僕がさっき作ったのでBになり
もう一回コピーすると「130520C.c」になります

eボタンで編集して

CTRL+xで終了:[縦6,横21]
CTRL+lでhelp:
00002:018:#include <stdio.h>⍗
00003:000:⍗
00004:007:main(){⍗
00005:014:   int i=1234;⍗
00006:022:   printf("i=%d\n",i);⍗
00007:001:}⍗


こんな感じで書き換えて、CTRL+Xで閉じます
最初の画面に戻ってwボタンを押すと



gcc -S 130520B.c -lm
gcc -o 130520B 130520B.c -lm
./130520B
i=1234
何かキーを押してください


と表示されます