De.coder
471 subscribers
458 photos
44 videos
191 files
300 links
Download Telegram
2. بازی کماندار در مقابل اُرگ
زبان نوشتاری ++C
سطح برنامه : متوسط
*********//*********
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class hero
{
protected:
string name;
double life;
double attack;
double defense;

public:
virtual void myattack(hero&)=0;

double get_life() const
{
return this->life;
}
string get_name() const
{
return this->name;
}
double get_defense() const
{
return this->defense;
}
void set_life(double x)
{
this->life = x;
}
void set_attack(double x)
{
this->attack = x;
}
};

class orc : public hero
{
public:
orc() {
this->name = "Ushk";
this->life = 60;
this->defense = 25;
}
~orc(){}

void myattack (hero &h)
{

double aux;
cout<<this->name<<" made an Axe attack - ";

if(this->attack > h.get_defense())
{
aux = (this->attack - h.get_defense());

cout<< h.get_name() <<" received "<< aux <<" damage\n";

aux = h.get_life() - aux;
h.set_life(aux);
}
else
cout<<" **Attack miss** \n";
}
};

class archer : public hero
{
public:
archer(){
this->name = "Legolas";
this->life = 40;
this->defense = 10;
}
~archer(){}

void myattack (hero& h)
{

double aux;
cout<< this->name <<" throw an Arrow - ";

if(this->attack > h. get_defense())
{
aux = (this->attack - h.get_defense());

cout<< h.get_name() <<" received "<< aux <<" damage\n";

aux = h.get_life() - aux;
h.set_life(aux);
}
else
cout<<" **Attack miss**\n";
}
};

int main() {
srand(time(NULL));
orc orc1;
archer arch1;
double num;

while(1)
{
num = rand()%13+13;

orc1.set_attack(num);
orc1.myattack(arch1);

if(arch1.get_life() <= 0){
cout<<arch1.get_name()
<< " **Died**";
break;
}

num = rand()%30+23;
arch1.set_attack(num);
arch1.myattack(orc1);

if(orc1.get_life() <= 0){
cout<<orc1.get_name()
<<" **Died**";
break;
}
}
return 0;
}

@de_coder
#include <iostream>
using namespace std;


int* bubbleSort(int arr[], int n);

int main()
{
// sample array
const int size = 7;
int arr[] = {9, 5, 12, 4, 8, 42, 3};

// sort the array
arr[size] = *bubbleSort(arr, size);

for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << "\n\n\ de.coder();" ;

return 0;
}

int* bubbleSort(int *arr, int n)
{
bool swapped = true;
int j = 0;
int tmp;
while(swapped)
{
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (arr[i] > arr[i + 1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
return arr;
}

@de_coder
در این برنامه منوی ما دارای 4 گزینه است

با انتخاب گزینه ی اول شما وارد یک برنامه می شوید که در آن با وارد کردن عدد دلخواه خود به شما نشان می دهد که آیا این عدد اول می باشد یا خیر
و سپس با وارد کردن عدد 0 به منوی اصلی بر میگردید

با وارد کردن عدد 2 وارد برنامه ای می شوید که در آن عدد مورد نظر را وارد می کنید و سپس آن عددی را که دوست دارید به آن توان برسد وارد می کنید و سپس با وارد کردن عدد 0 به منوی اصلی بر میگردید

با وارد کردن عدد 3 وارد برنامه ای می شوید که در آن عدد مورد نظر را وارد می کنید و برنامه مقدار فاکتوریل عدد شما رو چاپ می کند و با وارد کردن عدد 0 به منوی اصلی باز می گردید

با وارد کردن عدد 4 هم از منوی اصلی خارج می شوید

توجه داشته باشید که از تابع system("cls") برای clear screen استفاده کردیم که این متد در کتاب خانه زیر قرار دارد
#include<stdlib.h>

@de_coder
#include<iostream>
#include<cmath>
#include <ctime>
using namespace std;

int main()
{

time_t t = time(NULL);
tm* timePtr = localtime(&t);

cout << "seconds= " << (timePtr->tm_sec) << endl;
cout << "minutes = " << (timePtr->tm_min) << endl;
cout << "hours = " << (timePtr->tm_hour) << endl;
cout << "day of month = " << (timePtr->tm_mday) << endl;
cout << "month of year = " << (timePtr->tm_mon)+1 << endl;
cout << "year = " << (timePtr->tm_year)+1900 << endl;
cout << "weekday = " << (timePtr->tm_wday )<< endl;
cout << "day of year = " << (timePtr->tm_yday )<< endl;
cout << "daylight savings = " <<(timePtr->tm_isdst )<< endl;
cout << endl;
cout << endl;


cout << "Date " <<(timePtr->tm_mday)<<"/"<< (timePtr->tm_mon)+1 <<"/"<< (timePtr->tm_year)+1900<< endl;
cout << "Time " << (timePtr->tm_hour)<<":"<< (timePtr->tm_min)<<":"<< (timePtr->tm_sec) << endl;
cout<<" De.coder();";
return 0;
}


@de_coder
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int size = sizeof(alphanum) - 1;

int main()
{
//password length
int length = 8;

srand(time(0));
for (int i = 0; i < length; i++)
{
cout << alphanum[rand() % size];
}
cout << "\n\nDecoder();";
return 0;
}

@de_coder