跟着c++Primer Plus 第六版 学编程----循环和关系表达式 5.9编程练习
1、编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间的所有整数之和。这里假设先输入最小整数。
# include <iostream>
int main ( )
{
using namespace std;
int i, j, n;
n = 0 ;
cin >> i >> j;
for ( ; i <= j; i++ )
n += i;
cout << n << endl;
return 0 ;
}
2、使用 array 对象和 long double 重新程序清单5.4,并计算100!的值。
# include <iostream>
# include <array>
const int ArSize = 101 ;
int main ( )
{
using namespace std;
array< long double , ArSize> factorials;
factorials[ 0 ] = factorials[ 1 ] = 1L ;
for ( int i = 2 ; i < ArSize; i++ )
factorials[ i] = i * factorials[ i - 1 ] ;
for ( int i = 0 ; i < 16 ; i++ )
cout << i << "! = " << factorials[ i] << endl;
cout << "100! = " << factorials[ 100 ] << endl;
return 0 ;
}
3、编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。
# include <iostream>
const int ArSize = 101 ;
int main ( )
{
using namespace std;
int i;
cout << "Enter num: " ;
cin >> i;
while ( i != 0 )
{
cout << "程序在此处:" << i << endl;
cout << "Enter num: " ;
cin >> i;
}
return 0 ;
}
4、Daphne 以10%的单利投资了100美元。也就是说每一年的利润都是投资额的10%,即每年10美元:利息 = 0.10 x 原始存款。而 Cleo 以5%的复利投资了100美元:利息 = 0.05 x 当前存款。请编写一个程序,计算多少年后,Cleo 投资价值才能超过Daphne 的投资价值,并显示两人的投资价值。
# include <iostream>
int main ( )
{
using namespace std;
double Daphne, Cleo;
int count;
count = 0 ;
for ( Daphne = 100 , Cleo = 100 ; Cleo <= Daphne; Daphne += 10 , Cleo += ( 0.05 * Cleo) )
count++ ;
cout<< "after " << count << " years" << endl;
cout << "Daphne = " << Daphne << " ," << "Cleo = " << Cleo << endl;
return 0 ;
}
5、假设销售 C++ For Fools 一书。请编写一个程序,输入全年中每个月的销售量。通过循环,使用初始化为月份字符的 char* 数组逐月进行提示,并将输入的数据储存在一个 int 数组中。然后。程序计算数组中各元素的总数,并报告这一年的销售情况。
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
const int ArSize = 12 ;
const string months[ ArSize] =
{
"January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December"
} ;
int sum, sales[ ArSize] ;
sum = 0 ;
for ( int i = 0 ; i < ArSize; i++ )
{
cout << "Please enter number of books sold (" ;
cout << months[ i] << "): " ;
cin >> sales[ i] ;
}
for ( int i = 0 ; i < ArSize; i++ )
{
sum += sales[ i] ;
}
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;
return 0 ;
}
6、完成编程练习5,但这一次使用一个二维数组来储存输入 ——3年中每个月销售量。程序将报告每年销售量及三年销售总量。
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
const int ArSize_0 = 3 ;
const int ArSize_1 = 12 ;
const string months[ ArSize_1] =
{
"January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December"
} ;
int sum, sale[ ArSize_0] [ ArSize_1] ;
int i, j;
int Sale_First_Year, Sale_Second_Year, Sale_Threeth_Year;
Sale_First_Year = Sale_Second_Year = Sale_Threeth_Year = 0 ;
sum = 0 ;
for ( i = 0 ; i < ArSize_0; i++ )
{
cout << "Number " << i+ 1 << " year:" << endl;
for ( j = 0 ; j < ArSize_1; j++ )
{
cout << "Please enter number of books sold (" ;
cout << months[ i] << "): " ;
cin >> sale[ i] [ j] ;
}
}
for ( i = 0 ; i < ArSize_0; i++ )
{
for ( j = 0 ; j < ArSize_1; j++ )
{
if ( i == 0 )
Sale_First_Year += sale[ i] [ j] ;
if ( i == 1 )
Sale_Second_Year += sale[ i] [ j] ;
if ( i == 2 )
Sale_Threeth_Year += sale[ i] [ j] ;
}
}
int All_Sale = Sale_First_Year + Sale_Second_Year + Sale_Threeth_Year;
cout << "First year sale book: " << Sale_First_Year << endl;
cout << "Second year sale book: " << Sale_Second_Year << endl;
cout << "Threeth year sale book: " << Sale_Threeth_Year << endl;
cout << "Three years sale book: " << All_Sale << endl;
return 0 ;
}
7、设计一个名为 car 的结构,用它储存下述有关汽车的信息:生产商、生产年份。编写一个程序,向用户询问有多少辆车。随后,程序使用 new 来创建一个相应数量的 car 结构组成的动态数组。接下来程序提示用户输入每辆车的生产商和年份信息。
# include <iostream>
int main ( )
{
using namespace std;
const int ArSize = 20 ;
int ENTER;
struct car
{
char Car_Name[ 20 ] ;
int Car_Year;
} ;
cout << "How meny cars do you wish catalog? " ;
cin >> ENTER;
car* Pcar = new car[ ENTER] ;
for ( int i = 0 ; i < ENTER; i++ )
{
cout << "Car #" << i+ 1 << endl;
cin. get ( ) ;
cout << "Please enter the make : " ;
cin. getline ( Pcar[ i] . Car_Name, ArSize) ;
cout << "Please enter the year made: " ;
cin >> Pcar[ i] . Car_Year;
}
for ( int i = 0 ; i < ENTER; i++ )
{
cout << Pcar[ i] . Car_Year << " " << Pcar[ i] . Car_Name << endl;
}
return 0 ;
}
8、编写一个程序,它使用一个 char 数组和循环来每次读取一个单词,直到用户输入 done 为止。随后,该程序指出用户输入了多少单词。下面是该程序的运行情况:
# include <iostream>
# include <string>
int main ( )
{
using namespace std;
int count = 0 ;
char s1 = 'done' ;
char Search[ 20 ] ;
cout << "Enter words (to stop, type the word done):" << endl;
while ( strcmp ( Search, "done" ) != 0 )
{
cin >> Search;
++ count;
}
cout << "You entered a total of " << count - 1 << " words." << endl;
return 0 ;
}
9、编写一个满足前一个练习中描述的程序,但使用 string 对象而不是数组。请使用头文件 string ,并使用关系运算符来进行比较测试。
# include <iostream>
# include <string>
using namespace std;
int main ( )
{
string Search;
unsigned int count = 0 ;
cout << "Enter words (to stop, type the word done):" << endl;
while ( cin >> Search)
{
if ( "done" == Search)
{
break ;
}
++ count;
}
cout << "You entered a total of " << count << " words." << endl;
return 0 ;
}
10、编写一个嵌套循环程序,要求用户输入一个值,指出要显示多少行。然后,将程序显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推星号不够加句点。
# include <iostream>
# include <string>
int main ( )
{
using namespace std;
int Enter;
cout << "Enter number of rows: " ;
cin >> Enter;
for ( int i = Enter; i >= 0 ; i-- )
{
for ( int j = 0 ; j < Enter; j++ )
{
if ( j < i)
{
cout << ". " ;
}
else
cout << "* " ;
}
cout << endl;
}
return 0 ;
}