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

来源:互联网 发布:苹果app看书软件 编辑:程序博客网 时间:2024/05/16 01:31

注意,如果要将代码拷回去测试的话,注意输入的测试数据,因为我将几个问题融合在一起了,所以如果不看懂程序就输入数据进行测试的话,可能会出错

Code:
  1. #include<iostream>  
  2. #include<climits>     
  3. #include<cctype>   //支持一些特殊函数,详见程序  
  4. #include<fstream>  //用于文件输入输出  
  5. #include<cstdlib> //支持exit()的使用   
  6.   
  7. using namespace std;   
  8. const int SIZE = 60;   
  9.   
  10. bool is_int(double);   
  11.   
  12. int main()   
  13. {   
  14.  ////////// c++ 递增++ 和 递减--的使用注意事项 /////////////////   
  15.     int guests = 8;   
  16.     while (guests++ < 10)   
  17.         cout << "guests = " << guests << endl;   
  18.     int x = 3;   
  19.     int y;   
  20.     y = (4 + x++) + (6 + x++);    // 这种表达式中在计算表达式后,x递增两次,   
  21.     cout << "y = " << y << endl;  // 不是在计算每个子表达式后将x的值递增。   
  22.     cout << "x = " << x << endl;   
  23.     y = (4 + ++x) + (6 + ++x);    //在这里,是先将x的值递增两次,变成7之后在参与表达式运算   
  24.     cout << "y = " << y << endl;    
  25.     cout << "x = " << x << endl;   
  26.   
  27.     //////// 复合语句块,下面这一个例子是绝大多数新手没有想过的 ///////////////   
  28.     int m = 20;   
  29.     {   
  30.         int n = 100;   
  31.         cout << "m = " << m << endl;   
  32.         cout << "n = " << n << endl;   
  33.     }   
  34.     cout << "m = " << m << endl;   
  35. //  cout << n << endl;    这里会报错,说n没有定义,可见n只是内部语句块中的变量,   
  36.                        // 内部语句执行完后,变量释放   
  37.   
  38.     ////////// 文件尾条件 ///////////////////////////////////   
  39.     char ch;   
  40.     int count = 0;   
  41.     cin.get(ch);   
  42.     while(cin.fail() == false//EOF结束输入   
  43.     {   
  44.         cout << ch;   
  45.         ++count;               // 注意,这里计数的时候将换行符计入了     
  46.         cin.get(ch);   
  47.     }   
  48.     cout << endl << count << "characters read/n";   
  49.     // 上面这个例子的另一种表达方式 /////////   
  50.     cin.clear();      // 刚开始这里没有加这行代码,结果阻断了后面的输入,   
  51.     count = 0;            //估计EOF遗留在输入缓存中,直接赋给了ch   
  52.     ch = cin.get();   
  53.     while(ch != EOF)   
  54.     {   
  55.         cout.put(ch);   
  56.         ++count;   
  57.         ch = cin.get();   
  58.     }   
  59.     cout << endl << count << "characters read/n";       
  60.   
  61.     ///////// 一个友好的程序 ////////////////////////   
  62.     cin.clear();            //测试的时候没有加这个,结果导致后面的while循环是一个死循环       
  63.   
  64.     double num;   
  65.     cout << "Yo,dude! Enter an integer value: ";   
  66.     cin >> num;   
  67.     while(!is_int(num))  // 如果num超过int的表示范围,继续   
  68.     {   
  69.         cout << "Out of range -- please try again: ";   
  70.         cin >> num;   
  71.     }   
  72.     int val = int(num);   
  73.     cout << "You've entered the integer " << val << "/nBye/n";   
  74.   
  75.     /* 如果给读取int值的程序输入一个过大的值,很多c++实现只是将这个值截短为合适的大小,并不会通知  
  76.       丢失了数据。该程序避免了这样的问题,它首先将可能的int值作为double值来读取。然后自定义一个  
  77.       判断函数is_int来判断*/  
  78.   
  79.    //////////  字符函数库cctype的使用 ----该函数库我学c语言的时候就没用过,但是熟悉了很好用/////////   
  80.     cin.clear();   
  81.     cout << endl << "Enter text for analysis,and type @ "  
  82.                 "to terminate input./n";   
  83.     char temp;   
  84.     int whitespace = 0;   
  85.     int digits = 0;   
  86.     int chars = 0;   
  87.     int punct = 0;   
  88.     int others = 0;   
  89.   
  90.     cin.get(temp);   
  91.     while(temp != '@')   
  92.     {   
  93.         if(isalpha(temp))   
  94.             chars++;   
  95.         else if(isspace(temp))   
  96.             whitespace++;             //大家试一下,输出结果中whitespace每次都会多1,为什么呢?   
  97.         else if(isdigit(temp))   
  98.             digits++;   
  99.         else if(ispunct(temp))   
  100.             punct++;   
  101.         else  
  102.             others++;   
  103.         cin.get(temp);   
  104.     }   
  105.     cout << chars <<" letters, "  
  106.         << whitespace << " whitespace, "  
  107.         << digits << " digits, "  
  108.         << punct << " punctuations, "  
  109.         << others << " others./n";   
  110.        /* 该字符库中还有一些其他的判定函数,有兴趣的可以上网查阅,或者调出该函数库*/  
  111.   /////////////////// 简单文件输入/输出 ///////////////////////////   
  112.     cin.get();   
  113.     char automobile[50];   
  114.     int year;   
  115.     double a_price;   
  116.     double d_price;   
  117.   
  118.     ofstream outFile;   
  119.     outFile.open("carinfo.txt");   
  120.        
  121.     cout << endl << "Enter the make and model of automobile: ";   
  122.     cin.getline(automobile,50);   
  123.     cout << "Enter the model year: ";   
  124.     cin >> year;   
  125.     cout << "Enter the original asking price: ";   
  126.     cin >>a_price;   
  127.     d_price = 0.913 * a_price;   
  128.        
  129.     /* 通过cout将输出打印在屏幕上*/  
  130.        
  131.      cout << fixed;   
  132.      cout.precision(2);      // 输出的精度限制在这里,保留到小数点后两位数字   
  133. //   cout.setf(ios_base::showpoint);   
  134.      cout << "Make and model: " << automobile << endl;   
  135.      cout << "Year: " << year << endl;   
  136.      cout << "Was asking $" << a_price << endl;   
  137.      cout << "Now asking $" << d_price << endl;   
  138.       
  139.     /* 通过文件将输出打印到屏幕上*/  
  140.         
  141.      outFile << fixed;   
  142.      outFile.precision(2);   
  143. //   outFile.setf(ios_base::showpoint);   
  144.      outFile << "Year: " << year << endl;   
  145.      outFile << "was asking $" << a_price << endl;   
  146.      outFile << "Now asking $" << d_price << endl;   
  147.         
  148.      outFile.close();   
  149.  /**  
  150.    *    在上面的一段代码中,有点知识点:  
  151.         cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); 什么意思?  
  152.         setf()是追加标志字的函数,而flags()是设置标志字  
  153.         fixed标志是以定点形式显示浮点数  
  154.         showpoint标志是强制显示小数点  
  155.         precision就是精度,表示输出多少小数位。  
  156.  
  157.         以下是一些常见的控制函数的:   
  158.  
  159.         dec 置基数为10 相当于"%d"   
  160.         hex 置基数为16 相当于"%X"   
  161.         oct 置基数为8 相当于"%o"   
  162.         setfill(c) 设填充字符为c   
  163.         setprecision(n) 设显示小数精度为n位   
  164.         setw(n) 设域宽为n个字符   
  165.         这个控制符的意思是保证输出宽度为n。如:   
  166.         cout<<setw(3)<<1<<setw(3)<<10<<setw(3)<<100; 输出结果为   
  167.         1 10100 (默认是右对齐)当输出长度大于3时(<<1000),setw(3)不起作用。   
  168.         setioflags(ios::fixed) 固定的浮点显示   
  169.         setioflags(ios::scientific) 指数表示   
  170.         setiosflags(ios::left) 左对齐   
  171.         setiosflags(ios::right) 右对齐   
  172.         setiosflags(ios::skipws 忽略前导空白   
  173.         setiosflags(ios::uppercase) 16进制数大写输出   
  174.         setiosflags(ios::lowercase) 16进制小写输出   
  175.         setiosflags(ios::showpoint) 强制显示小数点   
  176.         setiosflags(ios::showpos) 强制显示符号   
  177.   **/  
  178.   
  179.         /////////////////// 文件输出的例子 ///////////////////////   
  180.         cin.get();   
  181.         char filename[SIZE];   
  182.         ifstream inFile;   
  183.   
  184.         cout << "Enter name of data file: ";   
  185.         cin.getline(filename, SIZE);   
  186.         inFile.open(filename);   
  187.         if(!inFile.is_open())   //如果所使用的编译器不支持inFile.is_open(),那么就用inFile.good();   
  188.         {   
  189.             cout << "Could not open the file " << filename << endl;   
  190.             cout << "Program terminating./n";   
  191.             exit(EXIT_FAILURE);   
  192.         }   
  193.         double value;   
  194.         double sum = 0.0;   
  195.         int fcount = 0;   
  196.   
  197.         inFile >> value;   
  198.         while(inFile.good())  //文件没有读完   
  199.         {   
  200.             ++fcount;   
  201.             sum += value;   
  202.             inFile >> value;   
  203.         }   
  204.         if(inFile.eof())    
  205.           cout << "End of file reached./n";   
  206.         else if(inFile.fail())    
  207.             cout << "Input terminated by data mismatch./n";   
  208.         else  
  209.             cout << "Input terminated for unknown reason./n";   
  210.         if(fcount == 0)   
  211.             cout << "No data processed./n";   
  212.         else  
  213.         {   
  214.             cout << "Items read: " << fcount << endl;   
  215.             cout << "Sum: " <<sum << endl;   
  216.             cout << "Average: " << sum/fcount <<endl;   
  217.         }   
  218.         inFile.close();   
  219.   
  220.         return 0;   
  221. }          
  222.   
  223. bool is_int(double x)   
  224. {   
  225.     if (x <= INT_MAX && x >= INT_MIN)   
  226.         return true;   
  227.     else  
  228.         return false;   
  229. }