Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <vector>
- #include <list>
- using namespace std;
- //Prototypy
- void WyborZadania();
- void Zad1();
- void Zad2();
- int Zad3a(int *a, int *b);
- int *Zad3b(int *a, int *b);
- int Zad4(int *tab, int n);
- int Zad5(int *tab1, int *tab2, int n);
- int main(){
- WyborZadania();
- return EXIT_SUCCESS;
- }
- void Zad1() {
- int n, x;
- cout << "Podaj n:"; cin >> n;
- int *tab = new int[n];
- for (int i = 0; i < n; i++) {
- cout << i + 1 << ": "; cin >> x;
- if (!isdigit(x)) tab[i] = x;
- else i--;
- }
- for(int i = 0; i < n; i++){
- cout << tab[i] << ", ";
- }
- }
- void Zad2(){
- }
- int Zad3a(int *a, int *b){
- if(*a < *b) return *a;
- else return *b;
- }
- int *Zad3b(int *a, int *b){
- if(*a < *b) return a;
- else return b;
- }
- int Zad4(int *tab, int n){
- int suma = 0;
- for(int i = 0; i < n; i++){
- suma += tab[i];
- }
- return suma/n;
- }
- int Zad5(int *tab1, int *tab2, int n){
- int suma = 0;
- for(int i = 0; i < n; i++){
- cout << "(" <<tab1[i] << " - " << tab2[i] << ")^2 = " << (tab1[i] - tab2[i])*(tab1[i] - tab2[i]) << endl;
- suma += (tab1[i] - tab2[i])*(tab1[i] - tab2[i]);
- }
- return suma;
- }
- class portfel{
- private:
- double kwota;
- public:
- portfel(){
- this->inicjuj();
- }
- void inicjuj(){
- this->kwota = 0;
- }
- void zarobki(double ilosc){
- this->kwota += ilosc;
- }
- void wydatki(double ilosc){
- this->kwota -= ilosc;
- }
- double zawartosc(){
- return this->kwota;
- }
- };
- class tablica{
- private:
- int * tab;
- int n;
- public:
- tablica(int n){
- this->n = n;
- tab = new int[n];
- for(int i = 0; i < n; i++){
- tab[i] = rand() % 100 + 1;
- cout << tab[i] << ", ";
- }
- }
- int & at(int indeks){
- if(indeks > n) {int & ref = tab[0]; return ref; }
- else{
- for(int i = 0; i < n; i++){
- if(i == indeks){
- int & ref = tab[i];
- return ref;
- }
- }
- }
- }
- };
- class wskaznik{
- private:
- int * wsk;
- public:
- wskaznik(int n){
- this->utworz(n);
- }
- void utworz(int n){
- if(n > 0){
- int * tab = new int[n];
- this->wsk = tab;
- }
- }
- int *zwroc(){
- return this->wsk;
- }
- void zwolnij(){
- this->wsk = NULL;
- }
- void kopiuj(int* &ref){
- ref = this->wsk;
- }
- };
- class czworokat{
- protected:
- double a, b, c, d;
- public:
- czworokat(double a, double b, double c, double d){
- this->a = a;
- this->b = b;
- this->c = c;
- this->d = d;
- }
- void wypisz(){
- cout << a << ", " << b << ", " << c << ", " << d << endl;
- }
- virtual double pole(){
- return a*b*c*d;
- }
- };
- class prostokat : public czworokat{
- public:
- prostokat(double x1, double x2) : czworokat(x1, x1, x2, x2){}
- void wymiary(double w1, double w2){
- czworokat::a = w1;
- czworokat::c = w1;
- czworokat::b = w2;
- czworokat::d = w2;
- }
- double pole(){
- return a * c;
- }
- };
- class kwadrat : public czworokat{
- public:
- kwadrat(double x1) : czworokat(x1, x1, x1, x1){}
- void wymiary(double w1){
- czworokat::a = w1;
- czworokat::b = w1;
- czworokat::c = w1;
- czworokat::d = w1;
- }
- double pole(){
- return a * a;
- }
- };
- void WyborZadania() {
- int zad;
- cout << "Wybierz zadanie: ";
- cin >> zad;
- switch (zad) {
- case 1:
- {
- Zad1();
- break;
- }
- case 2:
- {
- Zad2();
- break;
- }
- case 3:
- {
- int x = 10;
- int y = 15;
- int *pX = &x;
- int *pY = &y;
- cout << Zad3a(pX, pY) << endl;
- cout << *Zad3b(pX, pY);
- break;
- }
- case 4:
- {
- int n = 10;
- int *tab = new int[n];
- int *tab2 = new int[n];
- srand(time(NULL));
- for(int i = 0; i < n; i++){
- tab[i] = rand() % 100 + 1;
- cout << tab[i] << ", ";
- }
- cout << endl;
- for(int i = 0; i < n; i++){
- tab2[i] = rand() % 100 + 1;
- cout << tab2[i] << ", ";
- }
- cout << endl << "Srednia: " << Zad4(tab, n) << endl << "Srednia 2: " <<Zad4(tab2, n) << endl;
- break;
- }
- case 5:
- {
- int n = 10;
- int *tab = new int[n];
- int *tab2 = new int[n];
- srand(time(NULL));
- for(int i = 0; i < n; i++){
- tab[i] = rand() % 100 + 1;
- cout << tab[i] << ", ";
- }
- cout << endl;
- for(int i = 0; i < n; i++){
- tab2[i] = rand() % 100 + 1;
- cout << tab2[i] << ", ";
- }
- cout << endl << Zad5(tab, tab2, n) << endl;
- break;
- }
- case 6:
- {
- portfel x;
- x.zarobki(500);
- x.wydatki(150);
- cout << x.zawartosc();
- break;
- }
- case 7:
- {
- tablica t(10);
- cout << endl << t.at(5) << ", " << t.at(15);
- break;
- }
- case 8:
- {
- //(int*) &ref;
- wskaznik x(10);
- cout << x.zwroc();
- //x.kopiuj(ref); //nie dziala chuj wie czemu
- x.zwolnij();
- break;
- }
- case 9:
- {
- //Normalnie
- czworokat x(5,10,15,20);
- x.wypisz();
- cout << x.pole() << endl;
- prostokat y(10, 20);
- y.wypisz();
- cout << y.pole() << endl;
- kwadrat z(10);
- z.wypisz();
- cout << z.pole() << endl;
- cout << "--------" << endl;
- //Na wskaznikach
- czworokat * a = new czworokat(5,10,15,20);
- a->wypisz();
- cout << a->pole() << endl;
- prostokat * b = new prostokat(10,20);
- b->wypisz();
- cout << b->pole() << endl;
- kwadrat * c = new kwadrat(10);
- c->wypisz();
- cout << c->pole() << endl;
- break;
- }
- case 10:
- {
- list <int> lista;
- int el;
- cout << "Ile elementow chcesz dodac do listy? "; cin >> el;
- for(int i = 0; i < el; i++){
- int x;
- cout << i + 1 << ": "; cin >> x;
- lista.push_back(x);
- }
- //Wyswietlanie listy
- for(list<int>::iterator i = lista.begin(); i != lista.end(); i++){
- cout << *i << ", ";
- }
- cout << endl;
- //Usuwanie
- int del, j;
- cout << "Ktory element chcesz usunac? "; cin >> del;
- for(list<int>::iterator i = lista.begin(); i != lista.end(); i++, j++){
- if(j==del%lista.size()){
- i = lista.erase(i); break;
- }
- }
- //Wyswietlanie listy 2
- for(list<int>::iterator i = lista.begin(); i != lista.end(); i++){
- cout << *i << ", ";
- }
- break;
- }
- default:
- {
- WyborZadania();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement