How to resolve the algorithm Old Russian measure of length step by step in the Go programming language
How to resolve the algorithm Old Russian measure of length step by step in the Go programming language
Table of Contents
Problem Statement
Write a program to perform a conversion of the old Russian measures of length to the metric system (and vice versa).
It is an example of a linear transformation of several variables.
The program should accept a single value in a selected unit of measurement, and convert and return it to the other units: vershoks, arshins, sazhens, versts, meters, centimeters and kilometers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Old Russian measure of length step by step in the Go programming language
The provided Go program is a basic unit converter that allows users to convert values between different units of measurement. Here's a detailed explanation of the code:
-
Package and Imports:
- The program is in a package called
main
, indicating that it's the entry point for the application. - It imports several standard Go libraries:
bufio
: For reading user input from the standard input.fmt
: For formatting and printing output.os
: For interacting with the operating system.strconv
: For converting strings to numeric types.strings
: For manipulating strings.
- The program is in a package called
-
Measurement Units and Conversion Factors:
- Two slices,
units
andconvs
, are defined to represent the various units of measurement and their corresponding conversion factors, respectively. units
contains the names of the units in a specific order, whileconvs
stores the conversion factors to convert from the selected unit to the other units.
- Two slices,
-
Input Processing Loop:
- The program enters an infinite loop to repeatedly prompt the user for input and perform unit conversions.
- Inside the loop:
- It prints the list of available units to choose from, along with their corresponding numbers.
- It prompts the user to choose a unit by entering a number from 1 to 13.
- It validates the user's input to ensure it's within the valid range and prompts for input again if invalid.
- It decrements the unit number by 1 to match the index in the
units
andconvs
slices. - It prompts the user to enter a value in the chosen unit.
- It validates the user's input to ensure it's a non-negative number and prompts for input again if invalid.
-
Unit Conversion and Output:
- Once the user has provided valid input, the program calculates the equivalent values in all the other units.
- It iterates through the
units
slice, excluding the selected unit, and calculates the equivalent value using the conversion factors. - It prints the equivalent values in a formatted table.
-
User Prompt for Another Conversion:
- After displaying the conversion results, the program prompts the user if they want to perform another conversion.
- It reads input from the user and validates it to ensure it's either "y" (yes) or "n" (no), prompting for input again if invalid.
- If the user enters "n," the program exits.
In summary, this Go program provides an easy-to-use interactive unit converter that allows users to convert values between different units of measurement. It handles input validation and displays the conversion results in a clear table format.
Source code in the go programming language
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
units := []string{
"tochka", "liniya", "dyuim", "vershok", "piad", "fut",
"arshin", "sazhen", "versta", "milia",
"centimeter", "meter", "kilometer",
}
convs := []float32{
0.0254, 0.254, 2.54, 4.445, 17.78, 30.48,
71.12, 213.36, 10668, 74676,
1, 100, 10000,
}
scanner := bufio.NewScanner(os.Stdin)
for {
for i, u := range units {
fmt.Printf("%2d %s\n", i+1, u)
}
fmt.Println()
var unit int
var err error
for {
fmt.Print("Please choose a unit 1 to 13 : ")
scanner.Scan()
unit, err = strconv.Atoi(scanner.Text())
if err == nil && unit >= 1 && unit <= 13 {
break
}
}
unit--
var value float64
for {
fmt.Print("Now enter a value in that unit : ")
scanner.Scan()
value, err = strconv.ParseFloat(scanner.Text(), 32)
if err == nil && value >= 0 {
break
}
}
fmt.Println("\nThe equivalent in the remaining units is:\n")
for i, u := range units {
if i == unit {
continue
}
fmt.Printf(" %10s : %g\n", u, float32(value)*convs[unit]/convs[i])
}
fmt.Println()
yn := ""
for yn != "y" && yn != "n" {
fmt.Print("Do another one y/n : ")
scanner.Scan()
yn = strings.ToLower(scanner.Text())
}
if yn == "n" {
return
}
}
}
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the SQL/PostgreSQL programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the C++ programming language
You may also check:How to resolve the algorithm Arena storage pool step by step in the C programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the ColdFusion programming language
You may also check:How to resolve the algorithm Empty program step by step in the Processing programming language