いろんなプログラミング言語学習サイトを超速でこなしていたら、各言語の書き方が混ざってきたのでまとめます。
とりあえず超基本だけ。。。
ここで書かれているのはプログラミング超初心者の筆者が調べてまとめたものです
間違い等あったらご指摘頂けると嬉しいです。
ここで扱うプログラミング言語一覧
- C
- Python3
- Java
- JavaScript
- Ruby
- C#
各言語に対して明確に仕様の違いを記述することは困難なので、あくまで参考程度に。
出力
hello_world.c
printf("Hello World");
printf("%d",num); //整数出力
printf("%f",data); //実数出力
printf("%c",mozi); //文字出力
printf("%s",str); //文字列出力
hello_world.py
print("Hello World")
print(num)
print(str)
#シングルクォーテーション「'」と、ダブルクォーテーション「"」のどちらでも可 (面倒だから僕は「"」)
hello_world.java
System.out.println("Hello World"); //printlnの後ろから2文字目はl(エル)
hello_world.js
console.log("Hello World");
console.log(num);
console.log(str);
hello_world.rb
puts "Hello World"
puts "#{str}" #「#{変数}」で値を置き換えられる
hello_world.cs
Console.WriteLine("Hello World"); //改行付き
Console.Write("Hello World"); //改行なし
Console.WriteLine("{0}",num);
コメント
comment.c_&_comment.java_&_comment.js_&_comment.cs
//コメントだよ
comment.py_&_comment.rb
#コメントだよ
変数宣言(初期化)
box.c
int num = 0;
char mozi = 'a'; //一文字のみ
char str[] = "abc"; //文字列
box.py_&_box.rb
num = 0 #数値
str = "abc" #文字列
box.java
int num = 0; //整数
double num = 3.14; //実数
String str = "abc"; //文字列
boolean bool = true; //真偽値
box.js
var num = 0;
var str = "abc";
box.cs
int num = 0;
string str = "abc";
bool a = true;
var x = 10;
var y = "abc";
##文字列の連結
```strlink.c
#include <string.h>
char str1[80] = "DRAGON";
char str2[] = "QUEST";
strcat(str1,str2);
printf("%s",str1);
追記:
連結する分、配列の要素を余分に作っておく必要があります。(ex:str1[80])
shiracamus様、ご指摘ありがとうございます!
strlink.py
str1 = "DRAGON"
str2 = "QUEST"
print(str1 + str2)
strlink.java
String str1 = "DRAGON";
String str2 = "QUEST";
System.out.println(str1 + str2);
strlink.js
var str1 = "DRAGON";
var str2 = "QUEST";
console.log(str1 + str2);
strlink.rb
str1 = "DRAGON"
str2 = "QUEST"
puts str1 + str2
型変換(キャスト)
convert.c
itoa(num,str,10); //10進 //数値(int num)から文字列(str)
atoi(str); //文字列から数値
convert.py
str(num) #数値型から文字列型
int(str) #文字列型から数値型
Javaは自動型変換と手動型変換があるよ
convert.java
System.out.println(month + "月" + date + "日"); //int型のmonth変数とdate変数は自動的にString型に変換される
cast.java
int num = 13;
System.out.println((double)num); //手動型変換 出力結果は「13.0」となる
convert.rb
puts 1.to_s + "月" + 1.to_s + "日" #to_sで数値を文字列に変換
num = "13" #ダブルクォーテーションで囲ってるのでこれは文字列オブジェクト
puts num.to_i #to_iで文字列を数値に変換
convert.cs
int num = int.Parse(str);
string str = (string)123;
if文
if.c_&_if.java_&_if.js
if(num == 100 && score > 1000){
//処理文
}
else if(num < 100 || score != 1000){
//処理文
}
else{
//処理文
}
if.py
if num == 100 and score > 1000:
#処理文
elif num > 100 or score != 1000:
#処理文
else:
#処理文
if.rb
if num == 100 && score > 1000
#処理文
elsif num > 100 || score != 1000
#処理文
else
#処理文
end
#(処理文 if 条件式)でも可
switch文
switch.c_&_switch.java
switch(num){
case 0:
//処理文
break;
case 1:
//処理文
break;
default:
//処理文
break;
}
case文
case.rb
case x
when 1
#処理文
when 2
#処理文
else
#処理文
end
入力
input.c
scanf("%d",&num); //整数入力
scanf("%lf",&data); //実数入力
scanf("%c",mozi); //文字入力
scanf("%s",str); //文字列入力
input.py
count = input() #変数の型関係なし
input.java
import java.util.Scanner; //外部ライブラリ呼び出し
class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in); //Scannerの初期化
String name = scanner.next(); //コンソールからの入力待ち
}
}
input.rb
count = gets #getsで受け取った入力は文字列になっている
配列&リスト
array.c
int a[] = {1, 2, 3, 4};
int a[3][4];
画像はhttp://www.cc.kyoto-su.ac.jp/~yamada/programming/array.html から
array.java
String names[] = {"apple","banana"};
array.js
var names = ["apple","banana"];
console.log(names.length); //配列の要素の個数を取得
array.rb
names = ["apple","banana"]
puts names.size #配列の要素の個数を取得
names.push("grape") #配列の末尾に要素を追加
list.py
lists = ["apple","banana",200,300]
lists.append("pizza") #リスト末尾に要素追加
辞書(dict) (連想配列) (ハッシュ)
dict.py_&_dict.js
fruits = {"apple": 200, "banana": 300} //変数 = {"キー": 値,"キー": 値}
hash.rb
fruits = {"apple" => "200", "banana" => "300"} #変数 = "キー" => "値"}
puts fruits["apple"] #値の取り出し
fruits["grape"] = "400" #データの追加
シンボル
Rubyにある機能。コード上では文字列に見えるが比較や検索、参照などで文字列オブジェクトよりも速度が早い。ハッシュのキーによく使用。
symbol.rb
fruits = {:apple => "200", :banana => "300"}
puts fruits[:apple]
each文
配列またはハッシュに対して、先頭のデータから順に繰り返し処理を行うメソッド
each.rb
fruits.each do |key,value| #配列.each do |変数|
puts "#{key}:#{value}"
end
for文
for.c_&_for.js
for(int i = 0; i < 10; i++){
//処理文
}
for.java
for(int i = 0; i < 10; i++){
System.out.println(i);
}
String names = {"apple","banana"};
for(int i = 0; i < names.length; i++){ //lengthメソッドで配列の要素数を数えてくれる
System.out.println("eat" + names[i]);
}
exfor.java
String names = {"apple","banana"};
for(String name: names){ //変数nameに配列namesの要素自体が代入されている
System.out.println(name);
}
for.py
for i in range(0,10):
print(i)
for.rb
for i in 0..9 do #(doは省略可)
puts i
end
while文
while.c_&_while.java_&_while.js
int x = 1;
while(x <= 100){
//処理文
x++;
}
while.py
x = 1
while x <= 100:
#処理文
x += 1
while.rb
i = 1
while i <= 100 do #doは省略可
#処理文
i += 1
end
メソッド(関数)
func.c
#include <stdio.h>
int number(int num){
printf("数字は%d\nだよ",num);
num += 1;
return num;
}
int main()
{
int data = number(1);
printf("%d\n",data);
return 0;
}
func.py
#変数nameのスコープここから(変数が使える範囲)
def hello(name):
print("hello" + name)
name = "hello"
return name
#スコープここまで
hi = hello("world")
print(hi)
func.java
class Main{
public static void main(String[] args){
String hi;
hi = hello("world");
System.out.println(hi);
}
public static String hello(String name){
System.out.println("Hello" + name);
name = "hello";
return name;
}
}
func.js
function number(num){
console.log(num);
num += 1;
return num;
}
var data = number(1);
console.log(data);
func.rb
def number(num)
puts num
num += 1
return num
end
data = number(1)
puts(data)
Rubyでは、オブジェクトの種類によって固有のメソッドを持つ (↓はその一例)
func1.rb
puts "Hello".length #文字列オブジェクトの文字数を出力
puts 3.14.round #四捨五入した数値を出力
puts "Hello".reverse #文字を反転させて出力
ライブラリ(モジュール)(ヘッダファイル)
module.c
#include <~.h>
module.py_&_module.java
import ~
気づいたこと・注意点
- Python2とPython3では結構仕様が違うらしい
- Pythonではシングルクォーテーションでもダブルクォーテーションでもどちらでも使える(と思う)
- Python,Rubyでは「;」はいらない
- 他の言語を触ってみたらC言語が使いづらい()
- C,Python,Rubyの変数宣言はスネークケースが慣習(int user_number;)
- Javaの変数宣言はキャメルケースが慣習(ex: String userName;)
- プログラミング言語によっては、整数同士の割り算の結果を整数で返すか、実数で返すかが異なる
- C,Javaの論理演算子は「&&」「||」
- Pythonの論理演算子は「and」「or」
- Pythonでのelse ifは「elif」と表記する
- Pythonにswitch文はありません!
- Python,Rubyにはインクリメント・デクリメントはありません!
- 同じメソッド名で複数のメソッドを定義することをオーバーロードという。 (chitoku様、ご指摘ありがとうございます!)
- Rubyでは文字列や数値などの全ての値が「オブジェクト」となり、それぞれのオブジェクトは「データ」と「メソッド」を持つ
オーバーロード出来る時:
引数の数が異なる場合、引数の型が異なる場合、引数の順番が異なる場合
オーバーロードできないとき:
引数の名前だけが異なる場合、返り値のみが異なる場合
どんどん追記していきます!