1日目 問題
- 二つの文字列”apple”と”orange”を一行でスペース区切りで出力してみましょう
実行結果
apple orange
- 二つの数値11と1.5を出力してみましょう
実行結果
11 11.5
- 一つの数値を標準入力より読み込み、読み込んだ数値を出力してみましょう
※標準入力はstd::cinを使用します
実行結果
Please enter a number:5
The value entered is 5
- 二つの数値を半角スペース区切りで読み込み、読み込んだ数値を出力してみましょう
実行結果
Please enter two integers separated by a space:4 10
The values entered are 4 and 10
- 二つの文字列を標準入力より、読み込み、読み込んだ文字列をそれぞれ出力してみましょう
実行結果
Please enter two strings separated by a space:red blue
The values entered are red and blue
文字列はchar型の配列の他にstd::stringという型も存在します
リファレンス
2日目 問題
- 二つの整数を標準入力から受け取り、四則演算の結果を出力してみましょう
実行結果
Please enter two integers separated by a space:24 12
24 + 12=36
24 - 12=12
24 * 12=288
24 / 12=2
24 % 12=0
- 下記のように数値を標準入力から受け取り、ビット演算(左)、(右)出力してみましょう
実行結果
Please enter two integers separated by a space:3 1
3 >> 1 = 1
3 << 1 = 6
- 標準入力より、半径の値を取得し、円の面積を計算してみましょう
実行結果
Please enter radius:5
半径5の円の面積=78.5
- 標準入力より、直角三角形の底辺と高さを取得し、斜辺の長さを出力してみましょう
※sqrtを使用します
実行結果
Enter the length of the base:1
Please enter the height length:1
斜辺の長さ=1.41421
- 1年、2年、5年の秒数を計算してみましょう(ただし、うるう年は考えす、一年は365日としてください)
実行結果
1年の秒数=31536000
2年の秒数=63072000
5年の秒数=157680000
3日目 問題
- 標準入力から2つの整数を受け取って、どちらが大きいかを判定し、大きい方の数を標準出力に出力してみましょう
実行結果
Please enter two integers separated by a space:100 20
100
- 1を10回繰り返してみましょう
実行結果
Please enter two integers separated by a space:0 0
0
0
Please enter two integers separated by a space:-1 90
90
90
- 標準入力より1から100までの数値を1つ取得し、以下の処理を実行してみましょう
(1) 80点以上なら”優”と表示
(2) 80点未満、70点以上なら、”良”と表示
(3) 70点未満、60点以上なら、”可”と表示
(4) 60点未満なら、”不可”と表示
※すべてのif文を通過するようにテストをしましょう
実行結果
admin:~/work$ ./a.out
Please enter integers(0から100まで):100
優
admin:~/work$ ./a.out
Please enter integers(0から100まで):80
優
admin:~/work$ ./a.out
Please enter integers(0から100まで):70
良
admin:~/work$ ./a.out
Please enter integers(0から100まで):79
良
admin:~/work$ ./a.out
Please enter integers(0から100まで):69
可
admin:~/work$ ./a.out
Please enter integers(0から100まで):60
可
admin:~/work$ ./a.out
Please enter integers(0から100まで):59
不可
admin:~/work$ ./a.out
Please enter integers(0から100まで):0
不可
- 標準入力から2つの整数と1つの演算子(+, -, *, / のいずれか)を受け取り、その演算結果を標準出力に出力してみましょう
※割り算の場合は、整数値で商のみを出力する
※0は入力しないものとする
実行結果
Enter two integers separated by a space:10 20
Please enter one of the operators (+,-,*,/):+
10+20=30
admin:~/work$ ./a.out
Enter two integers separated by a space:10 20
Please enter one of the operators (+,-,*,/):-
10-20=-10
admin:~/work$ ./a.out
Enter two integers separated by a space:10 20
Please enter one of the operators (+,-,*,/):*
10*20=200
admin:~/work$ ./a.out
Enter two integers separated by a space:20 10
Please enter one of the operators (+,-,*,/):/
20/10=2
admin:~/work$ ./a.out
Enter two integers separated by a space:10 20
Please enter one of the operators (+,-,*,/):%
Operator is different
- 標準入力から1つの整数と1つの演算子(+, -, *, / のいずれか)を受け取り、一つ前の演算結果をもとに計算し、結果を標準出力に出力してみましょう
※割り算の場合は、整数値で商のみを出力する
※0で割ることがあった場合、エラーメッセージを出力する
※10回繰り返す
実行結果
Enter a integer:4
Please enter one of the operators (+,-,*,/):+
4
Enter a integer:5
Please enter one of the operators (+,-,*,/):*
20
Enter a integer:2
Please enter one of the operators (+,-,*,/):/
10
Enter a integer:0
Please enter one of the operators (+,-,*,/):/
Please enter a value other than 0
Enter a integer:0
Please enter one of the operators (+,-,*,/):*
0
Enter a integer:10
Please enter one of the operators (+,-,*,/):+
10
Enter a integer:3
Please enter one of the operators (+,-,*,/):-
7
Enter a integer:2
Please enter one of the operators (+,-,*,/):/
3
4日目 問題
- int型の配列に格納されている値から、最大値、最小値、平均値をそれぞれ求める関数を作成してみましょう
実行結果
最小値:18
最大値:85
平均値:50.75
- 引数の文字列を標準出力する関数を作成してみましょう
※引数の文字列は可変長引数とします
実行結果
3:Hello! World. Taro.
2:Hello! Taro.
1:Hello!
5日目 問題
- 配列を定義し、配列の値を順に表示するプログラムを作成してみましょう
ただし、負の値のときは表示しない、0の場合はループを終了します
array[7]={-1,5,10,0,12,-20}とします
実行結果
5
10
- 配列を定義し、隣り合う等しい要素が存在するときYES、存在しないときNOを出力してみましょう
array[20]={15, 20, 9, 14, 14, 18, 3, 20, 10, 9, 1, 20, 20, 1, 8, 7, 6, 1, 10, 12}
実行結果
YES
- 文字列を2つ取得し、1つ目の文字列の後ろに2つ目の文字列を逆順でつなげるプログラムを作成してみましょう
※2つの文字列をつなげた長さは255文字以下とし、入力する文字列の入力時、入力サイズを考慮するものとします
Enter the first string (maximum 99 characters):Hello!!
Enter the second string (maximum 99 characters)::World.
連結後の文字列:Hello!! .dlroW
6日目 問題
- 以下の機能をもつプログラムを作成してみましょう
(1) int, float, double型の変数a, b, cを宣言し,それぞれに,適当な値を代入します
(2) また変数a, b, cへ参照変数pa, pb, pcを宣言します
(3) (2)で宣言した変数を使い、a, b,cの値を表示するプログラムを作成してみましょう - 以下の機能をもつプログラムを参照を使用して作成してみましょう
(1) 2つの整数の値を入れ替える関数を実装します
(2) 長さ5の整数型の配列変数を用意し、1から100までの乱数を代入し、代入した値を表示します
(3) (2)で作成した配列を降順(大きい値の順番)に並び替えます
7日目 問題
- 簡単な家計簿を作成してみましょう
以下のメンバを持つ構造体(Item)を定義する
費目(例: 食費、交通費)
金額
支出かどうか(収入なら false、支出ならtrue)
(1) いくつかのItem型の変数を宣言して初期化します
(2) (1)で初期化した情報を順番に表示する関数を作成します
(3) 支出の合計と収入の合計を計算して表示します
実行結果
給与:+200000
食費:-20000
交通費:-7500
携帯代:-12000
臨時収入:+10000
**********
収入合計:210000
支出合計:39500
- 図形の情報を管理する構造体を作成してみましょう
長方形の場合は、幅と高さ、円の場合は半径を保持する構造体(Shape型)を作成します
(1) いくつかの Shape 型の変数を宣言して、円と長方形を作成します
(2) 図形の種類に応じて、面積を計算する関数を作成します
実行結果
(circle)19.625
(rectangle)20
- 簡単な在庫管理をする構造体を作成してみましょう
製品名と在庫数、割引の有無、単価または割引後の単価をもつ構造体(Product型)を作成します
(1) いくつかの Product 型の変数を宣言して、割引ありと割引なしの製品を作成します
(2) 各製品の名前、在庫数、そして割引の有無に応じて単価を表示する関数を作成します
(3) 各製品の現在の在庫総額を計算して表示します
実行結果
商品名:mobile battery
在庫数:3
単価:7500
**************
商品名:Gloves
在庫数:10
単価:4515.5
**************
在庫総額=67655
C++ 基礎編 解答例
1日目
- 二つの文字列”apple”と”orange”を一行でスペース区切りで出力してみましょう
#include <iostream>
int main(){
std::cout << "apple" << " " << "orange" << std::endl;
return 0;
}- 二つの数値11と1.5を出力してみましょう
#include <iostream>
int main(){
std::cout << 11 << " " << 11.5 << std::endl;
return 0;
}- 一つの数値を標準入力より読み込み、読み込んだ数値を出力してみましょう
※標準入力はstd::cinを使用します
#include <iostream>
int main(){
int x;
std::cout << "Please enter a number:" ;
std::cin >> x;
std::cout << "The value entered is " << x << std::endl;
return 0;
}
- 二つの数値を半角スペース区切りで読み込み、読み込んだ数値を出力してみましょう
#include
int main(){
int x,y;
std::cout << "Please enter two integers separated by a space:" ;
std::cin >> x >> y;
std::cout << "The values entered are " << x << " and " << y << std::endl;
return 0;
}- 二つの文字列を標準入力より、読み込み、読み込んだ文字列をそれぞれ出力してみましょう
#include
int main(){
char str1[100],str2[100];
std::cout << "Please enter two strings separated by a space:" ;
std::cin >> str1 >> str2;
std::cout << "The values entered are " << str1 << " and " << str2 << std::endl;
return 0;
}
※std::stringを使用した場合
#include
int main(){
std::string str1,str2;
std::cout << "Please enter two strings separated by a space:" ;
std::cin >> str1 >> str2;
std::cout << "The values entered are " << str1 << " and " << str2 << std::endl;
return 0;
}2日目
- 二つの整数を標準入力から受け取り、四則演算の結果を出力してみましょう
#include <iostream>
int main(){
int x,y;
int z;
std::cout << "Please enter two integers separated by a space:" ;
std::cin >> x >> y;
z = x + y;
std::cout << x << " + " << y << "=" << z << std::endl;
z = x - y;
std::cout << x << " - " << y << "=" << z << std::endl;
z = x * y;
std::cout << x << " * " << y << "=" << z << std::endl;
z = x / y;
std::cout << x << " / " << y << "=" << z << std::endl;
z = x % y;
std::cout << x << " % " << y << "=" << z << std::endl;
return 0;
}- 下記のように数値を標準入力から受け取り、ビット演算(左)、(右)出力してみましょう
#include <iostream>
int main(){
int x,y;
int z;
std::cout << "Please enter two integers separated by a space:" ;
std::cin >> x >> y;
z = x >> y;
std::cout << std::hex << x << " >> " << y << " = " << z << std::endl;
z = x << y;
std::cout << std::hex << x << " << " << y << " = " << z << std::endl;
return 0;
}- 標準入力より、半径の値を取得し、円の面積を計算してみましょう
#include <iostream>
int main(){
double r;
const double PI = 3.14;
std::cout << "Please enter radius:" ;
std::cin >> r;
double area = r * r * PI;
std::cout << "半径" << r << "の円の面積=" << area << std::endl;
return 0;
}- 標準入力より、直角三角形の底辺と高さを取得し、斜辺の長さを出力してみましょう
#include <iostream>
#include <cmath> // sqrt使用のため
int main(){
double width;
double height;
std::cout << "Enter the length of the base:" ;
std::cin >> width;
std::cout << "Please enter the height length:";
std::cin >> height;
double length = sqrt(width*width+height*height);
std::cout << "斜辺の長さ=" << length << std::endl;
return 0;
}- 1年、2年、5年の秒数を計算してみましょう(ただし、うるう年は考えす、一年は365日としてください)
#include <iostream>
int main(){
const int days = 365;
const int hours = 24;
const int minutes = 60;
const int seconds = 60;
int result = days * hours * minutes * seconds;
std::cout << "1年の秒数=" << result << std::endl;
std::cout << "2年の秒数=" << result*2 << std::endl;
std::cout << "5年の秒数=" << result*5 << std::endl;
return 0;
}3日目
- 標準入力から2つの整数を受け取って、どちらが大きいかを判定し、大きい方の数を標準出力に出力してみましょう
#include <iostream>
int main(){
int x,y;
std::cout << "Please enter two integers separated by a space:" ;
std::cin >> x >> y;
if(x>=y){
std::cout << x << std::endl;
}else{
std::cout << y << std::endl;
}
// 条件演算子を使用した場合
int z = x >= y ? x : y;
std::cout << z << std::endl;
return 0;
}- 1を10回繰り返してみましょう
#include <iostream>
int main(){
int x,y;
for(int i=0;i<10;i++){
std::cout << "Please enter two integers separated by a space:" ;
std::cin >> x >> y;
if(x>=y){
std::cout << x << std::endl;
}else{
std::cout << y << std::endl;
}
// 条件演算子を使用した場合
int z = x >= y ? x : y;
std::cout << z << std::endl;
}
return 0;
}- 標準入力より1から100までの数値を取得し、以下の処理を実行してみましょう
(1) 80点以上なら”優”と表示
(2) 80点未満、70点以上なら、”良”と表示
(3) 70点未満、60点以上なら、”可”と表示
(4) 60点未満なら、”不可”と表示
#include <iostream>
int main(){
int tensu;
std::cout << "Please enter integers(0から100まで):" ;
std::cin >> tensu;
if(tensu>=80){
std::cout << "優" << std::endl;
}else if(tensu>=70){
std::cout << "良" << std::endl;
}else if(tensu>=60){
std::cout << "可" << std::endl;
}else{
std::cout << "不可" << std::endl;
}
return 0;
}- 標準入力から2つの整数と1つの演算子(+, -, *, / のいずれか)を受け取り、その演算結果を標準出力に出力してみましょう
※割り算の場合は、整数値で商のみを出力します
※0は入力しないものとします
#include <iostream>
int main(){
int x,y;
char op;
std::cout << "Enter two integers separated by a space:" ;
std::cin >> x >> y;
std::cout << "Please enter one of the operators (+,-,*,/):" ;
std::cin >> op;
int z;
switch(op){
case '+':
z = x + y;
break;
case '-':
z = x - y;
break;
case '*':
z = x * y;
break;
case '/':
z = x / y;
break;
default:
std::cout << "Operator is different" << std::endl;
return 1;
break;
}
std::cout << x << op << y << "=" << z << std::endl;
return 0;
}- 標準入力から1つの整数と1つの演算子(+, -, *, / のいずれか)を受け取り、一つ前の演算結果をもとに計算し、結果を標準出力に出力してみましょう
#include <iostream>
int main(){
int x;
char op;
int z = 0;
for(int i=0;i<10;i++){
std::cout << "Enter a integer:" ;
std::cin >> x;
std::cout << "Please enter one of the operators (+,-,*,/):" ;
std::cin >> op;
switch(op){
case '+':
z = z + x;
break;
case '-':
z = z - x;
break;
case '*':
z = z * x;
break;
case '/':
if(x==0){
std::cout << "Please enter a value other than 0" << std::endl;
continue;
}
z = z / x;
break;
default:
std::cout << "Operator is different" << std::endl;
continue;
break;
}
std::cout << z << std::endl;
}
return 0;
}4日目
- int型の配列に格納されている値から、最大値、最小値、平均値をそれぞれ求める関数を作成してみましょう
#include <iostream>
int Min(int ary[],int size){
int min = ary[0];
for(int i=1;i<size;i++){
if(min>ary[i]){
min=ary[i];
}
}
return min;
}
int Max(int ary[],int size){
int max = ary[0];
for(int i=1;i<size;i++){
if(max<ary[i]){
max=ary[i];
}
}
return max;
}
double Average(int ary[],int size){
double sum=0;
for(int i=0;i<size;i++){
sum+=ary[i];
}
double ave = sum/size;
return ave;
}
int main(){
int ary[] = {25,54,18,49,22,75,22,42,59,70,37,39,85,55,80,73,60,63,35,52};
int size = sizeof(ary)/sizeof(int);
int min = Min(ary,size);
int max = Max(ary,size);
double ave = Average(ary,size);
std::cout << "最小値:" << min << std::endl;
std::cout << "最大値:" << max << std::endl;
std::cout << "平均値:" << ave << std::endl;
return 0;
}- 引数の文字列を標準出力する関数を作成してみましょう
#include <iostream>
#include <stdarg.h> // 可変長引数を定義するために使用
void Print(int count,...){
va_list ap; // 可変長引数リストの変数
va_start(ap, count); // 可変長引数リストの初期化
std::cout << count << ":" ;
for (int i = 0; i < count; i++) {
std::cout << va_arg(ap, const char*) << " ";// 可変長引数から文字列の値を取り出す
}
va_end(ap); // 可変長引数リストの終了処理
std::cout << std::endl;
}
int main(){
const char *str1="Hello!";
const char *str2="World.";
const char *str3="Taro.";
Print(3,str1,str2,str3);
Print(2,str1,str3);
Print(1,str1);
return 0;
}5日目
- 配列を定義し、配列の値を順に表示するプログラムを作成してみましょう
ただし、負の値のときは表示しない、0の場合はループを終了します
array[7]={-1,5,10,0,12,-20}とします
#include <iostream>
int main(){
int array[]={-1,5,10,0,12,-20};
int length = sizeof(array) / sizeof(int);
for(int i=0;i<length;i++){
if(array[i]<0){ // 負のとき
continue;
}
if(array[i]==0){ // 0のとき終了
break;
}
std::cout<<array[i]<<std::endl;
}
return 0;
}- 配列を定義し、隣り合う等しい要素が存在するときYES、存在しないときNOを出力してみましょう
array[20]={15, 20, 9, 14, 14, 18, 3, 20, 10, 9, 1, 20, 20, 1, 8, 7, 6, 1, 10, 12}
#include <iostream>
int main(){
int array[20]={15, 20, 9, 14, 14, 18, 3, 20, 10, 9, 1, 20, 20, 1, 8, 7, 6, 1, 10, 12};
bool flg = false;
int before = array[0];
int size = sizeof(array) / sizeof(int);
for(int i=1;i<size;i++){
if(before == array[i]){
flg = true;
break;
}
before = array[i];
}
std::cout << (flg==true ? "YES" : "NO") <<std::endl;
return 0;
}- 文字列を2つ取得し、1つ目の文字列の後ろに2つ目の文字列を逆順でつなげるプログラムを作成してみましょう
#include <iostream>
#include <cstring>
int main(){
// ユーザーに入力してもらうための文字列を格納する配列
char str1[100];
char str2[100];
char reversedStr2[100];
char combinedStr[200]; // 連結後の文字列を格納(余裕を持たせたサイズ)
// 1つ目の文字列の入力を促す
std::cout << "Enter the first string (maximum 99 characters):";
std::cin.getline(str1, sizeof(str1));
// 2つ目の文字列の入力を促す
std::cout << "Enter the second string (maximum 99 characters)::";
std::cin.getline(str2, sizeof(str2));
// 2つ目の文字列を逆順にする
int len2 = strlen(str2);
for (int i = 0; i < len2; i++) {
reversedStr2[i] = str2[len2 - 1 - i];
}
reversedStr2[len2] = '\0'; // 逆順にした文字列の終端文字
// 1つ目の文字列を連結後の文字列にコピー
strcpy(combinedStr, str1);
// 逆順にした2つ目の文字列を連結後の文字列の末尾に追加
strcat(combinedStr, reversedStr2);
// 結果を表示
std::cout << "連結後の文字列:" << combinedStr << std::endl;
return 0;
}std::cin.getline(str1, sizeof(str1))
std::cin >> は、空白文字(スペース、タブ、改行など)に出会うと、そこで入力の区切りと判断して取得を終了します
std::cin.getline(str, size) は、指定したサイズ (size-1文字まで)の文字を読むか、改行文字 (\n) を読み込むか、ファイルの終わりに達するまで、ずっと文字を読み込み続け、読み込んだ文字列の最後に自動的にヌル文字 \0 を追加してくれる処理を提供します
6日目
- 以下の機能をもつプログラムを作成してみましょう
(1) int, float, double型の変数a, b, cを宣言し,それぞれに,適当な値を代入します
(2) また変数a, b, cへの参照変数pa, pb, pcを宣言します
(3) (2)で宣言した変数を使い、a, b,cの値を表示するプログラムを作成してみましょう
#include <iostream>
int main(){
int a=1;
float b=1.2;
double c=1.23;
int &pa = a;
float &pb = b;
double &pc = c;
std::cout << "a=" << pa << std::endl;
std::cout << "b=" << pb << std::endl;
std::cout << "c=" << pc << std::endl;
return 0;
}- 以下の機能をもつプログラムを参照を使用して作成してみましょう
(1) 2つの整数の値を入れ替える関数を実装します
(2) 長さ5の整数型の配列変数を用意し、1から100までの乱数を代入し、代入した値を表示します
(3) (2)で作成した配列を降順(大きい値の順番)に並び替えます
#include <iostream>
#include <random>
// 2つの整数の値を入れ替える関数(参照渡しを使用)
void swapInts(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
void printArray(const int arr[], int size) {
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << (i == size - 1 ? "" : ", ");
}
std::cout << std::endl;
}
// バブルソートの実装(降順)
void bubbleSortDescending(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - 1 - i; ++j) {
// 現在の要素が次の要素より小さい場合、入れ替える(降順のため)
if (arr[j] < arr[j+1]) {
swapInts(arr[j], arr[j+1]); // 自作のswap関数を使用
}
}
}
}
int main() {
std::random_device rd; // 非決定論的な乱数生成器
std::mt19937 gen(rd()); // メルセンヌ・ツイスタ法による擬似乱数生成器
std::uniform_int_distribution<> dis(1, 100); // 1 から 100 の範囲の一様分布
const int ARRAY_SIZE = 5;
int numbers[ARRAY_SIZE];
std::cout << "生成された乱数と配列の初期値:" << std::endl;
for (int i = 0; i < ARRAY_SIZE; ++i) {
numbers[i] = dis(gen); // 1から100までの乱数を代入
}
printArray(numbers, ARRAY_SIZE); // 代入した値を表示
std::cout << std::endl;
std::cout << "--- 配列の降順ソート ---" << std::endl;
bubbleSortDescending(numbers, ARRAY_SIZE); // 自作のバブルソート関数を呼び出し
std::cout << "降順にソートされた配列:" << std::endl;
printArray(numbers, ARRAY_SIZE); // ソートされた値を表示
std::cout << std::endl;
return 0; // プログラムの正常終了
}7日目
- 簡単な家計簿を作成してみましょう
#include <iostream>
enum Himoku {syokuhi,koutuhi,mobile,salary,extra}; // 費目を定義
struct Item{
Himoku himoku;
int price;
bool isExpense;// 支出:true,収入:false
};
// 表示関数
void dispItem(Item item){
std::string himoku;
switch(item.himoku){
case syokuhi:
himoku = "食費";
break;
case koutuhi:
himoku = "交通費";
break;
case mobile:
himoku = "携帯代";
break;
case salary:
himoku = "給与";
break;
case extra:
himoku = "臨時収入";
break;
default:
himoku = "";
break;
}
std::cout << himoku << ":" << (item.isExpense==false ? "+" : "-") << item.price << std::endl;
}
int main(){
Item item[5]={
{salary,200000,false},
{syokuhi,20000,true},
{koutuhi,7500,true},
{mobile,12000,true},
{extra,10000,false}
};
// itemを一覧表示
for(int i=0;i<5;i++){
dispItem(item[i]);
}
int payment = 0; // 支出合計
int income = 0; // 収入合計
for(int i=0;i<5;i++){
if(item[i].isExpense){
payment += item[i].price;
}else{
income += item[i].price;
}
}
std::cout << "**********" << std::endl;
std::cout << "収入合計:" << income << std::endl;
std::cout << "支出合計:" << payment << std::endl;
return 0;
}- 図形の情報を管理する構造体を作成してみましょう
#include <iostream>
union ShapeData{
double radius; // 円の半径
struct { // 長方形
double width; // 幅
double height; // 高さ
} rectangle;
};
enum ShapeType {circle,rectangle};
struct Shape{
ShapeType type; // 図形の種類
ShapeData data; // 図形ごとのデータ
};
double getCircleArea(double radius){
const double PI = 3.14;
return radius*radius*PI;
}
double getRectangleArea(double width,double height){
return width * height;
}
int main(){
Shape sp[2]; // 構造体の変数を宣言
// 構造体の初期化
sp[0].type=circle;
sp[0].data.radius=2.5;
sp[1].type=rectangle;
sp[1].data.rectangle.width=5;
sp[1].data.rectangle.height=4;
double area;
for(int i=0;i<2;i++){
switch(sp[i].type){
case circle:
area = getCircleArea(sp[i].data.radius);
std::cout << "(circle)" << area << std::endl;
break;
case rectangle:
area = getRectangleArea(sp[1].data.rectangle.width,sp[1].data.rectangle.height);
std::cout << "(rectangle)" << area << std::endl;
break;
default:
break;
}
}
return 0;
}- 簡単な在庫管理をする構造体を作成してみましょう
#include <iostream>
union PriceInfo{
int unitPrice; //単価(整数)
double discountedPrice; //割引後の単価(浮動小数点数)
};
struct Product{
std::string name; // 製品名
int quantity; // 在庫数
PriceInfo price;
bool hasDiscount; // true:割引あり false:割引なし
};
// 商品の表示関数
void showProduct(Product product){
std::cout << "商品名:" << product.name << std::endl;
std::cout << "在庫数:" << product.quantity << std::endl;
std::cout << "単価:" << (product.hasDiscount ? product.price.discountedPrice : product.price.unitPrice) << std::endl;
std::cout << "**************" << std::endl;
}
int main(){
Product pro[2];
// 初期化
pro[0].name = "mobile battery";
pro[0].quantity = 3;
pro[0].hasDiscount = false;
pro[0].price.unitPrice = 7500;
pro[1].name = "Gloves";
pro[1].quantity = 10;
pro[1].hasDiscount = true;
pro[1].price.discountedPrice = 4515.5;
for(int i=0;i<2;i++){
showProduct(pro[i]);
}
double sum=0;
for(int i=0;i<2;i++){
double price = pro[i].hasDiscount ? pro[i].price.discountedPrice : pro[i].price.unitPrice;
price = price * pro[i].quantity;
sum = sum + price;
}
std::cout << "在庫総額=" << sum << std::endl;
}
コメント