L_04 - maski sieci w systemie dziesiętnym i dwójkowym.
Program jest przydatny przy dobieraniu masek dla sieci. Jego kod jest bardzo prosty. Rozpoczynamy od utworzenia nowego projektu c++, dodaniu pliku widget oraz utworzeniu formatki jak poniżej.
W polach dziesiętnych zmieniłem nazwy z lineEdit na p1 do p4, a w pozostałych polach jak poniżej:
Dla pola pierwszego nazwy pól binarnych są oznaczone a7 do a0
Dla pola drugiego nazwy pól binarnych są oznaczone b7 do b0
Dla pola trzeciego nazwy pól binarnych są oznaczone c7 do c0
Dla pola czwartego nazwy pól binarnych są oznaczone d7 do d0
Następnie tworzymy sloty i połączenia.
Dodajemy nowy plik main.cpp oraz definiujemy sloty w pliku form1.ui.h.
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
void Form1::przelicz(){
int liczba1,liczba2,liczba3,liczba4,i=0,j=0,k=0,l=0,x[8],y[8],w[8],z[8];
//------------------------------------------------------------------------
liczba1=atof(p1->text());
if(liczba1>=0 && liczba1<=255){
do{
x[i]=liczba1%2;
liczba1=liczba1/2;
i++;
}while(liczba1!=0);
if(x[7]==1){a7->setText(QString::number(x[7]));}else{a7->setText("0");}
if(x[6]==1){a6->setText(QString::number(x[6]));}else{a6->setText("0");}
if(x[5]==1){a5->setText(QString::number(x[5]));}else{a5->setText("0");}
if(x[4]==1){a4->setText(QString::number(x[4]));}else{a4->setText("0");}
if(x[3]==1){a3->setText(QString::number(x[3]));}else{a3->setText("0");}
if(x[2]==1){a2->setText(QString::number(x[2]));}else{a2->setText("0");}
if(x[1]==1){a1->setText(QString::number(x[1]));}else{a1->setText("0");}
if(x[0]==1){a0->setText(QString::number(x[0]));}else{a0->setText("0");}
}
else
{
p1->setText("od 0-255");
}
//------------------------------------------------------------------------
liczba2=atof(p2->text());
if(liczba2>=0 && liczba2<=255){
do{
y[j]=liczba2%2;
liczba2=liczba2/2;
j++;
}while(liczba2!=0);
if(y[7]==1){b7->setText(QString::number(y[7]));}else{b7->setText("0");}
if(y[6]==1){b6->setText(QString::number(y[6]));}else{b6->setText("0");}
if(y[5]==1){b5->setText(QString::number(y[5]));}else{b5->setText("0");}
if(y[4]==1){b4->setText(QString::number(y[4]));}else{b4->setText("0");}
if(y[3]==1){b3->setText(QString::number(y[3]));}else{b3->setText("0");}
if(y[2]==1){b2->setText(QString::number(y[2]));}else{b2->setText("0");}
if(y[1]==1){b1->setText(QString::number(y[1]));}else{b1->setText("0");}
if(y[0]==1){b0->setText(QString::number(y[0]));}else{b0->setText("0");}
}
else
{
p2->setText("od 0-255");
}
//------------------------------------------------------------------------
liczba3=atof(p3->text());
if(liczba3>=0 && liczba3<=255){
do{
z[k]=liczba3%2;
liczba3=liczba3/2;
k++;
}while(liczba3!=0);
if(z[7]==1){c7->setText(QString::number(z[7]));}else{c7->setText("0");}
if(z[6]==1){c6->setText(QString::number(z[6]));}else{c6->setText("0");}
if(z[5]==1){c5->setText(QString::number(z[5]));}else{c5->setText("0");}
if(z[4]==1){c4->setText(QString::number(z[4]));}else{c4->setText("0");}
if(z[3]==1){c3->setText(QString::number(z[3]));}else{c3->setText("0");}
if(z[2]==1){c2->setText(QString::number(z[2]));}else{c2->setText("0");}
if(z[1]==1){c1->setText(QString::number(z[1]));}else{c1->setText("0");}
if(z[0]==1){c0->setText(QString::number(z[0]));}else{c0->setText("0");}
}
else
{
p3->setText("od 0-255");
}
//------------------------------------------------------------------------
liczba4=atof(p4->text());
if(liczba4>=0 && liczba4<=255){
do{
w[l]=liczba4%2;
liczba4=liczba4/2;
l++;
}while(liczba4!=0);
if(w[7]==1){d7->setText(QString::number(w[7]));}else{d7->setText("0");}
if(w[6]==1){d6->setText(QString::number(w[6]));}else{d6->setText("0");}
if(w[5]==1){d5->setText(QString::number(w[5]));}else{d5->setText("0");}
if(w[4]==1){d4->setText(QString::number(w[4]));}else{d4->setText("0");}
if(w[3]==1){d3->setText(QString::number(w[3]));}else{d3->setText("0");}
if(w[2]==1){d2->setText(QString::number(w[2]));}else{d2->setText("0");}
if(w[1]==1){d1->setText(QString::number(w[1]));}else{d1->setText("0");}
if(w[0]==1){d0->setText(QString::number(w[0]));}else{d0->setText("0");}
}
else
{
p4->setText("od 0-255");
}
//------------------------------------------------------------------------
}
void Form1::wyczysc(){
p1->setText("0");
a7->setText("0");
a6->setText("0");
a5->setText("0");
a4->setText("0");
a3->setText("0");
a2->setText("0");
a1->setText("0");
a0->setText("0");
p2->setText("0");
b7->setText("0");
b6->setText("0");
b5->setText("0");
b4->setText("0");
b3->setText("0");
b2->setText("0");
b1->setText("0");
b0->setText("0");
p3->setText("0");
c7->setText("0");
c6->setText("0");
c5->setText("0");
c4->setText("0");
c3->setText("0");
c2->setText("0");
c1->setText("0");
c0->setText("0");
p4->setText("0");
d7->setText("0");
d6->setText("0");
d5->setText("0");
d4->setText("0");
d3->setText("0");
d2->setText("0");
d1->setText("0");
d0->setText("0");
}
Kompilujemy i uruchamiamy program 04.
Widzimy, że maska dla tej sieci wynosi /16.
Źródła tego programu można pobrać stąd.
do góry