Skip to content

4 weeks worth of tasks for both sem and prakt #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <cstring>
const size_t inputLenght=41;
const size_t PlateNumLenght = 9;
Comment on lines +3 to +4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const size_t inputLenght=41;
const size_t PlateNumLenght = 9;
const size_t INPUT_LENGTH = 41;
const size_t PLATE_NUM_LENGTH = 9;



enum Brands{
mitsubishi,
mazda,
opel
Comment on lines +8 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mitsubishi,
mazda,
opel
MITSUBISHI,
MAZDA,
OPEL

};
const char *brands[3] = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All caps и на двата масива имената.

"mitsubishi",
"mazda",
"opel"
};
const char *colors[3] = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По-добре да изнесем 3 като константа отвън и долу да използваме нея вместо sizeof(brands/colors)/sizeof(char*).

"red",
"green",
"blue"
};

enum Colors{
red,
green,
blue
Comment on lines +24 to +26
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
red,
green,
blue
RED,
GREEN,
BLUE

};

struct car{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct car{
struct Car{

Brands brand;
char PlateNum [PlateNumLenght];
Colors color;
};

int main(){
car newCar;
char input[inputLenght];
std::cin.getline(input,inputLenght);
for(size_t i =0 ;i< sizeof(brands)/sizeof(char*);i++)if(!strcmp(input,brands[i]))newCar.brand = (Brands)i;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нека да не е наблъскано всичко на един ред.

std::cin.getline(newCar.PlateNum,PlateNumLenght);
std::cin.getline(input,inputLenght);
for(size_t i =0 ;i< sizeof(colors)/sizeof(char*);i++)if(!strcmp(input,colors[i]))newCar.color = (Colors)i;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Същото тук.


std::cout<<"Brand "<<brands[newCar.brand]<<std::endl<<"plateNum "<<newCar.PlateNum<<std::endl
<<"color "<<colors[newCar.color];
return 0;
}
47 changes: 47 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <cstring>

enum Suit{
clubs,
spades,
diamonds,
hearts
Comment on lines +5 to +8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All caps!

};

enum Color{
red,
black
Comment on lines +12 to +13
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тук също.

};

const char * suits[] = {
"clubs",
"spades",
"diamonds",
"hearts"
};

const char * color[] = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All caps на имената на масивите.

"red",
"black"
};

struct Card{
Suit suit;
Color color;
char Honor;
};

int main(){
const size_t inputLenght = 9;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const size_t inputLenght = 9;
const size_t INPUT_LENGTH = 9;

char input[inputLenght];
Card newCard;
std::cin.getline(input,inputLenght);
for(size_t i=0;i< sizeof(suits)/sizeof(char*);i++)if(!strcmp(input,suits[i]))newCard.suit = (Suit)i;
std::cin.getline(input,inputLenght);
for(size_t i=0;i< sizeof(color)/sizeof(char*);i++)if(!strcmp(input,color[i]))newCard.color = (Color)i;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не ги наблъсквай на един ред и си отдели размера като константа.

std::cin.getline(input,inputLenght);
newCard.Honor = input[0];
std::cout<<suits[newCard.suit]<<' '<<color[newCard.color]<<' '<<newCard.Honor;

return 0;
}
126 changes: 126 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <iostream>
#include <cstring>
#include <cstdlib>

int rangeRandomAlg2 (int min, int max){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const for the arguments.


// Get a random number
int random = min + (rand() % (max - min + 1));

// Print the random number
return random;
Comment on lines +7 to +11
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the line indent and make it a one-liner. No need for the variable.

}

struct Player{
int hand[13] = {0,};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

13 should be a constant variable denoting the length.

int cardsInHand = 0;
};

enum Suit{
CLUBS,
SPADES,
DIAMONDS,
HEARTS
};

enum Color{
RED,
BLACK
};

const char * suits[] = {
"CLUBS",
"SPADES",
"DIAMONDS",
"HEARTS"
};

const char * color[] = {
"RED",
"BLACK"
};

struct Card{
Suit suit;
Color color;
char Honor[2];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The struct is not composed as given in the task.

};

void PrintHand(Player pl ,Card deck[],int cardsInDeck){

for(size_t i =0;i< pl.cardsInHand;i++){
std::cout<<suits[deck[pl.hand[i]].suit]<<' ';
std::cout<<color[deck[pl.hand[i]].color]<<' ';
std::cout<<deck[pl.hand[i]].Honor[0]<<deck[pl.hand[i]].Honor[1]<<' '<<std::endl;
}
}

int main()
{
// Providing a seed value
srand((unsigned) time(NULL));
Comment on lines +60 to +61
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix line indent.

const size_t numberOfCardsInDeck = 52;
Card deck[52] = {
{HEARTS, RED, '2'}, {HEARTS, RED, '3'}, {HEARTS, RED, '4'},
{HEARTS, RED, '5'}, {HEARTS, RED, '6'}, {HEARTS, RED, '7'},
{HEARTS, RED, '8'}, {HEARTS, RED, '9'}, {HEARTS, RED, '1','0'},
{HEARTS, RED, 'J'}, {HEARTS, RED, 'Q'}, {HEARTS, RED, 'K'},
{HEARTS, RED, 'A'},
{DIAMONDS, RED, '2'}, {DIAMONDS, RED, '3'}, {DIAMONDS, RED, '4'},
{DIAMONDS, RED, '5'}, {DIAMONDS, RED, '6'}, {DIAMONDS, RED, '7'},
{DIAMONDS, RED, '8'}, {DIAMONDS, RED, '9'}, {DIAMONDS, RED, '1','0'},
{DIAMONDS, RED, 'J'}, {DIAMONDS, RED, 'Q'}, {DIAMONDS, RED, 'K'},
{DIAMONDS, RED, 'A'},
{CLUBS, BLACK, '2'}, {CLUBS, BLACK, '3'}, {CLUBS, BLACK, '4'},
{CLUBS, BLACK, '5'}, {CLUBS, BLACK, '6'}, {CLUBS, BLACK, '7'},
{CLUBS, BLACK, '8'}, {CLUBS, BLACK, '9'}, {CLUBS, BLACK, '1','0'},
{CLUBS, BLACK, 'J'}, {CLUBS, BLACK, 'Q'}, {CLUBS, BLACK, 'K'},
{CLUBS, BLACK, 'A'},
{SPADES, BLACK, '2'}, {SPADES, BLACK, '3'}, {SPADES, BLACK,'4'},
{SPADES, BLACK, '5'}, {SPADES, BLACK, '6'}, {SPADES, BLACK,'7'},
{SPADES, BLACK, '8'}, {SPADES, BLACK, '9'}, {SPADES, BLACK, '1','0'},
{SPADES, BLACK, 'J'}, {SPADES, BLACK, 'Q'}, {SPADES, BLACK, 'K'},
{SPADES, BLACK, 'A'}
};
Comment on lines +62 to +84
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be a constant variable outside of main.

Player player1;
Player player2;
Player player3;
Player player4;
int currentCard = 0;
while(player1.cardsInHand+player2.cardsInHand+player3.cardsInHand+player4.cardsInHand != numberOfCardsInDeck){
int random = rangeRandomAlg2(1,4);
switch (random)
{
case 1:
if(player1.cardsInHand == 13)break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix line indent.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the body of the if statement to do a continue instead of break and move currentCard += 1 to the bottom of the while. This way you don't have to type it for every player and you are guaranteed that if a player has been dealt a card you can increment.

player1.hand[player1.cardsInHand] = currentCard;
player1.cardsInHand +=1;currentCard +=1;
break;
case 2:
if(player2.cardsInHand == 13)break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix line indent.

player2.hand[player2.cardsInHand] = currentCard;
player2.cardsInHand +=1;currentCard +=1;
break;
case 3:
if(player3.cardsInHand == 13)break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix line indent.

player3.hand[player3.cardsInHand] = currentCard;
player3.cardsInHand +=1;currentCard +=1;
break;
case 4:
if(player4.cardsInHand == 13)break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix line indent.

player4.hand[player4.cardsInHand] = currentCard;
player4.cardsInHand += 1;currentCard +=1;
break;
default:
break;
}
}
PrintHand(player1,deck,numberOfCardsInDeck);
std::cout<<std::endl;
PrintHand(player2,deck,numberOfCardsInDeck);
std::cout<<std::endl;
PrintHand(player3,deck,numberOfCardsInDeck);
std::cout<<std::endl;
PrintHand(player4,deck,numberOfCardsInDeck);
return 0;
}
24 changes: 24 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad3/cards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
enum Suit{
CLUBS,
SPADES,
DIAMONDS,
HEARTS
};

enum Color{
RED,
BLACK
};

struct Card{
Suit suit;
Color color;
char Honor[2];
Card(){
Honor[0] = ' ';
Honor[1] = ' ';
}
};


52 changes: 52 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad3/cards.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
HEARTS RED 2
HEARTS RED 3
HEARTS RED 4
HEARTS RED 5
HEARTS RED 6
HEARTS RED 7
HEARTS RED 8
HEARTS RED 9
HEARTS RED 10
HEARTS RED J
HEARTS RED Q
HEARTS RED K
HEARTS RED A
DIAMONDS RED 2
DIAMONDS RED 3
DIAMONDS RED 4
DIAMONDS RED 5
DIAMONDS RED 6
DIAMONDS RED 7
DIAMONDS RED 8
DIAMONDS RED 9
DIAMONDS RED 10
DIAMONDS RED J
DIAMONDS RED Q
DIAMONDS RED K
DIAMONDS RED A
CLUBS BLACK 2
CLUBS BLACK 3
CLUBS BLACK 4
CLUBS BLACK 5
CLUBS BLACK 6
CLUBS BLACK 7
CLUBS BLACK 8
CLUBS BLACK 9
CLUBS BLACK 10
CLUBS BLACK J
CLUBS BLACK Q
CLUBS BLACK K
CLUBS BLACK A
SPADES BLACK 2
SPADES BLACK 3
SPADES BLACK 4
SPADES BLACK 5
SPADES BLACK 6
SPADES BLACK 7
SPADES BLACK 8
SPADES BLACK 9
SPADES BLACK 10
SPADES BLACK J
SPADES BLACK Q
SPADES BLACK K
SPADES BLACK A
40 changes: 40 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad3/player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "player.h"
#include "cards.h"
#include <iostream>
#include <fstream>

const char * suits[] = {
"CLUBS",
"SPADES",
"DIAMONDS",
"HEARTS"
};

const char * color[] = {
"RED",
"BLACK"
};

void PrintHand(const Player pl ,const Card deck[],const int cardsInDeck){

for(size_t i =0;i< pl.cardsInHand;i++){
std::cout<<suits[deck[pl.hand[i]].suit]<<' ';
std::cout<<color[deck[pl.hand[i]].color]<<' ';
std::cout<<deck[pl.hand[i]].Honor[0]<<deck[pl.hand[i]].Honor[1]<<' '<<std::endl;
}
}
void PrintCard(const Card card){
std::cout<<suits[card.suit]<<' '<<color[card.color]<<' '<<card.Honor[0]<<card.Honor[1]<<'\n';
}

void EnterCardInFile(const Player pl ,const Card deck[],const int cardsInDeck){

std::ofstream file("result.txt", std::ios::app);
for(size_t i =0;i< pl.cardsInHand;i++){
file<<color[deck[pl.hand[i]].color]<<' ';
file<<suits[deck[pl.hand[i]].suit]<<' ';
file<<deck[pl.hand[i]].Honor[0]<<deck[pl.hand[i]].Honor[1]<<' '<<std::endl;
}
file<<'\n';

}
11 changes: 11 additions & 0 deletions Practicum/Week 01/Solutions/borislavivanov/zad3/player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "cards.h"

struct Player{
int hand[13] = {0,};
int cardsInHand = 0;
};

void PrintHand(const Player pl ,const Card deck[],const int cardsInDeck);
void PrintCard(const Card card);
void EnterCardInFile(const Player pl ,const Card deck[],const int cardsInDeck);
Loading