zoukankan      html  css  js  c++  java
  • 读C++ primer 的一些练习

    练习1.17 :编写程序, 要求用户输入一组数. 输出信息说明其中有多少个负数.

    下面的代码并不完美, 因为只能输入20个以内的数, 等以后找到解决办法再修改吧.

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 int main()
     7 {
     8     int array[20] = {0};
     9     int n;
    10     cout << "请输入数字的个数: " << endl;
    11     cin >> n;
    12     cout << "请输入这些数字: " << endl;
    13     for (int i = 0; i < n; ++i)
    14     {
    15         cin >> array[i];
    16     }
    17 
    18     int sum = 0;
    19 
    20     for (int i = 0; i < n; ++i)
    21     {
    22         if(array[i] < 0)
    23             sum++;
    24     }
    25 
    26     cout << "the num of negitive is  " << sum << endl;
    27     return 0;
    28 }

     这里还有一个方法,也有些不完美:

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 
     6 int main()
     7 {
     8 
     9     int sum = 0, value;
    10 
    11     while(cin >> value) //只有输入值不是int 类型值时, 循环才会结束
    12     {                   //这就需要在你输入完一组数后,再输入一个"非法"的数
    13         if(value < 0)   //以结束循环
    14             sum++;
    15     }
    16 
    17     cout << sum << endl;
    18     return 0;
    19 }

     练习1.19: 提示用户输入两个数, 并将这两个数范围的每个数写到标准输出, 每一行的输出不超过10 个数.

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6 
     7     int v1, v2;
     8     cout << "please enter two numbers: " << endl;
     9     cin >> v1 >> v2;
    10 
    11     int lower, upper;
    12     if (v1 <= v2)
    13     {
    14         lower = v1;
    15         upper = v2;
    16     }
    17     else
    18     {
    19         lower = v2;
    20         upper = v1;
    21     }
    22 
    23     for(int i = lower, count = 1; i <= upper; ++i, ++count)
    24     {
    25 
    26         if(count % 10 == 0) 
    27         {
    28             cout << i << endl;  //每计够10个数就换行一次.
    29 
    30         }
    31         else
    32         {
    33             cout << i << " "; 
    34 
    35         }
    36 
    37 
    38     }
    39 
    40 
    41     return 0;
    42 }

    还可以通过重置计数器的方法实现, 但感觉没有这个简便:

     1     int counter = 0;
     2     for(int i = lower; i <= upper; ++i)//只有输入值不是int 类型值时, 循环才会结束
     3     {
     4 
     5         if(counter < 10)               //
     6         {
     7             cout << i << " ";
     8 
     9         }
    10         else
    11         {
    12             cout << i << endl;
    13             counter = 0;
    14         }
    15     }

     练习1.23 编写程序, 读入几个具有相同ISBN的交易, 输出所有读入交易的和.

     1 #include <iostream>
     2 #include "Sales_item.h"
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     Sales_item book1, book2;
     9 
    10     if (cin >> book1)  //cin操作符返回的是bool值
    11     {
    12         while (cin >> book2)
    13         {
    14             if (book1.same_isbn(book2))
    15                 book1 += book2;
    16             else
    17             {
    18                 cout << "different ISBN" << endl;
    19 
    20 //              return -1;  //如果执行到这一句, 程序就会结束.
    21             }
    22 
    23             cout << book1 << endl;
    24         }
    25     }
    26 
    27 
    28     else
    29     {
    30         cerr << "No data ?" << endl;
    31 
    32         return -1;
    33     }
    34 
    35     return 0;
    36 }
  • 相关阅读:
    606. Construct String from Binary Tree
    557. Reverse Words in a String III
    551. Student Attendance Record I
    541. Reverse String II
    521. Longest Uncommon Subsequence I
    520. Detect Capital
    459. Repeated Substring Pattern
    人脸检测源码facedetection
    人脸检测的model类facemodel
    人脸检测解析json的工具类face_test
  • 原文地址:https://www.cnblogs.com/bigpo/p/4374795.html
Copyright © 2011-2022 走看看