C++ primer plus 第九章编程练习

来源:互联网 发布:科学论坛 数据可视化 编辑:程序博客网 时间:2024/05/17 08:07

1、

/********************************************************************************************Author:Tang QingyunTime:2017/03/15From:C++ primer plus 第九章编程练习 第1题*********************************************************************************************//***********************************golf.h***************************************************/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 "stdafx.h"#include<iostream>#include<string>#include"golf.h"using namespace std;void setgolf(golf & g, const char * name, int hc){strcpy_s(g.fullname, name);g.handicap = hc;}int setgolf(golf & g){cout << "Please enter the name:";cin.getline(g.fullname, Len);if (!strcmp(g.fullname, ""))return 0;cout << "Please enter the handicap:";cin >> g.handicap;cin.get();return 1;}void handicap(golf & g, int hc){g.handicap = hc;}void showgolf(const golf & g){cout << "fullname:" << g.fullname << endl;cout << "handicap:" << g.handicap << endl;}
/**************************main.cpp*****************************************/#include "stdafx.h"#include<iostream>#include"golf.h"using namespace std;const int StructSize = 3;int main(){golf ann;setgolf(ann, "Ann Birdfree", 24);showgolf(ann);handicap(ann, 12);showgolf(ann);golf g[StructSize];int j;for (int i = 0; i < StructSize; i++){j = setgolf(g[i]);if (j==0)break;showgolf(g[i]);}cout << "bye!\n";return 0;}
2、

#include"stdafx.h"#include<iostream>#include<string>using namespace std;const int ArSize = 10;void strcount(const string str);int main(){string input;char next;cout << "Enter a line:\n";getline(cin, input);while (input!=""){strcount(input);cout << "Enter next line:(empty line to quit)\n";getline(cin, input);}cout << "Bye\n";return 0;}void strcount(const string str){static int total=0;int count = 0;cout << "\"" << str << "\"contains ";count = str.size();total += count;cout << count << " characters\n";cout << total << " characters total\n";}
3、

#include"stdafx.h"#include<iostream>#include<string>struct chaff{char dross[20];int slag;};using namespace std;int main(){chaff *p = new chaff[2];strcpy_s(p[0].dross, "fuck");p[0].slag = 1;strcpy_s(p[1].dross, "what");p[1].slag = 2;for (int i = 0; i < 2; i++)cout << p[i].dross << "," << p[i].slag << endl;return 0;}
4、

/********************************************************************************************Author:Tang QingyunTime:2017/03/15From:C++ primer plus 第九章编程练习 第4题*********************************************************************************************//***********************************Sales.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);};

/******************Sales.cpp****************************/#include"stdafx.h"#include"Sales.h"#include<iostream>namespace SALES{using std::cout;using std::cin;using std::endl;double average(double arr[], int ArrSize){double s = 0;for (int i = 0; i < ArrSize; i++)s += arr[i];double average = s / ArrSize;return average;}double max(double arr[], int ArrSize){double temp = arr[0];for (int i = 1; i < ArrSize; i++){if (arr[i] > temp)temp = arr[i];}return temp;}double min(double arr[], int ArrSize){double temp = arr[0];for (int i = 1; i < ArrSize; i++){if (arr[i] < temp)temp = arr[i];}return temp;}void setSales(Sales & s, const double ar[], int n){int m = QUARTERS < n ? QUARTERS : n;for (int i = 0; i < m; i++){s.sales[i] = ar[i];}for (int i = m; i < QUARTERS; i++)s.sales[i] = 0;s.average = average(s.sales, m);s.max = max(s.sales, m);s.min = min(s.sales, m);}void setSales(Sales & s){cout << "Enter 4 sales:\n";for (int i = 0; i < QUARTERS; i++){cout << "sales " << i + 1 << " :";cin >> s.sales[i];}s.average = average(s.sales, QUARTERS);s.max = max(s.sales, QUARTERS);s.min = min(s.sales, QUARTERS);}void showSales(const Sales & s){cout << "sales:\n";for (int i = 0; i < QUARTERS; i++)cout << s.sales[i] << endl;cout << "average:" << s.average << endl;cout << "max:" << s.max << endl;cout << "min:" << s.min << endl;}};
/******************main.cpp****************************/#include"stdafx.h"#include"Sales.h"#include<iostream>using namespace std;using namespace SALES;int main(){Sales s1,s2;double ar[3] = { 1.2, 2.0, 3.1 };setSales(s1, ar, 3);showSales(s1);setSales(s2);showSales(s2);return 0;}



0 0