第五章编程题:
(1)
第一种解法:
#include <iostream>
using namespace std ;
int main()
{
cout << "Please input two integers: " << endl ;
int a, b ;
cin >> a ;
cin >> b ;
cout << "The sum of numbers between them: " ;
for (int i = (a+1); i<=b; i++)
a+=i ;
cout << a << endl ;
return 0 ;
}
第二种解法:
#include <iostream>
using namespace std ;
int main()
{
cout << "Please enter two integers: " << endl ;
int a, b, c;
cin >> a ;
cin >> b ;
c = a ;
while ( c < b )
{
c++ ;
a += c ;
}
cout << "The sum of these numbers between them: " ;
cout << a << endl ;
return 0 ;
}
第三种解法:
#include <iostream>
using namespace std ;
int main()
{
cout << "Please input two integers: " << endl ;
int a, b, c ;
cin >> a ;
cin >> b ;
c = a ;
do
{
a += ++c ;
} while (c < b) ;
cout << "The sum of numbers between them: " << endl ;
cout << a << endl ;
return 0 ;
}
(2)
#include <iostream>
#include <array>
using namespace std ;
const int ArraySize = 100 ;
int main()
{
array<long double, ArraySize> factorial ;
factorial[1] = factorial[0] = 1 ;
for (int i = 1; i<ArraySize; i++)
factorial[i] = i * factorial[i-1] ;
for (int i = 0; i<ArraySize; i++)
cout << i << "! = " << factorial[i] << endl ;
cout << "100! = " << 100 * factorial[99] << endl ;
return 0 ;
}
(3)
第一种解法:
#include <iostream>
using namespace std ;
int main()
{
int a=0, b ;
cout << "Please enter numbers casually: " << endl ;
cin >> b ;
while ( b != 0)
{
cout << "Now the sum = "
<< (a+=b) << endl ;
cin >> b ;
}
cout << "That's the end! " << endl ;
}
第二种解法:
#include <iostream>
using namespace std ;
int main()
{
cout << "Please enter numbers casually: " << endl ;
int a=0, b ;
do
{
cin >> b ;
a += b ;
cout << "The sum of them = "
<< a << endl ;
} while ( b != 0 ) ;
cout << "That's the end!" << endl ;
}
第三种解法:
#include <iostream>
using namespace std ;
int main()
{
cout << "Please enter numbers casually: " << endl ;
int a = 0, b ;
for (cin >> b; (b != 0)&&(a+=b);)
{
cout << "The sum of them = " << a << endl ;
cin >> b ;
}
cout << "That's the end ! " << endl ;
return 0 ;
}
(4)
第一种解法:
#include <iostream>
using namespace std ;
int main()
{
double Daphne, Cleo, i=0 ;
Daphne = Cleo = 100 ;
while (!(Cleo > Daphne))
{
i++ ;
Daphne += 100 * 0.10 ;
Cleo += 0.05 * Cleo ;
cout << "Now Daphne have " << Daphne << " dallors.\n"
<< "Cleo have " << Cleo << " dallors.\n" ;
}
cout << "After " << i << " years, Cleo's money beyond Daphne's.\n" ;
}
第二种解法:
#include <iostream>
using namespace std ;
int main()
{
double Daphne, Cleo, i = 0;
for (Daphne = Cleo = 100;
Cleo <= Daphne ;
i++, Daphne += 0.10 * 100, Cleo += 0.05 * Cleo) ;
cout << "After " << i << " years, "
<< "Cleo's money beyond Daphne's.\n"
<< "Now Daphne have " << Daphne << " dollars.\n"
<< "Cleo have " << Cleo << "dollars.\n" ;
}
(5)
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string month[12] = {"Jenuary", "Febrary", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"} ;
int books[12] = {0}, sum = 0 ;
cout << "Enter how much books have sold: " << endl ;
for(int i=1; i<=12; i++)
{
cout << month[i-1] << " : " ;
cin >> books[i-1] ;
sum += books[i-1] ;
}
cout << "This years have sold " << sum << " \"C++ for Fools\".\n" ;
}
(6)文章来源:https://www.toymoban.com/news/detail-563878.html
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string month[12] = {"Jenuary", "Febrary", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"} ;
int books[3][12] = {0} ;
int sum[3] = {0} ;
for (int i=1; i<=3; i++)
{
cout << "In year " << i << " :\n" ;
for (int j=1; j<=12; j++)
{
cout << month[j-1] << " : " ;
cin >> books[i-1][j-1] ;
sum[i-1] += books[i-1][j-1] ;
}
cout << "The sum of this year = " << sum[i-1] << endl ;
}
cout << "The sum of 3 years = " << sum[0] + sum[1] + sum[2]
<< endl ;
return 0 ;
}
(7)文章来源地址https://www.toymoban.com/news/detail-563878.html
#include <iostream>
#include <string>
using namespace std ;
struct Car{
string brand ;
int year ;
} ;
int main()
{
int c ;
cout << "How many cars do you wish to catalog? " ;
cin >> c ;
Car* k = new Car[c] ;
for (int i=1; i<=c; i++)
{
cout << "Car #" << i << ":\n" ;
cout << "Please enter the make: " ;
cin.get() ; // 这是非常必要的,用来消除换行符
getline(cin, k[i-1].brand) ;
cout << "Please enter the year made: " ;
cin >> k[i-1].year ;
}
cout << "Here is your collection: " << endl ;
for (int i=1; i<=c; i++)
{
cout << k[i-1].year << " " << k[i-1].brand << endl ;
}
delete [] k ;
}
到了这里,关于C++ Primer Plus笔记: 2023.07.14的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!