Author Topic: (Challenge) Time travel  (Read 5400 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) Time travel
« Reply #15 on: March 03, 2005, 01:52:11 PM »
Here's a C++ version.

Code: [Select]

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main(int argc, char * argv[])
{
    // configure inline arguments
    if (argc != 2)
    {
cout << "Enter a number followed by either a 'm' for miles\n";
cout << "or a 'k' for kilometers.  Example 1000m\n";
cerr << "Usage: " << argv[0] << " number(m/k)\n";
exit(1);
    }

    string str(argv[1]);
    float spol = 186000.00;
    float spos = 774.98;
    int strl = (str.size() - 1);

    if (str.find("m") == strl)
    {
float dist = atof(str.c_str());
cout << "Traveling @ the speed of light would take \n";
cout << dist / spol << " seconds to travel " << dist;
cout << " miles." << endl;

cout << "\nTraveling @ the speed of sound would take \n";
cout << dist / spos << " hours to travel " << dist;
cout << " miles." << endl;
    }

    else if (str.find("k") == strl)
    {
float dist = atof(str.c_str());
cout << "\nTraveling @ the speed of light would take \n";
cout << (dist * 0.621) / spol << " seconds to travel " << dist;
cout << " kilometers." << endl;

cout << "\nTraveling @ the speed of sound would take \n";
cout << (dist * 0.621) / spos << " hours to travel " << dist;
cout << " kilometers." << endl;
    }

    else
cout << "\nSorry I don't understand this -> (" << str << ") input";

    return 0;
}


Output

Code: [Select]

D:\>traveltime 1500m
Traveling @ the speed of light would take
0.00806452 seconds to travel 1500 miles.

Traveling @ the speed of sound would take
1.93553 hours to travel 1500 miles.
TheSwamp.org  (serving the CAD community since 2003)

t-bear

  • Guest
(Challenge) Time travel
« Reply #16 on: March 03, 2005, 03:16:33 PM »
Just a little FYI.  Here's how long it'd take me to get home:
Quote
Enter distance: 45m
Traveling @ the speed of light would take 0.00024194 seconds
Traveling @ the speed of sound would take 0.05806601 hours

At the speed of "old Dodge pickup", it takes just under one hour......... :roll: