On 06-13-97 VIRGIL GARNESS wrote to ALL...
VG> Hi All, How is it possible to pass switches to bypass a Switch
VG> in a program, say like from the dos prompt. I'm trying to
VG> make a program that can run on it's own normally with a menu
VG> or run in a batch file with switches to bypass the menus.
VG> Any help 'preciated
VG> Later.....
This program does exactly what you want to do. I will include the
entire program although you will undoubtedly want to do something
totally different. I hope that this helps.
Ed
/* zztst-x9.cpp
Convert Gregorian to Julian
Has some improved fail-safe functions.
Added the necessary code to be given a date and days hence then
return the new date in commercial Gregorian format. (Is able to go
past the year boundry.)
Needs more fail-safing for the Julian input.
(If days > 365 (366 for leap year) then work up the required code to be
able to handle the year boundry limits.)
Fixed the year boundry limits.
Added negative days failsafe code for date plus days delay. Added code
to handle negative days delay given two dates.
Added century leap year code. On centurys that are evenly devisiable by
400 the leap year has 366 days. On "leap years" that are not evenly
divisable by 400 the leap year is to have 365 days.
Use pointers to get to month definations (a memory saving technique).
Also added code to further expand the century leap year capability.
Added the capability of bringing in some of the values from the command
line.
Changed the error codes returned by the exit() function.
*/
#include
#include
#include
// Variables used by multiple functions (Common)
long year;
int month, day, total_days;
int days_per_month [ 12 ] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char* month_1 [ 3 ] [ 12 ] =
{ { "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" },
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" },
{ "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12" } };
// Prototype functions
long to_julian ( ); //Input Gregorian to Julian
long to_julian ( int, int, long ); //Convert to Julian
void to_gregorian ( long ); //Convert Julian to Gregorian
void to_gregorian ( long, int&, int&, int& ); //Convert to Julian
long leap_year ( long, int ); //Adj Feb if leap year X 1000
void leap_year ( long ); //Adj Feb if leap year
int in_day ( int ); //Input day
int in_month ( ); //Input month
int Total_days ( int, int ); //Return day of year
void greg_ ( int& , int& ); //Convert Julian to Greg M&D
int Days_Between ( long, long ); //Determine days between years
int Days_in_year ( long ); //Return days in year (long)
long double char_t_float ( char* ); //Convert char to float pt.
char character ( char* );
void main ( int argc, char* argv [ ] )
{
long temp;
int month_, day_, year_;
int month_dsp = 0;
int days = 0;
int xtemp = 0;
char direction;
long start_;
long end_;
long tempx [ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
if ( argc == 1 )
{
cout << "\nFrom Gregorian To Julian = 0, "
<< "\nFrom julian (to Gregorian) = 1,"
<< "\nAdd days of Delay to date = 2,"
<< "\nNumber of days delay Between two dates = 3"
<< "\nWhat is the desired function? ";
direction = getche ( );
cout << '\n';
}
else
{
if ( argc > 9 )
{
cout << "To many input parameters\n";
exit ( 100 );
}
direction = character ( argv [ 1 ] );
for ( int j = 2; j < argc; j ++ )
{
tempx [ j ] = char_t_float ( argv [ j ] );
}
}
switch ( direction )
{
case '0':
case 't':
case 'T':
case 'j':
case 'J':
if ( argc == 5 )
cout << to_julian
( tempx [ 2 ], tempx [ 3 ],
tempx [ 4 ] ) % 100000 << endl;
else
cout << '\n'
<< to_julian ( ) % 100000 << endl;
break;
case '1':
case 'f':
case 'F':
case 'g':
case 'G':
if ( argc == 4 )
{
if ( tempx [ 3 ] > 2 | tempx [ 3 ] < 0 )
{
cout << "Wrong month specification "
<< "(0 = Month, 1 = Mon, 2 = "
<< " month number )";
exit ( 120 );
}
month_dsp = tempx [ 3 ];
temp = tempx [ 2 ];
}
else
{
cout << "How do you want the month "
<< "displayed? \n"
<< "(0 = Month, 1 = Mon, 2 = "
<< "month number) ";
cin >> month_dsp;
if ( month_dsp > 2 | month_dsp < 0 )
{
cout << "Wrong month specification "
<< "(0 = Month, 1 = Mon, 2 = "
<< " month number )";
exit ( 121 );
}
cout << "\nPlease input the Julian date: ";
cin >> temp;
}
to_gregorian ( temp, month_, day_, year_ );
if ( month_dsp > 1 )
cout << month_1 [ month_dsp ] [ month_ - 1 ]
<< "/" << day_ << "/" << year_
<< endl;
else
cout << month_1 [ month_dsp ] [ month_ - 1 ]
<< " " << day_ << ", " << year_ << endl;
break;
case '2':
case 'D':
case 'd':
case 'A':
case 'a':
if ( argc >= 6 && argc <= 7 )
{
temp = to_julian ( tempx [ 2 ],
tempx [ 3 ], tempx [ 4 ] );
days = tempx [ 5 ];
if ( argc == 7 )
{
if ( tempx [ 6 ] >= 0
&& tempx [ 6 ] <= 2 )
{
month_dsp = tempx [ 6 ];
}
else
{
month_dsp = 0;
}
}
else
{
month_dsp = 0;
}
}
else
{
cout << '\n';
month_dsp = 0;
temp = to_julian ( );
cout << "Input number of days of delay = ";
cin >> days;
}
if ( days < 0 )
{
cout << "Error 167:48\n"
<< "Unable to handle negative days!\n";
exit ( 110 );
}
xtemp = Days_in_year ( temp );
if ( ( days + temp % 1000 ) > xtemp )
{
year_ = temp / 1000;
days += temp % 1000;
while ( days >= xtemp )
{
days -= xtemp;
year_++;
xtemp = Days_in_year ( year_ );
}
temp = year_;
temp *= 1000;
temp += days;
}
else
temp += days;
to_gregorian ( temp, month_, day_, year_ );
if ( month_dsp > 1 )
cout << month_1 [ month_dsp ] [ month_ - 1 ]
<< "/" << day_ << "/" << year_ << endl;
else
cout << month_1 [ month_dsp ] [ month_ - 1 ]
<< " " << day_ << ", " << year_ << endl;
break;
case '3':
case 'b':
case 'B':
case 'N':
case 'n':
if ( argc == 8 )
{
start_ = to_julian ( tempx [ 2 ],
tempx [ 3 ], tempx [ 4 ] );
end_ = to_julian ( tempx [ 5 ],
tempx [ 6 ], tempx [ 7 ] );
}
else
{
cout << "\nStart Date\n";
start_ = to_julian ( );
cout << "End Date\n";
end_ = to_julian ( );
}
cout << Days_Between ( start_, end_ )
<< " Days.\n";
break;
}
exit ( argc - 1 );
}
long to_julian ( )
{
cout << "Enter year: ";
cin >> year;
leap_year ( year );
month = in_month ( );
day = in_day ( month );
total_days = Total_days ( day, month );
return ( year * 1000 ) + total_days;
}
long to_julian ( int month, int day, long year )
{
long temp;
temp = leap_year ( year, 1000 );
total_days = Total_days ( day, month );
return temp + total_days;
}
void to_gregorian ( long julian )
{
int year_greg;
int day_greg;
int month_greg;
year_greg = julian / 1000;
day_greg = julian % 1000;
leap_year ( year_greg );
greg_ ( month_greg, day_greg );
cout << '\n'
<< "Gregorian date is : "
<< month_greg << "/"
<< day_greg << "/"
<< year_greg;
}
void to_gregorian
( long julian, int& month_greg, int& day_greg, int& year_greg )
{
year_greg = julian / 1000;
day_greg = julian % 1000;
leap_year ( year_greg );
greg_ ( month_greg, day_greg );
}
long leap_year ( long year, int k )
{
leap_year ( year );
return year *= k;
}
void leap_year ( long year )
{
if ( year % 4 == 0 )
days_per_month [ 1 ] = 29;
else
days_per_month [ 1 ] = 28;
if ( year % 100 == 0 && year % 400 != 0 )
days_per_month [ 1 ] = 28;
}
int in_month ( )
{
month = 0;
while ( month 12 )
{
cout << "Enter month (1 to 12): ";
cin >> month;
}
return month;
}
int in_day ( int month )
{
day = 0;
while ( day days_per_month [ month - 1 ] )
{
cout << "Enter day (1 to "
<< days_per_month [ month - 1 ] << "): ";
cin >> day;
}
return day;
}
int Total_days ( int day, int month_ )
{
int j;
total_days = day;
for ( j = 0; j < month_ - 1; j ++ )
total_days += days_per_month [ j ];
return total_days;
}
void greg_ ( int& monthx, int& daysx )
{
monthx = 1;
while ( daysx > days_per_month [ monthx - 1 ] )
{
daysx -= days_per_month [ monthx -1 ];
monthx ++;
}
}
int Days_Between ( long start, long end )
{
int temp1 = 0;
int temp2 = 0;
int startx;
int endx;
if ( ( end / 1000 ) > ( start / 1000 ) )
{
startx = start / 1000;
endx = end / 1000;
}
else
{
endx = start / 1000;
startx = end / 1000;
}
for ( int running = startx + 1; running <= endx; running ++ )
temp2 += Days_in_year ( running );
temp1 = ( ( end % 1000 ) - ( start % 1000 ) );
if ( ( end / 1000 ) > ( start / 1000 ) )
return temp1 + temp2;
else
return temp1 - temp2;
}
int Days_in_year ( long oops )
{
if ( oops > 10000 )
oops /= 1000;
if ( ( oops % 4 ) == 0 )
{
if ( oops % 100 == 0 && oops % 400 != 0 )
return 365;
else
return 366;
}
else
return 365;
}
char character ( char* doit )
{
return *doit;
}
/*
Routine to convert "character" numbers into long double decimal values.
(In this case used if values are brought through the command line input.)
*/
long double char_t_float ( char* ps )
{
long double build ( long double, int, int& ); // Prototype function
long double temp = 0.0;
int flag = 0;
int nflag = 0;
int pflag = 0;
while ( *ps )
{
switch ( *ps ++ )
{
case '0': // Convert 0
temp = build ( temp, 0, flag );
break;
case '1': // Convert 1
temp = build ( temp, 1, flag );
break;
case '2': // Convert 2
temp = build ( temp, 2, flag );
break;
case '3': // Convert 3
temp = build ( temp, 3, flag );
break;
case '4': // Convert 4
temp = build ( temp, 4, flag );
break;
case '5': // Convert 5
temp = build ( temp, 5, flag );
break;
case '6': // Convert 6
temp = build ( temp, 6, flag );
break;
case '7': // Convert 7
temp = build ( temp, 7, flag );
break;
case '8': // Convert 8
temp = build ( temp, 8, flag );
break;
case '9': // Convert 9
temp = build ( temp, 9, flag );
break;
case ',': // Ignores commas
break;
case '.': // Processes a period
if ( flag >= 1 )
{
cputs ( "\rProgram aborted! " );
cout << "To many decimal points.";
exit ( 210 );
}
flag = 1;
break;
case '-': // Processes a negative
if ( nflag >= 1 )
{
cputs ( "\rProgram aborted! " );
cout << "To many minus signs.";
exit ( 211 );
}
nflag = 1 ;
break;
case '+': // Processes a plus sign
if ( pflag >= 1 )
{
cputs ( "\rProgram aborted! " );
cout << "To many minus signs.";
exit ( 212 );
}
pflag = 1 ;
break;
default: // Take care of illegal character
cputs ( "\rProgram aborted! " );
cout << "Needs to be neumeric. "
<< "\n\rNo alphabitic or other "
<< "characters allowd.";
exit ( 213 );
}
}
if ( nflag == 0 ) // Return value rotine
return temp; // Returns value with same sign
else
return temp * ( -1.0 ); // Returns value with changed sign
}
/*
This function is called by the conversion routine.
*/
long double build ( long double ret_val, int no, int& flag )
{
long double temp;
temp = float ( no ); // Convert integer to floating point
if ( flag == 0 ) // Determine if left or right of decimal
{
return ( ret_val * 10.0 ) + temp; // Left of decimal
}
else
{
for ( int n = 1; n <= flag; n ++ ) // Right of decimal
{
temp /= 10.0;
}
flag ++;
return ret_val + temp; // Returns value
}
}
---
* OFFLINE 1.58
--- WILDMAIL!/WC v4.12
---------------
* Origin: Star Tech Diamond Bar, CA 909-396-4227 714-257-1175 (1:218/801.0)
|