伝えたいこと日記

WEB開発/読書/バイク/ポーカー 皆でやると楽しいよ?

第3章 オブジェクト、型、値

3.1 入力

何かを読み取るためには、それを読み込む場所が必要である。そのような【場所】をオブジェトという。

オブジェクトは配置できる情報の種類を指定する型を持つメモリ領域である

名前付きのオブジェクトは変数と呼ばれる

 #include "../../std_lib_facilities.h"
#include <string>
int main() {
 cout << "Please enter your first name (followed by 'Enter'):\n";
 string first_name;
 cin >> first_name;
 cout << "Hello," << first_name << "!\n";
 return 0;
}

改行によってコンピュータに注意をひかせることができるのである

 

3.2 変数

データを格納する【場所】はオブジェクトと呼ばれる

オブジェクトにアクセスするためには名前が必要である

名前付きのオブジェクトは変数と呼ばれ、①オブジェクトに配置するものと②適用できる操作を決定する型を持つ

C++には多くの型があるが実務上は5つの型に絞っても特に問題は発生しないだろうとのこと

int 整数

double 浮動小数

char 個々の文字

string 文字列

bool 論理変数

3.3 入力と型

string型に数字を入力した場合は自動的に文字列に変換される

 ホワイトスペースによって入力されたものが区別されるようになる

1つの出力文で複数の値を出力できるのと同様に1つの入力文で複数の文が入力できるのである

#include "Header.h"
int main() {
 
 while (1)
 {
  cout << "Please enter your first name and age:\n";
  string first_name="???";
  int age=0;
  cin >> first_name;
  cin >> age;
  cout << "Hello," << first_name << "(age" << age << ")\n";
  keep_window_open();
 }
 
}

 

3.4 演算と演算子

変数の型は変数に格納する値をを指定するだけでなく、変数に適用できる演算とそれらの意味も決定する

int count;

cin >> count;

int型によって演算子は整数にcountを読み込む

 

string name;

cin >> name;

>>演算子は文字列nameに読み込む

 

int c2 = count +2;

+演算子は整数を足す

string s2 name +"Jr";

+演算子は文字を足す

などなど

コンパイラは各変数に適用できる演算を正確に知っているので多くのミスを防ぐことができる

文字列の比較でアルファベット順での並び替えが可能

3.5 代入と初期化

代入と初期化は論理的に異なる

 初期化は常に変数をからであると見なす。

 代入は変数に新しい値を挿入する前にその古い値を削除しなければならない

値はコイン/箱が変数=>箱を空っぽにしておくことはできない

#include "Header.h"
int main(){
 while (1)
 {
  cout << "Please enter a floating value\n";
  double n;
  cin >> n;
  cout << "n==" << n
   << "\nn+1==" << n + 1
   << "\nthree times n==" << n * 3
   << "\n twice n==" << n * 2
   << "\n n squared==" << n*n
   << "\n half of n==" << n / 2
   << "\n square root of n==" << sqrt(n)
   << "\nn==" << n
   << "\nn==" << n
         <<endl;
 };
 
}

3.5.1 例:重複する単語の検出

ドリル

二重引用符は単純引用符と違う意味を持つ 

 

#include "Header.h"
#include<iostream>
using namespace std;
 
void main()
{
  string first_name = "";
  cout << "Enter the name of the person you want to write to\n";
  cin >> first_name;
  cout << "Dear " << first_name << "\n";
  string friend_name;
  cout << "Please enter your friend name!\n";
  cin >> friend_name;
  cout << "Have you seen " << friend_name << " lately.\n";
  char friend_gender = 0;
  cout <<"Please enter gender of your friend\n";
  cin >> friend_gender ;
  if (friend_gender == 'm')
   cout << "If you see " << friend_name << " please ask him to call me \n";
  if (friend_gender == 'f')
   cout << "If you see " << friend_name << " please ask her to call me \n";
  int age = 0;
  cout << "Please enter your age\n";
  cin >> age;
  cout << "I hear you just had a birthday and you are " << age << " years old\n";
  if (age < 0 || age>110)
   simple_error("you are kidding!");
  if (age < 12)
   cout << "Next year you will be " << age + 1;
  if (age == 17)
   cout << "Next you will be able to vote\n";
  if (age > 70)
   cout << "I hope you are enjoying retirement";
 
  keep_window_open();
 復習
Q1
ユーザーに行動を促すメッセージのこと
Q2
>>
Q3
int number;
cin >> number;
Q4
改行を表す特殊シーケンス
Q5
;
Q6
;
Q7
cout <<"Hello" << first_name <<"!\n";
Q8
値を入れるための【場所】。確保されたメモリ
Q9
表記法
Q10
文字列、浮動小数点、整数、文字
Q11
名前と型のついたオブジェクトであり、格納される値と演算子の役割を定義する
Q12
char 1バイト int 4バイト double ??バイト
Q13
ビット
Q14
=は代入演算子 ==は論理演算子
Q15
変数を定義する文
Q16
初期化は空のメモリに対して値を代入するのに対して、代入ではすでに存在している値を削除してから新しい値を代入するという2ステップを踏むことになる
Q17
文字列同士をつなげること。string型の変数に対して++演算子を利用することで文字列の連結を行うことができる。
Q18
_this_is 先頭がアンダーバー
correct? クエスチョンマークが使用されている
latest thing スペースが使用されている
The_$12 ドルマークが使用されている
Q19
大文字と小文字であることだけが異なる変数
標準ライブラリの機能の名前
意味のない名前
流すぎる名前
全て大文字の名前
Q20
人がプログラムする際に内容が理解しやすい名前にする
Q21
最初に定義した型に従ってオブジェクトが運用されること
Q22
doubleよりもintのほうが使用するビット数が少ないために情報がいくつか失われることになるから
Q23
値が別の型に移動するときに元の値と異なるものになる可能性がないもの
縮小変換は多くの情報が失われてしまうために非常に危険である

#include "Header.h"
#include<iostream>
using namespace std;

void main()
{
  string first_name = "";
  cout << "Enter the name of the person you want to write to\n";
  cin >> first_name;
  cout << "Dear " << first_name << "\n";
  string friend_name;
  cout << "Please enter your friend name!\n";
  cin >> friend_name;
  cout << "Have you seen " << friend_name << " lately.\n";
  char friend_gender = 0;
  cout <<"Please enter gender of your friend\n";
  cin >> friend_gender ;
  if (friend_gender == 'm')
   cout << "If you see " << friend_name << " please ask him to call me \n";
  if (friend_gender == 'f')
   cout << "If you see " << friend_name << " please ask her to call me \n";
  int age = 0;
  cout << "Please enter your age\n";
  cin >> age;
  cout << "I hear you just had a birthday and you are " << age << " years old\n";
  if (age < 0 || age>110)
   simple_error("you are kidding!");
  if (age < 12)
   cout << "Next year you will be " << age + 1;
  if (age == 17)
   cout << "Next you will be able to vote\n";
  if (age > 70)
   cout << "I hope you are enjoying retirement";

  keep_window_open();

#include "Header.h"
void main()
{
 double d = 0;
 while (cin >> d) {
  int i = d;
  char c = i;
  int i2 = c;
  cout << "d==" << d <<"\n";
  cout << "i==" << i << "\n";
  cout << "c==" << c << "\n";
  cout << "i2=" << i2 << "\n";
  cout << "char(" << c << ")\n";
 }
}

用語

代入

cin

連結

変換

宣言

デクリメント

定義

インクリメント

初期化

名前

初期化

名前

縮小

オブジェクト

演算

演算子

型安全

変数

 

練習問題

Q1 完了しました!

Q2#include "Header.h"
void main()
{
 cout << "これはマイルとキロメートルを変換するプログラムです。マイル数を入力してください!\n";
  int mile = 0;
  cin >> mile;
  cout << mile << "は " << 1.6*mile << " kmです!";
  keep_window_open();
}

Q3

やりました

Q4

#include "Header.h"
void main()
{
 cout << "これは入力した2つの値の最小値、最大値、合計、差、比率を求め表示するプログラムです。\n";
 cout << "スペースを空けて2つの数字を入力してください\n";
 int val1 = 0;
 int val2 = 0;
 cin >> val1 >> val2;
 int small = 0;
 int large = 0;
 if (val1 > val2)
  large = val1;
     small = val2;
 if (val2 > val1)
  large = val2;
     small = val1;
 cout << "2つの数字の最小値は " << small << " です\n";
 cout << "2つの数字の最大値は " << large << " です\n";
 cout << "2つの数字の合計は " << val1+val2 << " です\n";
 cout << "2つの数字の差は " << large-small<< " です\n";
 cout << "2つの数字の比率は " <<small/small <<"対" <<large/small << " です\n";
 keep_window_open();
}

Q5

#include "Header.h"
void main()
{
 cout << "これは入力した2つの値の最小値、最大値、合計、差、比率を求め表示するプログラムです。\n";
 cout << "スペースを空けて2つの数字を入力してください\n";
 double val1 = 0;
 double val2 = 0;
 cin >> val1 >> val2;
 double small = 0;
 double large = 0;
 if (val1 > val2)
  large = val1;
     small = val2;
 if (val2 > val1)
  large = val2;
     small = val1;
 cout << "2つの数字の最小値は " << small << " です\n";
 cout << "2つの数字の最大値は " << large << " です\n";
 cout << "2つの数字の合計は " << val1+val2 << " です\n";
 cout << "2つの数字の差は " << large-small<< " です\n";
 cout << "2つの数字の比率は " <<small/small <<"対" <<large/small << " です\n";
 keep_window_open();
}

Q6

#include "Header.h"
#include "string"
void main()
{
 int one = 0;
 int two = 0;
 int three = 0;
 cin >> one >> two >> three;
 int first;
 int second;
 int third;
 if (one>two && one>three)
  third = one;
   if (two > three)
    second = two;
    first = three;
   if (three > two)
    second = three;
    first = two;
      cout << "oneが1番大きいよ!\n";
 if (two>one && two>three)
   third = two;
    if (one > three)
     second = one;
     first = three;
    if (three > one)
     second = three;
     first = one;
      cout << "twoが1番大きいよ!\n";
 if (three>one && three>two)
   third = three;
   cout << "threeが1番大きいよ!\n";
    if (two > one)
     second = two;
     first = one;
    if (one > two)
     second = one;
     first = two;
      cout << "threeが1番大きいよ!\n";
 cout << first << "," << second << "," << third <<"\n";
 keep_window_open();

 

#include "Header.h"
#include "string"
void main()
{
 int one = 0;
 int two = 0;
 int three = 0;
 cin >> one >> two >> three;
 double smallest=0;
 double middle=0;
 double largest=0;
 if (one <= two && one <= three) {
  smallest = one;
  if (two < three) {
   middle = two;
   largest = three;
  }
  else {
   middle = three;
   largest = two;
  }
 }
 else if (two <= one && two <= three) {
  smallest = two;
  if (one > three) {
   middle = one;
   largest = three;
  }
  else {
   middle = three;
   largest = one;
  }
 }
 else {
  smallest = three;
  if (one > two) {
   middle = one;
   largest = two;
  }
  else {
   middle = two;
   largest = one;
  }
 }

 cout << smallest << "," << middle << "," << largest << "\n";
 keep_window_open();
}

Q8

#include "Header.h"
#include "string"
void main()
{
 int val = 0;
 cout << "こちらに数字を入力してください!\n";
 cin >> val;
 if (val % 2 == 0) {
  cout << val << " は偶数です。\n";
 }
 else {
  cout << val << " は奇数です。\n";
 }
 keep_window_open();
}

Q9

#include "Header.h"
#include "string"
void main()
{
 cout << "Please Enter strings of number\n";
 string val;
 cin >> val;
 if (val == "zero") {
  cout << 0 <<"\n";
  }
 else if(val=="one") {
  cout << 1 << "\n";
 }
 else if (val == "two") {
  cout << 2 << "\n";
 }
 else if (val == "three") {
  cout << 3 << "\n";
 }
 else if (val == "four") {
  cout << 4 << "\n";
 }
 else {
  cout << "not a number I know";
 }
 keep_window_open();
}

Q10

#include "Header.h"
#include "string"
void main()
{
 string operand;
 int val1 = 0;
 int val2 = 0;
 cout << "Please enter operand and two numbers!\n";
 cin >> operand >> val1 >> val2;
 if (operand == "+") {
  cout << "You want to caluclate " << val1 << "+" << val2 <<"\n"
 }
 else if (operand == "-") {
  cout << "You want to caluclate " << val1 << "-" << val2 << "\n";
 }
 else if (operand == "*") {
  cout << "You want to caluclate " << val1 << "*" << val2 << "\n";
 }
 else if (operand == "/") {
  cout << "You want to caluclate " << val1 << "/" << val2 << "\n";
 }
 else if (operand == "plus") {
  cout << "You want to caluclate " << val1 << "+" << val2 << "\n";
 }
 else if (operand == "minus") {
  cout << "You want to caluclate " << val1 << "-" << val2 << "\n";
 }
 else if (operand == "mul") {
  cout << "You want to caluclate " << val1 << "*" << val2 << "\n";

 }
 else if (operand == "div") {
  cout << "You want to caluclate " << val1 << "/" << val2 << "\n";
 }
 keep_window_open();
}

Q11

だるいから飛ばすわ。悪い。