Advertisement
Guest User

łorior

a guest
May 29th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. // bohaterowie.cpp : Defines the entry point for the console application.
  2. //
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <conio.h>
  10.  
  11. using namespace std;
  12.  
  13. FILE plik;
  14.  
  15. class Character
  16. {
  17. private:
  18.     char Name[128];
  19.     char Type[128];
  20. public:
  21.     Character(const char* imie, const char* typ);
  22.     char* GetName() const;
  23.     char* GetType() const;  
  24.     static Character** load(char* path, int &);
  25.     virtual void Draw() = 0;
  26. };
  27.  
  28. Character::Character(const char* imie, const char* typ)
  29. {
  30.     strcpy(Name, imie);
  31.     strcpy(Type, typ);
  32. }
  33.  
  34. void Character::Draw()
  35. {
  36.     cout << "Type: " << Type << " Name: " << Name;
  37. }
  38. char* Character::GetName() const
  39. {
  40.     char* name = (char*)malloc(sizeof(char) * strlen(Name) + 1);
  41.     strcpy(name, Name);
  42.     return name;
  43. }
  44.  
  45. char* Character::GetType() const
  46. {
  47.     char* type = (char*)malloc(sizeof(char) * strlen(Type) + 1);
  48.     strcpy(type, Type);
  49.     return type;
  50. }
  51.  
  52. class Warrior : public Character
  53. {
  54. private:
  55.     double _ArmorLvl;
  56. public:
  57.     Warrior(const char* Name, double ArmorLevel) : Character(Name, "Warrior"), _ArmorLvl(ArmorLevel) {};
  58.     void SetArmorLevel(double a)
  59.     {
  60.         _ArmorLvl = a;
  61.     };
  62.     double GetArmorLevel() const
  63.     {
  64.         return _ArmorLvl;
  65.     };
  66.     virtual void Draw()
  67.     {
  68.         Character::Draw();
  69.         cout << " LevelArmor: " << _ArmorLvl << endl;
  70.     };
  71. };
  72.  
  73. class Enemy : public Character
  74. {
  75. private:
  76.     double _Strength;
  77.     int _Enemies;
  78. public:
  79.     Enemy(const char* Name, double Strength, int Enemies) : Character(Name, "Enemy"), _Strength(Strength), _Enemies(Enemies) {};
  80.     void SetStrenght(double s)
  81.     {
  82.         _Strength = s;
  83.     };
  84.     double GetStrenght() const
  85.     {
  86.         return _Strength;
  87.     };
  88.     int GetConcurrentWarriors() const
  89.     {
  90.         return _Enemies;
  91.     };
  92.     virtual void Draw()
  93.     {
  94.         Character::Draw();
  95.         cout << " Strength: " << _Strength <<" ConcurrentWarriors: "<<_Enemies<< endl;
  96.     };
  97. };
  98.  
  99. Character** Character::load(char* path, int &k)
  100. {
  101.     int l = 0;
  102.     FILE* file;
  103.     file = fopen(path, "r");
  104.  
  105.     Character** lista = (Character**)malloc(sizeof(Character*));
  106.  
  107.     while (!feof(file))
  108.     {
  109.         char type[128];
  110.         char name[128];
  111.         float str;
  112.         int enemies;
  113.  
  114.         fgets(type, sizeof(type), file);
  115.  
  116.         strtok(type, "\n");
  117.  
  118.         if (!strcmp(type, "Warrior"))
  119.         {
  120.             fscanf(file, "%s %f", &name, &str);
  121.             l++;
  122.             lista = (Character**)realloc(lista, sizeof(Character*) * l);
  123.             lista[l - 1] = new Warrior(name, str);
  124.         }
  125.         else if (!strcmp(type, "Enemy"))
  126.         {
  127.             fscanf(file, "%s %f %d", &name, &str, &enemies);
  128.             l++;
  129.             lista = (Character**)realloc(lista, sizeof(Character*) * l);
  130.             lista[l - 1] = new Enemy(name, str, enemies);
  131.         }
  132.     }
  133.  
  134.     fclose(file);
  135.  
  136.     k = l;
  137.  
  138.     return lista;
  139. }
  140.  
  141. int main()
  142. {
  143.     const int charactersCount = 6;
  144.     Character* characters[charactersCount] = {};
  145.  
  146.     characters[0] = new Warrior("Batman", 10.2);
  147.     characters[1] = new Enemy("Joker", 5.1, 3);
  148.     characters[2] = new Warrior("Superman", 55.3);
  149.     characters[3] = new Enemy("Ultra-Humanite", 17.2, 10);
  150.     characters[4] = new Warrior("Daredevil", 33.7);
  151.     characters[5] = new Enemy("Wilson-Fisk", 3.1, 10);
  152.  
  153.  
  154.     for (int i = 0; i < charactersCount; ++i)
  155.     {
  156.         characters[i]->Draw();
  157.     };
  158.  
  159.    
  160.     cout << characters[5]->GetName() << endl;
  161.  
  162.     for (int i = 0; i < charactersCount; ++i)
  163.     {
  164.         if (characters[i])
  165.             delete characters[i];
  166.     };
  167.  
  168.     int l;
  169.  
  170.     Character** lista = Character::load("bohaterowie.txt", l);
  171.  
  172.     cout << "\nPo wczytaniu listy\n" << endl;
  173.  
  174.     for (int i = 0; i < l; i++)
  175.     {
  176.         lista[i]->Draw();
  177.     }
  178.  
  179.     _getch();
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement