0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C, C++言語 基本的なコード

Posted at

Ubuntuでコンパイラをインストール

sudo apt install gcc
sudo apt install g++

C言語のコンパイル

gccコマンドを使って、コンパイルするソース名を指定する。
-oの後ろにはコンパイル後のオブジェクトファイル名を指定する。
-oで指定しなければファイル名はa.outで出力される。

gcc Hello_World.cpp -o Hello_World
./Hello_World

C++言語のコンパイル

基本的にはgccがg++になっただけである。

g++ Hello_World.cpp -o Hello_World
./Hello_World

まずは初めのコード

#include <stdio.h>

int main() {
    printf("Hello,World\n");
}

実行結果

Hello,World

クラスメソッド

#pragma once

#include <stdio.h>
#include <iostream>

class Car{
    public:
        std::string tire = "tire";
};
#include <iostream>
#include "Class_Method.h"

int main() {
    Car car;
    printf("Hello,World\n");
    std::cout << car.tire << std::endl;
}

実行結果

Hello,World
tire

構造体

#pragma once

#include <stdio.h>
#include <iostream>

struct person_info {
    std::string name;
    int age;
    int weight;
    int height;
    std::string job;
};

class Person{
    public:
        person_info p1_info;
        void info_input();
};
#include "Struct.h"

void Person::info_input() {
    p1_info.name = "ando";
    p1_info.age = 30;
    p1_info.weight = 68;
    p1_info.height = 175;
    p1_info.job = "engineer";
}

int main() {
    Person person;
    person.info_input();

    std::cout << person.p1_info.name << std::endl;
    std::cout << person.p1_info.age << std::endl;
    std::cout << person.p1_info.weight << std::endl;
    std::cout << person.p1_info.height << std::endl;
    std::cout << person.p1_info.job << std::endl;
}

実行結果

ando
30
68
175
engineer

ラムダ式

2体のキャラクターが戦うようなシステムでラムダ式を使う。
同一の処理があるので、それをmain関数内でラムダ式でまとめた。

#pragma once

#include <stdio.h>
#include <iostream>

struct Monster{
    std::string name;
    int hp;
    int attack;
    int defence;
    int speed;
};

class Pika{
    public:
        Monster monster;
        void input_status();
};

class Nyasu{
    public:
        Monster monster;
        void input_status();
};
#include "Lambda.h"

void Pika::input_status() {
    monster.name = "pikachu";
    monster.hp = 35;
    monster.attack = 55;
    monster.defence = 40;
    monster.speed = 90;
}

void Nyasu::input_status() {
    monster.name = "nyaasu";
    monster.hp = 40;
    monster.attack = 45;
    monster.defence = 35;
    monster.speed = 90;
}

int main () {
    Pika monst1;
    Nyasu monst2;

    monst1.input_status();
    monst2.input_status();

    auto battle = [](Monster *mon1, Monster *mon2) {
        int damage = 0;

        damage = mon1->attack - mon2->defence;
        if (damage < 0) {
            damage = 1;
        }
        std::cout << "damage: " << damage << std::endl;

        mon2->hp = mon2->hp - damage;
        if (mon2->hp < 0 ) {
            mon2->hp = 0;
        }
        std::cout << "残りHP: " << mon2->hp << std::endl;
    };

    std::cout << "start" << std::endl;
    while (monst1.monster.hp != 0 && monst2.monster.hp !=0) {
        if (monst1.monster.speed >= monst2.monster.speed) {
            std::cout << "a pattern" << std::endl;
            battle(&monst1.monster, &monst2.monster);
        } else {
            std::cout << "b pattern" << std::endl;
            battle(&monst2.monster, &monst1.monster);
        }
    }

    std::cout << "end" << std::endl;

}

実行結果

start
a pattern
damage: 20
残りHP: 20
a pattern
damage: 20
残りHP: 0
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?