用于计算输出的税款和格式成本的源代码

模块 T1520IC2 显示在 图 1中。 它提供 calc_and_format() 函数。
图 1。 用于计算输出的税款和格式成本的样本 ILE C 源
        /* This function calculates the tax and formats the total cost. */
        /* The function calc_and_format() returns 1 if successful and 0 */
        /* if it fails. */
        #include <stdio.h>
        #include <string.h>
        #include <decimal.h>
        /* Tax rate is imported from the service program T1520SP2. */ 1 
        const extern  decimal (2,2)   taxrate;
        int calc_and_format (decimal (10,2)  price,
                             short int       quantity,
                             char            formatted_cost[22])
        {
          decimal (17,4) hold_result;
          char           hold_formatted_cost[22];
          int            i,j;
          memset(formatted_cost, ' ', 21);
          hold_result = (decimal(4,0))quantity *
          price * (1.00D+taxrate);         /* Calculate the total cost. */
          if (hold_result < 0.01D || hold_result > 1989800999801.02D)
          {
            printf("calc out of range:%17.4D(17,4)\n", hold_result);
            return(0);
          }
         /* Format the total cost.    */
            sprintf(hold_formatted_cost, "%21.2D(17,4)", hold_result);
            j = 0;
            for (i=0; i<22; ++i)
            {
              if (hold_formatted_cost[i] != ' ' &
                  hold_formatted_cost[i] != '0')
              {
                  hold_formatted_cost[j] = '$';
                  break;
              }
              j = i;
            }
            for (i=j=21; i>=0; --i)
            {
              if (j < 0) return(0);
              if (hold_formatted_cost[i] == '$')
              {
                formatted_cost[j] = hold_formatted_cost[i];
                break;
              }
                if (i<16 & !((i-2)%3))
              {
                formatted_cost[j] = ',';
                --j;
              }
              formatted_cost[j] = hold_formatted_cost[i];
              --j;
            }
         /* End of for loop, 21->0. */
              return(1);
         }
注:
  1. 此模块中的函数 calc_and_format 计算并格式化总成本。 要执行计算,将从服务程序 T1520SP2导入数据项 taxrate 。 必须导入此数据项,因为未在此模块 (T1520IC2) 中定义此数据项。