第五章 循环和关系表达式
#include <iostream>
using namespace std;
int main ( )
{
int a, b;
int sum = 0 ;
cout << "Please enter the first integer: " ;
cin >> a;
cout << "Please enter the second integer: " ;
cin >> b;
while ( a > b)
{
cout << "WRONG INPUT!" << endl;
cout << "Please enter the first integer: " ;
cin >> a;
cout << "Please enter the second integer: " ;
cin >> b;
}
for ( int i= a; i<= b; i++ )
sum + = i;
cout << "The sum is: " << sum << endl;
return 0 ;
}
#include <iostream>
#include <array>
using namespace std;
const int ArSize = 16 ;
int main ( )
{
array< long double , ArSize> factorials;
factorials[ 1 ] = factorials[ 0 ] = 1 ;
for ( int i= 2 ; i< ArSize; i++ ) factorials[ i] = i* factorials[ i- 1 ] ;
for ( int i= 0 ; i< ArSize; i++ ) cout << i << "! = " << factorials[ i] << endl;
long double ans = factorials[ ArSize- 1 ] ;
for ( int i= ArSize; i<= 100 ; i++ ) ans * = i;
cout << "100! = " << ans;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
long long sum = 0 ;
long long tmp;
cin >> tmp;
while ( tmp != 0 )
{
sum + = tmp;
cout << sum << endl;
cin >> tmp;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
double daphne = 100 , cleo = 100 ;
int cnt_yr = 0 ;
while ( cleo <= daphne)
{
daphne + = 10 ;
cleo + = cleo* 0.05 ;
cnt_yr++ ;
}
cout << cnt_yr << endl;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
char * month[ 12 ] = {
"January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" } ;
int sales[ 12 ] ;
int sum = 0 ;
for ( int i= 0 ; i< 12 ; i++ )
{
cout << month[ i] << ": " ;
cin >> sales[ i] ;
sum + = sales[ i] ;
}
cout << "sum: " << sum << endl;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
char * month[ 12 ] = {
"January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" } ;
int sales[ 3 ] [ 12 ] ;
int sum[ 3 ] = {
0 } ;
int total = 0 ;
for ( int i= 0 ; i< 3 ; i++ )
{
cout << "Year " << i+ 1 << ": " << endl;
for ( int j= 0 ; j< 12 ; j++ )
{
cout << month[ j] << ": " ;
cin >> sales[ i] [ j] ;
sum[ i] + = sales[ i] [ j] ;
}
}
for ( int i= 0 ; i< 3 ; i++ )
{
cout << "Year " << i+ 1 << ": " << sum[ i] << endl;
total + = sum[ i] ;
}
cout << "Total: " << total << endl;
return 0 ;
}
#include <iostream>
#include <string>
using namespace std;
struct car
{
string make;
int year;
} ;
int main ( )
{
int n;
cout << "How many cars do you wish to catalog? " ;
cin >> n;
car* Cars = new car [ n] ;
for ( int i= 0 ; i< n; i++ )
{
cout << "Car #" << i+ 1 << ":\n" ;
cout << "Please enter the make: " ;
cin. get ( ) ;
getline ( cin, Cars[ i] . make) ;
cout << "Please enter the year made: " ;
cin >> Cars[ i] . year;
}
cout << "Here is your collection: " << endl;
for ( int i= 0 ; i< n; i++ )
{
cout << Cars[ i] . year << " " << Cars[ i] . make << endl;
}
delete [ ] Cars;
return 0 ;
}
#include <iostream>
#include <cstring>
using namespace std;
int main ( )
{
char ch[ 20 ] ;
int cnt = 0 ;
cin >> ch;
while ( strcmp ( ch, "done" ) != 0 )
{
cnt++ ;
cin >> ch;
}
cout << "Total: " << cnt << " words" ;
return 0 ;
}
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ( )
{
string ch;
int cnt = 0 ;
cin >> ch;
while ( ch != "done" )
{
cnt++ ;
cin >> ch;
}
cout << "Total: " << cnt << " words" ;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "Enter number of rows: " ;
cin >> rows;
for ( int i= 0 ; i< rows; i++ )
{
for ( int j= 0 ; j< rows- i- 1 ; j++ ) cout << "." ;
for ( int k= rows- i- 1 ; k< rows; k++ ) cout << "*" ;
cout << endl;
}
return 0 ;
}