C++ primer plus 第七章编程练习
2017-03-09 16:32
465 查看
4、
5、
7、
8、a
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第8题a
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
const int seasons = 4;
const char* Snames[seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double arr[], int seasons);
void show(double arr[], int seasons);
int m
be41
ain()
{
double expenses[seasons];
fill(expenses,seasons);
show(expenses, seasons);
return 0;
}
void fill(double arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> arr[i];
}
}
void show(double arr[], int n)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < n; i++)
{
cout << Snames[i] << ":$" << arr[i] << endl;
total += arr[i];
}
cout << "Total Expenses:$" << total << endl;
}
8、b
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第8题b
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
const int seasons = 4;
const char* Snames[seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct box
{
double expenses[seasons];
};
void fill(box *p);
void show(box a);
int main()
{
box b;
fill(&b);
show(b);
return 0;
}
void fill(box *p)
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> p->expenses[i];
}
}
void show(box a)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << a.expenses[i] << endl;
total += a.expenses[i];
}
cout << "Total Expenses:$" << total << endl;
}
9、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第9题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
#include<string>
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 * ps);
void display3(const student pa[], int n);
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;
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Please enter the student "<<i+1<<" 's name:";
char temp1[SLEN];
cin.getline(temp1, SLEN);
if (!strcmp(temp1,""))
break;
strcpy_s(pa[i].fullname ,temp1);
cout << "Please enter the student " << i + 1 << " 's hobby:";
char temp2[SLEN];
cin.getline(temp2, SLEN);
strcpy_s(pa[i].hobby, temp2);
cout << "Please enter the student " << i + 1 << " 's ooplevel:";
int temp3;
cin >> temp3;
pa[i].ooplevel = temp3;
cin.get();
}
return i;
}
void display1(student st)
{
cout << "fullname:" << st.fullname<<endl;
cout << "hobby:" << st.hobby<<endl;
cout << "ooplevel:" << st.ooplevel<<endl;
}
void display2(const student * ps)
{
cout << "fullname:" << ps->fullname<<endl;
cout << "hobby:" << ps->hobby<<endl;
cout << "ooplevel:" << ps->ooplevel<<endl;
}
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "the "<<i+1<<" student's fullname:" << pa[i].fullname<<endl;
cout << "the " << i+1 << " student's hobby:" << pa[i].hobby<<endl;
cout << "the " << i+1 << " student's ooplevel:" << pa[i].ooplevel<<endl;
}
}
10、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第10题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
double add(double x, double y);
double sub(double x, double y);
double mean(double x, double y);
double calculate(double x, double y, double(*pf)(double, double));
string name[3] = { "add", "sub", "mean" };
int main()
{
double a, b;
double(*pf[3])(double, double) = { add, sub, mean };
cout << "Enter pairs of numbers(q to quit):";
while (cin >> a >> b)
{
for (int i = 0; i < 3; i++)
{
cout << a << " "<<name[i]<<" " << b << " =" << calculate(a, b, pf[i]) << endl;
}
cout << "Enter pairs of numbers(q to quit):";
}
return 0;
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mean(double x, double y)
{
return (x + y) / 2.0;
}
double calculate(double x, double y, double(*pf)(double, double))
{
return (*pf)(x, y);
}
#include "stdafx.h" #include<iostream> using namespace std; long double probability(unsigned numbers, unsigned picks); int main() { double total1, choice1, total2, choice2; cout << "Enter the total number of choices on the game card and\n" "the number of picks allowed:\n"; while ((cin >> total1 >> choice1>>total2>>choice2) && choice1 <= total1&&choice2<=total2) { cout << "You have one choice in "; cout << probability(total1, choice1)*probability(total2,choice2); cout << " of winning.\n"; cout << "Next four numbers(q to quit):"; } cout << "bye\n"; return 0; } long double probability(unsigned numbers, unsigned picks) { long double result = 1.0; long double n; unsigned p; for (n = numbers, p = picks; p > 0; n--, p--) result = result*n / p; return result; }
5、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/08 From:C++ primer plus 第七章编程练习 第5题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> using namespace std; long long int factorial(int n); int main() { int n; long long int m; cout << "please enter the number(q to stop):"; while (cin >> n) { m = factorial(n); cout << n << "!=" << m << endl; cout << "Next input:"; } return 0; } long long int factorial(int n) { long long int result; if (n < 0) cout << "input error!"; else if (n == 0) result=1; else if(n > 0) { result = n*factorial(n - 1); } return result; }6、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/08 From:C++ primer plus 第七章编程练习 第6题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> using namespace std; int Fill_array(double Arr[],int ArrSize); void Show_array(double Arr[], int ArrSize); void Reverse_array(double Arr[], int ArrSize); const int max = 10; int main() { double arr[max]; int n = Fill_array(arr, max); cout << "Show the array:\n"; Show_array(arr,n); Reverse_array(arr,n); cout << "Show the array reversed:\n"; Show_array(arr, n); Reverse_array(arr+1, n-2); cout << "Show the array again:\n"; Show_array(arr, n); return 0; } int Fill_array(double Arr[], int ArrSize) { double temp; int i; for (i = 0; i < ArrSize; i++) { cout << "please enter value#"<<i+1<<":"; cin >> temp; if (!cin) break; Arr[i] = temp; } return i; } void Show_array(double Arr[], int ArrSize) { for (int i = 0; i < ArrSize; i++) { cout << "value #" << i + 1 << " =" << Arr[i]<<endl; } } void Reverse_array(double Arr[], int ArrSize) { for (int i = 0, j = ArrSize - 1; i < j; i++, j--) { double temp; temp=Arr[i]; Arr[i] = Arr[j]; Arr[j] = temp; } }
7、
/******************************************************************************************** Author:Tang Qingyun Time:2017/03/08 From:C++ primer plus 第七章编程练习 第7题 *********************************************************************************************/ #include "stdafx.h" #include<iostream> using namespace std; double* fill_array(double * begin, double * end); void show_array(double * begin, double * end); void revalue(double r, double * begin, double * end); const int max = 5; int main() { double properties[max]; double * pbegin=properties; double * pend = fill_array(pbegin, pbegin+max); show_array(pbegin, pend); if ((pend - pbegin)>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, pbegin, pend); show_array(pbegin, pend); } cout << "Done.\n"; cin.get(); cin.get(); return 0; } double* fill_array(double * begin, double * end) { double temp; int i=1; while (begin<end) { cout << "Enter value #" << i << ": "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input;input process terminated.\n"; break; } else if (temp < 0) break; *begin = temp; begin++; i++; } return begin; } void show_array(double * begin, double * end) { int i = 1; while (begin < end) { cout << "Property #" << i<<":$"; cout << *begin << endl; begin++; i++; } } void revalue(double r, double * begin, double * end) { while (begin < end) { *begin *= r; begin++; } }
8、a
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第8题a
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
const int seasons = 4;
const char* Snames[seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double arr[], int seasons);
void show(double arr[], int seasons);
int m
be41
ain()
{
double expenses[seasons];
fill(expenses,seasons);
show(expenses, seasons);
return 0;
}
void fill(double arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> arr[i];
}
}
void show(double arr[], int n)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < n; i++)
{
cout << Snames[i] << ":$" << arr[i] << endl;
total += arr[i];
}
cout << "Total Expenses:$" << total << endl;
}
8、b
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第8题b
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
using namespace std;
const int seasons = 4;
const char* Snames[seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct box
{
double expenses[seasons];
};
void fill(box *p);
void show(box a);
int main()
{
box b;
fill(&b);
show(b);
return 0;
}
void fill(box *p)
{
for (int i = 0; i < seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses:";
cin >> p->expenses[i];
}
}
void show(box a)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < seasons; i++)
{
cout << Snames[i] << ":$" << a.expenses[i] << endl;
total += a.expenses[i];
}
cout << "Total Expenses:$" << total << endl;
}
9、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第9题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
#include<string>
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 * ps);
void display3(const student pa[], int n);
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;
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Please enter the student "<<i+1<<" 's name:";
char temp1[SLEN];
cin.getline(temp1, SLEN);
if (!strcmp(temp1,""))
break;
strcpy_s(pa[i].fullname ,temp1);
cout << "Please enter the student " << i + 1 << " 's hobby:";
char temp2[SLEN];
cin.getline(temp2, SLEN);
strcpy_s(pa[i].hobby, temp2);
cout << "Please enter the student " << i + 1 << " 's ooplevel:";
int temp3;
cin >> temp3;
pa[i].ooplevel = temp3;
cin.get();
}
return i;
}
void display1(student st)
{
cout << "fullname:" << st.fullname<<endl;
cout << "hobby:" << st.hobby<<endl;
cout << "ooplevel:" << st.ooplevel<<endl;
}
void display2(const student * ps)
{
cout << "fullname:" << ps->fullname<<endl;
cout << "hobby:" << ps->hobby<<endl;
cout << "ooplevel:" << ps->ooplevel<<endl;
}
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "the "<<i+1<<" student's fullname:" << pa[i].fullname<<endl;
cout << "the " << i+1 << " student's hobby:" << pa[i].hobby<<endl;
cout << "the " << i+1 << " student's ooplevel:" << pa[i].ooplevel<<endl;
}
}
10、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/09
From:C++ primer plus 第七章编程练习 第10题
*********************************************************************************************/
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
double add(double x, double y);
double sub(double x, double y);
double mean(double x, double y);
double calculate(double x, double y, double(*pf)(double, double));
string name[3] = { "add", "sub", "mean" };
int main()
{
double a, b;
double(*pf[3])(double, double) = { add, sub, mean };
cout << "Enter pairs of numbers(q to quit):";
while (cin >> a >> b)
{
for (int i = 0; i < 3; i++)
{
cout << a << " "<<name[i]<<" " << b << " =" << calculate(a, b, pf[i]) << endl;
}
cout << "Enter pairs of numbers(q to quit):";
}
return 0;
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mean(double x, double y)
{
return (x + y) / 2.0;
}
double calculate(double x, double y, double(*pf)(double, double))
{
return (*pf)(x, y);
}
相关文章推荐
- 【学习C++】C++ Primer Plus (第六版)第七章编程练习6-10
- C++ Primer Plus (第6版)编程练习 代码-----第七章
- 这是我关于c++primer plus第七章编程练习第十题的解答
- C++ PRIMER PLUS (第六版) 中文版 第七章编程练习答案
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- c++ primer plus(第6版)中文版 第七章编程练习答案
- 关于c++primerplus第七章编程练习第九题的解答
- C++ Primer Plus第五版 第七章 编程练习答案
- c++ primer plus 第五版 第七章 编程练习 第 8 题 (字符数组转成字符串,trim使用,判断空行)
- C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案
- c++ primer plus编程练习-第七章
- C++ Primer Plus课后编程练习——第七章 函数-C++的编程模块(部分)
- C++ Primer Plus第六版 第七章 编程练习答案
- c++primer plus第七章编程练习的解答1-8
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 1-5
- C++ Primer Plus 第五版 第七章 编程练习 5 6
- C++ Primer Plus(第6版) 第7章 编程练习
- 【C++ Primer Plus 编程练习】 3.7
- C++ Primer Plus(第6版) 第5章 编程练习
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习1