2012年1月8日日曜日

[C/C++] 新版・明解C++入門編 3-2 「while文」

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

List3-4
#include <iostream>

using namespace std;

int main()
{
  int x;

  cout << "Countdown!! \n";
  do {
    cout << "Please input a positive integer value.";
    cin >> x;
  } while(x <= 0);
  while (x >= 0){
    cout << x << "\n";
    x--;
  }

}
コンパイル,実行例:
$ g++ List3-4.cpp
$ ./a.out
Countdown!! 
Please input a positive integer value.10
10
9
8
7
6
5
4
3
2
1
0

演習3-4

#include <iostream>



using namespace std;


int main()
{
  int x;

  cout << "Countdown!! \n";
  do {
    cout << "Please input a positive integer value.";
    cin >> x;
  } while(x <= 0);
  while (x >= 0){
    cout << x << "\n";
    x--;
  }
  cout << "The value of x at the end of the while statement is " << x << ". \n"
}
コンパイル,実行例:

$ g++ P3-4.cpp
$ ./a.out

Countdown!! 

Please input a positive integer value.5
5
4
3
2
1
0
The value of x at the end of the while statement is -1. 

List3-5

#include <iostream>



using namespace std;


int main()
{
  int x;

  cout << "Countdown !! \n";
  do {
    cout << "Input positive integer: ";
    cin >> x;
  } while(x <= 0);

  while (x >= 0)
    cout << x-- << "\n";
}
コンパイル,実行例:

$ g++ List3-5.cpp
$ ./a.out

Countdown !! 

Input positive integer: -3
Input positive integer: 3
3
2
1
0

List3-6

#include <iostream>



using namespace std;


int main()
{
  int n;
  cout << "How many * will you display?";
  cin >> n;

  int i = 0;
  while (i < n){
    cout << '*';
    i++;
  }
  cout << '\n'
}
コンパイル,実行例:
$ g++ List3-6.cpp
$ ./a.out

How many * will you display?3

***

List3-7

#include <iostream>



using namespace std;


int main()
{
  int x = 0;

  ++x = 5;
  cout << "The value of x is " << x << ". \n";

  x++ = 10;
  cout << "The value of x is " << x << ". \n"
}
コンパイル,実行例:
$ g++ List3-7.cpp
List3-7.cpp:12:7: error: expression is not assignable
  x++ = 10;
  ~~~ ^
1 error generated.
 $ 

演習3-5

#include <iostream>

using namespace std;



int main()
{
    int n;
    cout << "How many * will you display? ";
    cin >> n;

    int i = 0;
    while (i < n) {
        cout << '*';
        i++;
    }
    if (n > 0)
        cout << '\n'
}
コンパイル,実行例:
$ g++ P3-5.cpp

$ ./a.out

How many * will you display? -3

$ ./a.out
How many * will you display? 8
********

演習3-6

#include <iostream>



using namespace std;


int main()
{
    int n;
    cout << "Input integer: ";
    cin >> n;

    int i = 0;
    while (i < n) {
        if (i % 2)
            cout << '-';
        else
            cout << '+';
        i++;
    }
    if (n > 0)
        cout << '\n';
}
コンパイル,実行例:
$ g++ P3-6.cpp

$ ./a.out

Input integer: -3

$ ./a.out
Input integer: 5
+-+-+

List3-8
#include <iostream>

using namespace std;

int main()
{
  int x;

  cout << "Display positive integer values in reverse order. \n";
  do {
    cout << "Please input a positive integer value: ";
    cin >> x;
  } while (x <= 0);

  cout << "Display in reverse order is ";
  while (x > 0){
    cout << x % 10;
    x /= 10;
  }
  cout << ". \n"

}
コンパイル,実行例:
$ g++ List3-8.cpp
$ ./a.out
Display positive integer values in reverse order. 
Please input a positive integer value: -3
Please input a positive integer value: 0
Please input a positive integer value: 1234567890
Display in reverse order is 0987654321. 



List3-9
 #include <iostream>

using namespace std;

int main()
{
  int n;
  cout << "Find the sum of 1 to n. \n";
  do {
    cout << "Input n: ";
    cin >> n;
  } while (n <= 0);

  int sum = 0;
  int i = 1;
  while (i <= n) {
    sum += i;
    i++;
  }
  cout << "The sum from 1 to " << n << " is " << sum << ". \n";
}
コンパイル,実行例:
$ g++ List3-9.cpp
$ ./a.out
Find the sum of 1 to n. 
Input n: 100
The sum from 1 to 100 is 5050. 

0 件のコメント :

コメントを投稿