Share mấy bài đã làm khi trước, chủ yếu tham khảo để làm các bài khác
1.Phân số :
Mã:
#include <iostream>
using namespace std;
struct phanso
{
int tu;
int mau;
};
//Tim uoc chung lon nhat 2 so
int ucln(int a, int b)
{
int i,uc;
for (i=1;i<=a;i++)
{
if (a%i==0&&b%i==0) uc=i;
}
return uc;
};
//Cong 2 phan so;
phanso cong(phanso a, phanso b)
{
phanso c;
c.tu=a.tu*b.mau+a.mau*b.tu;
c.mau=a.mau*b.mau;
return c;
}
//Tru 2 phan so
phanso tru(phanso a, phanso b)
{
phanso c;
c.tu=a.tu*b.mau-a.mau*b.tu;
c.mau=a.mau*b.mau;
return c;
}
//Nhan 2 phan so
phanso nhan(phanso a, phanso b)
{
phanso c;
c.tu=a.tu*b.tu;
c.mau=a.mau*b.mau;
return c;
}
//Chia 2 phan so
phanso chia(phanso a, phanso b)
{
phanso c;
c.tu=a.tu*b.mau;
c.mau=a.mau*b.tu;
return c;
}
void main()
{
phanso a,b,c;
char pheptoan;
int uoc_max;
//Nhap phan so thu nhat
cout<<"Nhap phan so thu 1..."<<endl;
cout<<"Tu so : "; cin>>a.tu;
do
{
if(a.mau==0) cout<<"\n Mau so phai #0";
cout<<"\n Mau so : "; cin>>a.mau;
}
while(a.mau==0);
cout<<endl;
//Nhap phan so thu 2
cout<<"Nhap phan so thu 2..."<<endl;
cout<<"Tu so : "; cin>>b.tu;
do
{
if(b.mau==0) cout<<"\n Mau so phai #0";
cout<<"\n Mau so : "; cin>>b.mau;
}
while(b.mau==0);
cout<<endl;
//Lua chon phep toan
cout<<"Lua chon phep toan (+,-,*,/).- Nhap 'e' de thoat.."<<endl;
cin>>pheptoan;
switch (pheptoan)
{
case '+':
c=cong(a,b);
break;
case '-':
c=tru(a,b);
break;
case '*':
c=nhan(a,b);
break;
case '/':
c=chia(a,b);
break;
default:
break;
}
//Toi gian phan so
uoc_max=ucln(c.tu, c.mau);
c.tu=c.tu/uoc_max;
c.mau=c.mau/uoc_max;
//Xuat ket qua
cout<<endl;
cout<<a.tu<<"/"<<a.mau<<" "<<pheptoan<<" "<<b.tu<<"/"<<b.mau<<" = "<<a.tu<<"/"<<c.mau<<endl;
}
2. Số phức:
Mã:
#include <iostream>
using namespace std;
struct sophuc
{
int thuc,ao;
};
void tong(sophuc a, sophuc b)
{
sophuc tg;
tg.thuc=a.thuc + b.thuc;
tg.ao= a.ao+b.ao;
cout<<"Tong la:("<<tg.thuc<<","<<tg.ao<<")";
}
void hieu(sophuc a, sophuc b)
{
sophuc tg;
tg.thuc=a.thuc-b.thuc;
tg.ao=a.ao-b.ao;
cout<<"Hieu 2 so phuc: ("<<tg.thuc<<","<<tg.ao<<")";
}
void tich(sophuc a, sophuc b)
{
sophuc tg;
tg.thuc=a.thuc * b.thuc;
tg.ao= a.ao * b.ao;
cout<<"Tich la:("<<tg.thuc<<","<<tg.ao<<")";
}
void thuong(sophuc a,sophuc b)
{
sophuc tg;
int tongbp=b.thuc*b.thuc+b.ao*b.ao;
tg.thuc=(a.thuc*a.ao+b.thuc*b.ao)/tongbp;
tg.ao=(a.ao*b.thuc-a.thuc*b.ao)/tongbp;
cout<<"Thuong 2 so phuc : ("<<tg.thuc<<","<<tg.ao<<")";
}
void main()
{
sophuc sp1,sp2;
cout<<"Nhap gia tri so phuc";
cout<<"So phuc thu 1:"<<endl;
cout<<"thuc";
cin>>sp1.thuc;
cout<<"ao";
cin>>sp1.ao;
cout<<"So phuc thu 2:"<<endl;
cout<<"thuc";
cin>>sp2.thuc;
cout<<"ao";
cin>>sp2.ao;
tong(sp1,sp2);
cout<<endl;
hieu(sp1,sp2);
cout<<endl;
tich(sp1, sp2);
cout<<endl;
thuong(sp1, sp1);
}
3. Danh sách sinh viên (struct)
Mã:
#include <iostream>
#include <string>
using namespace std;
struct sinhvien
{
char ten[50];
int tuoi;
char diachi[100];
};
sinhvien sv[50];
int n;
void nhap(sinhvien &sv)
{
cin.ignore();
cout<<"Ho ten..."; cin.getline(sv.ten,50);
cout<<"Tuoi ..."; cin>>sv.tuoi;
cin.ignore();
cout<<"Dia chi..."; cin.getline(sv.diachi,100);
cout<<endl;
};
void xuat(sinhvien sv)
{
cout<<sv.ten<<"\t"<<sv.tuoi<<"\t"<<sv.diachi;
};
void hoanvi(sinhvien a[1],sinhvien b[1])
{
sinhvien tmp[1];
tmp[0]=a[0];
a[0]=b[0];
b[0]=tmp[0];
};
void sapxep()
{
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(strcmp(sv[i].ten,sv[j].ten)>0)
hoanvi(&sv[i],&sv[j]);
return;
};
void main()
{
int n,i;
sinhvien sv[50];
cout<<"Nhap so luong sinh vien...";
cin>>n;
cout<<"Nhap thong tin sinh vien";
for(i=0;i<n;i++)
nhap(sv[i]);
cout<<"DS sinh vien da nhap..."<<endl;
for(i=0;i<n;i++)
{
xuat(sv[i]);
cout<<endl;
}
sapxep();
cout<<"Danh sach sinh vien da sap xep"<<endl;
for(i=0;i<n;i++)
{
xuat(sv[i]);
cout<<endl;
}
}
4. Con trỏ - mảng số nguyên:
Mã:
#include <iostream>
using namespace std;
//Ham nhap
void nhap(int **p, int n)
{
int i;int *q;
q=new int[n];
for(i=0;i<n;i++)
cin>>*(q+i);
*p=q;
};
//Ham xuat
void xuat(int *p, int n)
{
for(int i=0;i<n;i++)
cout<<*(p+i)<<"\t";
};
//Xoa 1 phan tu
void xoa(int *p, int &n,int k)
{
for(int i=k-1; i<n;i++)
*(p+i)=*(p+i+1);
n--;
};
//Them 1 phan tu
void chen(int *p, int &n, int x, int v)
{
for(int i=n;i>v-1;i--)
*(p+i)=*(p+i-1);
*(p+v-1)=x;
n++;
};
//Ham sap xep
void sapxep(int *p, int n)
{
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(*(p+i)>*(p+j))
{
int temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
};
void max_min(int *p, int n)
{
int max=*(p+0);
int min=*(p+0);
for(int i=0;i<n;i++)
{
if(max<*(p+i)) max=*(p+i);
if(min>*(p+i)) min=*(p+i);
}
cout<<"Max = "<<max<<endl;
cout<<"Min = "<<min<<endl;
}
void main()
{
int n,*p,k,v,x;
//Nhap
cout<<"Nhap so phan tu day(n)"<<endl;
cin>>n;
p=new int [n];
cout<<"Nhap gia tri phan tu"<<endl;
nhap(&p,n);
cout<<"Day sau khi nhap"<<endl;
xuat(p,n);
cout<<endl;
//Max- min
max_min(p,n);
cout<<endl;
//Xoa
cout<<"Nhap vi tri can xoa..."; cin>>k;
xoa(p,n,k);
cout<<"Day sau khi xoa"<<endl;
xuat(p,n);
cout<<endl;
//Them
cout<<"Nhap vi tri can chen"; cin>>v;
cout<<"\n Nhap gia tri can chen"; cin>>x;
chen(p,n,x,v);
cout<<"\n Day sau khi chen"<<endl;
xuat(p,n);
sapxep(p,n);
cout<<"\n Day sua khi sap xep"<<endl;
xuat(p,n);
}