প্রোগ্রামিংয়ের মৌলিক ধারণা/সুইফট শর্তের উদাহরণ
অবয়ব
তাপমাত্রা
[সম্পাদনা]// এই প্রোগ্রামটি ব্যবহারকারীর কাছ থেকে একটি ফারেনহাইট তাপমাত্রা জানতে চায়,
// প্রদত্ত তাপমাত্রাটি সেলসিয়াসে রূপান্তর করে,
// এবং ফলাফল প্রদর্শন করে।
//
// তথ্যসূত্র:
// https://www.mathsisfun.com/temperature-conversion.html
// https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
func getChoice() -> String {
var choice: String
print("সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন:")
choice = readLine(strippingNewline: true)!
return choice
}
func getTemperature(label: String) -> Double {
var temperature: Double
print("দিন " + label + " তাপমাত্রা:")
temperature = Double(readLine(strippingNewline: true)!)!
return temperature
}
func calculateCelsius(fahrenheit: Double) -> Double {
var celsius: Double
celsius = (fahrenheit - 32) * 5 / 9
return celsius
}
func calculateFahrenheit(celsius: Double) -> Double {
var fahrenheit: Double
fahrenheit = celsius * 9 / 5 + 32
return fahrenheit
}
func displayResult(temperature: Double, fromLabel: String, result: Double, toLabel: String) {
print(String(temperature) + "° " + fromLabel + " হল " + String(result) + "° " + toLabel)
}
func main() {
// main could either be an if-else structure or a switch-case structure
var choice: String
var temperature: Double
var result: Double
choice = getChoice()
// if-else approach
if choice == "C" || choice == "c" {
temperature = getTemperature(label:"ফারেনহাইট")
result = calculateCelsius(fahrenheit:temperature)
displayResult(temperature:temperature, fromLabel:"ফারেনহাইট", result:result, toLabel:"সেলসিয়াস") }
else if choice == "F" || choice == "f" {
temperature = getTemperature(label:"সেলসিয়াস")
result = calculateFahrenheit(celsius:temperature)
displayResult(temperature:temperature, fromLabel:"সেলসিয়াস", result:result, toLabel:"ফারেনহাইট")
}
else {
print("সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন")
}
// switch-case approach
switch choice {
case "C", "c":
temperature = getTemperature(label:"ফারেনহাইট")
result = calculateCelsius(fahrenheit:temperature)
displayResult(temperature:temperature, fromLabel:"ফারেনহাইট", result:result, toLabel:"সেলসিয়াস")
case "F", "f":
temperature = getTemperature(label:"সেলসিয়াস")
result = calculateFahrenheit(celsius:temperature)
displayResult(temperature:temperature, fromLabel:"সেলসিয়াস", result:result, toLabel:"ফারেনহাইট")
default:
print("সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন")
}
}
main()
আউটপুট
[সম্পাদনা]সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন: c ফারেনহাইট তাপমাত্রা দিন: 100 100° ফারেনহাইট হল 37.7777777777778° সেলসিয়াস সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন: f সেলসিয়াস তাপমাত্রা দিন: 100 100° সেলসিয়াস হল 212° ফারেনহাইট সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন: x সেলসিয়াসে রূপান্তর করতে C অথবা ফারেনহাইটে রূপান্তর করতে F লিখুন