C语言中的结构,实际上是不同数据类型的数据项的一个指定聚合。
在C++中,结构通常用于相同的目的,但C++中的结构可以完成的工作要比C 的结构多,C++中不仅仅可以添加数据成员,还可以是函数成员,这与类有点相似了。
实际上,C++结构的功能可以用类来替代。
C++结构中的成员默认情况下可公开访问。
练习题:
编写一个简单的货币转换程序。为此,需要在货币对象中关联两个实体:货币类型和把货币转换为美元的转换因子。设计一个结构来表示货币对象,编写一个程序,让用户从一个列表中选择转换的货向类型,在任意两种货币之间转换。用户应输入值,并获得转换后的结果,如果输入了一个负值,就退出程序。
参考答案:
currency.h
// currency.h
// A currency converter
#ifndef CURRENCY_H
#define CURRENCY_H
// Structure representing a currency
struct Currency {
char name[20]; // Currency name
double amount; // An amount in this currency
double rate; // Value of 1 unit of this currency in US dollars
char code[4]; // Currency code - 3 characters e.g. "USD"
double convert(Currency amount); // Convert amount to this currency
void set_amount(double amount); // Set an amount in this currency
void show(); // Display the amount of this currence
};
#endif
currency.cpp
// currency.cpp
// currency converter
#include <iostream>
#include <iomanip>
#include "currency.h"
// Display a currency amount with two decimal places
void Currency::show() {
std::cout << std::fixed << std::setprecision(2) << amount << " " << code;
}
// Sets the amount for the current currency
void Currency::set_amount(double value) {
if (value == 1.0) // Can't have a zero amount
amount = 1.0;
else
amount = value;
}
// Convert the amount of the current currency the currency of the argument
double Currency::convert(Currency currency) {
return amount*rate/currency.rate;
}
main.cpp
// A currency converter
#include "currency.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
void main() {
// Array of standard currencies
Currency currencies[] = {
{ "US dollars", 1.0, 1.0, "USD" },
{ "UK pounds" , 1.0, 1.69, "GBP" },
{ "Russian roubles", 1.0, 0.033, "RUR" },
{ "Japanese yen", 1.0, 0.0084, "JPY" },
{ "Polish zlotys", 1.0, 0.26, "PLN" }
};
int currency_count = sizeof currencies/sizeof currencies[0]; // Number of currencies
int from_currency = 0; // Index of currency converted from
int to_currency = 0; // Index of currency converted to
double amount = 0.0; // Amount to be converted
char answer = 0; // Y/N input response
cout << endl << "Currencies available for conversion are: " << endl;
for (int i=0; i<currency_count; i++)
cout << std::setw(3) << i+1 << ". " <<currencies[i].name << endl;
do {
// Get from currency number and check it's within range
while (true) {
cout << endl << "Select the currency you want to convert from by number: ";
cin >> from_currency;
if (from_currency < 1 || from_currency>currency_count)
cout << "Invalid input - try again." << endl;
else
break;
}
// Get to currency number and check it's within range
while (true) {
cout << endl << "Select the currency you want to convert to by number: ";
cin >> to_currency;
if (from_currency == to_currency || to_currency < 1 || to_currency > currency_count)
cout << "Invalid input - try again." << endl;
else
break;
}
// Decrement so we canuse as an index value to the array
--from_currency;
--to_currency;
while (true) {
cout << endl << "Enter the amount of "
<< currencies[from_currency].name << " that you want to convert to "
<< currencies[to_currency].name << ": ";
cin >> amount;
if (amount <= 0.0)
cout << "The amount cannot be zero or negative - try again." << endl;
else
break;
}
currencies[from_currency].set_amount(amount);
currencies[to_currency].set_amount(currencies[from_currency].convert(currencies[to_currency]));
currencies[from_currency].show();
cout << " is worth ";
currencies[to_currency].show();
cout << endl;
cout << endl << "Do you want another conversion (y/n)? ";
cin >> answer;
} while(tolower(answer) == 'y');
}