topical media & game development

talk show tell print

lib-ch-example-sample-matrixCalculator.cgi / cgi



  #!/bin/ch 
  /* Web Matrix Calculator source code written in CH/C */
  /* History: created by Harry H. Cheng, 2/8/1996 */
  /* Last modified: 2/22/96 */
  
  include <cgi.h>
  
  void sendMatrixApplet(int numMatrix, int RowA, int ColA, int RowB, int ColB, double A[:][:],
                        double B[:][:], char *expr) {
      int i, j;
      class CResponse Response;
  
      Response.setContentType("application/x-chs");
      Response.begin();
      printf("#include<stdio.h>\n");
      printf("#include<array.h>\n");
      printf("int main() {\n");
      printf("array double A[\%d][\%d];\n",RowA, ColA);
      printf("array double B[\%d][\%d];\n",RowB, ColB);
      for(i=0;i<RowA;i++) {
         for(j=0;j<ColA;j++) {
              printf("  A[\%d][\%d] = \%f;\n", i, j, A[i][j]);
         }
      }  
  
      for(i=0;i<RowB;i++) {
         for(j=0;j<ColB;j++) {
              printf("  B[\%d][\%d] = \%f;\n", i, j, B[i][j]);
         }
      }
      printf("  printf(\"A =\\n%%f\\n\", A);\n");
      if(numMatrix == 2)
        printf("  printf(\"B =\\n%%f\\n\", B);\n");
      printf("  printf(\"\%s =\\n%%f\\n\", \%s);\n", expr, expr);
      printf("  getchar();\n"); // Hold result for display in windows
      printf("}\n");
      Response.end();
  }
  
  void errorHandler(char *reason) {
      printf("Content-type: text/html\n\n"); 
      printf("<TITLE>Web Matrix Calculator Failed</TITLE>\n");
      printf("<body bgcolor=\"#FFFFFF\" text=\"#000000\" vlink=\"#FF0000\">\n");
      printf("<H3>Web Matrix Calculator Failed</H3>\n");
      printf("Your mathematical expression has not been submitted to Matrix Calculator,\n");
      printf("because \%s. ",reason);
      printf("<A HREF=\"/chhtml/toolkit/demos/cgi/sample/matrixCalculator.html\">Try again.</A>\n");
      printf("<p>
\n"); printf("<A HREF=\"http://www.softintegration.com\" target=\"_top\">\n"); printf("<img src=\"/chhtml/images/poweredbych.gif\" alt=\"Powered by Ch\"></A>\n"); exit(1); } int main() { chstrarray name, value; int num, i, j , k, rowA=1, colA=1, rowB=1, colB=1, numMatrix=1; string_t reason; class CRequest Request; num = Request.getFormNameValue(name, value); if(num==0) errorHandler("you submitted nothing"); if(!value[num-1]) errorHandler ("you didn't input a matrix expression"); numMatrix = atoi(value[0]); if(numMatrix == 1) { rowA = atoi(value[1]); colA = atoi(value[2]); k = 3; } else if(numMatrix == 2) { rowA = atoi(value[1]); colA = atoi(value[2]); rowB = atoi(value[3]); colB = atoi(value[4]); numMatrix=2; k = 5; } else { sprintf(reason,"\%d is not a valid number of matrices", atoi(value[0])); errorHandler(reason); } double A[rowA][colA]; /* variable length array */ double B[rowB][colB]; for(i=0;i<rowA;i++) { for(j=0;j<colA;j++,k++) { if(!isnum(value[k])) { sprintf(reason,"element A[\%d][\%d] is not a valid matrix", i+1, j+1); errorHandler(reason); } else A[i][j] = atof(value[k]); } } if(numMatrix == 2) { for(i=0;i<rowB;i++) { for(j=0;j<colB;j++,k++) { if(!isnum(value[k])) { sprintf(reason,"element B[\%d][\%d] is not a valid matrix", i+1, j+1); errorHandler(reason); } else B[i][j] = atof(value[k]); } } } else B[0][0] = 0; sendMatrixApplet(numMatrix, rowA, colA, rowB, colB, A, B, value[num-1]); exit(0); }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.