一* char字符(ASCⅡ编码)
//转换大小写
#include <iostream>
#include <cctype>
#include <string>
int main()
{
using namespace std;
char ch;
string str;
cout << "Input is begining: \n";
cin.get(ch);
while (ch != '@')
{
if (isalpha(ch))
{
if (islower(ch))
str += toupper(ch);//str += ch - 32;
else
str += tolower(ch);//str += ch + 32;
}
else if (isspace(ch))
str += ch;
cin.get(ch);
}
cout << "After convert, the result is: \n" << str;
return 0;
}
二* 逻辑运算符
//平均值
#include <iostream>
const short int Num = 10;
int main()
{
using namespace std;
double donation[Num],total=0,count=0;
short int i = 0;
donation[0] = 0;
cout << "donation #1: ";
for (; i<Num && cin>>donation[i]; i++)//最多10个数据且数据类型为数字
{
total += donation[i];
if (i < 9)
cout << "donation #" << i + 2 << ": ";
}
for (int n = 0; n < i; n++)
if (donation[n] > total / i )
++count;
cout << i << " donation numbers of average is " << total / i
<< ", "<<count << " numbers are upper than average";
return 0;
}
三* switch菜单
//菜单驱动程序雏形
#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Please enter one of the following choice:\a\a\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n";
cin >> ch;
while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
cout << "Please enter a c p t or g: \a\a";
cin >> ch;
}
switch (ch)
{
case 'c':cout << "A maple is a carnivore.";
case 'p':cout << "A maple is a pianist.";
case 't':cout << "A maple is a tree.";
case 'g':cout << "A maple is a game.";
}
return 0;
}
四* 结构数组
//BOP成员列表
#include <iostream>
using namespace std;
struct bop {//声明结构bop
char fullname[15];
char title[25];
char bopname[16];
int preference;
};
int main()
{
bop memberinfo[5]{//初始化结构数组
{ "Wimp Macho","Junior Programmer","March",0 },
{ "Raki Rhodes","Junior Programmer","April",1 },
{ "Celia Laiter","Intermediate Programmer","MIPS",2 },
{ "Hoppy Hipman","Senior Programmer","Analyst Trainee",2 },
{ "Pat Hand","Expert Programmer","LOOPY",2 }
};
cout << "Benevolent Order of Programmers Report\n"
"a. display by name b.display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n";
char ch;
cout << "Enter your choice: ";
cin >> ch;
while (ch != 'q') {
int i = 0;
for ( ; i < 5; i++) {
switch (ch) {
case 'a':cout << memberinfo[i].fullname << endl;
break;//跳过剩下的语句
case 'b':cout << memberinfo[i].title << endl;
break;
case 'c':cout << memberinfo[i].bopname << endl;
break;
case 'd': {
if (memberinfo[i].preference == 0)
cout << memberinfo[i].fullname << endl;
else if (memberinfo[i].preference == 1)
cout << memberinfo[i].title << endl;
else
cout << memberinfo[i].bopname << endl;
break;
}
}
}
i = 0;
cout << "Next choice: ";
cin >> ch;
}
cout << "Bye!";
return 0;
}
五* cin的数据类型判断
//Neutronia王国收入所得税
#include <iostream>
const float taxes1 = 0.1, taxes2 = 0.15, taxes3 = 0.2;
int main() {
using namespace std;
cout << "Enter income(tvarp): ";
float income,taxes;//float和double的第一位是符号位
cin >> income;//cin:输入数字,非数字结束程序
while (income > 0) {//输入大于0,另外*使用isdigit(income) && income>0会报错,待解
if (income <= 5000)
taxes = 0;
else if (income > 5000 && income <= 15000)
taxes = (income - 5000) * taxes1;
else if (income > 15000 && income <= 35000)
taxes = (income - 15000) * taxes2 + 10000 * taxes1;
else
taxes = (income - 35000) * taxes3 + 20000 * taxes2 + 10000 * taxes1;
cout << "Your taxes is " << taxes << endl
<< "Next income(tvarp): ";
cin >> income;
}
return 0;
}
六* 按条件输出
//捐款结构数组
#include <iostream>
struct patrones {
char name[30];
double amount;
};
int main() {
using namespace std;
unsigned int donor;
//获取数据
do {
cout << "Enter the number of donors: ";
cin >> donor;
} while (donor == 0);//至少1位捐赠者
patrones* members = new patrones[donor];
for (int i = 0; i < donor; i++) {
cout << "Patrones #" << i + 1 << ": ";
cin >> members[i].name;
cout << "Amuont #" << i + 1 << ": ";
cin >> members[i].amount;
}
//输出数据
unsigned int count = 0;
cout << endl << "Grand Patrones:\n";
for (int i = 0; i < donor; i++)
if (members[i].amount > 10000) {
cout << members[i].name << "\t\t" << members[i].amount << endl;
++count;
}
if (count == 0)
cout << "none";
count = 0;
cout << endl << "Patrones:\n";
for (int i = 0; i < donor; i++)
if (members[i].amount <= 10000) {
cout << members[i].name << endl;
++count;
}
if (count == 0)
cout << "none";
delete[]members;
return 0;
}
七 ?while判断字符串一直为true
//输入元音和辅音的数量
#include <iostream>
#include <cctype>
int main() {
using namespace std;
char ch[30];
unsigned short vowels=0,consonants=0,other=0;
cout << "Enter words (q to quit):\n";
cin >> ch;
while (ch != "q") {//循环条件无法判断?
if (isalnum(ch[0])) {
switch (ch[0]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':++vowels; break;
default:++consonants;
}
}
else
++other;
cin >> ch;
}
cout << vowels << " words beginning with vowels\n"
<< consonants << " words beginning with consonants\n"
<< other << " others";
return 0;
}
2023/1/19更新:
//while (ch != "q");改为如下语句
while (ch[0] != 'q' && sizeof(ch) != 1)
八 ?不能读取中文
//文件包含的字符
#include <iostream>
#include <fstream>
#include <cctype>
int main() {
using namespace std;
ifstream inFile;
char filename[60];//*char数组长度不能用const常量,编译器为VS2022
cout << "Enter name of data file: ";//MyFriend.txt
cin.getline(filename, 60);
inFile.open(filename);
for (int i = 0; !inFile.is_open(); i++) {//文件不能打开
if (i < 2) {
cout << "Could not open the file: " << filename << "\nPlease check the filename: ";
cin.getline(filename, 60);
inFile.open(filename);
}
else
exit(EXIT_FAILURE);
}
char ch;//计算文件包含的字符数量
unsigned int count=0;
inFile >> ch;
while (inFile.good()) {//eof判断
if (!isspace(ch))
++count;
inFile >> ch;
}
cout << filename << " includes the charactors: "<<count;
inFile.close();
return 0;
}
不能读取中文
九* cin.get()读取下一个字符
//捐款结构数组 -- 从文件读取信息
#include <iostream>
#include <fstream>
struct patrones {
char name[30];
double amount;
};
int main() {
using namespace std;
unsigned int donor;
ifstream inFile;
inFile.open("PatronesStructInfo.txt");
char ch[30];
//读取数据
inFile >> donor;
inFile.get();//读取下一个字符(换行符)
patrones* members = new patrones[donor];
for (int i = 0; i < donor; i++) {
inFile.getline(members[i].name, 30);
inFile >> members[i].amount;
inFile.get();
}
inFile.close();
//输出数据
unsigned int count = 0;
cout << endl << "Grand Patrones:\n";
for (int i = 0; i < donor; i++)
if (members[i].amount > 10000) {
cout << members[i].name << "\t\t" << members[i].amount << endl;
++count;
}
if (count == 0)
cout << "none";
count = 0;
cout << endl << "Patrones:\n";
for (int i = 0; i < donor; i++)
if (members[i].amount <= 10000) {
cout << members[i].name << endl;
++count;
}
if (count == 0)
cout << "none";
delete[]members;
return 0;
}