Six, Procedural Questions
1. Write a complex number class (operator overloading)
#include <iostream>using namespace Std;class complex{public:complex (double r=0.0,double i=0.0): Read (R), Imag (i) {}; Complex operator+ (const Complex &C2) const; Complex operator-(const Complex &C2) const;void display () const;private:double real,imag;}; Complex complex::operator+ (const Complex &C2) Const{return Complex (REAL=C2.REAL+REAL,IMAG=C2.IMAG+IMAG);} Complex complex::operator-(const Complex &C2) Const{return Complex (REAL=C2.REAL-REAL,IMAG=C2.IMAG-IMAG);} void Complex::d isplay () const{cout<< "(" <<real<< "," <<imag<< ")" <<ENDL;} void int main () {Complex C1 (5,4), C2 (2,10),c3;cout<< "c1=" <<c1.display ();cout<< "c2=" << C2.display ();c3=c1-c2;cout<< "c3=" <<c3.display ();
2. Write a string class--Go
#include <iostream>using namespace Std;class string{friend ostream& operator<< (ostream&,string &);p ublic:string (const char*str=null);//Assignment and constructor string (const string &other) const;//assignment Constructor string& operator= (const string&other);//=string& operator+ (const string&other);//+bool operator== (const String &other);//==char&operator[] (unsigned int);//[]size_t size () {return strlen (m_data);} ~string (void) {delete[] m_data;}; Private:char *m_data;}; Inline string::string (const char *str) {if (!STR) m_data=0;else{m_data=new Char[strlen (str) +1];strcpy (M_DATA,STR);}} Inline string::string (const string&other) {if (!other.m_data) m_data=0;else{m_data=new Char[strlen (Other.m_data) +1];strcpy (M_data,other.m_data);}} Inline string& sting::operator= (const string&other) const{if (this!=&other) {delete []m_data;if (!other.m_ Data) M_data=0;else{m_data=new Char[strlen (other.m_data) +1];strcpy (M_data,other.m_data);}} return this;} Inline string& sting::operator+ (const String&other) const{string newstring;if (!other.m_data) {newstring=*this;} else if (!m_data) {newstring=other;} else {newstring.m_data=new Char[strlen (other.m_data) +1];strcpy (newstring.m_data,m_data); Strcat (NewString.m_data, Other.m_data);} return this;} inline bool string::operator== (const sring &s) {if (strlen (s.m_data)!=strlen (m_data)) return False;else{return strcmp (m_data,s.m_data)? False:true;}} Inline char& string::operator[] (unsigned int e) {if (E>0&&<=strlen (m_data)) return m_data[e];} ostream& operator<< (ostream& os,string& str) {Os<<str.m_data;return OS;} void Main (int argc, char const *argv[]) {string str1= "I Love" string str2= "www.oschina.net!"; o Schina str3=str1+str2;cout<<str3<<endl;cout<<str3.size () <<endl;}
3.swap function
#include <iostream>using namespcae std;int main (int argc, char const *argv[]) {int X=6,y=7;int *p=&x;int *q= &y;//error notation void swap (int _x,int _y) { temp=_y; _y=_x; _x=temp;} Swap (x, y);//pointer void swap (int *_p,int *_q) {int *temp;temp=_p;_p=_q;_q=temp;} Swap (*P,*Q); return 0;}
4. Bubble function
#include <iostream>using namespace std;void Bubblesort (int a[],int size); int main () {int array[]={34,65,12,43,67,5,78,10,3,70},k; int len=sizeof (array)/sizeof (int); cout<< "The orginal array is:" <<endl; for (k=0;k<len;k++) cout<<array[k]<< ","; cout<<endl; Bubblesort (Array,len); cout<< "The sorted array Are:<<endl"; for (k=0;k<len;k++) cout<<array[k]<< ","; cout<<endl; System ("pause"); return 0;} void Bubblesort (int a[],int size) {int temp; for (int pass=1;pass<size;pass++) {for (int k=0;k<size-pass;k++) if (a[k]>a[k+1]) { TEMP=A[K]; A[K]=A[K+1]; A[k+1]=temp; } cout<< "<<pass<<" after the turn of the order: "; for (int i=0;i<size;i++) cout<<a[i]<< ","; cout<<endl; }}
January 5, 2015 XX University xx College Exam questions