1.大小写转换
使用cctype库里的函数进行大小写转换,但要注意使用toupper或tolower时要进行强制类型转换,否则会输出ASCII值
#include <iostream>
#include<cctype>
using namespace std;int main()
{cout << "请输入字符串(大小写转换将遇到@时结束):\n";char ch;char res;cin.get(ch);while (ch != '@'){if (islower(ch))cout<<(char) toupper(ch);else if (isupper(ch))cout<<(char)tolower(ch);cin.get(ch);}return 0;
}
2.平均值比较
重点在于前面读取num时的处理,当读取类型为数字时并且count<10时读取
#include <iostream>
#include<array>
#include<cctype>
using namespace std;int main()
{const int Arsize = 10;array<double, Arsize>donation;cout << "您最多可以输入10个数字,在遇到非数字输入时将结束:\n";double num;int count = 0;double sum = 0;int res = 0;while (cin >> num && count<Arsize){donation[count] = num;sum += num;count += 1;}double ave;ave = sum / count;int i = 0;while (i <count){if (donation[i] > ave)res += 1;i++;}cout << "输入数字大于平均值的个数为" << res<<'\n';cout << "输入数字平均值为" << ave;return 0;
}
3.菜单驱动
switch语句的使用,需要在每个分支加入break
#include <iostream>using namespace std;int main()
{char code;cout << "Please enter a c,p,t or g:\n";cin >> code;while (code != 'c' && code != 'p' && code != 't' && code != 'g'){cout << "Please enter a c,p,t or g:\n";cin >> code;}switch (code){case 'c':cout << "A maple is a carnivore.";break;case 'p':cout << "A maple is a pianist.";break;case 't':cout << "A maple is a tree.";break;case 'g':cout << "A maple is a game.";break;}return 0;
}
4.BOP大会
单引号只能用作引用字符,双引号用作引用字符串
for 的范围循环
#include <iostream>
#include<string>
using namespace std;const int strsize = 30;struct bop {char fullname[strsize];char title[strsize];char bopname[strsize];int preference;
};int main()
{cout << "Benevolent Order of Programmers Report\n"<< "a.display by name\t" << "b.display by title\n"<< "c.display by bopname\t" << "d.display by preference\n"<< "q.quit\n";bop man1 = { "Wimp Macho","Senior Programmer","David",0 };bop man2 = { "Raki Rhodes","Junior Programmer","Tom",1 };bop man3 = { "Celia Laiter","Manger","Mips",2 };bop man4 = { "Hoppy Hipman","Analyst Trainee","Bob",1 };bop man5 = { " Pat Hand","Engineer","Loopy",2 };bop people[5] = { man1,man2,man3,man4,man5 };cout << "Enter your choice:\n";char choice;while (cin >> choice){if (choice == 'q'){cout << "Bye!";break;}else{switch (choice){case 'a':for (bop man : people)cout << man.fullname << "\n";cout << "Next choice:\n";break;case'b':for (bop man : people)cout << man.title << "\n";cout << "Next choice:\n";break;case'c':for (bop man : people)cout << man.bopname<<"\n";cout << "Next choice:\n";break;case 'd':for (bop man : people)if (man.preference == 0)cout << man.fullname << "\n";else if (man.preference == 1)cout << man.title << "\n";elsecout << man.bopname << "\n";cout << "Next choice:\n";break;}}}return 0;
}
5.Neutronia王国税收
设定tvarp为int类型,通过cin>>tvarp来判断输入是否为数字,非数字会结束循环
#include <iostream>
using namespace std;
int main()
{cout << "请输入您的收入:\n";int tvarp;double tax;while (cin >> tvarp && tvarp >= 0){if (tvarp <= 5000)cout << "您所需缴纳税款为0tvarp\n";else if (tvarp > 5000 && tvarp <= 15000){tax = tvarp * 0.1;cout << "您所需缴纳税款为"<<tax<<"tvarps\n";}else if (tvarp > 15000 && tvarp <= 35000){tax = 10000 * 0.1 + (tvarp - 15000) * 0.15;cout << "您所需缴纳税款为" << tax << "tvarps\n";}else{tax = 10000 * 0.1 + 20000 * 0.15 + (tvarp - 35000) * 0.2;cout << "您所需缴纳税款为" << tax << "tvarps\n";}cout << "请输入您的收入:\n";}return 0;
}
6.捐款记录
当字符串和数字混合输入时,注意清除缓冲区的换行符,使用limits库中的ignore()函数
因为getline遇到换行符停止读取
最后记得释放内存
#include <iostream>
#include<string>
#include<limits>
using namespace std;const int Arsize = 20;
struct donation {string name;double value;
};
int main()
{int number;cout << "请输入捐款者的数量:\n";cin >> number;cin.ignore(); // 清除输入缓冲区中的换行符donation* donat = new donation[number];int * grand_patrons_num=new int[number];int j = 0;int * patrons_num=new int[number];int k = 0;for (int i = 0; i < number; i++){cout << "请输入捐款者的姓名:\n";getline(cin,donat[i].name);cout << "请输入捐款者的金额:\n";cin >> donat[i].value;cin.ignore(); // 清除金额后的换行符if (donat[i].value > 10000){grand_patrons_num[j] = i;j++;}else{patrons_num[k] = i;k++;}}cout << "Grand Patrons:\n";if (j > 0){for (int i = 0; i < j; i++)cout << donat[grand_patrons_num[i]].name << "\t" << donat[grand_patrons_num[i]].value << "\n";}elsecout << "none";cout << "Patrons:\n";if (k > 0){for (int i = 0; i < k; i++)cout << donat[patrons_num[i]].name << "\t" << donat[patrons_num[i]].value << "\n";}elsecout << "none";delete[] donat;delete[] grand_patrons_num;delete[] patrons_num;return 0;
}
7.判断元音辅音开头
#include <iostream>
#include<string>
#include<cctype>
using namespace std;
int others;
int begin_vowel;
int begin_consonant;int main()
{cout << "Enter words( q to quit):\n";string word;cin >> word;while (word != "q"){if (isalpha(word[0])){if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')begin_vowel++;elsebegin_consonant++;}else{others++;}cin >> word;}cout << begin_vowel << "words beginning with vowels.\n";cout << begin_consonant << "words beginning with consonant.\n";cout << others << "words beginning with others.";return 0;
}
8.读取文件
使用ofstream写入文件,fout可以当作cout类比来用;使用ifstream读取文件,fin当作cin类比来用
在读取文件时,要判断文件是否存在(即是否被打开)
#include <iostream>
#include<fstream>
using namespace std;int main()
{ifstream fin;fin.open("C:/Dev/CppSeries/HelloWorld/HelloWorld/file/file_test.txt");if (!fin.is_open()){cout << "could not open the file" << endl;cout << "Program terminating.\n";exit(EXIT_FAILURE);}char ch;int count = 0;while (fin.get(ch))count++;cout << "文件包含" << count << "个字符。";return 0;
}
9.捐款记录(从文件读取信息版)
#include <iostream>
#include<string>
#include<fstream>
#include<limits>
using namespace std;const int Arsize = 20;struct donation {char name[Arsize];double value;
};
int main()
{ifstream fin;fin.open("C:/Dev/CppSeries/HelloWorld/HelloWorld/file/donation.txt");if (!fin.is_open()) {cerr << "无法打开文件!" << endl;return EXIT_FAILURE;}int number;fin >> number;fin.ignore(); // 清除输入缓冲区中的换行符donation* donat = new donation[number];int* grand_patrons_num = new int[number];int j = 0;int* patrons_num = new int[number];int k = 0;for (int i = 0; i < number; i++){fin.getline(donat[i].name,Arsize);fin >> donat[i].value;fin.ignore(); // 清除金额后的换行符if (donat[i].value > 10000){grand_patrons_num[j] = i;j++;}else{patrons_num[k] = i;k++;}}//输出结果cout << "Grand Patrons:\n";if (j > 0){for (int i = 0; i < j; i++)cout << donat[grand_patrons_num[i]].name << "\t" << donat[grand_patrons_num[i]].value << "\n";}elsecout << "none";cout << "Patrons:\n";if (k > 0){for (int i = 0; i < k; i++)cout << donat[patrons_num[i]].name << "\t" << donat[patrons_num[i]].value << "\n";}elsecout << "none";delete[] donat;delete[] grand_patrons_num;delete[] patrons_num;return 0;
}