您的位置:首页 > 编程语言 > C语言/C++

C++基础语法知识点归纳III

2010-10-27 14:56 453 查看
下面是函数运用的部分, 代码都是很简单的,很易懂,所以没有做太多的解释

Code:

#include<iostream>

const int Max = 5;

using namespace std;

/////////////////// 函数与数组 ////////////////

int fill_array(double ar[], int limit);

void show_array(const double ar[], int n);

void revalue(double r, double ar[], int n);

int main()

{

double properties[Max];

int size = fill_array(properties,Max);

show_array(properties, size);

cout << "Enter revaluation factor: ";

double factor;

cin >> factor;

revalue(factor, properties, size);

show_array(properties,size);

cout << "Done./n";

return 0;

}

int fill_array(double ar[], int limit)

{

double temp;

int i;

for(i = 0; i < limit; i++)

{

cout << "Enter value #" << (i+1) <<": ";

cin >> temp;

if(!cin)

{

cin.clear();

while(cin.get()!='/n')

continue;

cout << "Bad input; input process terminated./n";

break;

}

else if(temp < 0)

break;

ar[i] = temp;

}

return i;

}

void show_array(const double ar[],int n) //这里用到了const,所以不能改数组的值

{

for(int i=0; i<n; i++)

{

cout << "Property #" << (i+1) << ": $";

cout << ar[i] << endl;

}

}

void revalue(double r, double ar[], int n)

{

for (int i=0; i<n; i++)

{

ar[i] *=r;

}

}

Code:

#include<iostream>

/////////////////// 返回c-风格字符串的函数 ////////////////

char *buildstr(char c,int n);

int main()

{

using namespace std;

int times;

char ch;

cout << "Enter a charater: ";

cin >> ch;

cout << "Enter an integer: ";

cin >> times;

char *ps=buildstr(ch,times);

cout << ps << endl;

delete[] ps;

ps = buildstr('+',20);

cout << ps << "-DONE-" << ps << endl;

delete[] ps;

return 0;

}

char *buildstr(char c, int n)

{

char *pstr = new char[n+1];

pstr
= '/0';

while(n-- >0)

pstr
= c;

return pstr;

}

Code:

#include<iostream>

#include<cmath>

using namespace std;

/////////////////// 一个处理结构的范例 ////////////////

struct polar

{

double distance;

double angle;

};

struct rect

{

double x;

double y;

};

polar rect_to_polar(rect xypos);

void show_polar(polar dapos);

int main()

{

rect rplace;

polar pplace;

cout << "Enter the x and y value: ";

while(cin >> rplace.x >> rplace.y)

{

pplace = rect_to_polar(rplace);

show_polar(pplace);

cout << "Next two numbers(q to quit): ";

}

cout << "Done./n";

return 0;

}

polar rect_to_polar(rect xypos)

{

polar answer;

answer.distance =

sqrt(xypos.x*xypos.x + xypos.y * xypos.y);

answer.angle = atan2(xypos.y, xypos.x);

return answer;

}

void show_polar(polar dapos)

{

const double Rad_to_deg = 57.29577951;

cout << "distance = " << dapos.distance ;

cout << ",angle = " << dapos.angle * Rad_to_deg;

cout << "degrees/n";

}

Code:

#include<iostream>

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];

int i;

for(i = 1; i< Len -2; i++)

ruler[i] = ' ';

ruler[Len-1] = '/0';

int max = Len - 2;

int min = 0;

ruler[min] = ruler[max] = '|';

cout << ruler << endl;

for(i = 1; i <= Divs; i++)

{

subdivide(ruler,min,max,i);

cout << ruler << endl;

for(int j = 1; j < Len -2; j++)

{

ruler[j] = ' ';

}

}

return 0;

}

void subdivide(char ar[], int low, int high, int level)

{

if(level == 0)

return;

int mid = (high + low)/2;

ar[mid] = '|';

subdivide(ar,low,mid,level-1);

subdivide(ar,mid,high,level-1);

}

Code:

#include<iostream>

double betsy(int);

double pam(int);

using namespace std;

/////////////////// 函数指针的范例 ////////////////

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;

}

double betsy(int lns)

{

return 0.05*lns;

}

double pam(int lns)

{

return 0.03 * lns + 0.0004 * lns * lns;

}

void estimate(int lines, double (*pf)(int)) // 函数指针

{

cout << lines << "lines will take ";

cout << (*pf)(lines) << " hour(s)/n";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: