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

来源:互联网 发布:抽签软件 编辑:程序博客网 时间:2024/04/28 07:22

下面是函数运用的部分, 代码都是很简单的,很易懂,所以没有做太多的解释

Code:
  1. #include<iostream>   
  2. const int Max = 5;   
  3. using namespace std;   
  4.   
  5.   /////////////////// 函数与数组 ////////////////   
  6. int fill_array(double ar[], int limit);   
  7. void show_array(const double ar[], int n);   
  8. void revalue(double r, double ar[], int n);   
  9.   
  10. int main()   
  11. {   
  12.     double properties[Max];   
  13.   
  14.     int size = fill_array(properties,Max);   
  15.     show_array(properties, size);   
  16.     cout << "Enter revaluation factor: ";   
  17.     double factor;   
  18.     cin >> factor;   
  19.     revalue(factor, properties, size);   
  20.     show_array(properties,size);   
  21.     cout << "Done./n";   
  22.     return 0;   
  23. }   
  24.                                                                                                                                                                                                                
  25. int fill_array(double ar[], int limit)   
  26. {   
  27.     double temp;   
  28.     int i;   
  29.     for(i = 0; i < limit; i++)   
  30.     {   
  31.         cout << "Enter value #" << (i+1) <<": ";   
  32.         cin >> temp;   
  33.         if(!cin)   
  34.         {   
  35.             cin.clear();   
  36.             while(cin.get()!='/n')   
  37.                 continue;   
  38.             cout << "Bad input; input process terminated./n";   
  39.             break;   
  40.         }   
  41.         else if(temp < 0)   
  42.             break;   
  43.         ar[i] = temp;   
  44.     }   
  45.     return i;   
  46. }   
  47.   
  48. void show_array(const double ar[],int n)  //这里用到了const,所以不能改数组的值   
  49. {   
  50.     for(int i=0; i<n; i++)   
  51.     {   
  52.         cout << "Property #" << (i+1) << ": $";   
  53.         cout << ar[i] << endl;   
  54.     }   
  55. }   
  56.   
  57. void revalue(double r, double ar[], int n)   
  58. {   
  59.     for (int i=0; i<n; i++)   
  60.     {   
  61.         ar[i] *=r;   
  62.     }   
  63. }  
Code:
  1. #include<iostream>   
  2.   
  3.   
  4.   /////////////////// 返回c-风格字符串的函数 ////////////////   
  5. char *buildstr(char c,int n);   
  6.   
  7. int main()   
  8. {   
  9.     using namespace std;   
  10.     int times;   
  11.     char ch;   
  12.   
  13.     cout << "Enter a charater: ";   
  14.     cin >> ch;   
  15.     cout << "Enter an integer: ";   
  16.     cin >> times;   
  17.     char *ps=buildstr(ch,times);   
  18.     cout << ps << endl;   
  19.     delete[] ps;   
  20.     ps = buildstr('+',20);   
  21.     cout << ps << "-DONE-" << ps << endl;   
  22.     delete[] ps;   
  23.     return 0;   
  24. }   
  25.   
  26. char *buildstr(char c, int n)   
  27. {   
  28.     char *pstr = new char[n+1];   
  29.     pstr[n] = '/0';   
  30.     while(n-- >0)   
  31.         pstr[n] = c;   
  32.     return pstr;   
  33. }   
Code:
  1. #include<iostream>   
  2. #include<cmath>   
  3. using namespace std;   
  4.   
  5.   /////////////////// 一个处理结构的范例 ////////////////   
  6. struct polar   
  7. {   
  8.     double distance;   
  9.     double angle;   
  10. };   
  11. struct rect   
  12. {   
  13.     double x;   
  14.     double y;   
  15. };   
  16.   
  17. polar rect_to_polar(rect xypos);   
  18. void show_polar(polar dapos);   
  19.   
  20. int main()   
  21. {   
  22.     rect rplace;   
  23.     polar pplace;   
  24.   
  25.     cout << "Enter the x and y value: ";   
  26.     while(cin >> rplace.x >> rplace.y)   
  27.     {   
  28.         pplace = rect_to_polar(rplace);   
  29.         show_polar(pplace);   
  30.         cout << "Next two numbers(q to quit): ";   
  31.     }   
  32.     cout << "Done./n";   
  33.     return 0;   
  34. }   
  35.   
  36. polar rect_to_polar(rect xypos)   
  37. {   
  38.     polar answer;   
  39.   
  40.     answer.distance =    
  41.         sqrt(xypos.x*xypos.x + xypos.y * xypos.y);   
  42.     answer.angle = atan2(xypos.y, xypos.x);   
  43.     return answer;   
  44. }   
  45.   
  46. void show_polar(polar dapos)   
  47. {   
  48.     const double Rad_to_deg = 57.29577951;   
  49.   
  50.     cout << "distance = " << dapos.distance ;   
  51.     cout << ",angle = " << dapos.angle * Rad_to_deg;   
  52.     cout << "degrees/n";   
  53. }  
Code:
  1. #include<iostream>   
  2.   
  3. using namespace std;   
  4.   
  5.   /////////////////// 一个包含多个递归调用的递归范例 ////////////////   
  6. const int Len = 66;   
  7. const int Divs = 6;   
  8.   
  9. void subdivide(char ar[],int low, int high, int level);   
  10.   
  11. int main()   
  12. {   
  13.     char ruler[Len];   
  14.     int i;   
  15.     for(i = 1; i< Len -2; i++)   
  16.         ruler[i] = ' ';   
  17.     ruler[Len-1] = '/0';   
  18.     int max = Len - 2;   
  19.     int min = 0;   
  20.     ruler[min] = ruler[max] = '|';   
  21.     cout << ruler << endl;   
  22.   
  23.     for(i = 1; i <= Divs; i++)   
  24.     {   
  25.         subdivide(ruler,min,max,i);   
  26.         cout << ruler << endl;   
  27.         for(int j = 1; j < Len -2; j++)   
  28.         {   
  29.             ruler[j] = ' ';   
  30.         }   
  31.     }   
  32.     return 0;   
  33. }   
  34.   
  35. void subdivide(char ar[], int low, int high, int level)   
  36. {   
  37.     if(level == 0)   
  38.         return;   
  39.     int mid = (high + low)/2;   
  40.     ar[mid] = '|';   
  41.     subdivide(ar,low,mid,level-1);   
  42.     subdivide(ar,mid,high,level-1);   
  43. }  
Code:
  1. #include<iostream>   
  2. double betsy(int);   
  3. double pam(int);   
  4.   
  5. using namespace std;   
  6.   
  7.   /////////////////// 函数指针的范例 ////////////////   
  8. void estimate(int lines, double (*pf)(int));   
  9.   
  10. int main()   
  11. {   
  12.     int code;   
  13.   
  14.     cout << "How many lines of code do you need? ";   
  15.     cin >> code;   
  16.     cout << "Here's Betsy's estimate: /n";   
  17.     estimate(code,betsy);   
  18.     cout << "Here's Pam's estimate: /n";   
  19.     estimate(code,pam);   
  20.     return 0;   
  21. }   
  22.   
  23. double betsy(int lns)   
  24. {   
  25.     return 0.05*lns;   
  26. }   
  27.   
  28. double pam(int lns)   
  29. {   
  30.     return 0.03 * lns + 0.0004 * lns * lns;   
  31. }   
  32.   
  33. void estimate(int lines, double (*pf)(int))   // 函数指针   
  34. {   
  35.     cout << lines << "lines will take ";   
  36.     cout << (*pf)(lines) << " hour(s)/n";   
  37. }  

 

原创粉丝点击