Gravity - Gravity Force
/************************************************************************************************
* Program name : Gravity force *
* Program ver. : 2.0 *
* Created by : SharpDevelop *
* Code author : Perić Željko *
* Code language : C# *
* Date created : 03.02.2012 *
* Time created : 15:22 *
* *
* *
* Program Description : *
* *
* Simple console application for calculating strength of gravity force ( F ) , *
* between two celestial bodies with known masses ( M and m ) , *
* and known distance between centers of body masses ( r ). *
* *
* Math formula : F = M * m * G / r ^ 2 *
* *
* Try to calculate gravity force between Sun and Earth. *
* *
* Mass of Sun M = 1,99 e+30 Kg *
* Mass of Earth m = 5,98 e+24 Kg *
* Distance r = 1,5 e+11 m *
* Gravity constant G = 6,67 e-11 N * m^2 / Kg^2 *
* *
* Result should be F = 3,527748177777778 e+22 N (newton) N = Kg * m / s^2 *
* *
* This result presumes ideal condition , that there are only these two bodies *
* in universe with no other forces of any kind that can influence them. *
* *
* Program accepts scientific format for large and small numbers. *
* *
* 4,3 e+5 = 4,3 * (10 ^ 5) = 4 300 000 e+5 stands for 10 raised to +5th power *
* *
* 587 e-5 = 587 * (10 ^-5) = 0,00587 e-5 stands for 10 raised to -5th power *
* *
* To enter such number , just type in 4,3e+5 or 587e-5. *
* *
* Number range that this program accept is from 1,7 e-308 up to 1,7 e+308 *
* *
* Program Accuracy Notice : *
* *
* All calculations in program are executed using double type of variable. *
* By default , a double value contains 15 decimal digits of precision , *
* although a maximum of 17 digits is maintained internally. *
* Result is shown in scientific format , rounded at fourteen decimals. *
* This could cause fatal error because , in some cases for accurate result , *
* many more digits are significant , especially when very large or very small *
* numbers are involved. *
* *
* Gravity constant value is rounded at two decimal places , *
* and usually input values that user type in , have it's own precision error... *
* *
* So the result that this program gives is always approximate and never 100 % accurate ! *
* *
* *
* All the best, *
* Author. *
************************************************************************************************/
using System;
namespace Gravity
{
class Program
{
public static void Main(string[] args)
{
//
// Description of Main.
//
// Declare new object named Start ,
// as an instance of GravityForce class ,
// and call it's method Calculate().
//
GravityForce Start = new GravityForce();
Start.Calculate();
}
}
class GravityForce
{
public void Calculate()
{
//
// Description of Calculate().
//
// Set values to necessary variables for calculation ,
// by calling GetValue(int cursor_x , int cursor_y) method.
// Calculate strength of gravity force and show result on
// console screen as formatted string representation of number.
//
//
// declaration of variables
//
const double G = 6.67e-11; // gravity constant , just two decimal places !!!
double M = 0; // mass of first body
double m = 0; // mass of second body
double r = 0; // distance between centers of body masses
double F = 0; // gravity force
string Result = ""; // result of calculation represented as string
//
// set console : title , window & buffer size , fore & back color and clear console
//
Console.Title = " Gravity - by Perić Željko (periczeljkosmederevo@yahoo.com)";
Console.SetWindowSize(70,40);
Console.SetBufferSize(70,40);
Console.ForegroundColor = ConsoleColor.Green; // text color
Console.BackgroundColor = ConsoleColor.Black; // back color
Console.Clear();
//
// declare new variable 'key' and set it's initial value to 'Y' - 'Yes' ,
// used for getting user's answer from keyboard at the end of program
//
ConsoleKeyInfo key = new ConsoleKeyInfo('Y',ConsoleKey.Y,true,true,true);
//
// restart calculation if answer is 'Y' - 'Yes'
//
while(key.Key == ConsoleKey.Y)
{
//
// set variables to initial values
//
M = 0;
m = 0;
r = 0;
F = 0;
//
// write program description to user
//
Console.Clear();
Console.Write("**********************************************************************");
Console.Write(" ");
Console.Write(" Program for calculating strength of gravity force between two bodies ");
Console.Write(" ");
Console.Write("**********************************************************************");
//
// get value for M
//
Console.SetCursorPosition(0,5);
Console.Write(" ");
Console.Write(" Enter value for mass of first body in kilograms : ");
Console.Write(" ");
Console.Write(" M = ");
Console.Write(" ");
Console.Write("**********************************************************************");
M = GetValue(5,8); // Call GetValue(int cursor_x , int cursor_y) method
//
// get value for m
//
Console.SetCursorPosition(0,11);
Console.Write(" ");
Console.Write(" Enter value for mass of second body in kilograms : ");
Console.Write(" ");
Console.Write(" m = ");
Console.Write(" ");
Console.Write("**********************************************************************");
m = GetValue(5,14); // Call GetValue(int cursor_x , int cursor_y) method
//
// get value for r
//
Console.SetCursorPosition(0,17);
Console.Write(" ");
Console.Write(" Enter value for distance between centers of body masses in meters : ");
Console.Write(" ");
Console.Write(" r = ");
Console.Write(" ");
Console.Write("**********************************************************************");
r = GetValue(5,20); // Call GetValue(int cursor_x , int cursor_y) method
//
// write values of parameters on console with it's basic units of measurement ,
// number format is scientific , rounded at four decimals that must be shown
//
Console.SetCursorPosition(0,23);
Console.Write(" ");
Console.Write(" M = " + M.ToString("0.0000 e+000") + " Kg" + "\n");
Console.Write(" m = " + m.ToString("0.0000 e+000") + " Kg" + "\n");
Console.Write(" r = " + r.ToString("0.0000 e+000") + " m" + "\n");
Console.Write(" G = " + G.ToString("0.0000 e+000") + " N * m^2 / Kg^2" + "\n");
Console.Write(" ");
Console.Write("**********************************************************************");
//
// calculate gravity force
//
r = r*r;
F = (M*m*G)/r;
//
// set value to Result ,
// number format is scientific rounded at fourteen decimals ,
// first four decimals must be shown even if equal zero ,
// next ten decimals are shown optionally , if not zero
//
Result = F.ToString("0.0000########### e+000");
//
// show result on console
//
Console.Write(" ");
Console.Write(" Strength of gravity force between two bodies is : ");
Console.Write(" ");
Console.Write(" F = " + Result + " N (newton) N = Kg * m / s^2 " + "\n");
Console.Write(" ");
Console.Write("**********************************************************************");
//
// end of program
//
Console.Write(" ");
Console.Write(" Another calculation (y/n) ? ");
key = Console.ReadKey(true);
}
}
private double GetValue(int cursor_x , int cursor_y)
{
//
// Description of GetValue(int cursor_x , int cursor_y).
//
// Controls input from keyboard on specified console position.
// Gets word from keyboard , preventing that entered word length
// is greater then 60 letters , and securing that letters entered
// are only from the list of possible letters.
//
// To enter very large or very small number
// use scientific format that program recognize.
//
// Mass of Sun M = 1,99 e+30 Kg
//
// type in 1,99e+30 or 1,99e30 or 1,99E+30 or 1,99E30
//
// Gravity constant G = 6,67 e-11 N * m^2 / Kg^2
//
// type in 6,67e-11 or 6,67E-11
//
// Instead of ',' use '.' if local regional settings
// declare point instead comma for decimal separator.
//
//
// declaration of variables
//
const string possible_letters = "1234567890eE+-,."; // list of possible letters
string word = ""; // entered word from keyboard
string letter = ""; // last typed letter on keyboard
int length = 0; // length of word
double number = 0; // converted word to double value
bool IsNumber = false; // number relevance switch
//
// get new word from keyboard ,
// repeat until user has entered correct double value
//
while(IsNumber == false)
{
word = "";
letter = "";
length = 0;
number = 0;
Console.SetCursorPosition(cursor_x, cursor_y);
Console.Write(" ");
Console.SetCursorPosition(cursor_x, cursor_y);
//
// get new letter from keyboard until pressed key is 'Enter'
// if typed key is 'Delete' or 'Backspace' delete last letter from word
//
ConsoleKeyInfo key = new ConsoleKeyInfo();
while(key.Key != ConsoleKey.Enter)
{
key = Console.ReadKey();
if(key.Key == ConsoleKey.Backspace || key.Key == ConsoleKey.Delete)
{
length--;
if(length < 0)
{
length = 0;
}
word = word.Substring(0, length);
Console.SetCursorPosition(cursor_x, cursor_y);
Console.Write(" ");
Console.SetCursorPosition(cursor_x, cursor_y);
Console.Write(word);
}
else
{
letter = key.KeyChar.ToString();
if(possible_letters.Contains(letter ))
{
length++;
if(length < 61)
{
word = word + letter;
}
else
{
length = 60;
Console.SetCursorPosition(cursor_x, cursor_y);
Console.Write(" ");
Console.SetCursorPosition(cursor_x, cursor_y);
Console.Write(word);
}
}
else if(!possible_letters.Contains( letter ))
{
Console.SetCursorPosition( cursor_x, cursor_y );
Console.Write(" ");
Console.SetCursorPosition( cursor_x, cursor_y );
Console.Write(word);
}
}
}
//
// check does entered word represents positive real number greater then zero
// if not , write error message and ask for new entry
//
IsNumber = double.TryParse(word.ToString(), out number);
if(IsNumber)
{
if(number <= 0)
{
Console.Beep();
Console.SetCursorPosition(0, cursor_y + 4);
Console.Write(" Entry error ! Entered value must be greater then zero ! ");
Console.Write(" ");
Console.Write(" Press any key to continue ...");
Console.ReadKey(true);
Console.SetCursorPosition(0, cursor_y + 4);
Console.Write(" ");
Console.Write(" ");
Console.Write(" ");
IsNumber = false;
}
}
else
{
Console.Beep();
Console.SetCursorPosition(0, cursor_y + 4);
Console.Write(" Entry error ! You have entered incorrect value ! ");
Console.Write(" ");
Console.Write(" Press any key to continue ...");
Console.ReadKey(true);
Console.SetCursorPosition(0, cursor_y + 4);
Console.Write(" ");
Console.Write(" ");
Console.Write(" ");
}
}
//
// return entered number
//
return number;
}
}
}
/************************************************************************
* Program Licence : *
* *
* Copyright 2012 , Perić Željko *
* (periczeljkosmederevo@yahoo.com) *
* *
* According to it's main purpose , this program is licenced *
* under the therms of 'Free Software' licence agreement. *
* *
* If You do not know what those therms applies to *
* please read explanation at the following link : *
* (http://www.gnu.org/philosophy/free-sw.html.en) *
* *
* Since it is Free Software this program has no warranty of any kind. *
************************************************************************
* Ethical Notice : *
* *
* It is not ethical to change program code signed by it's author *
* and then to redistribute it under the same author name , *
* especially if it is incorrect. *
* *
* It is recommended that if you make improvement in program code , *
* to make remarks of it and then to sign it by Your's name , *
* for further redistribution as new major version of program. *
* *
* Author name and references of old program code version should be *
* kept , for tracking history of program development. *
* *
* For any further information please contact code author at his email. *
************************************************************************/
/************************************
* List Of Revisions *
************************************
* Minor revision of version 1.0 *
* Author 15.02.2013 *
* new comments added *
* console user interface changed *
* number format changed *
* spelling checked *
* program licence added *
* New version number 1.1 *
************************************
* Major revision of version 1.1 *
* Author 20.02.2013 *
* class GravityForce created *
* New version number 2.0 *
************************************/