C++ Primer Plus第六版编程练习---第5章 循环和关系表达式

1、

#include <iostream>

int main()
{
    int startNum = 0;
    int endNum = 0;
    std::cout << "please enter tow num:" << std::endl;
    std::cin >> startNum;
    std::cin >> endNum;

    long long sum = 0;
    for(int i = startNum; i <= endNum; i++)
        sum += i;

    std::cout << "the sum betwen " << startNum << " and " << endNum << "is: " << sum << std::endl;
    return 0;
}

 2、

 1 #include <iostream>
 2 #include <array>
 3 const int ArSize = 100;
 4  
 5 int main()
 6 {
 7     std::array<long double, ArSize> factorials;
 8     factorials[1] = factorials[0] = 1;
 9     for( int i = 2; i< ArSize; i++)
10     {
11         factorials[i] = i * factorials[i-1];
12     }
13 
14     std::cout << "100! is : " << factorials[99] << std::endl;
15     return 0;
16 }

3、

 1 #include <iostream>
 2  
 3 int main()
 4 {
 5     long double sum = 0;
 6     int tempInput = 0;
 7     std::cin >> tempInput;
 8     while(tempInput != 0)
 9     {
10         sum += tempInput;
11         std::cin >> tempInput;
12     }
13     std::cout << "sum of all Numbers is : " << sum << std::endl;
14     return 0;
15 }

4、

 1 #include <iostream>
 2  
 3 int main()
 4 {
 5     long double investmentOfDaphne = 100;
 6     long double investmentOfCleo = 100;
 7     int yeas = 0;
 8     while(investmentOfDaphne >= investmentOfCleo)
 9     {
10         investmentOfDaphne += 10;
11         investmentOfCleo = investmentOfCleo*1.05;
12         yeas += 1;
13     }
14     std::cout << "After " << yeas << "yeas, Cleo's investment exceed Daphne's. " << std::endl 
15         << "At that time, Cleo's investment is : " << investmentOfCleo << ", Daphne's investment is: " << investmentOfDaphne << std::endl;
16     return 0;
17 }

5、

 1 #include <iostream>
 2 #include <string>
 3 #include <array>
 4  
 5 int main()
 6 {
 7     int salesOfMonths[12];
 8     std::array<std::string, 12> months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 9     int sumOfSales = 0;
10     for(int i = 0; i < 12; i++)
11     {
12         std::cout << "pleas enter the valume of sales of " << months[i] << std::endl;
13         std::cin >> salesOfMonths[i];
14         sumOfSales += salesOfMonths[i];
15     }
16 
17     std::cout << "the valume of sales of <<C++ For Fools>> this year is : " << sumOfSales << std::endl;
18     return 0;
19 }

6、

 1 #include <iostream>
 2 #include <string>
 3 #include <array>
 4  
 5 int main()
 6 {
 7     int salesOfMonths[3][12];
 8     std::array<std::string, 12> months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 9     int sumOfSales[3] = {0, 0, 0};
10     std::string years[3] = {"first year", "second year", "third year"};
11     for(int j = 0; j < 3; j++)
12     {
13         for(int i = 0; i < 12; i++)
14         {
15             std::cout << "pleas enter the valume of sales of " << months[i] << " of "<< years[j] << std::endl;
16             std::cin >> salesOfMonths[j][i];
17             sumOfSales[j] += salesOfMonths[j][i];
18         }
19         std::cout << "the valume of sales of <<C++ For Fools>> of "<<  years[j] << " is : " << sumOfSales[j] << std::endl;
20     }
21 
22     return 0;
23 }

7、

 1 #include <iostream>
 2 #include <string>
 3 #include <array>
 4 
 5 struct car{
 6     std::string producer;
 7     int madeYear;
 8 };
 9 
10 int main()
11 {
12     int countOfCar = 0;
13     std::cout << "How many cars do you have ?" << std::endl;
14     std::cin >> countOfCar;
15     std::cin.get();   //非常关键的cin.get(),貌似每次使用getline之前都要有cin.get()
16     if(countOfCar <= 0)
17     {
18         return -1;
19     }
20 
21     struct car *pYourCars = new struct car[countOfCar];
22     for(int i = 0; i < countOfCar; i++)
23     {
24         std::cout << "Car #" << (i+1) << ":" << std::endl;
25         std::cout << "Please enter the make: ";
26         getline(std::cin, pYourCars[i].producer);
27         std::cout << "Please enter the year made: ";
28         std::cin >> pYourCars[i].madeYear;
29         std::cin.get();   //非常关键的cin.get(),貌似每次使用getline之前都要有cin.get()
30     }
31 
32     std::cout << "Here is your collection: " << std::endl;
33     for(int i = 0; i < countOfCar; i++)
34     {
35         std::cout << pYourCars[i].madeYear << " " << pYourCars[i].producer << std::endl;
36     }
37 
38     delete [] pYourCars;
39     return 0;
40 }
View Code

8、

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 
 5 int main()
 6 {
 7     char word[100]={0};
 8     int countWords = 0;
 9     std::cout<< "Enter words (to stop, type the word done):" << std::endl;
10     while(std::cin>>word)
11     {
12         if (strcmp(word,"done"))
13         {
14             countWords++;
15         }
16         else
17         {
18             break;
19         }
20     }
21     std::cout << "Your entered a total of " << countWords << " words." << std::endl;
22     return 0;
23 }

9、

 1 #include <iostream>
 2 #include <string>
 3 
 4 
 5 int main()
 6 {
 7     std::string word;
 8     int countWords = 0;
 9     std::string strEnd("done");
10     std::cout<< "Enter words (to stop, type the word done):" << std::endl;
11     while(std::cin>>word)
12     {
13         if (word == strEnd)
14         {
15             break;
16         }
17         countWords++;
18     }
19     std::cout << "Your entered a total of " << countWords << " words." << std::endl;
20     return 0;
21 }

10、

 1 #include <iostream>
 2 
 3 int main()
 4 {
 5     int rows = 0;
 6     std::cout<<"Enter number of rows: " << std::endl;
 7     std::cin >> rows;
 8     for(int i=1; i<=rows; i++)
 9     {
10         for(int j=1; j<=rows-i;j++)
11         {
12             std::cout << ".";
13         }
14         for(int k=1; k<=i; k++)
15         {
16             std::cout << "*";
17         }
18         std::cout<<std::endl;
19     }
20     return 0;
21 }

 

posted on 2018-06-29 23:29  草丛里的蚂蚱  阅读(110)  评论(0)    收藏  举报

导航