程序设计基础(C++) 面向过程与面向对象
1 程序设计基础(C++) 面向过程与面向对象
字符串处理(面向过程) int maino char s1[20]=hello" s2[]="world"i cout<< strlen(s1)<<end//字符串长度 if( strcmp(s1,s2)>0)//字符串比较 strcat(s1,s2)P/字符串相加 cout≤≤s1<≤endI return o
2 字符串处理(面向过程) int main() { char s1[20]="hello", s2[]="world"; cout<<strlen(s1)<<endl; //字符串长度 if(strcmp(s1, s2)>0) //字符串比较 ... strcat(s1, s2); //字符串相加 cout<<s1<<endl; return 0; }
字符串处理(面向对象) int maino string si( hello"), s2( world )i cout<<s1 length(<<en//字符串长度 f(s1>s2)//字符串比较 s1+=s2F//字符串相加 cout <<s1<<enda return o }
3 字符串处理(面向对象) int main() { string s1("hello"), s2("world"); cout<<s1.length()<<endl; //字符串长度 if(s1>s2) //字符串比较 .... s1+=s2; //字符串相加 cout<<s1<<endl; return 0; }
复数及其运算(面向过程) 定义复数类型 struct Complext double imagi double real Ln」
4 复数及其运算(面向过程) 定义复数类型 struct Complex{ double imag; double real; }
复数及其运算(面向过程) /复数加运算 Complex addcomplex (Complex &c1, Complex &c2) Complex Ci c real =cl, real +c2, real; Cimag=climag+c2 imagi return Ci
5 复数及其运算(面向过程) //复数加运算 Complex addComplex(Complex &c1, Complex &c2) { Complex c; c.real =c1.real +c2.real; c.imag=c1.imag+c2.imag; return c; }