#include
#include//prototypes for character functions
using namespace std;
int main()
{
char ch;//定义一个char类型变量
cout << "Enter the character,and type @"< 大写
}
else if (isupper(ch))//当输入的是大写字母时
{
ch = tolower(ch);//大写 -> 小写
}
cout << ch ;
cin.get(ch);
}
system("pause");
return 0;
}
//Enter the character, and type @
//@
//@
//china
//CHINA
//
//
//CHINA
//china
#include
#include
using namespace std;
int main()
{
array donation;//使用array模板定义数组长度为10
double input;
int counter = 0;
double average, sum = 0;
int bigger = 0;
//sum ->和 average ->平均值 counter ->元素数 bigger->大于平均值的个数
cout << "Enter the double numerical:";
cin >> input;
while (input != 0 && counter < 10)
{
donation[counter++] = input;
cout << "No." << counter << " Data input to Array." << endl;
cout << "Enter the double numerical.";
cin >> input;
}//通过while循环输入数据,当输入非数字时或超过10个元素时推出循环
for (int i = 0; i < counter; i++)
{
sum += donation[i];//十个数的总和
}
average = sum / counter;
for (int i = 0; i < counter; i++)
{
if (donation[i] > average)
bigger++;
}//通过遍历计算大于平均值的元素个数
cout << "The Average is " << average << " and " << bigger;
cout << " data bigger than average." << endl;
system("pause");
return 0;
}
//Enter the double numerical : 10
//No.1 Data input to Array.
//Enter the double numerical.20
//No.2 Data input to Array.
//Enter the double numerical.30
//No.3 Data input to Array.
//Enter the double numerical.40
//No.4 Data input to Array.
//Enter the double numerical.50
//No.5 Data input to Array.
//Enter the double numerical.60
//No.6 Data input to Array.
//Enter the double numerical.70
//No.7 Data input to Array.
//Enter the double numerical.80
//No.8 Data input to Array.
//Enter the double numerical.90
//No.9 Data input to Array.
//Enter the double numerical.100
//No.10 Data input to Array.
//Enter the double numerical.110
//The Average is 55 and 5 data bigger than average.
//请按任意键继续. . .
#include
using namespace std;
void showmenu();//showmenu()函数声明
int main()
{
char choice;
showmenu();
cin.get(choice);//显示菜单,并读取用户输入,保存至choice变量中
while (choice != 'c' && choice != 'p' && choice != 't' && choice != 'g')
{
cin.get();
cout << "Please enter a,c,p,t,or g ";
cin.get(choice);
}
switch (choice)
{
case'c':
break;
case'p':
break;
case't':
cout << "Asample is a tree." << endl;
case'g':
break;
}
system("pause");
return 0;
}
void showmenu()
{
cout << "Please enter one of the following choice:\n";
cout << "c) carnivore \t\t\t p) pianist\n";
cout << "t) tree\t\t\t\t g)game\n";
}//showmenu()函数只负责菜单传递信息的输出
//Please enter one of the following choice :
//c) carnivore p) pianist
//t) tree g)game
//f
//Please enter a, c, p, t, or g q
//Please enter a, c, p, t, or g t
//Asample is a tree.
//请按任意键继续. . .
这题不会直接抄答案。
#include
#include
using namespace std;
const int strsize = 40;
const int usersize = 5;
//Benevolent Order of Programmer 姓名结构体
struct bop
{
char fullname[strsize];//真实姓名
char title[strsize];//头衔
char bopname[strsize];//秘密BOP姓名
int preference;//0 = fullname,1 = title;2 = bopname
};
bop bop_user[usersize] =
{
{"Wimp Macho","Programmer","MIPS",0},
{"Raki Rhodes","Junior Programmer",1},
{"Celia Laiter","","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","",1},
{"Pat Hand","","LOOPY",2},
};//定义常量,定义结构体初始化bop数组信息
void showmenu();//showmenu()函数声明
void print_by_name();//打印姓名函数声明
void print_by_pref();//打印偏好函数声明
void print_by_title();//打印头衔函数声明
void print_by_bopname();//打印秘密姓名函数声明
void create_info();
int main()
{
char choice;
//create_info();
showmenu();
cout << "Enter your choice:";
cin.get(choice);//显示菜单读取用户输入
while (choice != 'q')
{
switch (choice)
{
case 'a':
print_by_name();
break;
case 'b':
print_by_title();
break;
case 'c':
print_by_bopname();
break;
case 'd':
print_by_pref();
break;
default:
cout << "Please enter character a ,b ,c ,d , or q:" << endl;
}
//通过switch语句对应的函数进行显示
cout << "Next choice:";
cin.get(choice);
}
//将switch语句放在while循环中,可以反复循环给下那个功能
cout << "Bye!" << endl;
system("pause");
return 0;
}
void showmenu()
{
cout << "a. display by name\t\t b. display by title" << endl;
cout << "c. display by bopname\t\t d. display by preference" << endl;
cout << "q. quit" << endl;
}//显示菜单
void print_by_name()
{
for (int i = 0; i < usersize; i++)
{
if (bop_user[i].fullname == 0)
break;
else
cout << bop_user[i].fullname << endl;
}
}
void print_by_pref()
{
for (int i = 0; i < usersize; i++)
{
if (bop_user[i].fullname == 0)
break;
else
{
switch (bop_user[i].preference)
{
case 0:
cout << bop_user[i].fullname << endl;
break;
case 1:
cout << bop_user[i].title << endl;
break;
case 2:
cout << bop_user[i].bopname << endl;
break;
}
}
}
}
void print_by_title()
{
for (int i = 0; i < usersize; i++)
{
if (bop_user[i].fullname == 0)
break;
else
{
cout << bop_user[i].title << endl;
break;
}
}
}
void print_by_bopname()
{
for (int i = 0; i < usersize; i++)
{
if (bop_user[i].fullname == 0)
break;
else
{
cout << bop_user[i].bopname << endl;
break;
}
}
}
void create_info()
{
for (int i = 0; i < usersize; i++)
{
cout << "Enter the user's full name:";
cin.getline(bop_user[i].fullname, strsize);
cout << "Enter the user's title:";
cin.getline(bop_user[i].title, strsize);
cout << "Enter the user's bopname:";
cin.getline(bop_user[i].bopname, strsize);
cout << "Enter the user's preference:";
cin >> bop_user[i].preference;
cout << "Next...(f for finished):";
cin.get();
if (cin.get() == 'f') break;
}
}
//a.display by name b.display by title
//c.display by bopname d.display by preference
//q.quit
//Enter your choice : a
//Wimp Macho
//Raki Rhodes
//Celia Laiter
//Hoppy Hipman
//Pat Hand
//Next choice : Please enter character a, b, c, d, or q :
//Next choice : d
//Wimp Macho
//Raki Rhodes
//MIPS
//Analyst Trainee
//LOOPY
//Next choice : Please enter character a, b, c, d, or q :
//Next choice : q
//Bye!
//请按任意键继续. . .
#include
using namespace std;
int main()
{
float tax, salary;
cout << "Please enter your salary:";
cin >> salary;
while (salary > 0)
{
if (salary < 5000)
{
tax = 0;
}
else if (salary <= 15000)
{
tax = (salary - 5000) * 0.10;
}
else if (salary <= 35000)
{
tax = 10000 * 0.10 + (salary - 15000) * 0.15;
}
else if (salary > 35000)
{
tax = 10000 * 0.10 + 20000 * 0.15 + (salary - 30000) * 0.20;
}
cout << "Your salary is " << salary << " trarps,and you should pay ";
cout << tax << " trarps of tax." << endl;
cout << "Enter your salary:";
cin >> salary;
}
cout << "Bye!" << endl;
system("pause");
return 0;
}
//Please enter your salary : 4000
//Your salary is 4000 trarps, and you should pay 0 trarps of tax.
//Enter your salary : 15000
//Your salary is 15000 trarps, and you should pay 1000 trarps of tax.
//Enter your salary : 25000
//Your salary is 25000 trarps, and you should pay 2500 trarps of tax.
//Enter your salary : 40000
//Your salary is 40000 trarps, and you should pay 6000 trarps of tax.
//Enter your salary : 0
//Bye!
//请按任意键继续...
#include
#include
using namespace std;
struct patrons {
string full_name;
double fund;
};//捐款人基本信息的结构体
int main()
{
int patrons_number;
patrons *ppatrons;
cout << "How many patrons ? ";
cin >> patrons_number;
cin.get();
ppatrons = new patrons[patrons_number];//建立动态数组
int id = 0;
bool empty = true;
cout << "Starting to input patron's info:";
while (id < patrons_number)
{
cout << "Enter the full name of patrons: ";
getline(cin, ppatrons[id].full_name);
cout << "Enter the fuud of " << ppatrons[id].full_name << " : ";
cin >> ppatrons[id].fund;
cin.get();
id++;
cout << "Continue to input,or press (f) to finished.";
if (cin.get() == 'f')
break;
}//建立捐款人名单
cout << "Grand Patrons" << endl;
for (int i = 0; i < patrons_number; i++)
{
if (ppatrons[i].fund >= 1000) {
cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;
empty = false;
}
}
//查询Grand Patrons名单,如果empty为true,输出NONE
if (empty)
cout << "NONE" << endl;
empty = false;
cout << "Patrons" << endl;
for (int i = 0; i < patrons_number; i++)
{
if (ppatrons[i].fund < 1000) {
cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;
}
}
if (empty)
cout << "NONE" << endl;
system("pause");
return 0;
}
//How many patrons ? 2
//Starting to input patron's info:Enter the full name of patrons: LI MING
//Enter the fuud of LI MING : 5000
//Continue to input, or press(f) to finished.
//Enter the full name of patrons : LI HUA
//Enter the fuud of LI HUA : 1000
//Continue to input, or press(f) to finished.f
//Grand Patrons
//LI MING : 5000
//LI HUA : 1000
//Patrons
//请按任意键继续. . .
#include
#include
using namespace std;
int main()
{
char words[40];
int vowel, consonant, others;
vowel = consonant = others = 0;
cout << "Enter words(q to quit):" << endl;
cin >> words;
while (strcmp(words, "q") != 0)
{
if (!isalpha(words[0]))
{
others++;
}//统计非字母开头的单词
else {
switch (words[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel++;//统计以元音字母开头的单词
break;
default:
consonant++;//统计非元音字母开头的单词
}
}
cin >> words;
}
cout << vowel << " words beginning with vowel." << endl;
cout << consonant << " words beginning with consonants." << endl;
system("pause");
return 0;
}
//Enter words(q to quit) :
//The 12 awesome oxen ambled
//quitely across 15 meters of lawn
//q
//5 words beginning with vowel.
//4 words beginning with consonants.
//请按任意键继续. . .
#include
#include
using namespace std;
int main()
{
ifstream fin;//定义文件输入流
string file_name;
cout << "Enter the file name:";
getline(cin, file_name);
fin.open(file_name);
if (!fin.is_open()) {
cout << "Error to open file." << endl;
exit(EXIT_FAILURE);
}
char read_char;
int char_counter = 0;
while (!fin.eof()) {
fin >> read_char;
char_counter++;
}
cout << "The file " << file_name << " contains " << char_counter << " characters." << endl;
fin.close();//关闭文件
system("pause");
return 0;
}
建立txt文件如下所示:
#include
#include
#include
using namespace std;
struct patrons {
string full_name;
double fund;
};
//表示捐款人信息的结构体
int main()
{
ifstream fin;
string file_name;
cout << "Enter the file name:";
getline(cin, file_name);
fin.open(file_name);
if (!fin.is_open())
{
cout << "Error to open file:" << endl;
exit(EXIT_FAILURE);
}
int patrons_number;
patrons *ppatrons;
int id = 0;
bool empty = true;
fin >> patrons_number;
if (patrons_number <= 0)
{
exit(EXIT_FAILURE);
}
ppatrons = new patrons[patrons_number];
fin.get();
//读取人数,创建动态数组
while (!fin.eof() && id < patrons_number)
{
getline(fin, ppatrons[id].full_name);
cout << "Read Name: " << ppatrons[id].full_name << endl;
fin >> ppatrons[id].fund;
cout << "Read fund: " << ppatrons[id].fund << endl;
fin.get();
id++;
}
//循环读取捐款人信息,也可以使用for循环
fin.close();//关闭文件
cout << "Grand Patrons" << endl;
for (int i = 0; i < patrons_number; i++)
{
if (ppatrons[i].fund >= 10000) {
cout << ppatrons[i].full_name << " ; " << ppatrons[i].fund << endl;
empty = false;
}
}
if (empty)
cout << "NONE" << endl;
empty = false;
cout << "Patrons" << endl;
for (int i = 0; i < patrons_number; i++)
{
if (ppatrons[i].fund < 10000) {
cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;
}
}
if (empty) cout << "NONE" << endl;
system("pause");
return 0;
}
建立txt文件如下所示: