|
#include<stdio.h> #include<string.h> char* strins(char* str1,char* str2,int pos) { int s_len; int d_len; int i,j; pos--; s_len=strlen(str1); d_len=strlen(str2); for(i=s_len+1;i>=pos;i--) /*空出str2的空間*/ str1[i+d_len]=str1[i]; for(j=pos;str2[j-pos]!='\0';j++) /*將字符串str2插入str1中的特定位置*/ str1[j]=str2[j-pos];
return str1; }
void main() { char string1[200]; char string2[100]; int pos; printf("請輸入初始字符串:"); gets(string1); printf("請輸入插入字符串:"); gets(string2); printf("請輸入插入位置:"); scanf("%d",&pos); strins(string1,string2,pos); printf("插入后的字符串是%s\n",string1); }
運算符重載函數一般采用兩種形式,一種是定義為類的成員函數,另一種是定義為類的友元函數。 大多數情況下,使用成員函數和友元函數重載運算符在功能實現上是相同的,重載時如果沒有本質的區別,則應該首先考慮使用成員函數以保證數據封裝。然而在某些情況下,如C++不能直接進行復數加、減、乘、除的四則運算,但是使用友元函數就可以實現重載這些運算符。 如 定義 class complex { public: complex(){real=imag=0;} complex(double r,double i) { real=r,imag=r; } friend complex operator+(const complex &c1,const complex &c2) { return complex(c1.real+c2.real,c1.imag+c2.imag); }...
!注意友元運算符函數的參數類型是引用類型!
一般而言,以下兩種調用方法是等價的: aa@ bb //隱式調用 operator @ (aa,bb) // 顯式調用 @為運算符
在實際開發過程中,單目運算符建議重載為成員函數,而雙目運算符建議重載為友元函數。通常下雙目運算符重載為友元函數比重載為成員函數更方便,但是有時雙目運算符必須重載為成員函數,例如賦值運算符。
用友元函數重載“++”“--”時需要注意幾點: 1)運算符++、--都對單值操作數產生影響,因此使用成員運算符函數重載++和--的成員函數通常返回指針this。 2)由于友元運算符函數沒有this指針,因此不能引用this指針所指的對象,使用友元函數重載++、--時,應采用引用參數傳遞數據。 3)采用前綴和后綴方式的函數內部的語句可以相同,也可以不同,這取決于用戶的考慮。 例子: class book { public: book(int i=0,int j=0); void print(); friend book operator++(book &op); private: int x,y; };
book::book(int i,int j) { x=i; y=j; }
void book::print() { cout<<" x: "<<x<<", y "<<y<<endl; }
book operator++(book &op) //此處不能寫成book operator++(book op), 參數必須是引用傳遞類型,而不是值傳遞。若這樣做,以下主函數輸出的值是不變的 { ++op.x; ++op.y; return op; }
void main() { book ob(20,30); ob.print(); operator++(ob); ob.print(); ++ob; ob.print(); }
類的實現:
class CNumber { private: int n; public: CNumber(int number) { n=number; } ~CNumber(){ } int isEven() { if(n%2==0) return 1; else return 0; } int isOdd() { if(n%2!=0) return 1; else return 0; } int isPrime() { int i; if(n<1) return 0; for(i=2;i<=sqrt(n);i++) if(n%i==0) return 0; else return 1; return 0; } bool isAPrime(int n) { for(int i=2;i<sqrt(n);i++) if(n%i==0) return false; return true; } int isGoldBach() { int i; int halfnum=n/2; for(i=2;i<=halfnum;i++) if(isAPrime(i)&&isAPrime(n-i)) return 1; return 0; }
void print() { if(isEven()) cout<<"This number is even."<<endl; if(isOdd()) cout<<"This number is odd."<<endl; if(isPrime()) cout<<"This number is prime."<<endl; if(isGoldBach()) cout<<"This number is goldbach."<<endl; } };
主函數: void main() { int num; cout<<"Please enter one number:"<<endl; cin>>num; CNumber numb(num); numb.print(); }
二分法思想:假定f(x)在區間(x,y)上連續 先找到a、b屬于區間(x,y),使f(a),f(b)異號, 說明在區間(a,b)內一定有零點,然后求f[(a+b)/2], 現在假設f(a)<0,f(b)>0,a<b ①如果f[(a+b)/2]=0,該點就是零點, 如果f[(a+b)/2]<0,則在區間((a+b)/2,b)內有零點,(a+b)/2=>a,從①開始繼續使用 中點函數值判斷。 如果f[(a+b)/2]>0,則在區間(a,(a+b)/2)內有零點,(a+b)/2<=b,從①開始繼續使用 中點函數值判斷。 這樣就可以不斷接近零點。 通過每次把f(x)的零點所在小區間收縮一半的方法,使區間的兩個端點逐步迫近函數的零點,以求得零點的近似值,這種方法叫做二分法。 頭文件定義 class CEquation { private: double solution; //方程的近似解 double a, b; //近似解的區間 double (*p_fx)(double x); //p_fx是一個指向函數的指針,指向方程式求值函數 double (*p_solution)(double x, double y); //指向由近似解區間求近似解的函數的指針 double delta; //求解精度 public: CEquation(double av, double bv, double (*p1)(double), double (*p2)(double,double), double dv); double biSection(); //二分法求方程近似解 void printSolution() const; };
類的實現及主函數實現:
CEquation::CEquation(double a_val, double b_val, double (*p1)(double), double (*p2)(double, double), double delta_val)
  {
a = a_val;
b = b_val;
p_fx = p1;
p_solution = p2;
solution = p_solution(a, b);
delta = delta_val;
}

double fx(double x) //方程為: e^x+4^x3-6^x2+3x-2=0
  {
return exp(x)+4.0*x*x*x-6.0*x*x+3.0*x-2.0;
}

double middle(double x, double y) //中值
  {
return 0.5*(x+y);
}

double CEquation::biSection()
  {
double h;

while (fabs(a-b) > delta)
 {
h = p_solution(a, b);
if (p_fx(a)*p_fx(h) > 0)
a = h;
else
b = h;
}
solution = p_solution(a, b);
return solution;
}

void CEquation::printSolution() const
  {
cout << "Solution is: " << solution << endl;
}

void main ()
  {
CEquation a(0.0, 1.0, fx, middle, 1e-6);

a.biSection();
a.printSolution();
}

摘要: 小累,國慶假期過去一大半,C++這時候才把遞歸和函數這一塊知識點慢慢地啃完了,結束之前,今晚自己寫了一個小程序,實現四則運算,適合小學生使用。程序說明:1)允許用戶選擇一種類型的算術問題來學習,輸入1表示加法,2表示減法,3表示乘法,4表示除法,5表示四種混合運算;2)由于程序代碼中反復無窮遞歸,所以該程序的運算會不斷進行下去,退出請自動關閉程序;源代碼如下:
Code highl... 閱讀全文
下午照樣看C++how to program,加油,要到數組和指針了,接下來就是類的深入剖析了,這幾天在加強火力猛攻這塊地方,哈哈,還是編程菜鳥!不過我自己感覺比大一那時候真的有點進步了,有那種coding 的感覺了,努力學習還是有收獲的。閑話休說,把今天下午自己寫的代碼發上來!
#include<iostream> using namespace std; int main() { int row,line;//定義行、列數的變量 for(row=1;row<=10;row++) { for(line=1;line<=row;line++) cout<<"*"; cout<<endl; } cout<<"\n";//第一個圖形至此打印出來 for(row=10;row>=1;row--) { for(line=1;line<=row;line++) cout<<"*"; cout<<endl; } cout<<"\n";//第二個圖形打印出來 for(row=10;row>=1;row--) { for(line=1;line<=row;line++) cout<<"*"; cout<<endl; for(line=1;line<=(11-row);line++) cout<<" "; } cout<<"\n";// 第三個圖形打印出來 for(row=1;row<=10;row++) { for(line=1;line<=10-row;line++) cout<<" ";
for(line=1;line<=row;line++) cout<<"*"; cout<<endl; } //最后一個圖形! cout<<endl; return 0;
}
程序運行結果 :  
|