2012年1月2日月曜日

[C/C++] 新版 明解C++入門編 1-3 「変数」

書籍「新・明解C++入門編 (明解シリーズ)」の中に収められているプログラム等を実際にコンパイル,実行してみた際のメモです.

List 1-6
#include <iostream>

using namespace std;

int main()
{
  cout << "The sum of 18 and 63 is " << 18+ 63 << ". \n"

}
コンパイル,実行例:
$ g++ List1-6.cpp
$ ./a.out
The sum of 18 and 63 is 81. 

List 1-7

#include <iostream>



using namespace std;

int main()
{
  int x;
  int y;

  x = 63;
  y = 18;

  cout << "The value of x is " << x << " . \n";
  cout << "The value of y is " << y << " . \n";
  cout << "The total is " << x + y << ". \n";
  cout << "The average is " << ( x + y ) / 2 <<". \n";
}
コンパイル,実行例:
$ g++ List1-7.cpp
$ ./a.out
The value of x is 63 . 
The value of y is 18 . 
The total is 81. 
The average is 40. 

演習 1-6
#include <iostream>

using namespace std;

int main()
{
  int x;
  int y;

  x = 1;
  y = 2

  cout << "The value of x is " << x << ". \n";
  cout << "The value of y is " << y << ". \n";
  cout << "The total is " << x + y << ". \n";
  cout << "The average is " << (x + y) / 2 << ". \n";
}
コンパイル,実行例:
$ g++ P1-6.cpp
$ ./a.out
The value of x is 1. 
The value of y is 2. 
The total is 3. 
The average is 1. 

演習 1-7
#include <iostream>

using namespace std;

int main()
{
  int x;
  int y;
  int z; 

  x = 1;
  y = 2;
  z = 3;

  cout << "The total is " << x + y + z<< ". \n";
  cout << "The average is " << (x + y + z) / 3 << ". \n";
}
コンパイル,実行例:
$ g++ P1-7.cpp
$ ./a.out
The total is 6. 
The average is 2. 

List 1-8

#include <iostream>



using namespace std;

int main()
{
  int x;
  int y;

  cout << "The value of x is " << x << ". \n";
  cout << "The value of y is " << y << ". \n";
  cout << "The total is " << x + y << ". \n";
  cout << "The average is " << (x + y) / 2 << ". \n";
}
コンパイル,実行例:

$ g++ List1-8.cpp

$ ./a.out

The value of x is 1. 
The value of y is 519999542. 
The total is 519999543. 
The average is 259999771. 

List 1-9

#include <iostream>



using namespace std;

int main()
{
  int x = 63;
  int y = 18;

  cout << "The value of x is " << x << ". \n";
  cout << "The value of y is " << y << ". \n";
  cout << "The total is " << x + y << ". \n";
  cout << "The average is " << (x + y) / 2 << ". \n";
}
コンパイル,実行例:
$ g++ List1-9.cpp
$ ./a.out
The value of x is 63. 
The value of y is 18. 
The total is 81. 
The average is 40. 

0 件のコメント :

コメントを投稿