第六章 分支语句和逻辑运算符
#include <iostream>
#include <cctype>
using namespace std;
int main ( )
{
char ch;
cin. get ( ch) ;
while ( ch != '@' )
{
if ( islower ( ch) ) ch = toupper ( ch) ;
if ( ! isdigit ( ch) ) cout << ch;
cin. get ( ch) ;
}
return 0 ;
}
#include <iostream>
#include <cctype>
using namespace std;
int main ( )
{
double arr[ 10 ] ;
double sum = 0.0 ;
int cnt = 0 , ans = 0 ;
while ( cnt < 10 && cin >> arr[ cnt] )
{
sum + = arr[ cnt] ;
cnt++ ;
}
double avg = sum/ cnt;
for ( int i= 0 ; i< cnt; i++ )
{
if ( arr[ i] > avg) ans++ ;
}
cout << "Avg = " << sum/ cnt << "\nCount = " << cnt << "\nThe number of those bigger than avg = " << ans << endl;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
char choice;
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore\tp) pianist\n" << "t) tree\t\tg) game" << endl;
do
{
cin >> choice;
switch ( choice)
{
case 'c' : cout << "I am not a carnivore." << endl; break ;
case 'p' : cout << "He wants to be a pianist" << endl; break ;
case 't' : cout << "A maple is a tree" << endl; break ;
case 'g' : cout << "This is a game" << endl; break ;
default : cout << "Please enter a c, p, t, or g: " ;
}
} while ( choice != 'c' && choice != 'p' && choice != 't' && choice != 'g' ) ;
return 0 ;
}
#include <iostream>
using namespace std;
const int strsize = 30 ;
struct bop
{
char fullname[ strsize] ;
char title[ strsize] ;
char bopname[ strsize] ;
int preference;
} ;
int main ( )
{
bop All[ 2 ] =
{
{
"aaaa" ,
"AAAA" ,
"1111" ,
0
} ,
{
"bbbb" ,
"BBBB" ,
"2222" ,
2
}
} ;
char ch;
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name\tb.display by title\nc.display by bopname\td.display by preference\nq.quit" << endl;
cout << "Enter your choice: " ;
while ( cin >> ch)
{
switch ( ch)
{
case 'a' :
{
for ( int i= 0 ; i< 2 ; i++ ) cout << All[ i] . fullname << endl;
break ;
}
case 'b' :
{
for ( int i= 0 ; i< 2 ; i++ ) cout << All[ i] . title << endl;
break ;
}
case 'c' :
{
for ( int i= 0 ; i< 2 ; i++ ) cout << All[ i] . bopname << endl;
break ;
}
case 'd' :
{
for ( int i= 0 ; i< 2 ; i++ )
{
switch ( All[ i] . preference)
{
case 0 : cout << All[ i] . fullname << endl; break ;
case 1 : cout << All[ i] . title << endl; break ;
case 2 : cout << All[ i] . bopname << endl; break ;
default : cout << "Wrong Preference" << endl; break ;
}
}
break ;
}
case 'q' : cout << "Bye!" << endl; break ;
default : cout << "Wrong input." << endl; break ;
}
if ( ch == 'q' ) break ;
else cout << "Next choice: " ;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
double income, ans;
cout << "Please enter your income: " ;
while ( cin >> income && income >= 0 )
{
ans = 0 ;
if ( income > 5000 ) ( income <= 15000 ) ? ans + = 0.1 * ( income- 5000 ) : ans + = 0.1 * 10000 ;
if ( income > 15000 ) ( income <= 35000 ) ? ans + = 0.15 * ( income- 15000 ) : ans + = 0.15 * 20000 ;
if ( income > 35000 ) ans + = 0.2 * ( income- 35000 ) ;
cout << "Tax: " << ans << endl;
cout << "Please enter your income: " ;
}
return 0 ;
}
#include <iostream>
#include <string>
using namespace std;
struct Donation
{
string name;
double money;
} ;
int main ( )
{
int n;
int cntGrand = 0 , cntPat = 0 ;
cout << "The number of donors: " ;
cin >> n;
Donation* all = new Donation [ n] ;
for ( int i= 0 ; i< n; i++ )
{
cout << "Name: " ;
cin. get ( ) ;
getline ( cin, all[ i] . name) ;
cout << "Money: " ;
cin >> all[ i] . money;
}
cout << "Grand Patrons: " << endl;
for ( int i= 0 ; i< n; i++ )
{
if ( all[ i] . money > 10000 )
{
cout << all[ i] . name << "\n" << all[ i] . money << endl;
cntGrand++ ;
}
else cntPat++ ;
}
if ( cntGrand == 0 ) cout << "none" << endl;
cout << "Patrons: " << endl;
for ( int i= 0 ; i< n; i++ )
if ( all[ i] . money < 10000 ) cout << all[ i] . name << "\n" << all[ i] . money << endl;
return 0 ;
}
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main ( )
{
char words[ 50 ] ;
int vow = 0 , con = 0 , other = 0 ;
cout << "Enter words: (q to quit)" << endl;
cin >> words;
while ( strcmp ( words, "q" ) != 0 )
{
if ( words[ 0 ] == 'a' || words[ 0 ] == 'e' || words[ 0 ] == 'i' || words[ 0 ] == 'o' || words[ 0 ] == 'u' ) vow++ ;
else con++ ;
for ( int i= 0 ; i< strlen ( words) ; i++ )
{
if ( ! isalpha ( words[ i] ) && ! ispunct ( words[ i] ) )
{
other++ ;
if ( words[ 0 ] == 'a' || words[ 0 ] == 'e' || words[ 0 ] == 'i' || words[ 0 ] == 'o' || words[ 0 ] == 'u' ) vow-- ;
else con-- ;
break ;
}
}
cin >> words;
}
cout << vow << " words beginning with vowels\n" << con << " words beginning with consonants.\n" << other << " others" << endl;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ( )
{
string filename;
ifstream inFile;
cout << "Enter name of data file: " ;
getline ( cin, filename) ;
inFile. open ( filename) ;
int cnt = 0 ;
char ch;
if ( ! inFile. is_open ( ) )
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n" << endl;
exit ( EXIT_FAILURE) ;
}
inFile >> ch;
while ( inFile. good ( ) )
{
cnt++ ;
inFile >> ch;
}
if ( inFile. eof ( ) ) cout << "End of file reached.\n" ;
else if ( inFile. fail ( ) ) cout << "Input terminated by data mismatch.\n" ;
else cout << "Input terminated for unknown reason.\n" ;
cout << "The number of characters: " << cnt << endl;
inFile. close ( ) ;
return 0 ;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Donation
{
string name;
double money;
} ;
int main ( )
{
int n;
int cntGrand = 0 , cntPat = 0 ;
ifstream inFile;
string filename;
cout << "The file's name: " ;
cin >> filename;
inFile. open ( filename) ;
inFile >> n;
Donation* all = new Donation [ n] ;
for ( int i= 0 ; i< n; i++ )
{
inFile. get ( ) ;
getline ( inFile, all[ i] . name) ;
inFile >> all[ i] . money;
}
cout << "Grand Patrons: " << endl;
for ( int i= 0 ; i< n; i++ )
{
if ( all[ i] . money > 10000 )
{
cout << all[ i] . name << "\n" << all[ i] . money << endl;
cntGrand++ ;
}
else cntPat++ ;
}
if ( cntGrand == 0 ) cout << "none" << endl;
cout << "Patrons: " << endl;
for ( int i= 0 ; i< n; i++ )
if ( all[ i] . money < 10000 ) cout << all[ i] . name << "\n" << all[ i] . money << endl;
return 0 ;
}