Compute the sum number in PIC() clause of COBOL

2019-04-15 15:17发布

//*********************************************************************** //* This program is used to compute the sum value in the PIC clause. * //* It select the numbers in parenthesis, e.g. (02), and put 02 in dig * //* array in order. The var len is the digtal numbers, as above, it is * //* 2. And then use len to search the the number in comp_Num[] which * //* will be used to get the compensation of len. As above, it will get * //* 3. And now use 3-2 to get the weigh in weigh array. * //* In the main function, it scan dig[] in order and scan weigh[] in a * //* contrary order. * //*********************************************************************** #include static int weigh[5] = {0,1,10,100,1000}; static int comp_Num[5] = {1,2,3,4,5}; int getWeigh(int index); void main(int args, char** argb) { int sum = 0; int ch1 = 0; int ch2 = 0; int len = 0; int comp_Max = 0; int dig[4] = {0}; FILE* fp = fopen(argb[1],"r"); while(EOF != (ch1 = fgetc(fp))){ if(ch1 == '('){ while(EOF != (ch2 = fgetc(fp)) && ch2 != ')' ){ dig[len++] = ch2 - 48; } comp_Max = comp_Num[len]; while(len > 0){ sum += dig[len-1] * getWeigh(comp_Max-len); len--; } len = 0; } } printf("sum= %d ", sum); if(fp != NULL){ fclose(fp); } } int getWeigh(int index) { return weigh[index]; }