C++ (Qt)#ifndef GAME_H#define GAME_H #include <QJsonObject>#include <QList> #include "character.h"#include "level.h" //! [0]class Game{public: Game(); enum SaveFormat { Json, Binary }; const Character &player() const; const QList<Level> &levels() const; void newGame(); bool loadGame(SaveFormat saveFormat); bool saveGame(SaveFormat saveFormat) const; void read(const QJsonObject &json); void write(QJsonObject &json) const;private: Character mPlayer; QList<Level> mLevels;};//! [0] #endif // GAME_H
C++ (Qt)#ifndef CHARACTER_H#define CHARACTER_H #include <QJsonObject>#include <QString> //! [0]class Character{public: enum ClassType { Warrior, Mage, Archer }; Character(); Character(const QString &name, int level, ClassType classType); QString name() const; void setName(const QString &name); int level() const; void setLevel(int level); ClassType classType() const; void setClassType(ClassType classType); void read(const QJsonObject &json); void write(QJsonObject &json) const;private: QString mName; int mLevel; ClassType mClassType;};//! [0] #endif // CHARACTER_H
C++ (Qt)#ifndef LEVEL_H#define LEVEL_H #include <QJsonObject>#include <QList> #include "character.h" //! [0]class Level{public: Level(); const QList<Character> &npcs() const; void setNpcs(const QList<Character> &npcs); void read(const QJsonObject &json); void write(QJsonObject &json) const;private: QList<Character> mNpcs;};//! [0] #endif // LEVEL_H
C++ (Qt)#include <QCoreApplication> #include "game.h"//! [0]int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); Game game; game.newGame(); // Game is played; changes are made...//! [0]//! [1] if (!game.saveGame(Game::Json)) return 1; if (!game.saveGame(Game::Binary)) return 1; Game fromJsonGame; if (!fromJsonGame.loadGame(Game::Json)) return 1; Game fromBinaryGame; if (!fromBinaryGame.loadGame(Game::Binary)) return 1; return 0;}//! [1]
C++ (Qt)void Game::newGame() { mPlayer = Character(); mPlayer.setName(QStringLiteral("Hero")); mPlayer.setClassType(Character::Archer); mPlayer.setLevel(15); mLevels.clear(); Level village; QList<Character> villageNpcs; villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"), 10, Character::Warrior)); villageNpcs.append(Character(QStringLiteral("Terry the Trader"), 10, Character::Warrior)); village.setNpcs(villageNpcs); mLevels.append(village); Level dungeon; QList<Character> dungeonNpcs; dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"), 20, Character::Mage)); dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #1"), 5, Character::Warrior)); dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #2"), 5, Character::Warrior)); dungeon.setNpcs(dungeonNpcs); mLevels.append(dungeon);}
C++ (Qt)#include <QCoreApplication>#include <QTextStream>#include "rational.h" QTextStream cin(stdin);QTextStream cout(stdout); int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); // Перемножаем дроби и выводим результат на экран Rational a(2, 3), b(1, 2), c(3, 4), product; product = a * b * c; cout << "product = " << product.numerator() << "/" << product.denominator() << endl; cout.flush(); // Складываем дроби и выводим результат на экран Rational x(47, 48), y(15, 16), sum; sum = x + y; cout << "sum = " << sum.numerator() << "/" << sum.denominator() << endl; cout.flush(); // Вычитаем дроби и выводим результат на экран Rational m(47, 48), n(15, 16), sub; sub = m - n; cout << "sub = " << sub.numerator() << "/" << sub.denominator() << endl; cout.flush(); return app.exec();}
C++ (Qt)#ifndef RATIONAL_H#define RATIONAL_H class Rational{public: Rational(int numerator = 0, int denominator = 1); int numerator() const; void setNumerator(const int numerator); int denominator() const; void setDenominator(const int denominator); friend const Rational operator*(const Rational& lhs, const Rational& rhs); friend const Rational operator+(const Rational& lhs, const Rational& rhs); friend const Rational operator-(const Rational& lhs, const Rational& rhs); private: int mNumerator; int mDenominator;}; #endif // RATIONAL_H
C++ (Qt)#include "rational.h" Rational::Rational(int numerator, int denominator) : mNumerator(numerator), mDenominator(denominator){} int Rational::numerator() const{ return mNumerator;} void Rational::setNumerator(const int numerator){ mNumerator = numerator;} int Rational::denominator() const{ return mDenominator;} void Rational::setDenominator(const int denominator){ mDenominator = denominator;} const Rational operator*(const Rational &lhs, const Rational &rhs){ return Rational(lhs.mNumerator * rhs.mNumerator, lhs.mDenominator * rhs.mDenominator);} const Rational operator+(const Rational &lhs, const Rational &rhs){ Rational result; // Общий знаменатель int commonDenominator = lhs.denominator() * rhs.denominator(); // Числитель int a = lhs.numerator() * rhs.denominator(); int b = rhs.numerator() * lhs.denominator(); result.setNumerator(a + b); // Знаменатель result.setDenominator(commonDenominator); return result;} const Rational operator-(const Rational &lhs, const Rational &rhs){ Rational result; // Общий знаменатель int commonDenominator = lhs.denominator() * rhs.denominator(); // Числитель int a = lhs.numerator() * rhs.denominator(); int b = rhs.numerator() * lhs.denominator(); result.setNumerator(a - b); // Знаменатель result.setDenominator(commonDenominator); return result;}