基本知识
#include <iostream>
using namespace std;
void simple(void);
int main(void)
{
cout << "main() will call the simple() function:" << endl;
simple();
return 0;
}
void simple(void)
{
cout << "I'm but a simple function." << endl;
}
#include <iostream>
using namespace std;
void cheers(int n);
//double cube(double x);
double cube(double);
int main(void)
{
cheers(5);
cout << "Give me a number:";
double side;
cin >> side;
double volume = cube(side);
cout << side << " cube = " << volume << endl;
cheers(cube(2));
return 0;
}
void cheers(int n)
{
for(int i = 0; i < n; i++)
cout << "Cheers ";
cout << endl;
}
double cube(double x)
{
return x * x * x;
}
可以省区函数原型
#include <iostream>
using namespace std;
void simple(void)
{
cout << "I'm but a simple function." << endl;
}
int main(void)
{
cout << "main() will call the simple() function:" << endl;
simple();
return 0;
}
函数按值传递
函数原型中的变量名与函数定义中的变量名不必相同,而且可以省略。但是写上容易理解
#include <iostream>
using namespace std;
void n_chars(char c, int n);
int main(void)
{
char ch;
int times;
cout << "Enter a character: ";
cin >> ch; //cin.get()
while(ch != 'q')
{
cout << "Enter a integer:";
cin >> times;
n_chars(ch, times);
//cout << "times = " << times << endl;
cout << endl;
cout << "Enter another character or press q_key to quit:" << endl;
cin >> ch;
}
return 0;
}
void n_chars(char c, int n)
{
while(n-- > 0)
cout << c;
}
#include <iostream>
using namespace std;
long double probability(unsigned int numbers, unsigned int picks);
int main(void)
{
unsigned int total, choices;
cout << "Enter the total number of choices on the game card and the number of picks allowed:" << endl;
while((cin >> total >> choices) && choices <= total)
{
cout << "You have one chance in " << probability(total, choices) << " of winning." << endl;
cout << "Please enter next two number(q to quit):";
}
cout << "Bye" << endl;
return 0;
}
long double probability(unsigned int numbers, unsigned int picks)
{
double n, p;
long double result = 1.0;
for(n = numbers, p = picks; p > 0; n--, p--)
result = result * (n / p);
return result;
}
函数和数组
将数组的地址 包含元素的种类 以及数组元素的个数传入
#include <iostream>
using namespace std;
const int ArSize = 8;
int sum_arr(int arr[], int n);
int main(void)
{
int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
cout << "cookies address: " << cookies << endl;
cout << "size of cookies: " << sizeof cookies << endl;//求数组所占内存大小
int sum = sum_arr(cookies, ArSize);
cout << "Total cookies eaten: " << sum << endl;
sum = sum_arr(cookies, 3);
cout << "First three eater ate: " << sum << endl;
sum = sum_arr(cookies + 4, 4);
cout << "Last four eater ate: " << sum << endl;
return 0;
}
int sum_arr(int arr[], int n)
{
int total = 0;
cout << "arr address: " << arr << endl;
cout << "size of arr: " << sizeof arr << endl;//在传参时int* arr ==int arr[] 此时int arr[]中的arr并不是数组 而是指针 此时对它用sizeof运算符是求出这个指针所占内存大小
for(int i = 0; i < n; i++)
total += arr[i];
return total;
}
此时用int arr[]而不用Int* arr只是为了看上去更好理解
#include <iostream>
using namespace std;
const int Max = 5;
int fill_array(double arr[], int limit);
void show_array(const double arr[], int n);
void revalue(double r, double arr[], int n);
int main(void)
{
double properties[Max];
int size = fill_array(properties, Max);
show_array(properties, size);
if(size > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while(!(cin >> factor))
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Bad input: input process terminated." << endl;
}
revalue(factor, properties, size);
show_array(properties, size);
}
return 0;
}
int fill_array(double arr[], int limit)
{
double temp;
int i;
for(i = 0; i < Max; i++)
{
cout << "Enter value #" << i + 1 << ": ";
cin >> temp;
if(!cin)
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Bad input: input process terminated." << endl;
break;
}
else if(temp < 0)
break;
else
arr[i] = temp;
}
return i;
}
void show_array(const double arr[], int n)
{
for(int i = 0; i < n; i++)
{
cout << "Property #" << i + 1 << ": $";
cout << arr[i] << endl;
}
}
void revalue(double r, double arr[], int n)
{
for(int i = 0; i < n; i++)
arr[i] *= r;
}
指针常量与常量指针
//const int *pt;
//int *const pt;
//const int *const pt;
#include <iostream>
using namespace std;
int main(void)
{
int n = 10;
int m = 100;
const int *pt = &n;
cout << "1):n = " << n << endl;
//*pt = 20;
pt = &m;
cout << "2):n = " << n << endl;
cout << "*pt = " << *pt << endl;
cout << "m = " << m << endl;
return 0;
}
//const int *pt; == int const *pt;
//int *const pt;
//const int *const pt;
#include <iostream>
using namespace std;
int main(void)
{
int n = 10;
int m = 100;
int *const pt = &n;
cout << "1):n = " << n << endl;
*pt = 20;
cout << "2):n = " << n << endl;
// pt = &m;
//cout << "*pt = " << *pt << endl;
//cout << "m = " << m << endl;
return 0;
}
使用数组区间来传递
#include <iostream>
using namespace std;
const int ArSize = 8;
int sum_arr(const int *begin, const int *end);
int main(void)
{
int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
int sum = sum_arr(cookies, cookies + ArSize);
cout << "Total cookies eaten: " << sum << endl;
sum = sum_arr(cookies, cookies + 3);
cout << "First three eater ate: " << sum << endl;
sum = sum_arr(cookies + 4, cookies + 8);
cout << "Last four eater ate: " << sum << endl;
return 0;
}
int sum_arr(const int *begin, const int *end)
{
int total = 0;
const int *pt;
for(pt = begin; pt != end; pt++)
total += *pt;
return total;
}
禁止将const类型的地址赋值给非const类型的指针
#include <iostream>
using namespace std;
const int ArSize = 8;
int sum_arr(const int *begin, const int *end);
int main(void)
{
int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
int sum = sum_arr(cookies, cookies + ArSize);
cout << "Total cookies eaten: " << sum << endl;
sum = sum_arr(cookies, cookies + 3);
cout << "First three eater ate: " << sum << endl;
sum = sum_arr(cookies + 4, cookies + 8);
cout << "Last four eater ate: " << sum << endl;
return 0;
}
int sum_arr(const int *begin, const int *end)
{
int total = 0;
const int *pt;
for(pt = begin; pt != end; pt++)
total += *pt;
return total;
}
二维数组和指针
int (*arr)[4]定义一个指向由四个int类型构成的数组的指针 但是后面这种形式跟好理解
int arr[][4]
int * arr[4]定义一个由四个int*类型构成的数组
以上都将arr看作一个二维数组的名称
arr[r][c]==*(*(arr+r)+c)
函数与C风格字符串
#include <iostream>
using namespace std;
unsigned int c_in_str(const char *str, char ch);
int main(void)
{
char mmm[15] = "minimum";
const char *wail = "ululate";
unsigned int ms = c_in_str(mmm, 'm');
unsigned int us = c_in_str(wail, 'u');
cout << ms <<" m characters in " << mmm << endl;
cout << us <<" u characters in " << wail << 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>
using namespace std;
char *buildstr(char c, int n);
int main(void)
{
char ch;
int times;
cout << "Enter a character: ";
cin >> ch;
cout << "Enter an integer: ";
cin >> times;
char *ps = buildstr(ch, times);
cout << ps << endl;
delete []ps;
return 0;
}
char *buildstr(char c, int n)
{
char *pstr = new char[n+1];
pstr[n] = '\0';
for(int i = 0; i < n; i++)
pstr[i] = c;
return pstr;
}
函数和结构体
#include <iostream>
using namespace std;
struct travel_time
{
int hours;
int mins;
};
const int Mins_per_hr = 60;
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);
int main(void)
{
travel_time day1 = {5, 45};
travel_time day2 = {4, 55};
travel_time trip = sum(day1, day2);
cout << "Two days total: ";
show_time(trip);
travel_time day3 = {4, 32};
cout << "Three days total: ";
show_time(sum(trip, day3));
return 0;
}
travel_time sum(travel_time t1, travel_time t2)
{
travel_time total;
total.mins = (t1.mins + t2.mins) % Mins_per_hr;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / Mins_per_hr;
return total;
}
void show_time(travel_time t)
{
cout << t.hours << " Hours, " << t.mins << " Mintues." << endl;
}
#include <iostream>
#include <cmath>
using namespace std;
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
void rect_to_polar(const rect *pxy, polar *pda);
void show_polar(const polar *pda);
int main(void)
{
rect rplace;
polar pplace;
cout << "Enter the x and y values: ";
while(cin >> rplace.x >> rplace.y)
{
rect_to_polar(&rplace, &pplace);
show_polar(&pplace);
cout << "Next two number (q to quit):";
}
return 0;
}
void rect_to_polar(const rect *pxy, polar *pda)
{
pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
pda->angle = atan2(pxy->y, pxy->x);
}
void show_polar(const polar *pda)
{
const double Rad_to_deg = 57.29577951;
cout << "Distance = " << pda->distance << endl;
cout << "Angle = " << pda->angle * Rad_to_deg << " degree" << endl;
}
函数和string对象
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
void display(const string sa[], int n);
int main(void)
{
string list[SIZE];
cout << "Enter " << SIZE << " favorite food: " << endl;
for(int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, list[i]);
}
cout << "Your list: " << endl;
display(list, SIZE);
return 0;
}
void display(const string sa[], int n)
{
for(int i = 0; i < n; i++)
cout << i + 1 << ": " << sa[i] << endl;
}
函数与array对象
#include <iostream>
#include <string>
#include <array>
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> da);
int main(void)
{
array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(array<double, Seasons> *pa)
{
for(int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(array<double, Seasons> da)
{
double total = 0.0;
cout << "EXPENSES:" << endl;
for(int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total expenses: " << total << endl;
}
递归
#include <iostream>
using namespace std;
void countdown(int n);
int main(void)
{
countdown(4);
return 0;
}
void countdown(int n)
{
cout << "Counting down.... " << n << "(n at address: " << &n << ")" << endl; //statement1
if(n > 0)
countdown(n-1);
cout << n << ": Kadoom" << "(n at address: "<< &n << ")" << endl; //statement2
}
不同层变量的地址不同 不是一个值
#include <iostream>
using namespace std;
void countdown(int n);
int main(void)
{
countdown(4);
return 0;
}
void countdown(int n)
{
cout << "Counting down.... " << n << "(n at address: " << &n << ")" << endl; //statement1
if(n > 0)
countdown(n-1);
cout << n << ": Kadoom" << "(n at address: "<< &n << ")" << endl; //statement2
}
调用自己两次
#include <iostream>
using namespace std;
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int levels);
int main(void)
{
char ruler[Len];
for(int i = 0; i < Len; i++)
ruler[i] = ' ';
int min = 0;
int max = Len - 2;
ruler[Len-1] = '\0';
ruler[min] = ruler[max] = '|';
cout << ruler << endl;
for(int i = 1; i <= Divs; i++)
{
subdivide(ruler, min, max, i);
cout << ruler << endl;
}
return 0;
}
void subdivide(char ar[], int low, int high, int levels)
{
if(levels == 0)
return;
int mid = (low + high) / 2;
ar[mid] = '|';
subdivide(ar, low, mid, levels-1);
subdivide(ar, mid, high, levels-1);
}
函数指针
函数的地址就是函数名 int fuck(int a)中fuck是函数地址 而fuck(int a)是函数的返回值
int fuck(int a)的指针声明应该是 int (*pt)(int a) *pt是函数 pt是指针
#include <iostream>
using namespace std;
double Rick(int lines);
double Jack(int lines);
void estimate(int lines, double (*pf)(int));
int main(void)
{
int code;
cout << "How many lines of code do you need?: ";
cin >> code;
cout << "Here is Rick's estimate: " << endl;
estimate(code, Rick);
cout << "Here is Jack's estimate: " << endl;
estimate(code, Jack);
return 0;
}
double Rick(int lines)
{
return lines * 0.05;
}
double Jack(int lines)
{
return 0.03 * lines + 0.0004 * lines * lines;
}
void estimate(int lines, double (*pf)(int))
{
cout << lines << " lines code will take " << (*pf)(lines) << " seconds." << endl;
}