Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Faqs and RFCs
FileSearching
Google
Google Groups
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
temperature.c
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <errno.h> #include <math.h> #if !defined(__NetBSD__) #include <values.h> #include <float.h> #endif #define VARIANCE 0.00004 void usage(char *fname) { printf("Usage: %s n C|F|K\n", fname); } void handleRangeError(double value, int error, char *fname) { printf("Error: %d\tValue: %g\n", error, value); if (error == ERANGE) { /* We've already established that there was a range error. */ errno = 0; printf("Double is out of range. "); if ((value == HUGE_VAL)||(value == (-HUGE_VAL))) { printf("Absolute value cannot be greater than %.0g.\n", DBL_MAX); } else { printf("Absolute value cannot be smaller than %g.\n", DBL_MIN); } } else { printf("Missing value!\n\n"); usage(fname); } return; } int main(int argc, char **argv) { double c, f, k, v; char scale; char *scale_ptr; char value[255]; int okay = EXIT_SUCCESS; if (argc<2||argc>3) { usage(argv[0]); return EXIT_FAILURE; } errno=0; if (argc==3) { strncpy(value, argv[1], 255); v = strtod(value, &scale_ptr); if (errno) { handleRangeError(v, errno, argv[0]); return EXIT_FAILURE; } scale = tolower(argv[2][0]); } else { strncpy(value, argv[1], 255); v = strtod(value, &scale_ptr); if (errno) { handleRangeError(v, errno, argv[0]); return EXIT_FAILURE; } scale = tolower(*scale_ptr); } /* If scale_ptr == value then no numeric value was present */ if (value==scale_ptr) { handleRangeError(v, 0, argv[0]); return EXIT_FAILURE; } switch (scale) { case 'c': c = v; f = c*9/5+32; k = c+273.15; break; case 'k': k = v; c = k-273.15; f = c*9/5+32; break; case 'f': f = v; c = (f-32)*5/9; k = c+273.15; break; default: usage(argv[0]); return EXIT_FAILURE; break; } /* Limited precision can result in rounding to -0.00. Rounding up to avoid this. VARIANCE must be adjusted if we adjust precision. */ k += VARIANCE; c += VARIANCE; f += VARIANCE; if (k<0.0) { printf("The temperature cannot be below absolute zero!\n"); return EXIT_FAILURE; } printf("%+10.4fF\n%+10.4fC\n%+10.4fK\n", f, c, k); return EXIT_SUCCESS; }
This source has been viewed 23 times.
Friday, 6 December 2024
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org