Quick Little Program I Made

正在查看此主题的用户

Stonewall382

Knight at Arms
Hey guys, I got bored and decided to write a program in C++ (I'm learning C++ in a class right now, and am working to write a game).  I figured we could all use a break from the religion and racism threads, so if you feel like it, try it out (and please tell me what you think).

You'll need the HiC complier, which can be found by Googling "Hasker HiC" (I'm not sure if I'm allowed to give a direct link from the University website, so I won't).  Once you get it, paste this code into it, and hit F9 (or the green arrow):

(I know it's messy, but I didn't feel like making functions for the two options, because we haven't learned data structures yet, so the function calls would be messy as ****.)

Edit:  Like I said, I'm not sure if the free compiler can be used by people not in a course at some institution, but I don't know why this would be a problem.  Mods, if you don't like it, feel free to close the thread.  Here's the quote (from the HiC website) that may cause problems:
HiC, developed by Robert W. Hasker, is freely available for use by institutions of up to 15,000 students for on-campus (traditional) courses, though instructors should contact the author to get on a mailing list for updates. This mailing list will have very low traffic, and email addresses will not be shared with commercial organizations under any circumstances. Larger institutions, and instructors and students of online/distance courses must contact the author for permission to use HiC.

插入代码块:
/*    Written by [Stonewall382] March 12, 2007
      1st part written from ~ 11:00 to ~ 11:45.

      reg2 written from ~ 11:50 to ~ 12:10.
*/




#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int const MINUTES = 60;
int const HOURS = 60 * MINUTES;
int const DAYS  = 24 * MINUTES;
int const WEEKS = 7 * DAYS;
int const YEARS = 365.25 * DAYS;

int Month (string month);

int MonthInSeconds (int);

string OutputZero (int);


void main()
{
   int monthNumber1, day1, year1, hours1, minutes1, seconds1, sum,
   monthNumber2, day2, year2, hours2, minutes2, seconds2,
   yearsPassed, weeksPassed, daysPassed, hoursPassed, minutesPassed,
   secondsPassed,
   difference, reg1;

   string month1, month2, at, AND;

   char comma, colon;

   cout << "An example date would look like this:\n" <<
               "March 13, 2007, at 0:13 and 4.\n" <<
               "(This means March 13, 2007 at 12:13\n" <<
               "and four seconds in the a.m.)\n\n";

   sleep(2);

   while ( ! cin.eof())
   {
      cout << "1 for seconds between year 0 and a date, or\n" <<
              "2 for time between one date and another:\n";
      cin >> reg1;
      
      if (reg1 == 1)
      {
         cout << "Enter the month, day, year,\nand time (hours, minutes, seconds)" <<
         "\nin military format:\n";
         cin >> month1 >> day1 >> comma >> year1 >> at >>
                hours1 >> colon >> minutes1 >> AND >> seconds1;

         monthNumber1 = Month (month1);

         sum = (year1 * YEARS) + (monthNumber1 * MonthInSeconds (monthNumber1)) + (day1 * DAYS) +
               (hours1 * HOURS) + (minutes1 * MINUTES) +
               seconds1;

         cout << "\n\n" << sum << " seconds have passed\n" <<
         "between midnight January 1st, year 0 and\n" << month1 <<
         " " << day1 << ", " << year1 << " at time " << hours1 <<
         ":" << OutputZero (minutes1) << minutes1 << " and " << seconds1 << " seconds.\n\n";
      }
      else
      {
         cout << "Enter the earlier date:\n";
         cin >> month1 >> day1 >> comma >> year1 >> at >>
                hours1 >> colon >> minutes1 >> AND >> seconds1;
         cout << "\nEnter the later date:\n";
         cin >> month2 >> day2 >> comma >> year2 >> at >>
                hours2 >> colon >> minutes2 >> AND >> seconds2;

         monthNumber1 = Month (month1);
         monthNumber2 = Month (month2);

         difference = ((year2 * YEARS) + (monthNumber2 * MonthInSeconds (monthNumber2)) +
                       (day2 * DAYS) + (hours2 * HOURS) + (minutes2 * MINUTES) + seconds2) -
                       ((year1 * YEARS) + (monthNumber1 * MonthInSeconds (monthNumber1)) +
                       (day1 * DAYS) + (hours1 * HOURS) + (minutes1 * MINUTES) + seconds1);

         yearsPassed = difference / YEARS;
         difference /= YEARS;

         weeksPassed = difference / WEEKS;
         difference /= WEEKS;

         daysPassed = difference / DAYS;
         difference /= DAYS;

         hoursPassed = difference / HOURS;
         difference /= HOURS;

         minutesPassed = difference / MINUTES;
         difference /= MINUTES;

         secondsPassed = difference;

         cout << "\n\n" << yearsPassed<< " years, " << weeksPassed << " weeks, " <<
         daysPassed << " days, " << hoursPassed << " hours, " <<
         minutesPassed << " minutes, and " << secondsPassed << " seconds\n" <<

         "passed between " << month1 << " " << day1 << ", " << year1 << " at " <<
         hours1 << ":" << OutputZero (minutes1) << minutes1 << " and "

         << seconds1 << " seconds, and\n" <<
         "passed between " << month2 << " " << day2 << ", " << year2 << " at " <<
         hours1 << ":" << OutputZero (minutes2) << minutes2 << " and " <<
         seconds2 << " seconds";
      }
      cout << "\n\n";
   }
}

string OutputZero (int minutes)
{
   string bah;

   if (minutes < 10)
      bah = "0";

   return (bah);
}

int Month (string month)
{
   int monthNum;

   if (month == "January")
      monthNum = 1;
   else if (month == "February")
      monthNum = 2;
   else if (month == "March")
      monthNum = 3;
   else if (month == "April")
      monthNum = 4;
   else if (month == "May")
      monthNum = 5;
   else if (month == "June")
      monthNum = 6;
   else if (month == "July")
      monthNum = 7;
   else if (month == "August")
      monthNum = 8;
   else if (month == "September")
      monthNum = 9;
   else if (month == "October")
      monthNum = 10;
   else if (month == "November")
      monthNum = 11;
   else if (month == "December")
      monthNum = 12;

   return (0);
}

int MonthInSeconds (int month)
{
   int monthInSeconds;

   if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
      monthInSeconds = 31 * DAYS;
   else if (month == 2)
      monthInSeconds = 28 * DAYS;
   else
      monthInSeconds = 30 * DAYS;

   return monthInSeconds;
}
 
Looks like it should work with any C++ compiler. Have fun - It's a great language (I miss it).
 
Thanks for the info calandale.  Visual Studio is free, right?

Btw--I just realized that I didn't say what the program does.  It can compute the number of seconds between year 0 and a certain time after year 0 (like the exact current time), or can tell the number of years, weeks, days, hours, minutes, and seconds between two dates (so long as both are after year 0).
I didn't idiot-proof it yet, so putting in values above what they should be (85 seconds, for example) wll make it act a little weird (probably only on the output--it won't say minutes + 1 and seconds - 60, it'll just say the seconds), and dates before zero will probably make it go screwy (though I haven't checked).  So will entering a date earlier for one that should be later, and vice versa.
 
looks like all numbers would have to be calculated negatively from the first day of year 0 (which doesn't even exist). I would suggest sticking to A.D. only.
 
Yeah, I realized my idiocy considering the absense of a year 0, but it was only after I had disconnected (dial-up), turned off the compie, and gone to bed. :sad:

Oh, I understand your question Voutare.  I suppose it would have to be negative, yes.  How well does that work?
 
后退
顶部 底部