Pretvaranje iz dekadskog u heksadecimalni
#include<iostream> using namespace std; int main () { int niz[8]={0,0,0,0,0,0,0,0}; int broj; char x; cout << "Unesite broj:"; cin >> broj; for (int i=7; i>=0; i--) { niz[i]=broj%16; broj=broj/16; if (broj==0) { break; } } for (int i=0; i<8; i++) { if (niz[i]<10) { cout << niz[i] << " "; } else { switch (niz[i]) { case 10: x='a'; break; case 11: x='b'; break; case 12: x='c'; break; case 13: x='d'; break; case 14: x='e'; break; case 15: x='f'; break; } cout << x << " "; } } cout << endl; system("pause"); return 0; }
Pretvaranje iz dekadskog u heksadecimalni – bez nizova
#include<iostream> #include<windows.h> using namespace std; //funkcija za postavljanje kursora unutar terminala void gotoxy(int x, int y) { COORD pos = {x, y}; HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(output, pos); } int main () { unsigned long unos; unsigned long broj=1; int brojac=1; int x; int y=1; cout << "unesite broj: "; cin >> unos; while (1) { broj=broj*16; if (broj > unos){ break; } brojac++; } x=4+brojac; cout << "hex: "; while (1) { broj=unos%16; unos=unos/16; if (broj<10) { gotoxy(x,y); cout << broj; } else { gotoxy(x,y); cout << char(broj+55); } if (unos==0) {break;} x=x-1; } cout << endl; system("pause"); return 0; }
Prosti brojevi
#include<iostream> #include<cmath> using namespace std; int main () { int n; bool test=false; cout << "Unesite broj: "; cin >> n; if ((n==2) || (n==3)){ test=true; } if ((n>3) && (n%2!=0)) { test=true; for (int i=3; i<sqrt(float(n))+1; i=i+2) { if (n%i==0) { test=false; break; } } } if (test==true) { cout << n << " je prosti broj" << endl; } else { cout << n << " nije prosti broj" << endl; } system("pause"); return 0; }
Funkcija za iscrtavanje vješala
#include<iostream> using namespace std; //Funkcija za crtanje vjesala void vjesala (int kazna, int igrac) { char niz[9]={' ','o',' ','/',char(124),char(92),'/',' ',char(92)}; int k=0; int i=0; int j=0; int redak=2; cout << endl; cout << "Igrac: " << igrac << endl; cout << "________" << endl; cout << "|/ "; while (1) { cout << niz[i]; if (niz[i]!=' ') { j++;} if (i==redak){ cout << endl; cout << "| "; redak=redak+3; } if (kazna==j) { break; } i++; } //crtanje vertikalne linije if (kazna<2) {k=3; } else if (kazna<5) {k=2; } else if (kazna<6) {k=1; } cout << endl; for (i=0; i<k; i++) { cout << "| " << endl;} } int main () { //pozivanje funkcije vjesala(tezina kazne, broj igraca) vjesala(6,2); cout << endl; system("pause"); return 0; }
Križić kružić
#include<iostream> using namespace std; char niz[9]={'1','2','3','4','5','6','7','8','9'}; void ploca () { system("cls"); cout << endl; cout << " Krizic kruzic" << endl; cout << endl; cout << "Igrac 1: X | Igrac 2: O" << endl; cout << endl; for (int i=0; i<9; i=i+3) { cout <<" " <<niz[i] <<" | "<< niz[i+1]<<" | " << niz[i+2] << endl; if (i<6){ cout <<" ---|---|---" << endl; } } } void unos(int broj, int igrac) { bool test=false; if ((broj >=1) && (broj<=9)) { for (int i=0; i<9; i++) { if (niz[i]==char(broj+48)) { test=true; break;} } } if (test==true){ if (igrac==1) { niz[broj-1]='X';} else {niz[broj-1]='O';} ploca();} else { cout << "Pogresan unos!" << endl; } } int main () { int broj; int igrac=1; ploca(); while (1) { cout << endl; cout << "Igrac: " << igrac << endl; cout << "Unesite broj: "; cin >> broj; if (broj==0) { break; } else { unos(broj,igrac); if (igrac==1) igrac++; else igrac--; } } system("pause"); return 0; }