You are on page 1of 8

Vit chng trnh: Nhp 2 s phc a,b.

in ra kt qu ca cc php ton a+b, a-b, a*b, a/b


1. Khai bo cu trc s phc
2. Vit hm nhp 1 s phc
Vit hm in ra 1 s phc
3. Vit hm tnh tng 2 sp ->1sp
Vit hm tnh hiu 2 sp ->1sp
Vit hm tnh tch 2 sp ->1sp
Vit hm tnh thng 2 sp ->1sp
#include <iostream.h>
struct sp{
float t,a;
};
void nhap(sp &c){
cout<<"nhap phan thuc: ";
cin>>c.t;
cout<<"nhap phan ao: ";
cin>>c.a;
}
void xuat(sp c){
cout<<c.t<<(c.a>0?"+":"")<<c.a<<"i"<<endl;
}
sp cong(sp c1, sp c2){
sp temp;
temp.t=c1.t+c2.t;
temp.a=c1.a+c2.a;
return temp;
}
sp tru(sp c1, sp c2){
c2.t=-c2.t;
c2.a=-c2.a;
return cong(c1,c2);
}
sp nhan(sp c1, sp c2){

sp temp;
temp.t=c1.t*c2.t-c1.a*c2.a;
temp.a=c1.t*c2.a+c2.t*c1.a;
return temp;
}
sp lienhop(sp c){
sp lh;
lh.t=c.t;
lh.a=-c.a;
return lh;
}
sp chia(sp c1, sp c2){
sp lhc2,temp,mau;
lhc2=lienhop(c2);
temp=nhan(c1,lhc2);
mau=nhan(c2,lhc2);
temp.t/=mau.t;
temp.a/=mau.t;
return temp;
}
void main(){
sp x,y,z;
cout<<"Nhap sp thu nhat: ";
nhap(x);
cout<<"Nhap sp thu hai: ";
nhap(y);
cout<<"x+y=";z=cong(x,y);xuat(z);
cout<<"x-y=";z=tru(x,y);xuat(z);
cout<<"x*y=";z=nhan(x,y);xuat(z);
cout<<"x/y=";z=chia(x,y);xuat(z);
}

Cho cu trc vect nh sau:

struct vecto{
float x,y;
}
Nhp n vecto, in ra nhng cp vung gc vi nhau.
#include <iostream.h>
struct vecto{
float x,y;
};
void nhap(vecto v[], int &n){
cout<<"So luong vecto can nhap:";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Nhap 2 tp cua v["<<i<<"]=";
cin>>v[i].x>>v[i].y;
}
}
int vuonggoc(vecto v1,vecto v2){
return (v1.x*v2.x+v1.y*v2.y)==0;
}
void lietke(vecto v[], int n){
for(int i=0;i<n-1;i++)
for(int k=i+1;k<n;k++)
if(vuonggoc(v[i],v[k]))
cout<<"cap ("
<<v[i].x<<","<<v[i].y
<<"),("
<<v[k].x<<","<<v[k].y
<<") vuong goc"<<endl;
}
void main(){
vecto v[100];
int n;
nhap(v,n);

lietke(v,n);
cout<<"Het roai!";
}
Cho cu trc
struct thisinh{
int

sbd;

//S bo danh

char

hoten[25];

//H v tn

float m1,m2,m3;

//im ba mn thi

};
Vit chng trnh (c s dng cc hm)
a) Nhp thng tin n sinh vin
b) In ra mn hnh danh sch sp xp gim dn theo tng im
c) In ds sv trng tuyn vi im chun nhp t bn phm
(trng tuyn: k b mn no im 0, tng im k b hn im chun)

Cch lm:
Khai bo cu trc
Khai bo hm nhp : nhp soluong, nhp tng sv ( s lng th STOP)
Hm sp xp: gim dn theo tng im
Hm Lit k ton b
Hm Lit k nhng SV trng tuyn(tha k trng tuyn)

Hm main:
kb bin mng , bin s lng
gi cc hm trn l XONG!
#include <iostream.h>
struct ts{

};

int

sbd;

char

hoten[25];

float

m1,m2,m3;

void nhap(ts a[], int &n){


cout<<"nhap so luong thi sinh:";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Nhap thong tin thi sinh thu "<<(i+1)<<endl;
cout<<" So bao danh: ";cin>>a[i].sbd;
cin.ignore(1);
cout<<" Ho va ten: ";cin.getline(a[i].hoten,25);
cout<<" diem 3 mon: ";cin>>a[i].m1>>a[i].m2>>a[i].m3;
}
}
void sapxep(ts a[], int n){
for(int i=0;i<n-1;i++)
for(int k=i+1;k<n;k++)
if(a[i].m1+a[i].m2+a[i].m3<a[k].m1+a[k].m2+a[k].m3)
{ts t=a[i];a[i]=a[k];a[k]=t;}
}
void lietke(ts a[], int n){
cout<<"Danh sach thi sinh (sx giam dan theo tongn diem)"<<endl;
for(int i=0;i<n;i++)
cout<<a[i].sbd<<" "<<a[i].hoten<<"
"<<(a[i].m1+a[i].m2+a[i].m3)<<endl;
}
void lietkeTrungTuyen(ts a[], int n){
float diemchuan;
cout<<"Nhap diem chuan: ";
cin>>diemchuan;
cout<<"Danh sach thi sinh trung tuyen:"<<endl;
for(int i=0;i<n;i++)
if(a[i].m1+a[i].m2+a[i].m3>=diemchuan && a[i].m1>0 && a[i].m2>0
&& a[i].m3>0)
cout<<a[i].sbd<<" "<<a[i].hoten<<"
"<<(a[i].m1+a[i].m2+a[i].m3)<<endl;
}
void main(){
ts pt1[100];

int sl;
nhap(pt1,sl);
sapxep(pt1,sl);
lietke(pt1,sl);
lietkeTrungTuyen(pt1,sl);
Cho cu trc phn s gm 2 s nguyn t v mu.
Nhp n phn s, lit k cc phn s dng ti gin
#include <iostream.h>
struct ps{
int tu,mau;
};
void nhap(ps a[], int &n){
cout<<"nhap so luong phan so: ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Nhap phan so thu "<<(i+1)<<endl;
cout<<" tu : ";cin>>a[i].tu;
cout<<" mau: ";cin>>a[i].mau;
}
}
int toigian(ps x){
while(x.tu!=x.mau)
if(x.tu>x.mau)x.tu-=x.mau;
else x.mau-=x.tu;
return x.tu==1;
}
void lietke(ps a[], int n){
cout<<"Cac ps tg:";
for(int i=0;i<n;i++)
if(toigian(a[i]))
cout<<a[i].tu<<"/"<<a[i].mau<<" ";
}
void main(){

ps w[100];
int m;
nhap(w,m);
lietke(w,m);
}
Cho cu trc phn s gm 2 s nguyn t v mu.
Nhp n phn s, lit k, tnh tng cc phn s dng ti gin y/c tng l ps dng ti gin
#include <iostream.h>
struct ps{
int tu,mau;
};
void nhap(ps a[], int &n){
cout<<"nhap so luong phan so: ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Nhap phan so thu "<<(i+1)<<endl;
cout<<" tu : ";cin>>a[i].tu;
cout<<" mau: ";cin>>a[i].mau;
}
}
int uscln(int a, int b){
while(a!=b)
if(a>b)a-=b;
else b-=a;
return a;
}
int kt_toigian(ps x){
return uscln(x.tu,x.mau)==1;
}
ps toigian(ps y){
int t=uscln(y.tu,y.mau);
y.tu/=t;
y.mau/=t;

return y;
}
ps cong(ps a, ps b){
ps t;
t.tu=a.tu*b.mau+a.mau*b.tu;
t.mau=a.mau*b.mau;
return t;
}
void lietke(ps a[], int n){
cout<<"Cac ps tg:";
for(int i=0;i<n;i++)
if(kt_toigian(a[i]))
cout<<a[i].tu<<"/"<<a[i].mau<<" ";
}
ps tong(ps a[], int n){
ps s;
s.tu=0;
s.mau=1;
for(int i=0;i<n;i++)
if(kt_toigian(a[i]))
s=cong(s,a[i]);
return s;
}
void main(){
ps w[100];
int m;
nhap(w,m);
lietke(w,m);
ps s=tong(w,m);
s=toigian(s);
cout<<endl
<<"TOng cac ps tg="
<<s.tu<<"/"<<s.mau<<endl ;
}

You might also like