// 1
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cin.get(ch);
while(ch != '@')
{
if(!isdigit(ch))
{
if(isalpha(ch))
{
if(islower(ch))
ch = toupper(ch);
else
ch = tolower(ch);
}
cout << ch;
}
cin.get(ch);
}
cout << endl;
return 0;
}
// 2
#include <iostream>
#include <cstdio>
#define in_max 5
using namespace std;
bool input_is_number_or_not(char * str)
{
char * p = str;
while(*p != '\0')
{
if((*p >= '0' && *p <= '9') || *p == '.')
p++;
else
return false;
}
return true;
}
int main()
{
double donation[in_max];
char donation_s[20];
double AverageOfDonation = 0,SumOfDonation = 0;
int i,j,BiggerThanAverage = 0;
cout << "Please input the donation with double type to the double array:\n";
for(i = 0;i < in_max;i++)
{
cin >> donation[i];
sprintf(donation_s,"%f",donation[i]);
if(!input_is_number_or_not(donation_s))
break;
SumOfDonation += donation[i];
}
AverageOfDonation = SumOfDonation / i;
cout << "The average of donation array is " << AverageOfDonation << endl;
for(j = 0;j < i;j++)
{
if(donation[j] > AverageOfDonation)
BiggerThanAverage++;
}
cout << "there are " << BiggerThanAverage << " numbers is bigger than average of donation array\n";
return 0;
}
// 3
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter one of the following choices:\n"
<< "c)carnivore\t\t\t\tp)pianist\n"
<< "t)tree\t\t\t\tg)game\n";
// char ch;
// char input_flag = 1;
// while(input_flag)
// {
// cin >> ch;
// switch(ch)
// {
// case 'c': cout << "A maple is a carnivore.\n"; input_flag = 0; break;
// case 'p': cout << "A maple is a painist.\n"; input_flag = 0; break;
// case 't': cout << "A maple is a tree.\n"; input_flag = 0; break;
// case 'g': cout << "A maple is a game.\n"; input_flag = 0; break;
// default: cout << "Please enter a c,p,t, or g: "; break;
// }
//
// }
char ch;
cin >> ch;
while(1)
{
if(ch == 'c')
{
cout << "A maple is a carnivore.\n";
break;
}
else if(ch == 'p')
{
cout << "A maple is a painist.\n";
break;
}
else if(ch == 't')
{
cout << "A maple is a tree.\n";
break;
}
else if(ch == 'g')
{
cout << "A maple is a game.\n";
break;
}
else
{
cout << "Please enter a c,p,t, or g: ";
}
cin >> ch;
}
return 0;
}
// 4
#include <iostream>
#define strsize 20
using namespace std;
// Benevolent Order of Programmers name structure
struct bop {
char fullname[strsize]; // real name
char title[strsize]; // job title
char bopname[strsize]; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
void display(char ch,struct bop * p);
int main()
{
//struct *bop_array = new struct [10];
struct bop bop_array[6] = {
{"Jim green","teacher","bopJip",2},
{"Tim green","egineer","bopTim",1},
{"Tom green","master","bopTom",0},
{"Kane green","coach","bopKane",2},
{"Bine green","killer","bopkiller",1},
{"Herb green","coach","bopHerb",0},
};
cout << "a. display by name\t\t\tb.display by title\n"
<< "c. display by bopname\t\t\td. display by preference\n"
<< "Enter your choice: ";
char ch;
cin >> ch;
while(ch != 'q')
{
switch(ch)
{
case 'a' : display(ch,bop_array); break;
case 'b' : display(ch,bop_array); break;
case 'c' : display(ch,bop_array); break;
case 'd' : display(ch,bop_array); break;
default : cout << "a. display by name\t\t\tb.display by title\n"
<< "c. display by bopname\t\t\td. display by preference\n"
<< "Enter your choice: ";break;
}
cin >> ch;
}
cout << "Bye!\n";
}
void display(char ch,struct bop * p)
{
for(int i = 0;i <= 5;i++)
{
if(ch == 'a')
cout << p[i].fullname << endl;
else if(ch == 'b')
cout << p[i].title << endl;
else if(ch == 'c')
cout << p[i].bopname << endl;
else
{
if(p[i].preference == 0)
cout << p[i].fullname << endl;
else if(p[i].preference == 1)
cout << p[i].title << endl;
else
cout << p[i].bopname << endl;
}
}
cout << "Next choice: ";
}
// 5
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
int incomeatoi(char *income_str);
int main()
{
int income,tax;
cout << "Please input the income: ";
char income_str[20];
cin >> income_str;
while(income = incomeatoi(income_str))
{
if(income >= 35000)
tax = 5000 * 0.00 + 10000 * 0.10 + 20000 * 0.15 + (income - 35000) * 0.20;
else if(income >= 15000)
tax = 5000 * 0.00 + 10000 * 0.10 + (income - 15000) * 0.15;
else if(income >= 5000)
tax = 5000 * 0.00 + (income - 5000) * 0.10;
else
tax = 0;
cout << "when the income is " << income << "tvarps, the tax is " << tax << " tvarps\n";
cout << "Please input the income: ";
cin >> income_str;
}
return 0;
}
int incomeatoi(char * income_str)
{
char *p = income_str;
int income;
while(*p != '\0')
{
if(isdigit(*p) != true)
break;
p++;
}
if(*p != '\0')
return false;
else
{
income = atoi(income_str);
cout << "income = " << income << endl;
return income;
}
}
// 6 封装后的
#include <iostream>
#include <string>
using namespace std;
struct patrons
{
//string name;
char name[20];
double sum_of_money;
};
struct patrons * get_patron_informations(struct patrons *patrons_ry,int patrons_num);
void output_patron_informations(struct patrons *patrons_ry,int patrons_num);
int main()
{
int patrons_num;
cout << "Please input the number of the donors: ";
cin >> patrons_num;
struct patrons * patrons_ry = new struct patrons [patrons_num];
patrons_ry = get_patron_informations(patrons_ry,patrons_num); // get the informations of the patrons
output_patron_informations(patrons_ry,patrons_num); // list the informations of the patrons
return 0;
}
struct patrons * get_patron_informations(struct patrons *patrons_ry,int patrons_num)
{
struct patrons *ps = patrons_ry;
for(int i = 0; i < patrons_num; i++)
{
cout << "Please input the " << i + 1 << " donor's informations: \n";
cout << "Name: ";
cin.get();
//cin >> ps[i].name;
//cin.getline(ps[i].name,20);
cin.get(ps[i].name,20).get();
cout << "Sum_Of_Money: ";
cin >> ps[i].sum_of_money;
}
return ps;
}
void output_patron_informations(struct patrons *patrons_ry,int patrons_num)
{
int grand_patron_num = 0,patron_num = 0;
cout << "Grand Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money >= 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
grand_patron_num++;
}
}
if(grand_patron_num == 0)
cout << "none\n";
cout << "Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money < 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
patron_num++;
}
}
if(patron_num == 0)
cout << "none\n";
}
/* // 封装前的
int main()
{
int patrons_num;
cout << "Please input the number of the donors: ";
cin >> patrons_num;
struct patrons * patrons_ry = new struct patrons [patrons_num];
for(int i = 0; i < patrons_num; i++)
{
cout << "Please input the " << i + 1 << " donor's informations: ";
cin >> patrons_ry[i].name;
cin >> patrons_ry[i].sum_of_money;
}
cout << "\n\nThe informations of the donors :\n";
int grand_patron_num = 0,patron_num = 0;
cout << "Grand Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money >= 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
grand_patron_num++;
}
}
if(grand_patron_num == 0)
cout << "none\n";
cout << "Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money < 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
patron_num++;
}
}
if(patron_num == 0)
cout << "none\n";
delete [] patrons_ry;
return 0;
}
*/
// 7
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
char str[20];
int vowels_num = 0,consonants_num = 0,others_num = 0;
cout << "Enter words (q to quit):\n";
cin >> str;
while(str[0] != 'q' || strcmp(str,"quit") != 0)
{
if(isalpha(str[0]))
{
if(str[0] == 'a' || str[0] == 'e' || str[0] == 'i' || str[0] == 'o' || str[0] == 'u')
vowels_num++;
else
consonants_num++;
}
else
others_num++;
cin >> str;
}
cout << vowels_num << " words beginning with vowels\n"
<< consonants_num << " words beginning with consonants\n"
<< others_num << " others\n";
return 0;
}
// 8 C实现
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
char filename[] = "mktdt00.txt";
int main()
{
FILE * fp;
char ch;
long ch_num;
if((fp = fopen(filename,"r")) == NULL)
{
perror("open the txtfile failed");
exit(0);
}
for(ch_num = 0;(ch = fgetc(fp)) != EOF; ch_num++);
cout << "there are " << ch_num << " characters in the " << filename << endl;
fclose(fp);
return 0;
}
//8 C++实现
#include <iostream>
#include <fstream>
#include <cstdlib>
//#include <windows.h>
using namespace std;
char filename[] = "mktdt00.txt";
int main()
{
char ch;
long ch_num = 0;
ifstream inFile; // object for handling file input
inFile.open(filename); // associate inFile with a file
if(!inFile.is_open()) // failed to open file
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
while(inFile.good()) // while input good and not at EOF
{
++ch_num;
// cout << ch_num;
inFile >> ch;
// cout << ch;
}
cout << "there is " << ch_num << " characters in this file.\n";
if(inFile.eof())
cout << "End of file reached.\n";
inFile.close(); // finished with the file
return 0;
}
// 6 与第九题作比较
#include <iostream>
#include <string>
using namespace std;
struct patrons
{
//string name;
char name[20];
double sum_of_money;
};
struct patrons * get_patron_informations(struct patrons *patrons_ry,int patrons_num);
void output_patron_informations(struct patrons *patrons_ry,int patrons_num);
int main()
{
int patrons_num;
cout << "Please input the number of the donors: ";
cin >> patrons_num;
struct patrons * patrons_ry = new struct patrons [patrons_num];
patrons_ry = get_patron_informations(patrons_ry,patrons_num); // get the informations of the patrons
output_patron_informations(patrons_ry,patrons_num); // list the informations of the patrons
return 0;
}
struct patrons * get_patron_informations(struct patrons *patrons_ry,int patrons_num)
{
struct patrons *ps = patrons_ry;
for(int i = 0; i < patrons_num; i++)
{
cout << "Please input the " << i + 1 << " donor's informations: \n";
cout << "Name: ";
cin.get();
//cin >> ps[i].name;
//cin.getline(ps[i].name,20);
cin.get(ps[i].name,20).get();
cout << "Sum_Of_Money: ";
cin >> ps[i].sum_of_money;
}
return ps;
}
void output_patron_informations(struct patrons *patrons_ry,int patrons_num)
{
int grand_patron_num = 0,patron_num = 0;
cout << "Grand Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money >= 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
grand_patron_num++;
}
}
if(grand_patron_num == 0)
cout << "none\n";
cout << "Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money < 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
patron_num++;
}
}
if(patron_num == 0)
cout << "none\n";
}
//9.txt 其实每行后面都有个\n 在接收时注意要把\n读出来
4
Sam Stone
20000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
// 9
#include <iostream>
#include <fstream>
#include <cstdlib>
//#include <string>
#define filename "9.txt"
using namespace std;
struct patrons
{
//string name;
char name[20];
double sum_of_money;
};
int main()
{
int patrons_num;
ifstream inFile; // object for handling file input
inFile.open(filename); // associate inFile with a file
if(!inFile.is_open()) // failed to open file
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
char ch = 0;
inFile >> patrons_num;
inFile >> ch;
struct patrons * patrons_ry = new struct patrons [patrons_num];
for(int i = 0; i < patrons_num; i++)
{
inFile.getline(patrons_ry[i].name,20);
inFile >> patrons_ry[i].sum_of_money;
inFile >> ch;
}
cout << "\n\nThe informations of the donors :\n";
int grand_patron_num = 0,patron_num = 0;
cout << "Grand Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money >= 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
grand_patron_num++;
}
}
if(grand_patron_num == 0)
cout << "none\n";
cout << "Patrons:\nName:\t\t\t\tSum_Of_Money\n";
for(int i = 0; i < patrons_num; i++)
{
if(patrons_ry[i].sum_of_money < 10000)
{
cout << patrons_ry[i].name << "\t\t\t\t" << patrons_ry[i].sum_of_money << endl;
patron_num++;
}
}
if(patron_num == 0)
cout << "none\n";
delete [] patrons_ry;
return 0;
}