54. Berilgan ikkita qiymatni o’zaro almashtirish funksiyasini funksiya shabloni orqali tuzing.
#include
using namespace std;
template
class Change{
T x;
T y;
public:
Change(T a, Tb){
x = a;
y = b;
}
void charge()
{
T z=x;
x=y;
y=z;
cout<<"x= "<}
};
int main()
{
T x,y;
cout<<"x : ";cin>>x;cout<<"y : ";cin>>y;
Charge A(x,y);
cout<}
|
78. Ikki xonali son berilgan. Foydalanuvchi bu sonning ikkilik sanoq sistemasidagi ko’rinishini yozadi. Boshqa sanoq sistemadagi har qanday son istisnoni generatsiya qilishga olib keladi.
#include
using namespace std;
class Binary{
int dec_number;
string bin_number;
public:
class Bin{ };//istisno sinf
Binary(){
dec_number=0;
bin_number="";
}
Binary(int n,string o){
dec_number=n;
bin_number=o;
}
numCheck(){
string s,a;
int n=dec_number;
while(n>0){
if(n%2 == 0){
s+='0';
}else{
s+='1';
}
n/=2;
//cout<}
for(int i=s.length()-1; i>=0; i--){
a+=s[i];
//cout <}
if(a != bin_number){
throw Bin();
}
}
string getBin(){
return bin_number;
}
};
int main()
{
int n; string s;
cout << "Sonni kiriting: ";
cin >> n;
cout << "Shu sonning ikkilik sanoq sistemasidagi ko'rinishini kiriting: ";
cin >> s;
try{
Binary bin(n,s);
bin.numCheck();
cerr << bin.getBin();
}
catch(Binary::Bin){
cout << "Noto'g'ri sanoq sistemasidagi son kiritildi: " <}
return 0;
}
|
95. “A” futbol klubining 30 ta o’yinda to’plagan ochkolari quyidagi ko’rinishda faylda yozilgan:
3 0 0 0 0 1 0 0 0 3 0 0 3 3 3 3 3 1 1 1 0 0 0 0 0 0 0 3 3 3 …
3 – g’alaba
1 – during
0 – mag’lubiyat.
Quyidagilarni aniqlash uchun sinf hosil qiling:
1) Jamoaning umumiy ochkosini aniqlash metodi
2) Jami g’alabalar sonini aniqlash metodi
3) Jami duranglar sonini aniqlash metodi
4) Jami mag’lubiyatlar sonini aniqlash metodi
#include
#include
#define LINER getline
using namespace std;
class Football_club{
fstream file;
string path;
public:
int elm;
class fileOpen{};;
Football_club(string str){
path = str;
file.open(str);
if(!file.is_open())
throw fileOpen();
}
void newFile(string str){
path = str;
file.close();
fOpen();
}
void fOpen(){
file.open(path);
if(!file.is_open())
throw fileOpen();
}
int allElm(){
int elmt=0;
while(!file.eof()){
file>>elm;
if(elm==3 or elm==1 or elm==0){
elmt++;
}
}
file.close();
fOpen();
return elmt;
}
int allVictory(){
int victory = 0;
while(!file.eof()){
file>>elm;
if(elm==3)
victory++;
}
file.close();
fOpen();
return victory;
}
int allDraw(){
int draw = 0;
while(!file.eof()){
file>>elm;
if(elm==1)
draw++;
}
file.close();
fOpen();
return draw;
}
int allDefeat(){
int defeat = 0;
while(!file.eof()){
file>>elm;
if(elm==0)
defeat++;
}
file.close();
fOpen();
return defeat;
}
int allPoint(){
int point = 0;
while(!file.eof()){
file>>elm;
if(elm==3 or elm==1)
point++;
}
file.close();
fOpen();
return point;
}
float Statistics(){
int maxs =allElm();
int defeat = allDefeat();
if(maxs!=0)
return (defeat*100/maxs);
else
return 0;
}
};
int main()
{
try{
Football_club A("D:/club.txt");
cout<}catch(Football_club::fileOpen){
cout<<"Football club faylini ochishda xatolik"<}
return 0;
}
|
Do'stlaringiz bilan baham: |