Catsum.c category sums
From Initq
This short program teaches us arrays, lists, database structure, record keeping and spread sheets. This is all done on a very basic scale but if your carefully understand this program you will understand that big programs like oracle, mysql, postgresql, mssql etc are exactly the same and they all had the same start. We will break down the program and show you step my step what is going on.
Contents |
Header files
The #include is a preprocessor directive that instructs the compiler to read and compile another source file.
#include <stdio.h> #include <stdlib.h> #include <string.h>
| Header | Functions |
|---|---|
| stdio.h | printf, getchar, putchar |
| stdlib.h | printf, getchar, putchar |
| string.h | printf, getchar, putchar |
Macros
#define is used to perform macro-substitutions of one piece of text for another throughout the file in which it is used. In our code we used the following three #define statements.
#define LASTCAT 99 #define YES 1 #define NO 0
Now when YES is encountered it will denote 1 and NO will be replaced with 0.
Extern
extern void main(void); void main(void)
extern is a data type modifier used to tell the compiler that a variable is declared elsewhere in the program. This is often used in conjunction with separately compiled files that share the same global data and are linked together. In essence, it notifies the compiler about the type of a variable without redeclaring it.
Data types
long amount; long grand_total = 0L; /* total for all categories */ short category; short icat; static long sums[LASTCAT + 1] = {0L}; /* sums for categories */ static short counts[LASTCAT + 1] = {0}; /* category counts */ char buf[512]; short iline; static char *cat_names[LASTCAT + 1] = {NULL}; int load_cats = NO; /* flag (0/1): now loading descriptions */ int got_cats = NO; /* flag (0/1): descriptions obtained? */
- long (32 bits)
- short (16 bits)
- static (static makes a variable in existence during the lifetime of a program.
- char buf[512] (single dimension character array)
- static char *cat_names (character pointer single dimensional array)
Full Listing of catsum.c
Here is the Full code.
/* catsum.c: filter sums a list of numbers in categories * Format of each line of input is: category number * Grand total of all categories (except 0) is shown */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define LASTCAT 99 #define YES 1 #define NO 0 extern void main(void); void main(void) { long amount; long grand_total = 0L; /* total for all categories */ short category; short icat; static long sums[LASTCAT + 1] = {0L}; /* sums for categories */ static short counts[LASTCAT + 1] = {0}; /* category counts */ char buf[512]; short iline; static char *cat_names[LASTCAT + 1] = {NULL}; int load_cats = NO; /* flag (0/1): now loading descriptions */ int got_cats = NO; /* flag (0/1): descriptions obtained? */ printf("CATSUM Input Data Format: Category_# Amount_in_Cents\n"); /* loop for each line of standard input data */ for (iline = 0; gets(buf); ++iline) { if (!load_cats && buf[0] != '*' && /* extract category and amount from start of line */ 2 == sscanf(buf, "%d%ld", &category, &amount)) { if (category < 0 || category > LASTCAT) category = 0; sums[category] += amount; ++counts[category]; } else if (!load_cats && 0 == strcmp(buf, "* Category Descriptions")) { load_cats = YES; /* begin loading category names */ } else if (load_cats) { if (0 == strcmp(buf, "*") || buf[0] != '*') { load_cats = NO; /* end of category names */ } else { /* save category name in list */ category = atoi(&buf[1]); if (category > 0 && category <= LASTCAT) { cat_names[category] = strdup(&buf[1]); got_cats = YES; } else { printf("category name error: %s\n", buf); } } } } /* report sums of each of the categories */ printf("%d Lines of Data Input\n\n\n", iline); printf(got_cats ? "%-50s %10s %10s\n\n" : "%10s %14s %14s\n\n", "Category", "Total", "Count"); for (icat = 0; icat <= LASTCAT; ++icat) { if (counts[icat]) { if (got_cats) printf("%-50s %10ld %10d\n", cat_names[icat] ? cat_names[icat] : "no description", sums[icat], counts[icat]); else printf("%10d %10ld %10d\n", icat, sums[icat], counts[icat]); if (icat) grand_total += sums[icat]; } } /* report grand total of all category sums */ printf(got_cats ? "%-50s %10s\n%-50s %10ld\n" : "%14s %10s\n%14s %10ld\n", " ", "----------", "Grand Total: ", grand_total); }
Input File
* Full Year Income and Expense * * Category Descriptions * 1 Office and Computer Supplies * 2 Driving Personal Car for Business * 3 Computer Equipment * 4 Conference and Professional Seminar Fees * 1 457 * paper clips 2/6/92 1 2118 * computer paper 3/4/92 3 62334 * 200 MB hard drive 4/13/92 4 20000 * conference registration 5/28/92 4 39500 * seminar fee 7/1/92
Output
[lexiana@tosh 1]$ ./catsum < catsum.txt CATSUM Input Data Format: Category_# Amount_in_Cents 13 Lines of Data Input Category Total Count 1 Office and Computer Supplies 2575 2 3 Computer Equipment 62334 1 4 Conference and Professional Seminar Fees 59500 2 ---------- Grand Total: 124409
