1、
代码
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
char line;
char ch;
cout << "Enter Line:";
cin.get(ch);
while (ch != '@')
{
if (!isdigit(ch))
{
if (islower(ch))
ch = toupper(ch);
else if(isupper(ch))
ch = tolower(ch);
cout << ch;
}
cin.get(ch);
}
cout <<endl;
return 0;
}
运行结果
2、
代码
#include<iostream>
const int MAX=10;
int main()
{
using namespace std;
double donation[MAX];
cout << "Plaese enter the money of your donation:\n";
cout << "R#1: ";
int i=0;
while (i<MAX && cin >> donation[i]){
if (++i < MAX)
cout << "R#" << (i+1) << ": ";
}
double sum = 0.0;
for (int j=0; j<i; j++)
sum += donation[j];
if (i == 0)
cout << "No donation\n";
else {
double avg = sum/i;
int count = 0;
for (int k=0; k<i; k++)
if (donation[k] > avg)
count++;
cout << avg << "=average money of " << i << " donation\n";
cout << count << " times over average values.\n";
}
return 0;
}
运行结果
3、
代码
#include<iostream>
using namespace std;
void showmenu();
int main()
{
showmenu();
char word;
cout << "Please enter a c,p,t or g:";
cin >> word;
if (word != 'c' && word != 'p' && word != 't' && word != 'g')
{
cout << "Please enter a c,p,t or g:";
cin >> word;
}
switch(word)
{
case 'c':cout << "carnivore" <<endl;
break;
case 'p':cout << "pianist" <<endl;
break;
case 't':cout << "tree" <<endl;
break;
case 'g':cout << "game" <<endl;
break;
default: cout << "not found" <<endl;
}
return 0;
}
void showmenu()
{
cout << "Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree q) game\n";
}
运行结果
4、
代码
#include<iostream>
using namespace std;
void showmune();
const int strsize=60;
struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
showmune();
char choice;
bop persons[3] =
{
{"ZhangSan","A students","xiaozhang",0},
{"Lisi","A teacher","xiaoli",1},
{"WangWu","A phper","xiaowang",2}
};
cout << "Enter your choice:";
cin >> choice;
if (choice == 'q')
cout << "bey!\n";
while (choice && choice != 'q')
{
int i=0;
switch(choice)
{
case 'a':
for (i=0;i<3;i++)
{
cout << persons[i].fullname<<endl;
}
break;
case 'b':
for (i=0;i<3;i++)
{
cout << persons[i].title<<endl;
}
break;
case 'c':
for (i=0;i<3;i++)
{
cout << persons[i].bopname<<endl;
}
break;
case 'd':
for (i=0;i<3;i++)
{
switch(persons[i].preference)
{
case 0:cout << persons[i].fullname << endl; break;
case 1:cout << persons[i].title << endl; break;
case 2:cout << persons[i].bopname << endl; break;
}
}
break;
default: cout << "Please enter a,b,c,d or q" << endl;
}
cout << "Next choice:";
cin >> choice;
}
}
void showmune()
{
cout << "Benevolent Order of Programmers Report\n"
"a.display by name b.display by title\n"
"c.display bu bopname d.display by preference\n"
"q.quit\n";
}
运行结果
5、
代码
#include<iostream>
const double LEV1 = 5000;
const double LEV2 = 15000;
const double LEV3 = 35000;
const double RATE1 = 0.10;
const double RATE2 = 0.15;
const double RATE3 = 0.20;
int main()
{
using namespace std;
double income;
double tax = 0.0;
cout << "请输入收入:";
cin >> income;
while (income && income > 0)
{
if (income <= LEV1)
tax = 0;
else if(income > LEV1 && income <= LEV2)
tax = (income-LEV1)*RATE1;
else if(income > LEV2 && income <= LEV3)
tax = (income-LEV2)*RATE2 + (LEV2-LEV1)*RATE1;
else
tax = (income-LEV3)*RATE3 + (LEV3-LEV2)*RATE2 + (LEV2-LEV1)*RATE1;
cout << "当收入为 " << income << " tvarp时,个人所得税应缴 " << tax << "tvarp\n";
cout << "请输入收入:";
cin >> income;
}
return 0;
}
运行结果
6、
代码
#include<iostream>
const int STRSIZE = 60;
const double LINE = 1000;
struct donation{
char name[STRSIZE];
double money;
};
int main()
{
using namespace std;
int num;
int count=0;
cout << "请输入捐献者数目:";
cin >> num;
while(cin.get() != '\n');
donation *ps = new donation[num];
for (int i=0;i < num; i++)
{
cout << "请输入姓名:";
cin.getline(ps[i].name, STRSIZE);
cout << "请输入捐献金额:";
cin >> ps[i].money;
while(cin.get() != '\n');
}
for (int j=0;j < num; j++)
{
if (ps[j].money > LINE){
cout <<"Grand Patrons: " << ps[j].name << " " << ps[j].money << endl;
count++;
}
if (count == num)
cout << "None\n";
if (ps[j].money <= LINE)
{
cout <<"Patrons: " << ps[j].name << " " << ps[j].money << endl;
}
}
delete [] ps;
return 0;
}
运行结果
7、
代码
#include<iostream>
#include<cctype>
#include<string>
int main()
{
using namespace std;
string word;
char ch;
int vowels = 0;
int consonants =0;
int other =0;
cout << "Enter words (q to quit):\n";
cin >> word;
while(word != "q")
{
if (word == "q")
break;
ch = tolower(word[0]);
if (!isalpha(ch))
other++;
else
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u')
consonants++;
else
vowels++;
cin >> word;
}
cout << vowels << " words beginning with vowels.\n";
cout << consonants << " words beginning with consonants.\n";
cout << other << " others.\n";
return 0;
}
运行结果
8、
代码
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main()
{
using namespace std;
char filename[SIZE];
int num = 0;
ifstream inFile;
cout << "输入文件名称:";
cin.getline(filename, SIZE);
inFile.open(filename);
// if (!inFile.is_open(filename))
// {
// cout << "Not open the file " << filename << endl;
// exit(EXIT_FAILURE);
// }
char ch;
inFile >> ch;
while (inFile.good())
{
num++;
inFile >> ch;
}
cout << num <<endl;
inFile.close();
return 0;
}
错误:
14行用
inFile.is_open 或
inFile.good 报错
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::good(char [60])’
test.txt文档为
Flitz Pinata
test
运行结果
9、
代码
#include<iostream>
#include<fstream>
using namespace std;
const int SIZE = 60;
struct donation{
char name[SIZE];
double money;
};
int main()
{
char filename[SIZE];
char ch[SIZE];
int num;
ifstream inFile;
cout << "Enter filename:";
cin.getline(filename, SIZE);
inFile.open(filename);
inFile >> num;
int i=0;
donation *ps = new donation[num];
while(i < num)
{
inFile.get();
inFile.getline(ps[i].name, SIZE);
inFile >> ps[i].money;
i++;
}
for (i=0; i< num; i++)
{
cout << ps[i].name << " " << ps[i].money <<endl;
}
delete [] ps;
return 0;
}
读取的文件test.txt内容:
2
Flitz Pinata
1200
guugle
800
运行结果