Ohjelmointi 1/2020, demo 1 vastauksia
T1. Järjestyksen vaihtaminen Taunolla
int temp;
temp = t[0];
t[0] = t[5];
t[5] = temp;
temp = t[1];
t[1] = t[4];
t[4] = temp;
temp = t[2];
t[2] = t[3];
t[3] = temp;
int temp;
temp = t[0];
t[0] = t[1];
t[1] = temp;
temp = t[2];
t[2] = t[3];
t[3] = temp;
temp = t[4];
t[4] = t[5];
t[5] = temp;
1. Hakemistot
Annetut komennot:
=================
mkdir -p kurssit/ohj1/demot/demo1
mkdir -p kurssit/ohj1/demot/demo2
mkdir -p kurssit/ohj1/ht
ls -laR kurssit
Tuloste
=======
kurssit:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ../
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ohj1/
kurssit/ohj1:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ../
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 demot/
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ht/
kurssit/ohj1/demot:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ../
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 demo1/
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 demo2/
kurssit/ohj1/demot/demo1:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ../
kurssit/ohj1/demot/demo2:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:32 ../
kurssit/ohj1/ht:
total 0
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ./
drwxr-xr-x 1 vesal 197121 0 Sep 2 19:33 ../
2. Kirjoittaminen
1. Kirjoitin ensimmäisen rivin ja painoin enter.
2. Ctrl+A, Ctrl+V (10 kertaa), ensimmäinen Ctrl+V korvaa aiemmin kirjoitetun.
3. Ctrl+A, Ctrl+V (10 kertaa), -''-
4. 10 kertaa viiva manuaalisesti.
5. Ctrl+Shift+Home.
6. Ctrl+C, Ctrl+V (8 kertaa).
7. Enter
8. Nuoli ylös, Shift+Alas.
9. Ctrl+C, Ctrl+V (2 kertaa).
10. Kirjoitetaan väliin manuaalisesti rivit x1..x9,x0 lopuksi taas Enter.
11. Shifti pohjassa maalataan x1..x0 ja lopun rivinvaihto.
12. Ctrl+C, Ctrl+V (4 kertaa)
13. Valitaan sopivat rivit ja Find+Replace (Ctrl+R) muutetaan x oikeaksi luvuksi
14. Toistetaan 13 kunnes jäljellä enää viimeinen x0.
15. Muutetaan x0-->40.
2. Komentojonolla Bash-shellin tapaan
#! /bin/bash
for i in {1..100}; do printf "Harjoittelen tekstitiedoston tekemistä!\n"; done
printf -- "--------------------------------------------------------------------------------\n"
for i in {1..40}; do printf "%02d\n" $i; done
printf -- "--------------------------------------------------------------------------------\n"
2. Komentojonolla Bash-shellin tapaan lyhyempi versio
#! /bin/bash
printf "Harjoittelen tekstitiedoston tekemistä!\n%.0s" {1..100}
printf "%.s-" {1..80}; printf "\n"
printf "%02d\n" {1..40}
printf "%80s\n" " " | tr " " -
2. Komentojonolla Windows
@echo off
:: Kirjoitetaan vaikka komentotiedosto teht2.bat ja sitten ajetaan se komentorivilta
for /L %%i in (1,1,100) do echo Harjoittelen tekstitiedoston tekemistä! >>teht2-2.txt
echo -------------------------------------------------------------------------------->>teht2-2.txt
for /L %%i in (1,1,9) do echo 0%%i>>teht2-2.txt
for /L %%i in (10,1,40) do echo %%i>>teht2-2.txt
echo -------------------------------------------------------------------------------->>teht2-2.txt
2. Komentojonolla Unix (vanha Bourne shell)
i=1; while test $i -le 100; do echo Harjoittelen tekstitiedoston tekemistä!; i=`expr $i + 1`; done
echo --------------------------------------------------------------------------------
i=1; while test $i -le 9; do echo 0$i; i=`expr $i + 1`; done
while test $i -le 40; do echo $i; i=`expr $i + 1`; done
echo --------------------------------------------------------------------------------
2. Komentojonolla Unix (Posix-standardin mukainen)
i=1; while [ $i -le 100 ]; do echo Harjoittelen tekstitiedoston tekemistä!; i=$((i+1)); done
echo --------------------------------------------------------------------------------
i=1; while [ $i -le 9 ]; do echo 0$i; i=$((i+1)); done
while [ $i -le 40 ]; do echo $i; i=$((i+1)); done
echo --------------------------------------------------------------------------------
2. C#-versio
Teht2.cs
00001
00002
00003
00004
00005
00006
00007 public class Harjoittelen
00008 {
00009
00010
00011
00012 public static void Main()
00013 {
00014 for (int i=1; i<=100; i++)
00015 System.Console.WriteLine("Harjoittelen tekstitiedoston tekemistä!");
00016 for (int i=1; i<=80; i++)
00017 System.Console.Write("-");
00018 System.Console.WriteLine();
00019 for (int i=1; i<=40; i++)
00020 System.Console.WriteLine("{0:00}", i);
00021 for (int i=1; i<=80; i++)
00022 System.Console.Write("-");
00023 System.Console.WriteLine();
00024 }
00025 }
2. Python-versio
for i in range(0,100):
print("Harjoittelen tekstitiedoston tekemistä!")
print("-"*80)
for i in range(1,40+1):
print(f"{i:02}")
print("-"*80)
2. TIM-versio
{%for i in range(0,100)%}
Harjoittelen tekstitiedoston tekemistä!\
{%endfor%}
{%for i in range(0,80)%}-{%endfor%}\
{%for i in range(1,40+1)%}
%%i|fmt("02d")%%\
{%endfor%}
{% for i in range(0,80)%}-{%endfor%}\
4. Tiedon tallennusmuodot
binääri dec char hex
==========================
01010100 = 84 => T 54
01010101 = 85 => U 55
01010100 = 84 => T 54
01001011 = 75 => K 4B
01001001 = 73 => I 49
01000101 = 69 => E 45
01001100 = 76 => L 4C
01001101 = 77 => M 4D
01010011 = 83 => S 53
Eli teksti: TUTKIELMS
Viimeinen pitäisi olla
01000001 = 65 => A:sta on lyöty 19 mm liikaa oikealle. (heksana 41)
5. Konsolipohjaiset C#-ohjelmat
Mina.cs
00001
00002
00003
00004
00005
00006
00007
00008 public class Mina
00009 {
00010
00011
00012
00013
00014 public static void Main(string[] args)
00015 {
00016 System.Console.WriteLine("Vesa Lappalainen");
00017 System.Console.WriteLine("HTC 7 Pro");
00018 System.Console.WriteLine("Sonera");
00019
00020
00021
00022 }
00023 }
6. Lumiukolle silmät
Lumiukko/Lumiukko.cs
00001 using Jypeli;
00002
00003
00004
00005
00006
00007
00008
00009 public class Lumiukko : PhysicsGame
00010 {
00011
00012
00013
00014 public override void Begin()
00015 {
00016 Camera.ZoomToLevel();
00017 Level.Background.Color = Color.Black;
00018
00019 PhysicsObject p1 = new PhysicsObject(2 * 100.0, 2 * 100.0, Shape.Circle);
00020 p1.Y = Level.Bottom + 200.0;
00021 Add(p1);
00022
00023 PhysicsObject p2 = new PhysicsObject(2 * 50.0, 2 * 50.0, Shape.Circle);
00024 p2.Y = p1.Y + 100 + 50;
00025 Add(p2);
00026
00027 PhysicsObject p3 = new PhysicsObject(2 * 30.0, 2 * 30.0, Shape.Circle);
00028 p3.Y = p2.Y + 50 + 30;
00029 Add(p3);
00030
00031 PhysicsObject silmaVasen = new PhysicsObject(2 * 5.0, 2 * 5.0, Shape.Circle);
00032 silmaVasen.X = p3.X - 30.0 / 2;
00033 silmaVasen.Y = p3.Y;
00034 p3.Add(silmaVasen);
00035
00036 PhysicsObject silmaOikea = new PhysicsObject(2 * 5.0, 2 * 5.0, Shape.Circle);
00037 silmaOikea.X = p3.X + 30.0 / 2;
00038 silmaOikea.Y = p3.Y;
00039 p3.Add(silmaOikea);
00040
00041 silmaVasen.Color = Color.Black;
00042 silmaOikea.Color = Color.Black;
00043
00044 PhysicsObject nena = new PhysicsObject(2 * 3.0, 2 * 3.0, Shape.Circle);
00045 nena.Y = p3.Y - 30.0 / 3;
00046 p3.Add(nena);
00047 nena.Color = Color.Black;
00048
00049 PhysicsObject suu = new PhysicsObject(2 * 7.0, 2 * 3.0, Shape.Circle);
00050 suu.Y = p3.Y - 30.0 * 2 / 3;
00051 p3.Add(suu);
00052 suu.Color = Color.Black;
00053 }
00054 }
B2. Pähkinä
6
2 - 63 = 1
eli
64 - 63 = 1
G1-1. Luvut joiden neliöjuuri kokonaisluku
Neliojuuri.cs
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 public class Neliojuuri
00036 {
00037
00038
00039
00040
00041
00042 public static void Main(string[] args)
00043 {
00044 for (int i = 0; i * i <= 1000; i++)
00045 System.Console.WriteLine("sqrt(" + i * i + ") = " + i);
00046 }
00047 }