/*
函數(shù)和函數(shù)模板
1.函數(shù)的參數(shù)以及傳遞方式
參數(shù)參數(shù)傳地址值和傳地址對象對象指針對象引用也可以使用const限制,函數(shù)的參數(shù)還可以設(shè)計(jì)成默認(rèn)形式。
3.1.1對象作為函數(shù)參數(shù)。
3.1.2對象指針作為函數(shù)參數(shù)
3.1.3引用作為函數(shù)參數(shù)。
3.5間接引用數(shù)組的例子
3.1.4默認(rèn)參數(shù)可以多于一個但必須放在參數(shù)序列的后面,默認(rèn)參數(shù)需要致命一個特定值在其之前的所有參數(shù)都必須賦值。
3.1.5使用const保護(hù)數(shù)據(jù)
3.2返回引用的函數(shù)。
3.2.3返回指針函數(shù)。
3.2.3返回對象的函數(shù)。
3.2.4函數(shù)返回值作為函數(shù)的參數(shù)
3.3內(nèi)聯(lián)函數(shù)
3.4函數(shù)的重載和默認(rèn)參數(shù)
3.5函數(shù)模板
3.16使用類模板作為函數(shù)模板參數(shù)的程序
3.17使用顯示規(guī)則和關(guān)鍵字typename編制函數(shù)模板的例子
作業(yè)題
1.函數(shù)原型聲明C++函數(shù)double abc (double,char)表示一個返回值是double的函數(shù)。參數(shù)一個是double一個是char
2.inline
3.三種對象引用對象指針可以當(dāng)參數(shù)傳遞
4.int * method(char ,int )
二。A string input(const int);不允許干煸函數(shù)參數(shù)的聲明
2.重載的正確C函數(shù)的返回值可以相同
3.template
三。改錯題
1.T不是變量而是一種類型不能做運(yùn)算。
2.y沒有類型
3.const是不可以更改參數(shù)。可以使用。
四編程題
1.求方程的根并且寫出判斷。
2.定義函數(shù)up(ch)如果字符變量ch是小寫字母就轉(zhuǎn)換成大寫字母并通過up返回,否則字符ch不改變,要求在短小而
完全的程序中顯示程序怎樣被調(diào)用?
3.編寫主程序調(diào)用帶實(shí)數(shù)r和整數(shù)n兩個參數(shù)的函數(shù)并輸出r的n次冪。
4.編寫有字符參數(shù)C和整形參數(shù)N的函數(shù)讓他們顯示出由字符C組成三角形其中方式為第一行有
1個字符C第2行有2個字符C等等。
5.編寫一個ieqiu字符串長度的函數(shù)strlen()再用strlen()函數(shù)編寫一個函數(shù)revers(s)的倒序遞歸程序使字符串s逆序。
6.用函數(shù)模板實(shí)現(xiàn)3個數(shù)值中按最小值到最大值排序的程序。
7.利用函數(shù)模板設(shè)計(jì)一個求數(shù)組元素中和的函數(shù)。
8.重載上題中的函數(shù)模板,使他能夠進(jìn)行兩個數(shù)組的求和。
*/
/* 7.利用函數(shù)模板設(shè)計(jì)一個求數(shù)組元素中和的函數(shù)。
8.重載上題中的函數(shù)模板,使他能夠進(jìn)行兩個數(shù)組的求和。
#include
using namespace std;
template
T sum(T a[],int n)
{
int i;
T s=0;
for (i=0;i
s=s+a[i];
}
return s;
}
template
T sum(T a[],int n,T b[],int m)
{
return sum(a, n)+sum(b, m);
}
int main()
{
int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,10};
int s1=sum(a, 5);
int s2=sum(a, 5, b, 10);
cout<
cout<
return 0;
}
*/
/* 6.用函數(shù)模板實(shí)現(xiàn)3個數(shù)值中按最小值到最大值排序的程序。
#include
using namespace std;
typedef double Array[8];
template
void sort(T a,T b,T c)
{
T Array[3],temp;
int i,j;
Array[0]=a;
Array[1]=b;
Array[2]=c;
for ( i=0;i<3 ; i++) {
for ( j=0; j<2; j++) {
if (Array[j]>Array[j+1]) {
temp=Array[j];
Array[j]=Array[j+1];
Array[j+1]=temp;
}
}
}
cout<
}
int main()
{
sort(1,8,4);
}
*/
/* 5.編寫一個ieqiu字符串長度的函數(shù)strlen()再用strlen()函數(shù)編寫一個函數(shù)revers(s)的倒序遞歸程序使字符串s逆序
#include
using namespace std;
int strlen(char * str)
{
int len=0;
while (str[len]!='\0') {
len++;
}
return len;
}
void reverse(char * b)
{
char c;
int j,len;
len=strlen(b);
j=len/2-1;
while (j>=0) {
c=*(b+j);
*(b+j)=*(b+len-j-1);
*(b+len-j-1)=c;
j--;
}
b[len]='\0';
}
intmain()
{
char str[]={"123456789"};
cout<
reverse(str);
cout<
}
。*/
/*4.編寫有字符參數(shù)C和整形參數(shù)N的函數(shù)讓他們顯示出由字符C組成三角形其中方式為第一行有
1個字符C第2行有2個字符C等等。
#include
using namespace std;
void print_ln(char c,int n)
{
int i,j;
for (i=0; i
for (j=0; j<=i; j++) {
cout<
}
cout<
}
}
int main()
{
print_ln('a', 10);
}
*/
/**編程題
3.3.編寫主程序調(diào)用帶實(shí)數(shù)r和整數(shù)n兩個參數(shù)的函數(shù)并輸出r的n次冪
#include
using namespace std;
#include
double power(double a,int b)
{
int i;
double result =1.0;
for (i=0; i
result =result*a;
}
returnresult;
}
int main()
{
double r;
int n;
cout<<"r=";
cin>>r;
cout<<"n=";
cin>>n;
cout<<"r的n次冪是"<< power(r,n);
}
*/
/*編程題
2.定義函數(shù)up(ch)如果字符變量ch是小寫字母就轉(zhuǎn)換成大寫字母并通過up返回,否則字符ch不改變,要求在短小而
完全的程序中顯示程序怎樣被調(diào)用?
#include
using namespace std;
char up(char c)
{
if (c>=97&&c<=123) {
return c-32;
}
else
{
return c;
}
}
int main()
{
char c[4]={'a','S','D','T'};
for (int i=0; i<4; i++) {
cout<
cout<
}
return 0;
}
*/
/*四編程題1.求方程的根并且寫出判斷。
#include
#include
using namespace std;
//a>b
void equaltion_1(int a,int b,int c)
{
double x1,x2,temp;
temp =b*b-4*a*c;
x1=(-b+sqrt(temp))/(2*a*1.0);
x2=(-b-sqrt(temp))/(2*a*1.0);
//兩個不相等的實(shí)根
cout<
}
//a==b
void equaltion_2(int a,int b,int c)
{
double x1,x2,temp;
temp =b*b-4*a*c;
x1=(-b+sqrt(temp))/(2*a*1.0);
x2=x1;
cout<<"兩個相等的實(shí)根"<
cout<
}
//a
void equaltion_3(int a,int b,int c)
{
double temp,real1,real2,image1,image2;
temp=-(b*b-4*a*c);
real1=-b/(2*a*1.0);
real2=real1;
image1=sqrt(temp);
image2=-image1;
}
int main()
{
int a, b, c;
double temp;
cin>>a>>b>>c;
temp =b*b-4*a*c;
if (temp>0) {
equaltion_1(a, b, c);
}
if (temp==0) {
equaltion_1(a, b, c);
}
if (temp<0) {
equaltion_1(a, b, c);
}
}**/
/* 3.4函數(shù)的重載和默認(rèn)參數(shù)引出3.5函數(shù)模板conplex模板類參數(shù)的重載使用
#include
#include
using namespace std;
template
T ma7(T m1,T m2)
{
return (m1>m2)?m1:m2;
}
template
void printer(complex a)
{
string str1("dsad"),str2="dssa";
cout<
}
int main()
{
cout<< ma7(2,3 );
complexnum(1,2);
complexnum1(2,3);
printer(num);
printer(num1);
return 0;
}
*/
/* 3.3內(nèi)聯(lián)函數(shù)除了循環(huán)和switch的語句外其他函數(shù)都可以稱為內(nèi)聯(lián)函數(shù)。
#include
using namespace std;
int isnumber(char c){
return( c>'0'&&c<='9')?1:0;
}
int main()
{
char c;
cin>>c;
if (isnumber(c)) {
cout<<"是數(shù)字字符";
}
else
{
cout<<"不是數(shù)字字符";
}
return 0;
}
*/
/* 3.2.4函數(shù)返回值作為函數(shù)的參數(shù)
#include
using namespace std;
int max(int,int);
int main()
{
cout<
}
int max(int a,int b)
{
return (a>b)?a:b;
}
*/
/* 3.2返回引用的函數(shù)
3.2.3返回指針函數(shù)。
3.10返回對象
#include
using namespace std;
int a[] ={2,4,6,8};
int& index(int i);
float * input(int &);
string inputs(const int);
int main()
{
index(3)=16;
cout<
int num;
float * data;
data=input(num);
if (data) {
for (int i=0; i
cout<
}
}
delete data;
cout<<"請輸入一個整數(shù)";
int n;
cin>>n;
cout<
}
int& index(int i)
{
return a[i];
}
float * input(int & n)
{
cout<<"請輸入一個整數(shù)";
cin>>n;
if (n==0) {
return NULL;
}
float * buf=new float[n];
if (buf) {
return NULL;
}
for (int i=0; i<4; i++) {
cin>>buf[i];
}
return buf;
}
string inputs(const int n)
{
string s1,s2;
for (int i=0; i
cin>>s1;
s2=s2+s1+"";
}
return s2;
}
。*/
/*
3.1.5使用const保護(hù)數(shù)據(jù)主要是表示參數(shù)不能修改原函數(shù)沒有修改。
#include
#include
using namespace std;
void change(const string&);
int main()
{
string str("Can you change it");
change (str);
cout<
return 0;
}
void change(const string&s)
{
string s2=s+"No";
cout<
} */
/* 3.1.4默認(rèn)參數(shù)可以多于一個但必須放在參數(shù)序列的后面,默認(rèn)參數(shù)需要致命一個特定值在其之前的所有參數(shù)都必須賦值。設(shè)計(jì)一個根據(jù)參數(shù)數(shù)量輸出信息的函數(shù)
#include
using namespace std;
void Display(string s1,string s2="",string s3="");
int main()
{
string str1("現(xiàn)在"),str2("過去"),str3("將來");
Display(str1,str2,str3);
Display(str3,str1);
Display(str2,str3);
return 0;
}
void Display(string s1,string s2,string s3)
{
if (s2==""&&s3=="") {
cout<
}else if (s3=="" && s2!="")
{
cout<
}
else
{
cout<
}
}
*/
/*
3.5間接引用數(shù)組的例子
#include
using namespacestd;
typedef double Array[8];
void avccout(Array &b,int n)
{
double ave(0);
intcount(0);
for (int j=0; j
ave=ave+b[j];
if (b[j]<60) {
count++;
}
//平均數(shù)
//不及格的人數(shù)
b[n-2]=ave/(n-2);
b[n-1]=count;
}
}
int main()
{
Array b={12,1324,3214324,43,9,0};
avccout(b,8);
cout<
return 0;
}
*/
/*
3.1.1對象作為函數(shù)參數(shù)。
3.1.2對象指針作為函數(shù)參數(shù)
3.1.3引用作為函數(shù)參數(shù)。
#include
#include
using namespace std;
//不會改變原來對象數(shù)據(jù)成員值的例子。
void swap(string,string);
//指針作為參數(shù)
void swap1(string *,string *);
//引用作為函數(shù)參數(shù)
void swap2(string&,string &);
int main()
{
string str1("現(xiàn)在"),str2("過去");
cout<
swap(str1,str2);
cout<
swap1(&str1, &str2);
cout<
swap2(str1, str2);
cout<
return 0;
}
void swap(string s1,string s2)
{
string temp=s1; s1=s2; s2=temp;
cout<
}
void swap1(string * s1,string * s2)
{
string temp=*s1; *s1=*s2;*s2=temp;
cout<<*s1<<*s2<
}
void swap2(string& s1,string & s2)
{
string temp=s1; s1=s2;s2=temp;
cout<
}*/