一
//调和平均数
#include <iostream>
float average(float,float);
int main() {
float a, b;
std::cout << "Enter two numbers: ";
while (std::cin>>a>>b) {
if (a != 0 && b != 0) {
std::cout << "The harmonic mean of " << a << " and " << b << " is " << average(a, b);
std::cout << "\nEnter two numbers: ";
}
else if (a == 0 && b == 0) {
std::cout << "The harmonic mean of " << a << " and " << b << " is " << average(a, b);
std::cout << "\nEnter two numbers: ";
}
else
break;
}
return 0;
}
float average(float x, float y) {
return 2.0*x*y/(x+y);
}
二* &&顺序点:左到右
//高尔夫成绩
#include <iostream>
using namespace std;
const short int Max=10;
short int times;
float golf[Max],total;
short int input();输入,返回输出成绩数量
float output(short int);//输出,返回总成绩
void average(short int,float);//输出平均数
int main() {
cout << "Welcome to Golf Score System, 10 enter max, q to quit\n";
times=input();
total=output(times);
if(times!=0)
average(times,total);
return 0;
}
short int input() {
short int i=0;
cout << "Golf Score 1: ";
while (i<Max && cin >> golf[i]) {//*cin放&&右边防止数组溢出
if (golf[i] >= 0) {
if(i<9)
cout << "Golf Score " << i + 2 << ": ";
i++;
}
else
cout << "Please enter a right score, Golf Score "<<i+1<<": ";
}
cout << endl;
return i;
}
float output(short int i) {
float ave=0;
for (int n = 0; n < i; n++) {
cout << "Golf Score " << n + 1 << ": "<<golf[n]<<endl;
ave += golf[n];
}
return ave;
}
void average(short int a,float b ) {
times==1? cout << "The average of a golf score is " << b / a:
cout << "The average of "<<a << " golf scores is " << b / a;
}
三
//计算和显示box信息
#include <iostream>
using std::cout;
struct box {
char maker[40];
float height;
float width;
float length;
float volum;
};
void display(struct box);
float volum(struct box*);
int main() {
box sbox{ "Black Side",20,20,40 };
sbox.volum = volum(&sbox);
display(sbox);
return 0;
}
float volum(struct box* vol) {
return vol->height*vol->length*vol->width;
}
void display(struct box dis) {
cout << "Here are the information of box: "
"\nMeker: "<<dis.maker
<<"\nHeight: " << dis.height
<< "\nWidth: " << dis.width
<< "\nLength: " << dis.length
<< "\nVolum: " << dis.volum;
}
四* 数据转换/概率公式
//lotto.cpp -- probability of winning
#include <iostream>
long double probability(unsigned ,unsigned, unsigned);
int main() {
using namespace std;
cout << "Welcom to Happy Lottery! You can win 9000000 dollars just cast 6dollars!\n"
"5 dollars for 5 Fild Number: 100000 to 100100, 1 dollar for a Special Number: 0 to 50\n"
"Now, let me tell you the Probability of Win the First Prize.\n\n";
short int fild = 101, fildpicks = 5, special = 51;
cout << "You have one chance in " << probability(fild,fildpicks,special) << " of winning.\n";
cout << "Bye!\n";
return 0;
}
long double probability(unsigned number,unsigned picks,unsigned spe) {
long double result = 1.0;
long double n = number;
unsigned p = picks;
for ( ; p > 0; n--, p--)
result *= n / p;
return 1/result/spe;
}
/*unsigned short int* getNumbers() {
cout << "\nEnter the Fild Number #1: ";
for (int i = 0; i < 5; ) {
cin >> fild[i];
if (fild[i] >= 100000 && fild[i] <= 100100) {
fild[i] -= 100000;
++i;
cout << "\nEnter the Fild Number #" << i + 1 << ": ";
}
else
cout << "Please enter the correct Fild Number #" << i + 1 << ": ";
}
cout << "\nThe last step, Enter a Special Number: ";
cin >> fild[5];
while (fild[5] > 50 || fild[5] < 0) {
cout << "Please enter a correct Special Number: ";
cin >> fild[5];
}
return fild;
}*/
五* 递归-循环
//递归 -- 阶乘
#include <iostream>
unsigned int factorial(int);
int main() {
using namespace std;
int n;
cout << "Enter a number: ";
while (cin >> n) {
cout << n << "! = " << factorial(n);
cout << "\nEnter a number: ";
}
return 0;
}
unsigned int factorial(int n) {
unsigned int fac = 1;
n != 0 ? fac *= n*factorial(n - 1):fac;
return fac;
}
六* 反转数组-
//数组反转
#include <iostream>
using namespace std;
const unsigned int arSize = 10;
unsigned short Fill_array(double [], unsigned int);
void Show_array(double[], unsigned int);
void Reverse_array(double[], unsigned int);
void Reverse_arraypl(double[], unsigned int);
int main() {
double num[arSize];
unsigned int arsize = Fill_array(num, arSize);
Show_array(num, arsize);
Reverse_array(num, arsize);
cout << endl;
Show_array(num, arsize);
Reverse_arraypl(num, arsize);
cout << endl;
Show_array(num, arsize);
return 0;
}
unsigned short Fill_array(double number[], unsigned int size) {
unsigned int i=0;
cout << "Enter a double: ";
for ( ; i < arSize && cin >> number[i];i++)
if(i<arSize-1)
cout << "Enter a double: ";
return i;
}
void Show_array(double number[], unsigned int size) {
cout << "Array include: ";
for (int i = 0; i < size; i++)
cout << number[i] <<"\t";
}
void Reverse_array(double number[], unsigned int size) {
double temp;
for (int i = 0; i < size / 2; +i++) {
temp = number[i];
number[i] = number[size - i - 1];
number[size - i - 1] = temp;
}
}
void Reverse_arraypl(double number[], unsigned int size) {
double temp;
for (int i = 1; i < size / 2; +i++) {
temp = number[i];
number[i] = number[size - i - 1];
number[size - i - 1] = temp;
}
}
七 待修改
// arrfun3.cpp -- array function and const
#include <iostream>
const int Max = 5;
const double* fill_array(const double*,const double*);
void show_array(const double*,const double*);
void revalue(double r,const double*,const double*);
int main() {
using namespace std;
double properties[Max];
const double *arrend = fill_array(properties, properties+Max);
show_array(properties,arrend);
if (arrend > 0) {
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, arrend);
show_array(properties, arrend);
}
cout << "Done.\n";
cin.get();
cin.get();
return 0;
}
const double* fill_array(const double* begin,const double* end) {
using namespace std;
double temp;
const double* pt;
for (pt = begin; pt!=end; pt++) {
cout << "Enter value #" << *(pt + 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;
pt = &temp;
}
return pt;
}
void show_array(const double* begin, const double* end) {
using namespace std;
for (const double* pt=begin;begin!=end; pt++) {
cout << "Property #" << *(pt + 1) << ": $";
cout << pt << endl;
}
}
void revalue(double r,const double* begin,const double* end) {
double rev;
for (const double* pt = begin; pt != end; pt++) {
rev = *pt * r;
pt =&rev;
}
}
八* char*一维数组-指针-结构
//arrobj.cpp -- function with array objects(C++11)
#include <iostream>
const int Seasons = 4;
const char* Snames[Seasons]= { "Spring","Summer","Fall","Winter" };
void fill(double pa[Seasons]);
void show(double da[Seasons]);
int main() {
double expenses[Seasons];
fill(expenses);
show(expenses);
return 0;
}
void fill(double pa[Seasons]) {
using namespace std;
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa[i];
}
}
void show(double da[Seasons]) {
using namespace std;
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 Expense: $" << total << endl;
}
//arrobj.cpp -- function with array objects(C++11)
#include <iostream>
const int Seasons = 4;
struct name {
const char* Snames[Seasons];
};
name Snames = {"Spring","Summer","Fall","Winter"};
void fill(double pa[Seasons]);
void show(double da[Seasons]);
int main() {
double expenses[Seasons];
fill(expenses);
show(expenses);
return 0;
}
void fill(double pa[Seasons]) {
using namespace std;
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames.Snames[i] << " expenses: ";
cin >> pa[i];
}
}
void show(double da[Seasons]) {
using namespace std;
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames.Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expense: $" << total << endl;
}
九* char数组和cctype
//学生信息
#include <iostream>
#include <cctype>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n) {
int i = 0;
for (; i < n ; ++i) {
cout << "Enter the information of studnet " << i + 1 << "\nName: ";
cin.getline(pa[i].fullname, SLEN);
if (! isalpha(pa[i].fullname[0]))
break;
cout << "Hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}
void display1(student st) {
cout << "\n\nName: " << st.fullname
<< "\nHobby: " << st.hobby
<< "\nOoplevel: " << st.ooplevel;
}
void display2(const student* ps) {
cout << "\n\nName: " << ps->fullname
<< "\nHobby: " << ps->hobby
<< "\nOoplevel: " << ps->ooplevel;
}
void display3(const student pa[], int n) {
for (int i = 0; i < n; ++i) {
cout << "\n\nName: " << pa[i].fullname
<< "\nHobby: " << pa[i].hobby
<< "\nOoplevel: " << pa[i].ooplevel;
}
}
int main() {
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";
return 0;
}
十* 函数指针与函数指针数组
//函数指针(*pf)
#include <iostream>
double add(double x, double y) {
return x + y;
}
double substract(double x, double y) {
return x - y;
}
double multiply(double x, double y) {
return x * y;
}
double division(double x, double y) {
return x / y;
}
double calculate(double a, double b,double (*pf)(double x, double y)) {
return pf(a,b);
}
int main() {
double num1, num2=1;
while (num2!=0) {
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout <<"CALCULATE:\n"<< num1 << " + " << num2 << " = " << calculate(num1, num2, add) << std::endl;
std::cout << num1 << " - " << num2 << " = " << calculate(num1, num2, substract) <<std::endl;
std::cout << num1 << " * " << num2 << " = " << calculate(num1, num2, multiply) << std::endl;
std::cout << num1 << " / " << num2 << " = " << calculate(num1, num2, division) << std::endl<<std::endl;
}
return 0;
}
//函数指针数组
#include <iostream>
double add(double x, double y) {
return x + y;
}
double substract(double x, double y) {
return x - y;
}
double multiply(double x, double y) {
return x * y;
}
double division(double x, double y) {
return x / y;
}
double (*pf[4])(double x, double y) {
add, substract, multiply, division
};
double calculate(double a, double b,double (*pf[])(double x, double y)) {
if (pf[0])
return pf[0](a, b);
else if (pf[1])
return pf[1](a, b);
else if (pf[2])
return pf[2](a,b);
else if(pf[3)
return pf[3];
}
int main() {
double num1, num2=1;
while (num2!=0) {
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
for (int i = 0; i < 4; ++i)
std::cout<<calculate(num1, num2,&pf[i])<<"\t";
std::cout << std::endl<<std::endl;
}
return 0;
}