Skip to content

Instantly share code, notes, and snippets.

@kbkk
Created March 1, 2019 16:17
Show Gist options
  • Save kbkk/72d3f186c6348415dfb1ae4a8d215ea2 to your computer and use it in GitHub Desktop.
Save kbkk/72d3f186c6348415dfb1ae4a8d215ea2 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include "zadanie3.h"
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
std::vector<int> przemnoz(const std::vector<int> &idVec)
{
std::vector<int> resultVec;
for (int i = 0; i < idVec.size(); i++)
{
auto value = idVec.at(i);
auto multiplier = 1;
for (int j = 0; j < i; j++) {
multiplier *= idVec.at(j);
}
resultVec.push_back(value * multiplier);
}
return resultVec;
}
void zadanie1()
{
std::fstream file("./numer_indeksu.txt");
std::vector<int> idVec;
int i;
while (file >> i) {
idVec.push_back(i);
}
auto resultVec = przemnoz(idVec);
std::fstream outFile("./zadanie1.txt", std::ios_base::out);
for (auto &value : resultVec)
{
outFile << value << ' ';
}
outFile.flush();
outFile.close();
}
double fun(int n)
{
if (n == 1) {
return 100;
}
return (fun(n - 1) / 10) + 10 + n;
}
void zadanie2()
{
int input;
std::cout << "podaj n: " << std::endl;
std::cin >> input;
auto result = fun(input);
std::cout << "wynik: " << result << std::endl;
}
samochod wczytaj_zad3()
{
samochod sam; int temp;
std::cout << "Podaj wiek: ";
std::cin >> temp;
sam.wiek = temp;
std::cout << "Podaj cene: ";
std::cin >> temp;
sam.cena = temp;
std::cout << "Podaj przebieg: ";
std::cin >> temp;
sam.przebieg = temp;
return sam;
}
double atrak(const samochod &sam)
{
return 1000 / (sam.cena * sam.wiek * sam.przebieg);
}
void zadanie3()
{
const int CARS_AMOUNT = 4;
std::vector<samochod> samVec;
for (int i = 0; i < CARS_AMOUNT; i++) {
samVec.push_back(wczytaj_zad3());
}
for (auto &sam : samVec)
{
std::cout << "Wiek: " << sam.wiek << std::endl;
std::cout << "Cena: " << sam.cena << std::endl;
std::cout << "Przebieg: " << sam.przebieg << std::endl;
std::cout << "Atrakcyjnosc: " << atrak(sam) << std::endl;
std::cout << std::endl;
}
}
void printVec(const std::vector<int> &v)
{
for (auto &value : v)
{
std::cout << value << std::endl;
}
}
void zadanie4()
{
std::vector<int> v;
int input, temp;
while (std::cin >> input)
{
if (input != 1) {
v.push_back(input);
continue;
}
std::fstream s("./wynik.bin", std::ios_base::binary | std::ios_base::trunc | std::ios_base::in | std::ios_base::out);
printVec(v);
// write
for (auto &value : v)
{
s.write(reinterpret_cast<char*>(&v), sizeof v);
}
s.flush();
// read
s.seekp(0);
v.clear();
while (s >> temp) {
v.push_back(temp);
}
printVec(v);
}
}
int main()
{
//zadanie1();
//zadanie2();
//zadanie3();
zadanie4();
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment