0% found this document useful (0 votes)
58 views

#Include #Include #Include

This document defines C++ classes for students (sv), fractions (phanso), and generic sets (set) of different types. It then demonstrates creating sets of floats, fractions, and students, performing set operations on them like union, intersection, and difference, and outputting the results.

Uploaded by

Ngu Mà Lì
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

#Include #Include #Include

This document defines C++ classes for students (sv), fractions (phanso), and generic sets (set) of different types. It then demonstrates creating sets of floats, fractions, and students, performing set operations on them like union, intersection, and difference, and outputting the results.

Uploaded by

Ngu Mà Lì
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream.h> #include <stdlib.h> #include <conio.

h> class sv { private : char ten[100]; float Diem; public: sv() { Diem=0; } sv(char a[],float D) { strcpy(ten,a); Diem=D; } sv(sv&a) { Diem = a.Diem; strcpy(ten,a.ten); } void set_sv(char a[],float D) { strcpy(ten,a); Diem=D; } float get_diem()const { return Diem; } char* get_ten() { return ten; } friend ostream&operator <<(ostream&out,sv&); friend istream&operator>>(istream&in,sv&); operator float() { return float(Diem); } }; ostream&operator <<(ostream&out,sv&a) { cout<<"\n\n\t\t\tTen "<<a.ten<<endl; cout<<"\t\t\tDiem "<<a.Diem<<endl; } istream&operator>>(istream&in,sv&a) { cout<<"\t\t\tNhap ten "; cin.ignore(); cin.getline(a.ten,50); cout<<"\t\t\tNhap diem "; cin>>a.Diem;

} int ucln(int a,int b) { int r; while(b) { r = a%b; a = b; b=r; } return a; } class phanso { private: float tu,mau; public: phanso(float a=1,float b=1) { if(b) { tu = a; mau = b; } else { tu =1; mau=1; } } void set_phanso(float a,float b) { tu =a; mau = b; } void nhap() { cout<<"\t\t\tNhap du lieu cho phan so "<<endl; cout<<"\t\t\tTu "; cin>>tu; cout<<"\t\t\tMau "; cin>>mau; toigian(); } void toigian() { int t=ucln(tu,mau); tu = tu/t; mau = mau/t; } operator float() { return float(tu/mau); } friend ostream&operator <<(ostream&out,phanso&a);

friend istream&operator >>(istream&in,phanso&a); }; ostream&operator<<(ostream&out,phanso&a) { out<<a.tu<<"/"<<a.mau<<"->"; } istream&operator >>(istream&in,phanso&a) { cout<<"\t\tTu "; cin>>a.tu; cout<<"\t\tMau "; cin>>a.mau; } template <class T,int n> class set { private: T data[n]; int spt; public: set() { spt=0; } set(const set&a) { for(int i=0;i<a.spt;i++) data[i]=a.data[i]; spt = a.spt; } void them(T&a); bool search(T&a); friend ostream& operator<<(ostream&out,set<T,n>&a); friend set operator +(set&a,set&b); friend set operator *(set&a,set&b); friend set operator -(set&a,set&b); set operator =(const set&b) { for(int i=0;i<b.spt;i++) data[i]=b.data[i]; spt=b.spt; return (*this); } }; template <class T,int n> void set<T,n>::them(T&a) { if(spt<n) data[spt++]=a; else cout<<"\t\tMang da day rui khong them duoc nua dau "<<endl; } template <class T,int n> bool set<T,n>::search(T&a) {

for(int i=0;i<spt;i++) if(data[i]==a) return true; return false; } template <class T,int n> ostream&operator<<(ostream&out,set<T,n>&a) { if(a.spt==0) out<<" rong "<<endl; for(int i=0;i<a.spt;i++) { out<<a.data[i]; if(i<a.spt-1) cout<<"->"; } } template <class T,int n> set<T,n> operator +(set<T,n>&a,set<T,n>&b) { set<T,n> r(a); for(int i=0;i<b.spt;i++) if(!a.search(b.data[i])) r.them(b.data[i]);

return r; } template <class T,int n> set<T,n> operator -(set<T,n>&a,set<T,n>&b) { set<T,n> r; for(int i=0;i<a.spt;i++) if(!b.search(a.data[i])) r.them(a.data[i]); return r; } template <class T,int n> set<T,n> operator *(set<T,n>&a,set<T,n>&b) { set<T,n> r; for(int i=0;i<a.spt;i++) if(b.search(a.data[i])) r.them(a.data[i]); return r; } void main() { set<float,100> a; set<float,100> c; set<float,100> d; set<float,100> e;

set<float,100> f; set<sv,100> g; set<phanso,100> b; int n,m,l; float r; sv A; phanso s; cout<<"\t\t\tNhap so luong cac so thu "; cin>>n; for(int i=0;i<n;i++) { cout<<" nhap so thu "<<(i+1)<<":"; cin>>r; a.them(r); }clrscr(); cout<<"\t\t\tNhap so luong phan so "; cin>>m; for(int i=0;i<m;i++) { cout<<"\t\t\tNhap phan so thu "<<(i+1)<<endl; cin>>s; b.them(s); c.them(s);clrscr(); } clrscr(); cout<<"\t\t\tNhap so luong cac sinh vien "; cin>>l; for(int i=0;i<l;i++) { cout<<"\t\t\tNhap du lieu cho sinh vien thu "<<(i+1)<<endl; cin>>A; g.them(A); clrscr(); } clrscr(); textcolor(YELLOW+RED); cprintf("%s","\t\t\tchuong trinh da gan cac so 1 cach tu dong ta duoc "); cout<<"\n\nday so thuc vua nhap "<<endl; cout<<a; cout<<"\n\nday phan so vua nhap "<<endl; cout<<b; cout<<"\n\tDay sinh vien vua nhap "<<endl; cout<<g; getch();clrscr(); d = a+c; cout<<"\n\n hop cua hai tap hop phan so va so thuc la "<<endl;; cout<<d; e=a*c; cout<<"\n\n giao cua hai tap so thuc va phan so la "<<endl; cout<<e; cout<<"\n\nhieu cua hai tap so thuc va phan so la "<<endl; f=a-c; cout<<f;

getch();

You might also like