اصول کامپیوتر 2
كتابهاي الكترونيكي درس
كتاب آموزشي سي شارپ
#include <iostream.h> class Person { public: Person() {} ~Person() {} virtual void Name() { cout << "My Name is PERSON" << endl;} void Address() { cout << "My Address" << endl; } virtual void Move() {} }; class Personal : public Person { public: Personal() {} ~Personal() {} double Hoghoogh() { return 100; } int PerID() { return 12345; } virtual void Name() { cout << "My Name is PERSONAL" << endl;} }; class Stuff : public Personal { public: Stuff() {} ~Stuff() {} void WorkingTime() { cout << "SAT till WED" << endl; } virtual void Name() { cout << "My Name is STUFF" << endl;} }; class Student : public Person { public: Student() {} ~Student() {} int StdID() { return 2222; } void Field() { cout << "My Field" << endl; } virtual void Name() { cout << "My Name is STUDENT" << endl;} }; void main0() { Personal pl; Person pr; Student st; Stuff sf; pl.Name(); pr.Name(); st.Name(); sf.Name(); } void main1() { Person* pr = new Person(); //pr->Name(); Stuff* sf = new Stuff(); //sf->Name(); Person* p = 0; p = pr; p->Name(); p = sf; p->Name(); delete sf; delete pr; } void main() { Person* p[5]; p[0] = new Person(); p[1] = new Personal(); p[2] = new Student(); p[3] = new Stuff(); p[4] = new Personal(); for(int i=0; i<5; i++) { p[i]->Name(); } /* while( !GameOver() ) { for(int i=0; i<5; i++) { p[i]->Move(); } wait(100); //100 ms } //*/ }
#include <iostream.h> #include <iomanip.h> int** CreateMat(int n, int m) { int** p = new int*[n]; for(int i=0; i<n; i++) { p[i] = new int[m]; } return p; } void DelMat(int** &mat, int n, int m) { for(int i=0; i<n; i++) { delete [] mat[i]; mat[i] = 0; } delete [] mat; mat = 0; } void main_aval() { int** m = CreateMat(3, 4); m[0][0] = 1; cout << m[0][0] << endl; DelMat(m, 3, 4); cout << m << endl; } //##### functions ##### void SetI(int** mat, int n) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { mat[i][j] = i == j ? 1 : 0; } } } void PrintMat(int** mat, int n) { for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cout << setw(4) << mat[i][j]; } cout << endl; } } void main() { int n; cin >> n; int** p = CreateMat(n, n); SetI(p, n); PrintMat(p, n); DelMat(p, n, n); }
#include <iostream.h> #include <string.h> class CStudent { public: char name[20]; public: CStudent() { name[0] = '\0'; } CStudent(const char name[]) { SetName(name); } void SetName(const char name[]) { strcpy(this->name, name); } ~CStudent() { cout << "ByBy!!" << endl; } }; ostream& operator<<(ostream& out, CStudent& st) { out << st.name; return out; } void main_class() { /* CStudent st; st.SetName("Ali"); cout << st << endl; //*/ CStudent *p = new CStudent;//("Hasan"); p->SetName("Ali"); cout << *p << endl; delete p; p = 0; } //---------------- struct Date { int year, month, day; }; void main1() { Date* p = new Date; if( !p ) { cerr << "Not enough memory!" << endl; return; } p->year = 1389; p->month = 1; delete p; p = 0; } void main0() { double* b = new double; if( !b ) { cerr << "Not enough memory!" << endl; return; } *b = 5; (*b)++; (*b) += 7; cout << *b << endl; delete b; b = 0; } // ------------ New Vector ----------- void main() { int n; cout << "Enter N>"; cin >> n; double* y = new double[n]; for(int i=0; i<n; i++) cin >> y[i]; for(i=n-1; i>=0; i--) cout << y[i] << endl; delete [] y; y = 0; }
#include <iostream.h> void main0() { int a = 2; cout << &a << endl; int* p = &a; cout << p << endl; cout << *p << endl; (*p)++; cout << *p << endl; cout << a << endl; int* q = 0;// = (int*)70; if( q ) *q = 2; } void main1() { int a[5] = {1, 4, 2, 3, 5}; cout << a << endl; int* p = a; for(int i=0; i<5; i++) { cout << *p << '\t'; p++; } cout << endl; for(i=0; i<5; i++) cout << *(a+i) << '\t';; cout << endl; for(i=0; i<5; i++) cout << a[i] << '\t';; cout << endl; for(i=0; i<5; i++) cout << i[a] << '\t';; /* int b[5] = {0}; b = a; const int N = 5; N = 2; //*/ char* b = "aQcdef"; //char b[] = "aQcdef"; cout << endl << b[1] << endl; } void swap(int& a, int& b) { int t = a; a = b; b = t; } void swap2(int* a, int* b) { int t = *a; *a = *b; *b = t; } void main2() { int x = 5, y = 2; swap(x, y); cout << "x="<< x << ", y=" << y << endl; swap2(&x, &y); cout << "x="<< x << ", y=" << y << endl; int a = 2; cout << *(&a) << endl; } struct Date { int year, month, day; }; void main5() { Date d = {1388, 12, 17}; cout << &d << endl; Date* p = &d; cout << (*p).year << endl; cout << (*p).month << endl; cout << p->day << endl; cout << p->year << endl; cout << p->month << endl; } class CDate { public: int year, month, day; CDate(int y, int m, int d) { this->year = y; this->month = m; this->day = d; } ~CDate() {} }; void main() { CDate d(1388, 12, 17); cout << &d << endl; CDate* p = &d; cout << (*p).year << endl; cout << (*p).month << endl; cout << p->day << endl; cout << p->year << endl; cout << p->month << endl; }
#include <iostream.h> const int MAX = 200; class CSet { private: int items[ MAX ]; int cnt; int find(int val) { for(int i=0; i<cnt; i++) { if( items[i] == val ) return i; } return -1; } bool isValid(int idx) { return idx >= 0 && idx < cnt; } public: CSet() { cnt = 0; } ~CSet() { } void Print() { cout << '{'; for(int i=0; i<cnt; i++) { cout << items[i]; if( i != cnt-1 ) cout << ", "; } cout << '}' << endl; } void Add(int val) { if( cnt == MAX || find(val) >= 0 ) return; items[cnt] = val; cnt++; } void Add(CSet another) { for(int i=0; i<another.cnt; i++) { Add( another.items[i] ); } } int Count() { return cnt; } int GetItem(int idx) { if( !isValid(idx) ) return -1000; else return items[idx]; } void SetItem(int idx, int val) { if( isValid(idx) ) items[idx] = val; } void RemoveIndex(int idx) { if( !isValid(idx) ) return; for(int i=idx; i<cnt; i++) items[i] = items[i+1]; cnt--; } void RemoveItem(int val) { RemoveIndex( find(val) ); } bool Has(int val) { return find(val)>=0; } void RemoveAll() { cnt = 0; } }; CSet operator+(CSet A, CSet B) { CSet P; P.Add( A ); P.Add( B ); return P; } CSet operator*(CSet A, CSet B) { CSet P; for(int i=0; i<A.Count(); i++) { if( B.Has( A.GetItem(i) ) ) P.Add( A.GetItem(i) ); } return P; } CSet operator-(CSet A, CSet B) { CSet P; for(int i=0; i<A.Count(); i++) { if( !B.Has( A.GetItem(i) ) ) P.Add( A.GetItem(i) ); } return P; } CSet operator^(CSet A, CSet B) { return (A-B)+(B-A);//(A+B)-(A*B) } ostream& operator<<(ostream& out, CSet p) { out << '{'; for(int i=0; i<p.Count(); i++) { out << p.GetItem(i); if( i != p.Count()-1 ) out << ", "; } out << '}'; return out; } istream& operator>>(istream& in, CSet& p) { p.RemoveAll(); int val = 0; while( true ) { char ch = cin.get(); if( int(ch) == 10 ) break; cin.putback( ch ); cin >> val; p.Add( val ); } return in; } /* void main() { CSet p, q; p.Add(2); p.Add(3); p.Add(4); p.Add(7); q.Add( 7 ); q.Add( 8 ); q.Add( 8 ); q.Add( p ); q.Print(); cout << q.Count() << endl; cout << q.GetItem(10) << endl; q.SetItem(0, 9); q.Print(); } //*/ void main() { CSet p, q; p.Add(2); p.Add(3); p.Add(4); q.Add( 1 ); q.Add( 2 ); q.Add( 3 ); cout << (p^q) << endl; cin >> q; cout << q << endl; /* p.RemoveIndex(0); p.Print(); p.RemoveIndex(1); p.Print(); p.RemoveIndex(0); //*/ }
#include <iostream.h> #include <math.h> class Complex { public: double real, image; Complex() { real = 0; image = 0; cout << "1-man oomadam!!" << endl; } Complex(double r) { real = r; image = 0; cout << "2-man oomadam!!" << endl; } Complex(double r, double i) { real = r; image = i; cout << "3-man oomadam!!" << endl; } //*/ ~Complex() { cout << "ByBy ... " << endl; } double mag() { return sqrt( real*real+image*image ); } private: }; Complex operator+(Complex c1, Complex c2) { Complex r; r.real = c1.real+c2.real; r.image = c1.image+c2.image; return r; } Complex operator-(Complex c1, Complex c2) { Complex r; r.real = c1.real-c2.real; r.image = c1.image-c2.image; return r; } Complex operator*(Complex c1, Complex c2) { Complex r; r.real = c1.real*c2.real-c1.image*c2.image; r.image = c1.image*c2.real+c1.real*c2.image; return r; } Complex operator-(Complex c) { Complex r; r.real = -c.real; r.image = -c.image; return r; } Complex operator~(Complex c) { Complex r; r.real = c.real; r.image = -c.image; return r; } Complex operator/(Complex c1, Complex c2) { Complex r = c1*~c2; Complex mag = c2*~c2; r.real /= mag.real; r.image /= mag.real; return r; } ostream& operator<<(ostream& out, Complex c) { out << c.real << '+' << c.image << 'i'; return out; } istream& operator>>(istream& in, Complex& c) { in >> c.real >> c.image; return in; } void main() { /* Complex a = {4, -2}, b = {2, 0}; //Complex c = sum(a, b); Complex c = a/b;//*b-a; c.print(); cout << endl; cout << c.mag() << endl; ///*/ /* Complex c(4, 3);; cout << 2*c-3 << endl; Complex p; cin >> p; cout << p << endl; ///*/ Complex p(2); }
#include <iostream.h> /* struct Date { int year, month, day; }; struct Student { char name[100]; double math; struct Date birth; }; void printDate(Date d) { //1388/12/04 if( d.year < 10 ) cout << 130; else if( d.year < 100 ) cout << 13; cout << d.year << '/'; if( d.month < 10 ) cout << 0; cout << d.month << '/'; if( d.day < 10 ) cout << 0; cout << d.day; } void incDate(Date& d) { d.day ++; if( d.month <= 6 ) { if( d.day > 31 ) { d.day -= 31; d.month ++; } } else if( d.month <= 11 ) { if( d.day > 30 ) { d.day -= 30; d.month ++; } } else if( d.year %4 == 3 ) { if( d.day > 30 ) { d.day -= 30; d.month = 1; d.year ++; } } else { if( d.day > 29 ) { d.day -= 29; d.month = 1; d.year ++; } } } void incDate(Date& d, int n) { for(int i=0; i<n; i++) incDate( d ); } //*/ /* void main() { Date d1 = {1304, 12, 29}; incDate( d1, 365 ); printDate( d1 ); cout << endl; } //*/ /* void main() { Student st = { "ali", 13, {70, 5, 14} }; printDate( st.birth ); cout << endl; st.birth.year --; printDate( st.birth ); cout << endl; } //*/ struct Complex { double real, image; }; void print(Complex c) { cout << c.real << '+' << c.image << 'i'; } Complex operator+(Complex c1, Complex c2) { Complex r; r.real = c1.real+c2.real; r.image = c1.image+c2.image; return r; } Complex operator-(Complex c1, Complex c2) { Complex r; r.real = c1.real-c2.real; r.image = c1.image-c2.image; return r; } Complex operator*(Complex c1, Complex c2) { Complex r; r.real = c1.real*c2.real-c1.image*c2.image; r.image = c1.image*c2.real+c1.real*c2.image; return r; } Complex operator-(Complex c) { Complex r; r.real = -c.real; r.image = -c.image; return r; } Complex operator~(Complex c) { Complex r; r.real = c.real; r.image = -c.image; return r; } Complex operator/(Complex c1, Complex c2) { Complex r = c1*~c2; Complex mag = c2*~c2; r.real /= mag.real; r.image /= mag.real; return r; } void main() { Complex a = {4, -2}, b = {2, 0}; //Complex c = sum(a, b); Complex c = a/b;//*b-a; print(c); cout << endl; }
#include <iostream.h> #include <iomanip.h> #include <string.h> /* const int N = 3; void main() { char name[N][100]; char stdID[N][10]; double dini[N]; double math[N]; double oc2[N]; for(int i=0; i<N; i++) { cin >> name[i] >> stdID[i] >> dini[i] >> math[i] >> oc2[i]; } for(i=0; i<N-1; i++) { for(int j=i+1; j<N; j++) { if( (dini[i]+math[i]+oc2[i]) < (dini[j]+math[j]+oc2[j]) ) { char ts[100]; strcpy(ts, name[i]); strcpy(name[i], name[j]); strcpy(name[j], ts); strcpy(ts, stdID[i]); strcpy(stdID[i], stdID[j]); strcpy(stdID[j], ts); double t = dini[i]; dini[i] = dini[j]; dini[j] = t; t = math[i]; math[i] = math[j]; math[j] = t; t = oc2[i]; oc2[i] = oc2[j]; oc2[j] = t; } } } for(i=0; i<N; i++) { cout<< setw(12) << name[i] << setw(12) << stdID[i] << setw( 4) << dini[i] << setw( 4) << math[i] << setw( 4) << oc2[i] << endl; } } //*/ const int N = 3; struct Student { char name[100]; char stdID[10]; double dini, math, oc2, phys; }; double ave(Student s) { return (s.dini+s.math+s.oc2+s.phys)/4; } void writeStudent(Student st) { cout<< setw(12) << st.name << setw(12) << st.stdID << setw( 4) << st.dini << setw( 4) << st.math << setw( 4) << st.oc2 << setw( 4) << st.phys << endl; } void readStudent(Student& st) { cin >> st.name >> st.stdID >> st.dini >> st.math >> st.oc2 >> st.phys ; } void main() { Student db[N]; for(int i=0; i<N; i++) readStudent( db[i] ); for(i=0; i<N-1; i++) { for(int j=i+1; j<N; j++) { if( ave( db[i] ) < ave( db[j] ) ) { Student t = db[i]; db[i] = db[j]; db[j] = t; } } } for(i=0; i<N; i++) writeStudent( db[i] ); }
- امتحان میان ترم:
- 1389/02/14 - 13:30
- امتحان پایان ترم:
- 1389/04/05 - 08:30