Книга программиста/Задачи на Small Basic

К оглавлению

Все программы, код которых выложен здесь, являются работоспособными. На момент написания программ использовалась среда Microsoft Small Basic.

Простой калькулятор править

While 1 = 1
  TextWindow.BackgroundColor = "Blue" ' Изменяем цвет фона
  TextWindow.ForegroundColor = "White" ' Изменяем цвет текста
  TextWindow.Write("a: ")
  a = TextWindow.ReadNumber()
  TextWindow.Write("b: ")
  b = TextWindow.ReadNumber()
  
  TextWindow.Write("операция: ")
  operation = TextWindow.Read()
  
  TextWindow.BackgroundColor = "Blue"
  TextWindow.ForegroundColor = "Yellow"
  If operation = "+" Then
    TextWindow.WriteLine(a + " + " + b + " = " + (a + b))
  ElseIf operation = "-" Then
    TextWindow.WriteLine(a + " - " + b + " = " + (a - b))
  ElseIf operation = "*" Then
    TextWindow.WriteLine(a + " * " + b + " = " + (a * b))
  ElseIf operation = "/" Then
    TextWindow.WriteLine(a + " / " + b + " = " + (a / b))
  Else
    TextWindow.WriteLine("-> операция " + operation + " не определена.")
  EndIf
EndWhile

Вставка числа в массив править

TextWindow.WriteLine("Количество элементов: ")
count = TextWindow.ReadNumber()
TextWindow.WriteLine("Введите массив: ")
For i = 0 To count - 1
  a[i] = TextWindow.ReadNumber()
EndFor

TextWindow.WriteLine("Позиция: ")
position = TextWindow.ReadNumber()
If position >= count Then
  TextWindow.WriteLine("Недопустимый индекс.")
Else
  TextWindow.WriteLine("Значение: ")
  value = TextWindow.ReadNumber()
  For i = count To position + 1 Step -1
    a[i] = a[i - 1]
  EndFor
  a[position] = value
  
  TextWindow.WriteLine("Массив после вставки: ")
  For i = 0 To count
    TextWindow.WriteLine(a[i])
  EndFor
EndIf

Вывод матрицы править

TextWindow.Write("Количество строк: ")
rows = TextWindow.ReadNumber()
TextWindow.Write("Количество столбцов: ")
cols = TextWindow.ReadNumber()

For i = 0 To rows - 1
  For j = 0 To cols - 1
    a[i][j] = TextWindow.ReadNumber()
  EndFor
EndFor

TextWindow.Write("Количество символов: ")
length = TextWindow.ReadNumber()
For i = 0 To rows - 1
  For j = 0 To cols - 1
    l = Text.GetLength(a[i][j])
    s = ""
    For count = 1 To length - l ' Узнаем сколько надо добавить пробелов для выравнивания
      s = s + " "
    EndFor
    TextWindow.Write(a[i][j] + s)
  EndFor
  TextWindow.WriteLine("") ' Для перевода на новую строку
EndFor