#编程练习1
#include <iostream>
#include <cctype>
using namespace std;
int main ( )
{
cout << "Enter something: " ;
char ch;
while ( cin. get ( ch) && ch != '@' )
{
if ( ! isdigit ( ch) )
{
if ( islower ( ch) )
cout << char ( toupper ( ch) ) ;
else if ( isupper ( ch) )
cout << char ( tolower ( ch) ) ;
}
}
cout << "Hello world!" << endl;
return 0 ;
}
#编程练习2
#include <iostream>
#include <array>
const int ArSize = 10 ;
using namespace std;
int main ( )
{
cout << "Enter donation.\n" ;
int i= 0 ;
double sum = 0.0 ;
cout << "#donation[" << i << "]: " ;
array< double , ArSize> donation;
while ( i < ArSize && cin >> donation[ i] )
{
sum + = donation[ i] ;
if ( ++ i < ArSize)
cout << "#donation[" << i << "]: " ;
}
double average;
average = sum / i;
cin >> fixed;
cin. precision ( 6 ) ;
cout << "Average is : " << average << endl;
int count = 0 ;
for ( int j= 0 ; j< i; j++ )
{
if ( donation[ j] > average)
count ++ ;
}
cout << "There are " << count << " number bigger than average.\n" ;
return 0 ;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main ( )
{
cout << "Please enter one of the following choices:\n"
<< "c) carnivore\tp) pianist\n"
<< "t) tree\t\tg) game\n" ;
char ch;
while ( cin >> ch)
{
switch ( ch)
{
case 'c' : cout << "carnivore\n" ;
exit ( EXIT_FAILURE) ;
break ;
case 'p' : cout << "pianist\n" ;
exit ( EXIT_FAILURE) ;
break ;
case 't' : cout << "A maple is a tree.\n" ;
exit ( EXIT_FAILURE) ;
break ;
case 'g' : cout << "This is a game.\n" ;
exit ( EXIT_FAILURE) ;
break ;
default : cout << "Please enter a a,p ,t, or g:" ;
}
}
return 0 ;
}
#include <iostream>
void show ( ) ;
void enter ( ) ;
void display_fullname ( ) ;
void display_title ( ) ;
void display_bopname ( ) ;
void display_preference ( ) ;
const int strsize = 50 ;
const int ArSize = 5 ;
struct bop
{
char fullname[ strsize] ;
char title[ strsize] ;
char bopname[ strsize] ;
int preference;
} ;
using namespace std;
const bop Bopname[ ArSize] =
{
{ "ChengJie" , "CJ" , "Xcj" , 2 } ,
{ "HouJie" , "HJ" , "Xhj" , 0 } ,
{ "ChenYiHou" , "CYH" , "Xcyh" , 1 } ,
{ "BaoChen" , "BC" , "Xbc" , 2 } ,
{ "YangLuQian" , "YLQ" , "Xylq" , 1 }
} ;
int main ( )
{
show ( ) ;
char str;
cout << "Enter your choice: " ;
cin >> str;
while ( cin && str!= 'q' )
{
switch ( str)
{
case 'a' :
display_fullname ( ) ;
break ;
case 'b' :
display_title ( ) ;
break ;
case 'c' :
display_bopname ( ) ;
break ;
case 'd' :
display_preference ( ) ;
break ;
}
enter ( ) ;
cin >> str;
}
if ( str == 'q' )
cout << "Bye!" << endl;
return 0 ;
}
void show ( )
{
cout << "Benevolent Order Programmers Report\n"
<< "a. display by name\tb. display by title\n"
<< "c. display by bopname\td. display by preference\n"
<< "q. quit\n" ;
}
void enter ( )
{
cout << "Next choice: " ;
}
void display_fullname ( )
{
for ( int i= 0 ; i< ArSize; i++ )
cout << Bopname[ i] . fullname << endl;
}
void display_title ( )
{
for ( int j= 0 ; j< ArSize; j++ )
cout << Bopname[ j] . title << endl;
}
void display_bopname ( )
{
for ( int m= 0 ; m< ArSize; m++ )
cout << Bopname[ m] . bopname << endl;
}
void display_preference ( )
{
for ( int n= 0 ; n< ArSize; n++ )
{
if ( Bopname[ n] . preference == 0 )
cout << Bopname[ n] . fullname<< endl;
else if ( Bopname[ n] . preference == 1 )
cout << Bopname[ n] . title << endl;
else if ( Bopname[ n] . preference == 2 )
cout << Bopname[ n] . bopname << endl;
}
}
#include <iostream>
double pay ( long num) ;
using namespace std;
int main ( )
{
long income;
double income_tax;
cout << "Enter your income: " ;
while ( cin >> income && income > 0 )
{
income_tax = pay ( income) ;
cout << income << " have " << income_tax << " tvarps.\n" ;
cout << "Enter your income: " ;
}
cout << "end!\n" ;
return 0 ;
}
double pay ( long num)
{
double tax;
if ( num >= 0 && num <= 5000 )
{
tax = 0 ;
return tax;
}
else if ( num >= 5001 && num <= 15000 )
{
tax = ( num- 5000 ) * 0.10 ;
return tax;
}
else if ( num >= 15001 && num <= 35000 )
{
tax = ( num- 15000 ) * 0.15 + 10000 * 0.1 ;
return tax;
}
else
{
tax = ( num- 35000 ) * 0.2 + 20000 * 0.15 + 10000 * 0.1 ;
return tax;
}
}
#include <iostream>
#include <string>
using namespace std;
struct donation
{
string name;
double money;
} ;
int main ( )
{
int num;
cout << "Please input the number of donor: " ;
cin >> num;
cout << "Please input donor's name and fund.\n" ;
donation * ps = new donation[ num] ;
for ( int i = 0 ; i< num ; i++ )
{
cin. get ( ) ;
cout << "#" << i+ 1 << ": name: " ;
getline ( cin, ( ps+ i) - > name) ;
cout << "\tfund: " ;
cin >> ( ps+ i) - > money;
}
int count = 0 ;
cout << "Grand Patrons:\n" ;
for ( int j= 0 ; j< num; j++ )
{
if ( ( ( ps+ j) - > money ) > 10000 )
{
count++ ;
cout << ( ps+ j) - > name << ":" << ( ps+ j) - > money << endl;
}
}
if ( count == 0 )
{
cout << "none\n" ;
}
cout << "\n" ;
cout << "Patrons: \n" ;
count = 0 ;
for ( int j = 0 ; j< num; j++ )
{
if ( ( ( ps+ j) - > money) < 10000 )
{
cout << ( ps+ j) - > name << endl;
count ++ ;
}
}
if ( count== 0 )
{
cout << "none\n" ;
}
delete [ ] ps;
return 0 ;
}
#include <iostream>
#include <cctype>
const int ArSize = 50 ;
using namespace std;
int main ( )
{
cout << "Enter words (q to quit): \n" ;
char ch[ ArSize] ;
int count = 0 ;
int consonants = 0 ;
int others = 0 ;
cin >> ch;
while ( ch[ 0 ] != 'q' )
{
if ( isalpha ( ch[ 0 ] ) )
switch ( ch[ 0 ] )
{
case 'a' : count ++ ; break ;
case 'e' : count ++ ; break ;
case 'i' : count ++ ; break ;
case 'o' : count ++ ; break ;
case 'u' : count ++ ; break ;
default : consonants++ ;
}
else
{
others++ ;
}
cin >> ch;
}
cout<< count << " words beginning with vowels\n " ;
cout << consonants << " words beginning with consonants\n" ;
cout << others << " others\n" ;
return 0 ;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ( )
{
ifstream inFile;
inFile. open ( "test.txt" ) ;
if ( ! inFile. is_open ( ) )
{
cout << "Could not open the file!\n" ;
exit ( EXIT_FAILURE) ;
}
int ch;
int count = 0 ;
ch = inFile. get ( ) ;
while ( ch!= EOF )
{
count ++ ;
cout << char ( ch) ;
ch = inFile. get ( ) ;
}
cout << "The total words in this words are: "
<< count << endl;
inFile. close ( ) ;
cin. get ( ) ;
return 0 ;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct donation
{
string name;
double money;
} ;
int main ( )
{
ifstream inFile;
inFile. open ( "test.txt" ) ;
if ( ! inFile. is_open ( ) )
{
cout << "Could not open the file!\n" ;
exit ( EXIT_FAILURE) ;
}
int num;
cout << "Please input the number of donor: " ;
inFile >> num;
cout << num << endl;
cout << "Please input donor's name and fund.\n" ;
donation * ps = new donation[ num] ;
while ( inFile. eof ( ) == false )
{
for ( int i = 0 ; i< num ; i++ )
{
inFile. get ( ) ;
cout << "#" << i+ 1 << ": name: " ;
getline ( inFile, ( ps+ i) - > name) ;
cout << ( ps+ i) - > name<< endl;
cout << "\tfund: " ;
inFile >> ( ps+ i) - > money;
cout << ( ps+ i) - > money << endl;
}
}
int count = 0 ;
cout << "Grand Patrons:\n" ;
for ( int j= 0 ; j< num; j++ )
{
if ( ( ( ps+ j) - > money ) > 10000 )
{
count++ ;
cout << ( ps+ j) - > name << ":" << ( ps+ j) - > money << endl;
}
}
if ( count == 0 )
{
cout << "none\n" ;
}
cout << "\n" ;
cout << "Patrons: \n" ;
count = 0 ;
for ( int j = 0 ; j< num; j++ )
{
if ( ( ( ps+ j) - > money) < 10000 )
{
cout << ( ps+ j) - > name << endl;
count ++ ;
}
}
if ( count== 0 )
{
cout << "none\n" ;
}
delete [ ] ps;
return 0 ;
}