training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

sales.c (2161B)


      1 /* Exercise 6.22 */
      2 
      3 #include <stdio.h>
      4 
      5 #define VEND 4
      6 #define PROD 5
      7 
      8 int main()
      9 {
     10    double sales[PROD + 1][VEND + 1] = { 0 };
     11    int v, p;
     12    double t;
     13 
     14    while(1) {
     15       printf("\nEnter new monthly vendor ticket.\n\n");
     16       printf("Vendor ID (%d to end): ", EOF);
     17       scanf("%d", &v);
     18 
     19       if( (v >= 1 && v <= VEND) || v == EOF )
     20 	 break;
     21       else
     22 	 printf("Invalid vendor ID: %d\n"
     23 	        "Please check your vendor and try again.\n", v);
     24    }
     25 
     26    while(v != EOF) {
     27 
     28       while(1) {
     29          printf("Product ID: ");
     30          scanf("%d", &p);
     31 
     32 	 if(p < 1 || p > PROD)
     33 	    printf("Invalid product ID: %d\n"
     34 		   "Please check your product and try again.\n", p);
     35          else
     36 	    break;
     37       }
     38 
     39       while(1) {
     40          printf("Monthly total for this product: ");
     41          scanf("%lf", &t);
     42 
     43 	 if( t < 0 )
     44 	    printf("Invalid value: %d\n", t);
     45 	 else
     46 	    break;
     47       }
     48 
     49       sales[p][v] += t;
     50 
     51       while(1) {
     52          printf("\nEnter new monthly vendor ticket.\n\n");
     53          printf("Vendor ID (%d to end): ", EOF);
     54          scanf("%d", &v);
     55 
     56          if( (v >= 1 && v <= VEND) || v == EOF )
     57 	    break;
     58          else
     59 	    printf("Invalid vendor ID: %d\n"
     60 	           "Please check your vendor and try again.\n", v);
     61       }
     62    }
     63 
     64    printf("\nThe results for this run are the follow:\n");
     65 
     66    /* print the header (cols - vendors) */
     67    printf("\nV:");
     68    for(v = 1; v <= VEND; v++)
     69       printf("\t V0%d\t", v);
     70    printf("\t TOT\n\n");
     71 
     72    /* sum the rows and the cols for the total */
     73    /* of each product and each vendor. */
     74    for(p = 1; p <= PROD; p++) {
     75       t = 0;
     76       printf("P%d:\t", p);
     77       for(v = 1; v <= VEND; v++) {
     78 	 t += sales[p][v];
     79 	 printf("$%.2f\t\t", sales[p][v]);
     80       }
     81       printf("$%.2f\n", t);
     82       t = 0;
     83    }
     84    printf("\nTOT\t");
     85 
     86    /* print the totals of all products for each vendor */
     87 
     88    sales[0][0] = 0; /* total of *all* products for *all* vendors */
     89 
     90    for(v = 1; v <= VEND; v++) {
     91       t = 0;
     92       for(p = 1; p <= PROD; p++) {
     93 	 t += sales[p][v];
     94       }
     95       printf("$%.2f\t\t", t);
     96       sales[0][0] += t;
     97    }
     98    printf("$%.2f\n\n", sales[0][0]);
     99 
    100    return 0;
    101 } /* E0F main */
    102