代码都是自己编写。
第一题就是one()
第二题就是two()
…
以此类推
第二章
/**
该程序用于编写第一章的编程练习
**/
#include <iostream>
using namespace std;
//1
void one() {
string name = "xxx";
string address = "Wen Chang,Hai Nan Province";
cout << "name:" << name << ",address:" << address<<endl;
}
//2
void two() {
cout << "input a \"long\" number:";
long num;
cin >> num;
cout << num * 220 << "码" << endl;
}
//3
void t_print1() {
cout << "Three blind mice"<<endl;
}
void t_print2() {
cout << "See how they run" << endl;
}
void three() {
t_print1();
t_print1();
for (int i = 0; i < 2; i++) {
t_print2();
}
}
//4
void four() {
cout << "Enter your age:";
int age;
cin >> age;
cout << "Your age contain " << age*12 << " months" << endl;
}
//5
int f_function(int value) {
return 1.8 * value + 32.0;
}
void five() {
cout << "Please enter a Celsius value:";
int value;
cin >> value;
cout << value << " degrees Celsius is " << f_function(value) << " degrees Fahrenheit." << endl;
}
//6
double s_function(double l_years) {
return l_years * 63240;
}
void six() {
cout << "Enter the number of light years:";
double l_years;
cin >> l_years;
cout << l_years << " light years = " << s_function(l_years) << " astronomical units." << endl;
}
//7
void se_function(int hour,int minute) {
cout << "Time: " << hour << ":" << minute << endl;
}
void seven() {
int hour, minute;
cout << "Enter the number of hours:";
cin >> hour;
cout << "Enter the number of minute";
cin >> minute;
se_function(hour,minute);
}
int main() {
one();
two();
three();
four();
five();
six();
seven();
return 0;
}
第三章
#include <iostream>
using namespace std;
//1
void one() {
cout << "Enter your inch:___\b\b\b";
int inch;
const int c_factor = 12;
cin >> inch;
cout << "Your foot is: " << inch / c_factor << " and inch is:" << inch % c_factor << endl;
}
//2
void two() {
int foot, inch;
cout << "Enter your foot:";
cin >> foot;
cout << "Enter your inch";
cin >> inch;
int pound;
cout << "Enter your pound:";
cin >> pound;
const float c_factor_inch_meter = 0.0254f;
float meter = float(foot * 12 + inch) * c_factor_inch_meter;
cout << "Your height is:" << foot << " foot and " << inch << " inch\n";
const float c_factor_kilo_pound = 2.2f;
float weight = float(pound) / c_factor_kilo_pound;
cout << "Your BMI is:" << weight / (meter * meter) << endl;
}
//3
void three() {
int degrees, minutes, seconds;
cout << "Enter a latitude in degree,minutes,and seconds:\nFirst,enter the degrees:";
cin >> degrees;
cout << "Next,enter the minutes of arc:";
cin >> minutes;
cout << "Finally,enter the seconds of arc:";
cin >> seconds;
cout << degrees << " degrees," << minutes << " minutes," << seconds << " seconds = " << degrees + minutes * 1.0 / 60 + seconds * 1.0 / (60 * 60) << " degress\n";
}
//4
void four() {
long seconds;
const short d_h = 24, h_m = 60, m_s = 60;
cout << "Enter the number of seconds:";
cin >> seconds;
int day=seconds/m_s/h_m/d_h, hours=(seconds-day*d_h*h_m*m_s)/m_s/h_m, minutes=(seconds-day*d_h*h_m*m_s-hours*h_m*m_s)/60, seconds2=seconds-(minutes*m_s+hours*h_m*m_s+day*d_h*h_m*m_s);
cout << seconds << " seconds = " << day << " days," << hours << " hours," << minutes << " minutes," << seconds2 << " seconds\n";
}
//5
void five() {
long long global_population, american_population;
cout << "Enter the world's population:";
cin >> global_population;
cout << "Enter the population of the US:";
cin >> american_population;
cout << "The population of the US is "<<american_population*1.0/global_population*100<<"% of the world population\n";
}
//6
void six() {
int distance,gas_num;
cout << "Enter distance(KM) and gas_num(L),use space split them up:";
cin >> distance >> gas_num;
cout << "The oil consumption is:" << gas_num*1.0 / (distance * 1.0 / 100) << endl;
}
//7
void seven() {
float gas_100km;
cout << "Enter oil consumption:";
cin >> gas_100km;
//前面是100km=62.14英里后面是多少加仑
cout << "Result:" <<62.14 / (gas_100km / 3.875)<<endl;
}
int main() {
one();
two();
three();
four();
five();
six();
seven();
return 0;
}
第四章
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//1
void one() {
string first_name,last_name;
char deserve;
short age;
cout << "What is your first name?";
getline(cin, first_name);
cout << "What is your last name?";
getline(cin, last_name);
cout << "What letter grade do you deserve?";
cin >> deserve;
cout << "What is your age?";
cin >> age;
getchar();
cout << "Name:" << last_name << "," << first_name << endl;
cout << "Grade:" << char(deserve+1) << endl;
cout << "Age:" << age << endl;
}
//2
void two() {
//using namespace std;
const int ArSize = 20;
string name,dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
}
//3
void three() {
char first_name[20], last_name[20];
cout << "Enter your first name:";
cin.getline(first_name, 20);
cout << "Enter you last name:";
cin.getline(last_name, 20);
char all_name[40];
strcpy(all_name, last_name);
strcat(all_name, ", ");
strcat(all_name, first_name);
cout << "Here's the information in a single string:" << all_name << endl;
}
//4
void four() {
string first_name, last_name;
cout << "Enter your first name:";
getline(cin,first_name);
cout << "Enter you last name:";
getline(cin, last_name);
string all_name;
all_name = last_name + ", " + first_name;
cout << "Here's the information in a single string:" << all_name << endl;
}
//5
struct CandyBar {
string brand;
float weight;
int calorie;
};
void five() {
CandyBar snack{"Mocha Munch",2.3,350};
cout << "brand:" << snack.brand << ",weight:" << snack.weight << ",calorie:" << snack.calorie << endl;
}
//6
void six() {
CandyBar s[3]{ {"ab c",1.1,100},{"awjdio",5.5,200},{"a",3.4,500}};
for (int i = 0; i < 3; i++) {
cout << "brand:" << s[i].brand << ",weight:" << s[i].weight << ",calorie:" << s[i].calorie << endl;
}
}
//7
struct Pizza {
string company_name;
float diameter,weight;
};
void seven() {
Pizza pizza;
cout << "Enter the pizza's company name:";
cin >> pizza.company_name;
cout << "Enter the pizza's diameter:";
cin >> pizza.diameter;
cout << "Enter the pizza's weight:";
cin >> pizza.weight;
cout << "The information of this pizza is:(company name:" << pizza.company_name << ",diameter:" << pizza.diameter << ",weight:" << pizza.weight << ")\n";
}
//8
void eight() {
Pizza* pizza = new Pizza;
cout << "Enter the pizza's diameter:";
cin >> pizza->diameter;
cout << "Enter the pizza's company name:";
cin >> pizza->company_name;
cout << "Enter the pizza's weight:";
cin >> pizza->weight;
cout << "The information of this pizza is:(company name:" << pizza->company_name << ",diameter:" << pizza->diameter << ",weight:" << pizza->weight << ")\n";
}
//9
void nine() {
CandyBar* s = new CandyBar[3]{ {"ab c",1.1,100},{"awjdio",5.5,200},{"a",3.4,500} };
for (int i = 0; i < 3; i++,s++) {
cout << "brand:" << s->brand << ",weight:" << s->weight << ",calorie:" << s->calorie << endl;
}
}
//10
#include <array>
void ten() {
const int times = 3;
array<float, times> scores;
int total=0;
float average;
cout << "Please input your three scores of Fourty-Meter Run,use space split them up:";
for (int i = 0; i < scores.size(); i++) {
cin >> scores[i];
total += scores[i];
}
average = float(total) / times;
cout << times << " times,and the average is " << average << endl;
}
int main() {
one();
two();
three();
four();
five();
six();
seven();
eight();
nine();
ten();
return 0;
}
第五章
#include <iostream>
#include <array>
#include <cstring>
#include <string>
using namespace std;
//1
void one() {
int a, b;
cout << "Enter two int numbers,use space split them up:";
cin >> a >> b;
int total=0;
for (int i = a; i <= b; i++) {
total += i;
}
cout << "Total:" << total << endl;
}
//2
void two() {
const int size = 101;
array<long double, size> factorials = {1,1};
for (int i = 2; i < size; i++)
factorials[i] = i * factorials[i - 1];
cout << factorials[100];
}
//3
void three() {
int total = 0, num;
cout << "Enter a number(use '0' stop input):";
while ((cin >> num) && num != 0) {
total += num;
cout << "Total:" << total << endl;
cout << "Enter a number(use '0' stop input):";
}
}
//4
void four() {
double Daphne=100, Cleo=100,D_interest=0.1,C_interest=0.05;
int years=0;
while (Cleo <= Daphne) {
Daphne += 100 * D_interest;
Cleo += Cleo * C_interest;
years++;
}
cout << years << " years.Daphne=" << Daphne << ",Cleo=" << Cleo << endl;
}
//5
void five() {
string months[12] = {"1","2","3","4","5","6","7","8","9","10","11","12"};
int sales[12],total=0;
for (int i = 0; i < 12; i++) {
cout << "Enter the sales:";
cin >> sales[i];
cout << months[i] << ":"<<sales[i]<<endl;
total += sales[i];
}
cout<< "Total:" << total << endl;
}
//6
void six() {
int sales[3][12];
int total = 0;
for (int i = 0; i < 3; i++) {
int total2 = 0;
cout << "Enter 12 times num:";
for (int j = 0; j < 12; j++) {
cin >> sales[i][j];
total2 += sales[i][j];
}
cout << i+1 << "year,total is " << total2<<endl;
total += total2;
}
cout << "Three years total is " << total << endl;
}
//7
struct Car {
string manufacturer;
int productive_year;
};
void seven() {
cout << "How many cars do you wish to catalog?";
int size;
cin >> size;
Car* car = new Car[size];
for (int i = 0; i < size; i++) {
cout << "Car #" << i + 1 << ":\n";
cout << "Please enter the make:";
getchar();
getline(cin,car[i].manufacturer);
cout << "Please enter the year made:";
cin >> car[i].productive_year;
}
cout << "Here is your collection:\n";
for (int i = 0; i < size; i++) {
cout << car[i].productive_year << " " << car[i].manufacturer << endl;
}
}
//8
void eight() {
char word[100];
int count=0;
cout << "Enter words(to stop,type the word done):"<<endl;
while ((cin>>word)&&strcmp(word, "done")) {
count++;
}
cout << "You entered a total of " << count << " words.\n";
}
//9
void nine() {
string word;
int count = 0;
cout << "Enter words(to stop,type the word done):" << endl;
while ((getchar()) != '\n');//清除输入缓冲区
while ((cin >> word) && word != "done") {
count++;
}
cout << "You entered a total of " << count << " words.\n";
}
//10
void ten() {
int rows;
cout << "Enter number of rows:";
cin >> rows;
for (int i = 0; i < rows; i++) {
for (int j = rows - i - 1; j > 0; j--) {
cout << '.';
}
for (int j = 0; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}
int main() {
/*
one();
two();
three();
four();
five();
six();
seven();
*/
eight();
nine();
ten();
return 0;
}
第六章
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
//1
void one() {
char ch;
while ((ch=getchar())&&ch != '@') {
if (isdigit(ch))
continue;
else if (isupper(ch))
ch = tolower(ch);
else if (islower(ch))
ch = toupper(ch);
cout << ch;
}
cout << endl;
}
//2
#include <array>
void two() {
char ch;
const short size = 10;
short count = 0;
array<int, size> nums;
while ((cin>>ch) && isdigit(ch)&&count!=9) {
nums[count] = int(ch-'0');
count++;
}
double average = 0;
for (int i = 0; i < count; i++)
average += nums[i];
if (count > 0)
average /= count;
cout << "平均值为:" << average << endl;
short num = 0;
for (int i = 0; i < count; i++)
if (nums[i] > average)
num++;
cout << "有" << num << "个数字大于平均值" << endl;
}
//3
void three() {
char ch;
cout << "Please enter one of the following choices:\n";
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
string str;
while ((cin >> ch)) {
switch (ch)
{
case 'c':
str = "carnivore";
break;
case 'p':
str = "pianist";
break;
case 't':
str = "tree";
break;
case 'g':
str = "game";
break;
default:
cout << "Please enter one of the following choices:";
continue;
}
break;
}
cout << "A maple is a " << str << "." << endl;
}
//4
#define strsize 20
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void four() {
bop programmers[5] = {
{"Wimp Macho","WM","wm",0},{"Raki Rhodes","RR","rr",1},{"Celia Laiter","CL","cl",2},{"Hoppy Hipman","HH","hh",0},{"Pat Hand","PH","ph",1}
};
char ch;
cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name b. display by title\n";
cout << "c. display by bopname d. display by preference\n";
cout << "q. quit\n";
cout << "Enter your choice:";
while (1) {
bool flag = true;
cin >> ch;
switch (ch)
{
case 'a':
for (bop b : programmers)
cout << b.fullname << endl;
break;
case 'b':
for (bop b : programmers)
cout << b.title << endl;
break;
case 'c':
for (bop b : programmers)
cout << b.bopname << endl;
break;
case 'd':
for (bop b : programmers) {
if (b.preference == 0)
cout << b.fullname << endl;
else if (b.preference == 1)
cout << b.title << endl;
else
cout << b.bopname << endl;
break;
}
case 'q':
cout << "Bye";
flag = false;
break;
default:
break;
}
if (!flag)
break;
cout << "Next choice : ";
}
}
//5
bool IsOver(string str) {
if (str.size() == 0)
return true;
for (char ch : str) {
if (!isdigit(ch))
return true;
}
return false;
}
void five() {
string str;
while (1) {
double result = 0;
cin >> str;
if (IsOver(str))
break;
int num = atoi(str.c_str());
if (num >= 5000) {
num -= 5000;
}
//收10的税
if (num <= 10000) {
result += num * 0.1;
num = 0;
}
else {
result += 10000 * 0.1;
num -= 10000;
}
if (num <= 20000) {
result += num * 0.15;
num = 0;
}
else {
result += 20000 * 0.15;
num -= 20000;
}
result += num * 0.2;
cout << " 你的所得税为:" << result<<endl;
}
}
//6
struct Patron {
string name;
double money;
};
void six() {
int count;
cout << "输入捐献者数量:";
cin >> count;
Patron* patrons = new Patron[count];
for (int i = 0; i < count; i++) {
cout << "请输入第" << i + 1 << "个捐献者信息" << endl;
cout << "请输入名字:";
getline(cin,patrons[i].name);
cout << "请输入款数:";
cin >> patrons[i].money;
}
cout << "重要捐款人:\n";
int grand_patrons = 0;
bool* patrons_flag = new bool[count];
for (int i = 0; i < count; i++)
if (patrons[i].money > 10000) {
cout << patrons[i].name << endl;
grand_patrons++;
patrons_flag[i] = true;
}
else {
patrons_flag[i] = false;
}
if (!grand_patrons)
cout << "none\n";
cout << "其它捐款者:";
grand_patrons = 0;
for(int i=0;i<count;i++)
if (!patrons_flag[i]) {
cout << patrons[i].name << endl;
grand_patrons++;
}
if (!grand_patrons)
cout << "none\n";
}
//7
void seven() {
string word;
cout << "输入单词(只输入q退出):\n";
int vowel_num = 0, consonant_num = 0, other_num = 0;;
while ((cin >> word) && word != "q") {
if (!isalpha(word[0]))
other_num++;
else {
if (word[0] == 'a' || word[0] == 'o' || word[0] == 'i' || word[0] == 'e' || word[0] == 'u')
vowel_num++;
else
consonant_num++;
}
}
cout << vowel_num << "个单词以元音开头\n" << consonant_num << "个单词以辅音开头\n" << other_num << "个其它单词\n";
}
//8
#include <fstream>
void eight() {
ifstream inFile;
inFile.open("test.txt");
if (!inFile.is_open()) {
cout << "文件打开失败!";
exit(EXIT_FAILURE);
}
char ch;
int num=0;
inFile >> ch;
while (inFile.good()) {
num++;
inFile >> ch;
}
cout << "该文件有" << num << "个字符\n";
}
//9
void nine() {
ifstream inFile;
inFile.open("patrons.txt");
if (!inFile.is_open())
exit(EXIT_FAILURE);
int count;
inFile >> count;
string str;
getline(inFile, str);//吞掉使用>>运算符剩下的回车号
Patron* patrons = new Patron[count];
for (int i = 0; i < count; i++) {
//getline(inFile,str);
getline(inFile, patrons[i].name);
inFile >> patrons[i].money;
getline(inFile, str);
}
cout << "重要捐款人:\n";
int grand_patrons = 0;
bool* patrons_flag = new bool[count];
for (int i = 0; i < count; i++)
if (patrons[i].money > 10000) {
cout << patrons[i].name << endl;
grand_patrons++;
patrons_flag[i] = true;
}
else {
patrons_flag[i] = false;
}
if (!grand_patrons)
cout << "none\n";
cout << "其它捐款者:\n";
grand_patrons = 0;
for (int i = 0; i < count; i++)
if (!patrons_flag[i]) {
cout << patrons[i].name << endl;
grand_patrons++;
}
if (!grand_patrons)
cout << "none\n";
}
int main() {
//one();
//two();
//three();
//four();
//five();
//six();
//seven();
//eight();
//nine();
return 0;
}
第七章
// 第七章.cpp
#include <iostream>
using namespace std;
//1
double one_f1(double a, double b) {
return 2.0 * a * b / (a + b);
}
void one() {
double a, b;
while (1) {
cin >> a >> b;
if (!a || !b)
break;
cout << one_f1(a, b) << endl;
}
}
//2
int two_f1(double score[]) {
int count = 0;
while (1) {
cin >> score[count];
if (score[count] < 0)
break;
if (count == 9)
break;
count++;
}
return count;
}
void two_f2(const double score[],int count) {
cout << "scores:" << endl;
for (int i = 0; i <= count; i++) {
if (!i)
cout << score[i];
else
cout << " " << score[i];
}
}
void two_f3(const double score[], int count) {
cout << "average:" ;
double average = 0;
for (int i = 0; i <= count; i++) {
average += score[i];
}
cout << average/count << endl;
}
void two() {
double score[10];
int count;
count=two_f1(score);
two_f2(score,count);
two_f3(score, count);
}
//3
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};
void three_f1(box b) {
cout << b.maker << " " << b.height << " " << b.width << " " << b.length << " " << b.volume << endl;
}
void three_f2(box* b) {
b->volume = b->width * b->length * b->height;
}
void three() {
box b{"abcd",1,2,3,4};
three_f1(b);
three_f2(&b);
three_f1(b);
}
//4
long double probability(int n, int c) {
long double result = 1;
while (n && c) {
result = result * n / c;
n--, c--;
}
return result;
}
void four() {
cout << "请输入两次数字总数和选取的数字数:" << endl;
int n, c;
long double proba = 1;
for (int i = 0; i < 2; i++) {
cin >> n >> c;
proba*=probability(n, c);
}
cout << "中头奖概率为:" << proba << endl;
}
//5
void five_f1(int num) {
long long result=1;
if (num > 1) {
while (num!=1) {
result *= num;
num--;
}
}
cout << result << endl;
}
void five() {
int num;
while (1) {
cout << "输入一个数,计算阶乘,输入小于0的数则退出:";
cin >> num;
if (num < 0)
break;
five_f1(num);
}
}
//6
int Fill_array(double a[],int a_size) {
string str;
bool flag;
int d_point_count = 0,i;//小数点个数
cout << "输入double值,用空格隔开,输入非数字退出:";
for (i = 0; i < a_size; i++) {
cin >> str;
//判断是否是数字
d_point_count = 0;
flag = true;
for (int j = 0; j < str.size();j++) {
char ch = str[j];
if (!j) {
if (ch != '-'&&!isdigit(ch)) {
flag = false;
break;
}
}
if (j) {
if (ch == '.') {
d_point_count++;
if (d_point_count > 1) {
flag = false;
break;
}
}
else if (!isdigit(ch)) {
flag = false;
break;
}
}
}
if (!flag)
break;
a[i] = atof(str.c_str());
}
return i;
}
void Show_array(double a[], int a_size) {
cout << "数组内容如下:" << endl;
if (!a_size)
cout << "none";
for (int i = 0; i < a_size; i++)
cout << a[i] << " ";
cout << endl;
}
void Reverse_array(double a[],int a_size) {
double temp;
for (int i = 0; i < (a_size / 2)-1; i++) {
temp = a[i];
a[i] = a[a_size - 1 - i];
a[a_size - i - 1] = temp;
}
}
void six() {
double a[10];
int a_size = 10;
a_size=Fill_array(a, a_size);
Show_array(a, a_size);
Reverse_array(a, a_size);
Show_array(a, a_size);
}
//7
double* fill_array(double ar[], int limit) {
double temp;
int i;
for (i = 0; i < limit; i++) {
cout << "Enter value #" << (i + 1) << ": ";
cin>>temp;
if (!cin) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input;input process terminated.\n";
break;
}
else if (temp < 0)
break;
ar[i] = temp;
}
return (ar + i);
}
void show_array(const double ar[], double* end) {
for (int i = 0; ar+i != end; i++) {
cout << "Property #" << (i + 1) << ":$";
cout << ar[i] << endl;
}
}
void revalue(double r, double ar[], double* end) {
for (int i = 0; ar + i != end; i++) {
ar[i] *= r;
}
}
void seven() {
const int Max = 5;
double properties[Max];
double *end = fill_array(properties, Max);
show_array(properties, end);
if (end != properties) {
cout << "Enter revaluiation factor:";
double factor;
while (!(cin >> factor)) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input;Please enter a number:";
}
revalue(factor, properties, end);
show_array(properties, end);
}
cout << "Done.\n";
}
//8
const int Seasons = 4;
const char* Snames[4] = { "Spring","Summer","Fall","Winter" };
void fill(double expeneses[]) {
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i]<<" expenses:";
cin >> expeneses[i];
}
}
void show(double da[]) {
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ":$" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses:$" << total << endl;
}
void eight_f1() {
double expenses[4];
fill(expenses);
show(expenses);
}
struct E {
double expense;
};
void fill_2(E expeneses[]) {
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses:";
cin >> expeneses[i].expense;
}
}
void show_2(E da[]) {
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ":$" << da[i].expense << endl;
total += da[i].expense;
}
cout << "Total Expenses:$" << total << endl;
}
void eight_f2() {
E expenses[Seasons];
fill_2(expenses);
show_2(expenses);
}
void eight() {
//a
eight_f1();
//b
eight_f2();
}
//9
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n) {
int entered = 0; int i;
for ( i = 0; i < n; i++) {
cout << "Name:";
cin.getline(pa[i].fullname,SLEN);
if (pa[i].fullname[0] == '\0')
break;
cout << "Hobby:";
cin.getline(pa[i].hobby, SLEN);
cout << "Ooplevel:";
cin >> pa[i].ooplevel;
while ((cin.get() != '\n'))
continue;
}
return i;
}
void display1(student st) {
cout << "Name:";
cout << st.fullname << endl;
cout << "Hobby:";
cout << st.hobby << endl;
cout << "Ooplevel:";
cout << st.ooplevel << endl;
}
void display2(const student* ps) {
cout << "Name:";
cout << ps->fullname << endl;
cout << "Hobby:";
cout << ps->hobby << endl;
cout << "Ooplevel:";
cout << ps->ooplevel << endl;
}
void display3(const student pa[], int n) {
for (int i = 0; i < n; i++) {
cout << "Name:";
cout << pa[i].fullname << endl;
cout << "Hobby:";
cout << pa[i].hobby << endl;
cout << "Ooplevel:";
cout << pa[i].ooplevel << endl;
}
}
void nine() {
cout << "Enter class size:";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student* ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++) {
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done\n";
}
//10
double add(double a, double b) {
return a + b;
}
double mu(double a,double b) {
return a * b;
}
double calculate(double a,double b,double (*function)(double,double)) {
return function(a, b);
}
void ten() {
double a, b;
/**
while (1) {
cin >> a >> b;
cout << a << "+" << b << "=";
cout<<calculate(a, b, add)<<endl;
cout << a<<"*"<<b<<"=";
cout << calculate(a, b, mu) << endl;
}
**/
double (*functions[2])(double, double) = {add,mu};
while (1) {
cin >> a >> b;
cout << a << "+" << b << "=";
cout << calculate(a, b, functions[0]) << endl;
cout << a << "*" << b << "=";
cout << calculate(a, b, functions[1]) << endl;
}
}
int main()
{
//one();
//two();
//three();
//four();
//five();
//six();
//seven();
//eight();
//nine();
//ten();
return 0;
}
第八章
// 第八章.cpp
#include <iostream>
#include <string>
using namespace std;
//1
void one_f1(const string &str,int n=1) {
if (n > 1)
one_f1(str, n-1);
cout << str << endl;
}
void one() {
string str;
getline(cin,str);
one_f1(str,2);
}
//2
struct CandyBar {
string brand;
double weight;
int heat;
};
void two_f1(CandyBar& bar, const char* c="Millennium Munch", const double& weight=2.85, const int& heat=350) {
bar.brand = c;
bar.weight = weight;
bar.heat = heat;
}
void two_f2(const CandyBar& bar) {
cout << "brand=" << bar.brand << " weight=" << bar.weight << " heat=" << bar.heat << endl;
}
void two() {
CandyBar bar;
two_f1(bar);
two_f2(bar);
}
//3
void three_f1(string& str) {
for (char &ch : str)
if (islower(ch))
ch = toupper(ch);
}
void three() {
string str;
while (1) {
cout << "Enter a string (q to quit):";
getline(cin, str);
if (str == "q")
break;
three_f1(str);
cout << str << endl;
}
cout << "Bye." << endl;
}
//4
#include <cstring>
struct stringy {
char* str;
int ct;
};
void show(const stringy& str,int n=1) {
while(n--)
cout << str.str << endl;
}
template<typename T>
void show(const T& str,int n=1) {
while (n--)
cout << str << endl;
}
void set(stringy &beany,char testing[]) {
beany.str = new char[strlen(testing)];
beany.str = testing;
}
void four() {
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("Done!");
}
//5
template<typename T>
T max5(const T t[5]) {
T maxnum = t[0];
for (int i = 1; i < 5;i++) {
if (t[i] > maxnum)
maxnum = t[i];
}
return maxnum;
}
void five() {
int a[5] = { 1,2,5,3,4 };
cout<<max5(a)<<endl;
double b[5] = { 1.1,2.2,3.3,10.1,20 };
cout << max5(b)<<endl;
}
//6
template<typename T>
T maxn(T t[],const int &count) {
T maxnum = t[0];
for (int i = 1; i < count; i++) {
maxnum = maxnum > t[i] ? maxnum : t[i];
}
return maxnum;
}
template<> const char* maxn(const char* t[], const int& count) {
const char* result=t[0];
for (int i = 1; i < count; i++) {
result = strlen(result) > strlen(t[i]) ? result : t[i];
}
return result;
}
void six() {
int a[6] = { 1,2,3,4,5,6 };
cout<<maxn(a, 6);
double b[4] = { 1.1,3.3,2.2,5.5 };
cout<<endl<<maxn(b, 4);
const char * c[5] = {"abc","abcd","abcde","abcdefg","aaaaaaaaa"};
cout<<endl<<maxn(c, 5)<<endl;
}
//7
struct debts {
char name[50];
double amount;
};
template<typename T>
T SumArray( T t[],int n) {
T sum = 0;
cout << "A" << endl;
for (int i = 0; i < n; i++) {
sum += t[i];
}
return sum;
}
template<typename T>
T SumArray(T* t[], int n) {
T sum = 0;
cout << "B" << endl;
for (int i = 0; i < n; i++) {
sum += *t[i];
}
return sum;
}
void seven() {
int things[6] = { 13,31,103,301,310,130 };
struct debts mr_E[3] = {
{"Ima Wolfe",2400.0},
{"Ura Foxe",1300.0},
{"Iby Stout",1800.0}
};
double* pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "things Sum:";
cout<<SumArray(things,6)<<endl;
cout << "pd Sum:";
cout << SumArray(pd, 3)<<endl;
}
int main()
{
//one();
//two();
//three();
//four();
//five();
//six();
//seven();
}
第九章
main.cpp:
// 第九章.cpp
#include <iostream>
using namespace std;
//1
#include "golf.h"
void one() {
golf g1, g2;
setgolf(g1, "abcd efg", 1);
setgolf(g2);
showgolf(g1);
showgolf(g2);
handicap(g1, 3);
showgolf(g1);
}
//2
const int ArSize = 10;
void strcount(const string str) {
cout << "/" << str << "/count:" << str.size() << endl;
}
void two() {
string str;
cout << "Enter a line:\n";
getline(cin, str);
while (str != "") {
strcount(str);
cout << "Enter next line (empty line to quit):\n";
getline(cin, str);
}
cout << "Bye\n";
}
//3
#include <new>
struct chaff {
char dross[20];
int slag;
};
//char buffer[512];
void three() {
char *buffer=new char[512];
chaff* c = new (buffer) chaff[2];
strcpy(c[0].dross, "abcd wef");
strcpy(c[1].dross, "64849aw awd");
c[0].slag = 0;
c[1].slag = 1;
for (int i = 0; i < 2; i++) {
cout << c[i].dross << " " << c[i].slag << endl;
}
}
//4
#include "four.h"
using namespace SALES;
void four() {
Sales s1,s2;
double ar[3] = { 1.1,2.2,3.3 };
setSales(s1, ar, 3);
setSales(s2);
showSales(s1);
showSales(s2);
}
int main()
{
//one();
//two();
//three();
//four();
return 0;
}
golf.h:
#include <string>
#include <iostream>
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
golf.cpp:
#include "golf.h"
using namespace std;
void setgolf(golf& g, const char* name, int hc) {
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g) {
cout << "input name:";
cin.getline(g.fullname, Len);
cout << "input hc:";
cin >> g.handicap;
return strlen(g.fullname) == 0 ? 0 : 1;
}
void handicap(golf& g, int hc) {
g.handicap = hc;
}
void showgolf(const golf& g) {
cout << "name:" << g.fullname << endl;
cout << "hc:" << g.handicap << endl;
}
four.h:
namespace SALES {
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales& s, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
four.cpp:
#include "four.h"
#include <iostream>
void SALES::setSales(Sales& s, const double ar[], int n) {
double max = ar[0],min=ar[0];
double total = 0;
for (int i = 0; i < n; i++) {
total += ar[i];
max = max < ar[i] ? ar[i] : max;
min = min > ar[i] ? ar[i] : min;
s.sales[i] = ar[i];
}
for (int i = n; i < QUARTERS; i++) {
s.sales[i] = 0;
}
s.max = max;
s.min = min;
s.average = total / 4;
}
using namespace std;
void SALES::setSales(Sales& s) {
cout << "Enter four items:" << endl;
double max, min;
double total = 0;
for (int i = 0; i < 4; i++) {
cin >> s.sales[i];
if (!i)
max = min = s.sales[i];
else {
max = max < s.sales[i] ? s.sales[i] : max;
min = min > s.sales[i] ? s.sales[i] : min;
}
total += s.sales[i];
}
s.max = max;
s.min = min;
s.average = total / 4;
}
void SALES::showSales(const Sales& s) {
for (int i = 0; i<QUARTERS; i++) {
cout << s.sales[i] << " ";
}
cout << endl << "av:" << s.average << " max:" << s.max << " min:" << s.min << endl;
}
第十章
main.cpp:
// 第十章.cpp
#include <iostream>
using namespace std;
//1
class Bank_account {
private:
string name, account;
double deposit;
public:
Bank_account(string name, string account, double deposit) :name(name), account(account), deposit(deposit) {}
void Show() {
cout << "name:" << name << " account:" << account << " deposit:" << deposit << endl;
}
void Save(double money) {
this->deposit += money;
}
void Draw(double money) {
this->deposit -= money;
}
};
void one() {
Bank_account account("a b","123456",100.6);
account.Show();
account.Save(60);
account.Show();
account.Draw(0.6);
account.Show();
}
//2
class Person {
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
Person() { lname = ""; fname[0] = '\0'; }
Person(const string& ln, const char* fn = "Heyyou");
void Show() const;
void FormalShow()const;
};
Person::Person(const string& ln, const char* fn) {
this->lname = ln;
strcpy(fname, fn);
}
void Person::Show()const {
cout << fname << " " << lname << endl;
}
void Person::FormalShow()const {
cout << lname << " " << fname << endl;
}
void two() {
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
one.FormalShow();
two.Show();
two.FormalShow();
three.Show();
three.FormalShow();
}
//3
#include <string>
class golf {
private:
static const int Len = 40;
char fullname[Len];
int handicap;
public:
golf(const char* name, int hc) {
strcpy(fullname, name);
handicap = hc;
}
golf() {
string fname;
int hc;
cout << "input name:";
getline(cin, fname);
cout << "input hc:";
cin >> hc;
golf g(fname.c_str(), hc);
*this = g;
}
void chandicap(int hc) {
handicap = hc;
}
void showgolf() {
cout << "name:" << fullname << endl;
cout << "hc:" << handicap << endl;
}
};
void three() {
//声明时就会调用构造
golf g1("abcd efg", 1), g2;
g1.showgolf();
g2.showgolf();
g1.chandicap(2);
g1.showgolf();
}
//4
#include "four.h"
void four() {
using namespace SALES;
double ar[3] = { 1.1,2.2,3.3 };
Sales s1(ar,3), s2;
s1.showSales();
s2.showSales();
}
//5
#include <stack>
struct customer {
char fullname[35];
double payment;
};
void five() {
stack<customer> s;
customer c1 = {"abcd a",3.5};
s.push(c1);
double total = 0;
total += s.top().payment;
s.pop();
cout << "total:" << total << endl;
}
//6
class Move {
private:
double x;
double y;
public:
Move(double a = 0, double b = 0) {
x = a;
y = b;
}
void showmove() const {
cout << "x:" << x << " y:" << y << endl;
}
Move add(const Move& m)const {
return Move(m.x+this->x,m.y+this->y);
}
void reset(double a = 0, double b = 0) {
x = a;
y = b;
}
};
void six() {
Move a(1, 1);
Move b(2, 2);
a.showmove();
b.showmove();
Move c=a.add(b);
a.showmove();
c.showmove();
}
//7
class Plorg {
private:
char name[19];
int CI;
public:
Plorg(const char n[], int ci = 50) {
strncpy(name, n, 19);
CI = ci;
}
void setCI(int ci) {
CI = ci;
}
void show() {
cout << "name:" << name << endl << "CI:" << CI << endl;
}
};
void seven() {
Plorg p("abcd efgawidhioawdhawiodhawd");
p.show();
p.setCI(1);
p.show();
}
//8
#include "list.h"
template<typename T>
void print_list(T d) {
cout << d << " ";
}
void eight() {
List<int> l1;
l1.visit(print_list);
l1.Add(6);
for (int i = 0; i < 49; i++) {
l1.Add(1);
}
l1.visit(print_list);
l1.Add(4);
l1.visit(print_list);
}
int main()
{
//one();
//two();
//three();
//four();
//five();
//six();
//seven();
//eight();
}
four.h:
#include <iostream>
using namespace std;
namespace SALES {
const int size = 4;
class Sales {
private:
double sales[size];
double average;
double max;
double min;
public:
Sales(const double ar[], int n) {
this->average = 0;
this->max = this->min = ar[0];
for (int i = 0; i < n; i++) {
this->sales[i] = ar[i];
this->max = this->max > ar[i] ? this->max : ar[i];
this->min = this->min < ar[i] ? this->min : ar[i];
this->average += ar[i];
}
for (int i = n; i < 4; i++)
this->sales[i] = 0;
this->average /= n;
}
Sales() {
cout << "Enter four items:";
this->average = 0;
for (int i = 0; i < 4; i++) {
cin >> this->sales[i];
if (!i)
this->max = this->min = this->sales[i];
else {
this->max = this->max > this->sales[i] ? this->max : this->sales[i];
this->min = this->min < this->sales[i] ? this->min : this->sales[i];
}
this->average += this->sales[i];
}
this->average /= 4;
}
void showSales() {
cout << "sales:";
for (int i = 0; i < 4; i++) {
cout << this->sales[i] << " ";
}
cout << endl << "max=" << this->max << " min=" << this->min << endl;
}
};
}
list.h:
#include <iostream>
using namespace std;
template<typename T>
class List
{
private:
static const int maxSize = 50;
T data[maxSize];
int now;
public:
List() {
now = 0;
}
bool Empty() {
return now == 0;
}
bool Full() {
return now == maxSize;
}
bool Add(T d) {
if (this->Full()) {
cout << "列表满了" << endl;
return false;
}
data[now++] = d;
return true;
}
void visit(void (*pf)(T&)) {
if (this->Empty()) {
cout << "列表为空" << endl;
return;
}
for (int i = 0; i < now; i++) {
pf(data[i]);
}
cout << endl;
}
};
---------------未完待续