The `Eta' function
#include <>
#include <>
#include <>
#include <>
#define PACKAGE "eta"
#define VERSION "0.0.1"
#define MAXLINE 1024
/* status epilepticus, print help and exit with exval */
void print_help(int exval);
/* print program version and exit with exval */
void print_version(int exval);
/* `\056' `float' detection ? */
/* returns -1 on error, 1 on `no dot', 0 on `dot' */
int is_float(char *val);
int main(int argc, char *argv[]) {
char line[MAXLINE]; /* fgets buff */
int overview_flag = 0; /* print overview only */
int count = 0; /* nr of input elements */
float total = 0; /* eta */
float num = 0; /* the actual element */
int opt = 0; /* parsed opt nr */
float highest = 0; /* the lowest encounterd nr */
float lowest = 0; /* the highest encounterd nr */
/* option parser */
while((opt = getopt(argc, argv, "hve")) != -1) {
switch(opt) {
case 'h': /* print help and exit */
print_help(0);
case 'v': /* print version and exit */
print_version(0);
case 'e':
overview_flag = 1;
break;
case '?':
fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
print_help(1);
}
}
/* no remaining argumenst left ? read stdin() */
if((argc - optind) == 0) {
while((fgets(line, MAXLINE, stdin)) != NULL) {
/* strip newlines */
if(line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
count++;
if(is_float(line) == 0)
num = atof(line);
else
num = atoi(line);
if(count == 1) highest = num, lowest = num;
if(num > highest) highest = num;
if(num < lowest =" num;" num =" atoi(argv[optind]);" num =" atof(argv[optind]);" count ="="" highest =" num," lowest =" num;"> highest) highest = num;
if(num < lowest) lowest = num;
total += num;
}
}
if(overview_flag == 1) {
printf("Nr of elements : %d\n", count);
printf("Sum of elements : %.2f\n", total);
printf("Average element : %.2f\n", (total / count));
printf("highest element : %.2f\n", highest);
printf("lowest element : %.2f\n", lowest);
} else
printf("%.2f\n", (total / count));
return 0;
}
/* returns -1 on error, 1 on `no dot', 0 on `dot' */
int is_float(char *val) {
int retval = -1;
if(strchr(val, '.') == NULL)
retval = 1;
else
retval = 0;
return retval;
}
/* status epilepticus, print help and exit with exval */
void print_help(int exval) {
printf("%s,%s mathiCheesEcal `eta' function\n", PACKAGE, VERSION);
printf("Usage: %s [-h] [-v] [-e] [[NUM1 NUM2 NUM3...]]\n\n", PACKAGE);
printf(" -h print this help and exit\n");
printf(" -v print version and exit\n");
printf(" -e print a more complete overview of parsed input\n\n");
exit(exval);
}
/* print program version and exit with exval */
void print_version(int exval) {
exit(exval);
}
Read more: http://cmagical.blogspot.com/2010_02_14_archive.html#ixzz0xgXkwiYt
Under Creative Commons License: Attribution Non-Commercial No Derivatives
Wednesday, 25 August 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment