You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB
Go

package main
import (
"fmt"
"os"
"strconv"
)
var ascii_intro string = `
_ ___ _ _ _____ _
| | / (_) | | (_) / ___| | |
| |/ / _ _ __ ___ _ __ ___ __ _| |_ _ ___ ___ \ ` + "`" + `--. ___ | |_ _____ _ __
| \| | '_ \ / _ \ '_ ` + "`" + ` _ \ / _` + "`" + ` | __| |/ __/ __| ` + "`" + `--. \/ _ \| \ \ / / _ \ '__|
| |\ \ | | | | __/ | | | | | (_| | |_| | (__\__ \ /\__/ / (_) | |\ V / __/ |
\_| \_/_|_| |_|\___|_| |_| |_|\__,_|\__|_|\___|___/ \____/ \___/|_| \_/ \___|_|
`
// GenDisplaceFn produces functions that solves a linear kinematics scenario for a given time value.
// `acceleration` is a float64 number of the units the meters per second squared (m/s^2)
// `initialVelocity` is a float64 number of the units meters per second (m/s)
// `initialDisplacement` is a float64 number in the unit of meters(m) refering to the starting position of
// object under consideration
// s = 1/2*a*t^2 +vo*t+so
// vo is initial displacement so initial displacement
func GenDisplaceFn(acceleration float64, initialVelocity float64, intialDisplacement float64) func(float64) float64 {
displacementFunc := func(t float64) float64 {
return (0.5*acceleration*t*t + initialVelocity*t + intialDisplacement)
}
return displacementFunc
}
func processUserInput(value string) float64 {
userFloat, err := strconv.ParseFloat(value, 64)
if err != nil {
fmt.Println("Opps you entered and incorrect input! Please enter float values only.")
os.Exit(1)
}
return userFloat
}
func main() {
var acceleration, initialVelocity, initialDisplacement, displaceTime float64
var userInput string
fmt.Println(ascii_intro)
fmt.Println("Ok cowboy! Let's solve some linear kinematics.")
fmt.Println("Give me a value for \"acceleration\":")
fmt.Scan(&userInput)
acceleration = processUserInput(userInput)
fmt.Println("Give me a value for \"initial velocity\":")
fmt.Scan(&userInput)
initialVelocity = processUserInput(userInput)
fmt.Println("Give me a value for \"initial displacement\":")
fmt.Scan(&userInput)
initialDisplacement = processUserInput(userInput)
displacementFn := GenDisplaceFn(acceleration, initialVelocity, initialDisplacement)
fmt.Println("What value of \"time\" will we calculate displacement for?:")
fmt.Scan(&userInput)
displaceTime = processUserInput(userInput)
fmt.Println(displacementFn(displaceTime))
}