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";
}
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";
}
相关文章推荐
- C++基础语法知识点归纳IV
- C++基础语法知识点归纳II
- C++基础语法知识点归纳
- C++基础语法知识点归纳V
- c++基础学java知识点(基本语法)
- c++基础学java知识点(类的基本语法)
- C++ 1. 基础语法 输入输出、流程控制、初识类
- 给Java新手的一些建议——Java知识点归纳(Java基础部分)
- C++语法基础--模板与泛型编程--函数模板,类模板,模板形参,非模板形参
- 《专题:C++语法基础》篇四:C++分支程序设计
- C++_01_基础语法
- C++_02_基础语法
- C/C++基础知识点总结
- CSS基础语法和CSS经常用到的知识点总结
- c++模板的语法基础
- C++语法基础--类(class)--类型别名,this指针,初始化式,友元的一些注意细节
- 一个基础但是隐晦的c++语法问题
- C++基础入门教程(七):一些比较特别的基础语法总结
- C++语法基础--智能指针
- C++基础知识容易忽略的知识点