函数和C type 字符串
简单统计
#include <iostream>
#include <array>
#include <vector>
using namespace std;
unsigned int c_in_str(const char *str, char ch);
int main()
{
char mmm[15] = "minimum";
char * wail = "ululate";
unsigned int ms = c_in_str(mmm, 'm');
unsigned int us = c_in_str(wail, 'u');
cout << "ucount:" << us << endl;
cout << "count:" << ms << endl;
return 0;
}
unsigned int c_in_str(const char *str, char ch)
{
unsigned int count = 0;
while (*str)
{
if (*str == ch)
{
count++;
}
str++;
}
return count;
}
输入个数和展示
#include <iostream>
#include <array>
#include <vector>
using namespace std;
unsigned int c_in_str(const char *str, char ch);
char *buildStr(char ch, int n);
int main()
{
int times;
char ch;
cout << "Enter a character:";
cin >> ch;
cout << "Enter an integer: ";
cin >> times;
char *ps = buildStr(ch, times);
cout << ps << endl;
delete [] ps;
cout << ps << endl;
ps = buildStr('+', 9);
cout << ps << endl;
delete [] ps;
return 0;
}
char *buildStr(char ch, int n)
{
char *pstr = new char[n + 1];
*(pstr + n) = '\0';
while (n-- > 0)
{
pstr[n] = ch;
}
return pstr;
}
unsigned int c_in_str(const char *str, char ch)
{
unsigned int count = 0;
while (*str)
{
if (*str == ch)
{
count++;
}
str++;
}
return count;
}
函数和string
#include <iostream>
using namespace std;
const int SIZE = 5;
void display(const string * ar,int n);
int main()
{
string list[SIZE];
cout << "Enter your " << SIZE << " string. " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, list[i]);
}
cout << "your list:\n";
display(list,SIZE);
return 0;
}
void display(const string * ar,int n)
{
for (int i = 0; i < n; i++)
{
cout << i + 1 << ": " + *(ar + i) <<endl;
}
}
函数和结构体
坐标转换,传地址
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int Mins_per_hr = 60;
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
void showPolar(const polar* p);
void rect_to_polar(const rect * xypos,polar * pda);
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values: ";
while (cin >> rplace.x >> rplace.y)
{
rect_to_polar(&rplace,&pplace);
showPolar(&pplace);
}
}
void showPolar(const polar * p)
{
const double Rad_to_deg = 57.29577951;
cout << "polar distance:" << p->distance << ",polar angle:" << p->angle*Rad_to_deg << endl;
}
void rect_to_polar(const rect * xypos,polar * pda)
{
pda->distance = sqrt(xypos->x) + sqrt(xypos->y);
pda->angle = atan2(xypos->y, xypos->x);
}
坐标转换,传值。时间结构计算时间
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
struct travel_time
{
int hours;
int mins;
};
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time time);
const int Mins_per_hr = 60;
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
void showPolar(polar p);
polar rect_to_polar(rect xypos);
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values: ";
while (cin >> rplace.x >> rplace.y)
{
pplace = rect_to_polar(rplace);
showPolar(pplace);
}
}
void showPolar(polar p)
{
const double Rad_to_deg = 57.29577951;
cout << "polar distance:" << p.distance << ",polar angle:" << p.angle*Rad_to_deg << endl;
}
polar rect_to_polar(rect xypos)
{
polar answer;
answer.distance = sqrt(xypos.x) + sqrt(xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
int main2()
{
travel_time day1 = {5, 45};
travel_time day2 = {4, 55};
travel_time trip = sum(day1, day2);
cout << "2 day total:";
show_time(trip);
travel_time day3 = {4, 32};
show_time(sum(trip, day3));
return 0;
}
void show_time(travel_time time)
{
cout << "hours:" << time.hours << ",mins " << time.mins << endl;
}
travel_time sum(travel_time t1, travel_time t2)
{
travel_time total;
total.mins = (t1.mins + t2.mins) % 60;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / 60;
return total;
}
函数和数组
记录四季消费和总额
#include <iostream>
#include <string>
using namespace std;
const int Seasons = 4;
const array<string, Seasons> Snames = {"Spring", "Summer", "Fall", "Winter"};
void fill(array<double, Seasons> *pa);
void show(array<double, Seasons> ds);
int main()
{
array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void show(array<double, Seasons> ds)
{
double total = 0.0;
cout << "\nExpenses\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << ds[i] << endl;
total += ds[i];
}
cout << "total: $" << total << endl;
}
void fill(array<double, Seasons> *pa)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
函数的递归
基本递归
#include <iostream>
#include <string>
using namespace std;
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
cout << "Countdown......" << n << " " << &n << endl;
if(n > 0)
{
countdown(n - 1);
}
cout << n << " kaboom!" << " " << &n << endl;
}
复杂输出递归函数
#include <iostream>
#include <string>
using namespace std;
const int Len = 66;
const int Divs = 6;
void subdivide(char *ar, int low, int high, int level);
int main()
{
char ruler[Len];
for (int i = 1; i < Len - 2; i++)
{
ruler[i] = ' ';
}
ruler[Len - 1] = '\0';
int max = Len - 2;
int min = 0;
ruler[0] = ruler[max] = '|';
cout << ruler << endl;
for (int i = 1; i <= Divs; i++)
{
subdivide(ruler, min, max, i);
cout << ruler << endl;
for (int j = 0; j < Len - 2; j++)
{
ruler[i] = ' ';
}
}
return 0;
}
void subdivide(char *ar, int low, int high, int level)
{
if (level == 0)
return;
int mid = (low + high) / 2;
ar[mid] = '|';
subdivide(ar, low, mid, level - 1);
subdivide(ar, mid, high, level - 1);
}
| |
| | |
| | | | |
| | | | | | | | |
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
函数指针
获取函数地址
声明函数指针
使用函数指针调用函数
#include <iostream>
#include <string>
using namespace std;
double betsy(int lns);
double pam(int lns);
void estimate(int lines, double (*pf)(int));
int main()
{
int code;
cout << "How many lines of code do you need? ";
cin >> code;
cout << "Here's Betsy's estimate:\n";
estimate(code, betsy);
cout << "Here's Pam's estimate:\n";
estimate(code, pam);
return 0;
}
void estimate(int lines, double (*pf)(int))
{
cout << lines << "lines wiil take " << (*pf)(lines) << endl;
}
double betsy(int lns)
{
return 0.05 * lns;
}
double pam(int lns)
{
return 0.03 * lns + 0.0004 * lns * lns;
}
默认参数
#include <iostream>
#include <string>
using namespace std;
const int ArSize = 80;
char *left(const char *str, int n = 1);
int main()
{
char sample[ArSize];
cout << "Enter a string: \n";
cin.get(sample, ArSize);
char *ps = left(sample, 4);
cout << ps << endl;
ps = left(sample);
cout << ps << endl;
delete[] ps;
return 0;
}
char *left(const char *str, int n)
{
if (n < 0)
n = 0;
char *p = new char[n + 1];
int i;
for (i = 0; i < n && str[i]; i++)
{
p[i] = str[i];
}
while (i <= n)
{
p[i++] = '\0';
}
return p;
}
优化1
char *left(const char *str, int n)
{
int len = strlen(str);
if (n < 0)
len = 0;
char *p = new char[len + 1];
int i;
for (i = 0; i < n && str[i]; i++)
{
p[i] = str[i];
}
while (i <= len)
{
p[i++] = '\0';
}
return p;
}
优化2
char *left(const char *str, int n)
{
int m = 0;
while (m <= n && str[m])
m++;
char *p = new char[m + 1];
int i;
for (i = 0; i < n && str[i]; i++)
{
p[i] = str[i];
}
while (i <= m)
{
p[i++] = '\0';
}
return p;
}
函数重载记录数字个数
#include <iostream>
#include <string>
using namespace std;
char *left(const char *str, int n = 1);
unsigned long left(unsigned long num, unsigned ct);
int main()
{
unsigned long n = 12345678;
const char * trip = "Hawaii!!";
char * temp;
int i;
for (i = 0; i < 10; i++)
{
cout << left(n, i) << endl;
temp = left(trip, i);
cout << temp << endl;
delete[] temp;
}
return 0;
}
char *left(const char *str, int n)
{
int m = 0;
while (m <= n && str[m])
m++;
char *p = new char[m + 1];
int i;
for (i = 0; i < n && str[i]; i++)
{
p[i] = str[i];
}
while (i <= m)
{
p[i++] = '\0';
}
return p;
}
unsigned long left(unsigned long num, unsigned ct)
{
unsigned digits = 1;
unsigned long n = num;
if (ct == 0 || num == 0)
return 0;
while (n /= 10)
digits++;
if (digits > ct)
{
ct = digits - ct;
while (ct--)
num /= 10;
return num;
}
else
return num;
}
引用重载
void staff(double &rs);
void staff(const double &rcs);
void stove(double &r1);
void stove(const double &r2);
void stove(double &&r3);
模板函数
template <typename T>
void Swap(T &a,T &b)
{
T temp;
temp =a;
a = b;
b = temp;
}
模版函数重载
include <iostream>
#include <string>
using namespace std;
const int Limit = 8;
void swapr(int & a, int & b);
void swapr2(int * a, int * b);
void swapr3(int a, int b);
template <typename T>
void Swap(T &a,T &b)
{
T temp;
temp =a;
a = b;
b = temp;
}
template <typename T>
void Swap(T *a,T *b,int n)
{
T temp;
for (int i = 0; i < n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void show(int a[]);
int main()
{
int wallet1 = 100;
int wallet2 = 350;
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using references to swap contents:\n";
swapr(wallet1,wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using pointers to swap contents:\n";
swapr2(&wallet1,&wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using value to swap contents:\n";
swapr3(wallet1,wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
Swap(wallet1,wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
double i = 20;
double j = 30;
Swap(i,j);
cout << "i = "<< i << " j = " << j << endl;
int d1[Limit] = {2,3,4,5,1,7,8,9};
int d2[Limit] = {2,3,2,6,1,8,9,9};
show(d1);
show(d2);
Swap(d1,d2,Limit);
show(d1);
show(d2);
return 0;
}
void swapr(int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
void swapr2(int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swapr3(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void show(int * a)
{
cout << a[0] << a[1] << "/";
cout << a[2] << a[3] << "/";
for (int i = 4; i < Limit; i++)
{
cout << a[i];
}
cout << endl;
}
- 非模版函数
void Swap(job &, job &); - 模版函数
template <typename T>
void Swap(T &, T &); - 根据模版生成的具体化函数
template <> void Swap<job>(job &, job &);
如果出现冲突,编译器识别优先级,非模版函数 》 模版生成的具体函数
隐式实例化:编译器帮我们生成的代码。
显示具体化:程序员,根据某个模版要求编译器生成具体的函数
template <> void Swap<job>(job &, job &)
{......}
另外一种模版显示实例化:template void Swap<int>(int, int);
#include <iostream>
#include <string>
using namespace std;
struct debts
{
char name[50];
double amount;
};
template <typename T>
void ShowArray(T arr[], int n);
template <typename T>
void ShowArray(T ** arr,int n);
int main()
{
int things[6] = {13, 31, 103, 301, 310, 130};
struct debts mr_E[3] =
{
{"Ima Wolfe", 2400.0},
{"Ura Foxe", 1300.0},
{"David", 1800.0}};
double *pd[3];
for (int i = 0; i < 3; i++)
{
pd[i] = &mr_E[i].amount;
}
"cout << Liting Mr.E's debts:\n";
ShowArray(things,6);
ShowArray(pd, 3);
return 0;
}
template <typename T>
void ShowArray(T * arr, int n)
{
cout << "templateA\n";
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
cout << endl;
}
}
template <typename T>
void ShowArray(T ** arr,int n)
{
cout << "templateB\n";
for (int i = 0; i < n; i++)
{
cout << *arr[i] << ' ';
cout << endl;
}
}
根据模版生成的具体化函数 编译的时候会动态找合适的模版,如果void ShowArray(T ** arr,int n) 去掉的话,那么, ShowArray(pd, 3); 打印的将会是地址。加上这个模版打印的就是具体的数值了。