オリジナルのC ++ Primer Plus Chapter 7授業後のプログラミング演習1-9

// 1题
#include doubleharmonic_mean
(double、double);
int main()
{
名前空間stdを使用;
ダブルa、b;
double har_m;
cout << "请输入俩个数:";
while(cin >> a >> b && a!= 0 && b!= 0)
{
while(cin.get()!= '\ n')
continue;
har_m = harmonic_mean(a、b);
cout << a << "" << b << "の调和平均数は:" << har_m << endl;
}
0を返します。
}

double調和倍数(ダブルa、ダブルb)
{
ダブルhn = 2.0 * a * b /(a + b);
hnを返します。
}

// 2题
#include
const int Max = 10;
void in_put(double *);
void display(const double *);
二重平均(const double *);
名前空間stdを使用します。
int main()
{
double aver;
double golf_scores [Max];
in_put(golf_scores);
display(golf_scores);
aver = average(golf_scores);
cout << "\ n平均成绩=" << aver << endl;

return 0;

}

void in_put(double * pf)
{
int i = 0;
cout << "最大10のゴルフ結果を入力してください(qの終わりを入力してください):\ n";
while(cin >> pf [i] && pf [i]!= 'q')
{
i ++;
if(i> = Max)
{
cout << "Array is full!\ n";
break;
}
}

}

void display(const double * n)
{
cout << "高尔夫成绩:“;
for(int i = 0; i <Max; i ++)
{
if(n [i] == 0)
break;
else
cout << n [ i] << "";
}
}
double average(const double * n)
{
double totle = 0;
二重平均= 0;
int count = 0;
for(int i = 0; i <Max; i ++)
{
if(n [i]!= 0)
++ count;
totle + = n [i];
}
aver = totle / count;

return aver;

}

// 3题
#include
struct box {
char maker [40];
フロートの高さ;
フロート幅;
フロートの長さ;
フロートボリューム;
};
void display(const struct box);
void product(struct box *);
()INTメイン
{
構造体ボックスB = {
「wawawawa」、
4、
5、
6、
0
}。
display(b);
製品(&b);
display(b);

return 0;

}

void display(const struct box a)
{
std :: cout << "maker =" << a.maker << std :: endl;
std :: cout << "height =" << a.height << std :: endl;
std :: cout << "width =" << a.width << std :: endl;
std :: cout << "length =" << a.length << std :: endl;
std :: cout << "volume =" << a.volume << std :: endl;
}

void product(struct box * a)
{
a-> volume = a-> height * a-> width * a-> length;
}

// 4题

長い二重確率を含める(符号なしの数値、符号なしのピック、符号なしの数値2、符号なしのピック2);
int main()
{
名前空間stdを使用;
double total1、choices1、total2、choices2;
cout <<「最初の選択の総数と\ n」
選択可能な数を入力してください:\ n」;
while((cin >> total1 >> choices1)&& choices1 <= total1)
{
cout <<「2番目の選択の総数を入力してください\ n」
「および選択可能な数:\ n」;
cin >> total2 >> choices2;
if(total2 <= choices2)
{
cout <<“正しい数字を入力してください:\ n”;
cin >> total2> > choices2;
}
cout <<「あなたには1回のチャンスがあります。
cout <<確率(合計1、選択肢1、合計2、選択肢2);
勝利のc << "。\ n";
cout << "次の2つの数値(終了するq):";
}
cout <<“ bye \ n”;

return 0;

}

長い二重確率(符号なし数値、符号なしピック、符号なし数値2、符号なしピック2
{
long double result1 = 1.0;
long double result2 = 1.0;
long doubleの結果= 1.0;
long double n;
符号なしp;
(n =数値、p =ピック; p> 0; n–、p–)
result1 = result1 * n / p;
(n = numbers2、p = picks2; p> 0; n–、p–)の場合
result2 = result2 * n / p;
結果=結果2 *結果1;

return result;

}
// 5つの質問

長い二重階乗を含む(int n);
int main()
{
長い二重fac;
int n;
std :: cout << "階乗クエリが必要な整数を入力してください:";
std :: cin >> n;
fac = factorial(n);
stdの階乗:: cout << n << "=" << fac << std :: endl;

return 0;

}

long double factorial(int n)
{
long double fact;
if(n == 0)
return fact = 1;
fact = n * factorial(n-1);

return fact;

}

// 6题
#include
void Fill_array(double []、int n);
void Show_array(double []、int n);
void Reverse_array(double []、int n);
const int SIZE = 10;
名前空間stdを使用します。
int main()
{
double darray [SIZE];
Fill_array(darray、SIZE);
Show_array(darray、SIZE);
Reverse_array(darray、SIZE);
Show_array(darray、SIZE);

return 0;

}

void Fill_array(double a []、int n)
{
cout <<“请输入 "<< n <<"个浮点数:\ n ";
for(int i = 0; i <n; i ++)
{
if(!(cin >> a [i]))
break;
}
}
void Show_array(double a []、int n)
{
cout <<“查看的数组状況事是:\ n”;
for(int i = 0; i <n; i ++)
cout << a [i] << "\ t";
cout << "\ n";
}

void Reverse_array(double a []、int n)
{
int i、j;
倍数;
for(i = 1、j = n-2; i <n / 2-1; i ++、j–)
{
num = a [i];
a [i] = a [j];
a [j] = num;
}
}

// 7题
#include
const int Max = 5;
double * fill_array(double *、double *);
void show_array(double *、double *);
void revalue(double b、double *、double *);

int main()
{
using namespace std;
double properties [Max];
double * pk、* pj; // Start k、end j
pk = properties; //配列の先頭へのポインターを
見つけるpj =&properties [Max-1]; / /配列の末尾へのポインタ
pj = fill_array(pk、pj);
show_array(pk、pj);
if(pj> pk)
{
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、pk、pj);
show_array( pk、pj);
}
cout <<“ Done。\ n”;
cin.get();
cin.get();

return 0;

}

double * fill_array(double * pk、double * pj)
{
名前空間stdを使用;
二重温度;
int i = 1;
while(pk <= pj)
{
cout <<“ Enter value#” << i << ":";
cin >> temp;
if(!cin)
{
cin.clear();
while(cin.get()!= '\ n')
continue;
cout <<“ Bab input; 入力プロセスが終了しました。」;
ブレーク;
}
else if(temp <0)
break;
* pk = temp;
++ pk;
++ i;
}
pj = --pk;

    return pj;

}

void show_array(double * pk、double * pj)
{
名前空間stdを使用;
int i = 1;
while(pk <= pj)
{
cout <<“ Property#” << i ++ << ":$";
cout << * pk << endl;
++ pk;
}
}

void revalue(double b、double * pk、double * pj)
{
while(pk <= pj)
{
* pk * = b;
++ pk;
}
}

// 8つの質問
// a
#include
#include
const int Seasons = 4;
//ポインタの配列
const char * Snames [Seasons] = {"Spring"、 "Summer"、 "Fall"、 "Winter"}; / /
void fill(double pa []);
void show(const double da []);

int main()
{
二重費用[シーズン];
fill(expenses);
表示(費用);
0を返します。
}

void fill(double pa [])
{
名前空間stdを使用;
for(int i = 0; i <Seasons; i ++)
{
cout <<“ Enter "<< Snames [i] <<"費用: ";
cin >> pa [i];
}
}

void show(const double da [])
{
名前空間stdを使用;
二重合計= 0.0;
cout <<“ EXPENSES \ n”;
for(int i = 0; i <Seasons; i ++)
{
cout << Snames [i] << ":$" << da [i] << endl;
合計+ = da [i];
}
cout <<“ Total Expenses:$” << total << endl;
}

// b
#include
#include
const int Seasons = 4;
//記憶指针的数组
const char * Snames [Seasons] = {“ Spring”、“ Summer”、“ Fall”、“ Winter”}; //
void fill(struct Expenses * pa);
void show(const struct Expenses * da);
struct Expenses {
double exp [Seasons];
};
int main()
{
struct Expenses費用;
fill(&expenses);
show(&expenses);
0を返します。
}

void fill(struct Expenses * pa)
{
名前空間stdを使用;
for(int i = 0; i <Seasons; i ++)
{
cout <<“ Enter "<< Snames [i] <<"費用: ";
cin >> pa-> exp [i];
}
}

void show(const struct Expenses * da)
{
名前空間stdを使用;
二重合計= 0.0;
cout <<“ EXPENSES \ n”;
for(int i = 0; i <Seasons; i ++)
{
cout << Snames [i] << ":$" << da-> exp [i] << endl;
合計+ = da-> exp [i];
}
cout <<“ Total Expenses:$” << total << endl;
}

// 9题
#include
using namespace std;
const int SLEN = 30;
struct Student {
char fullname [SLEN];
char hobby [SLEN];
int ooplevel;
};
int getinfo(student pa []、int n);
void display1(student st);
void display2(const student * st);
void display3(const student st []、int n);

int main()
{
cout << "Enter class size:";
int class_size;
cin >> class_size;
if(!cin)
{
cin.clear();
cout <<「入力が正しくありません!\ n」;
0を返します。
}
while(cin.get()!= '\ n')
continue;
student * ptr_stu = new student [class_size];
入力された整数= getinfo(ptr_stu、class_size);
for(int i = 0; i <入り; i ++)
{
display1(ptr_stu [i]);
display2(&ptr_stu [i]);
}
display3(ptr_stu、入り);
削除[] ptr_stu;
cout <<「完了\ n」;

return 0;

}

int getinfo(student pa []、int n)
{
int num = 0;
for(int i = 0; i <n; i ++)
{
cout <<“完全な学生名を入力してください:\ n”;
cin.getline(pa [i] .fullname、SLEN);
cout <<「趣味を入力してください:\ n」;
cin.getline(pa [i] .hobby、SLEN);
cout <<“ ooplevelを入力してください:\ n”;
cin >> pa [i] .ooplevel;
cin.get();
if(pa [i] .fullname [0] == '\ 0' || pa [i] .hobby [0] == '\ 0')
break;
num + = 1;
}

return num;

}
void display1(student st)
{
cout << "fullname:" << st.fullname << endl;
cout <<“ hobby: "<< st.hobby << endl;
cout <<“ ooplevel:" << st.ooplevel << endl;
cout <<” \ n \ n”;
}
void display2(const student * st)
{
cout << "fullname:" << st-> fullname << endl;
cout <<“ hobby: "<< st-> hobby << endl;
cout <<“ ooplevel:" << st-> ooplevel << endl;
cout <<” \ n \ n”;
}
void display3(const student st []、int n)
{
for(int i = 0; i <n; i ++)
{
cout << "fullname:
"<< st [i] .fullname << endl; cout <<" hobby: "<< st [i] .hobby << endl;
cout <<“ ooplevel: "<< st [i] .ooplevel << endl;
cout <<” \ n \ n”;
}
}

// 10题
#include
double calculate(double、double、double(* hp)(double、double));
double add1(double x、double y){return x + y;};
double add2(double x、double y){return x-y;};
double add3(double x、double y){return x * y;};
double add4(double x、double y){return x / y;};
double(* hp [4])(double、double)= {add1、add2、add3、add4};

int main()
{
using namespace std;
double a、b、result;
cout <<“ Please input two floating point numbers(q exit):\ n”;
while(cin >> a >> b)
{
for(int i = 0; i <4; i ++)
{
結果=計算(a、b、hp [i]);

       if (0 == i)
           cout << a <<" + " << b <<" = "<< result << endl;
        else if (1 == i)
            cout << a <<" - " << b <<" = "<< result << endl;
        else if (2 == i)
            cout << a <<" * " << b <<" = "<< result << endl;
        else if(3 == i)
                cout << a <<" / " << b <<" = "<< result << endl;
    }
}

}

double calculate(double x、double y、double(* hp)(double、double))
{
double num;
num =(* hp)(x、y);

return num;

}

在这里插入代码片
オリジナルの記事を85件公開 Like1 Visits1889

おすすめ

転載: blog.csdn.net/Tekkenwxp/article/details/103896934