/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/30
From : C++ Primer Plus第五版第9章编程练习 第1题
*******************************************************************************************************************/
#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
golf Ann;
setgolf(Ann);
showgolf(Ann);
setgolf(Ann, "Ann Birdfree", 24);
cout << "The recorded name and handicap are: \n";
showgolf(Ann);
handicap(Ann, 66);
cout << "Your amended name and handicap are: \n";
showgolf(Ann);
system("pause");
return 0;
}
#ifndef GOLF_H_
#define 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);
#endif
#include "golf.h"
#include<iostream>
#include<cstring>
using namespace std;
void setgolf(golf &Ann, const char *name, int hc)
{
strcpy(Ann.fullname, name);
Ann.handicap = hc;
}
int setgolf(golf &Ann)
{
cout << "Please enter your name: ";
cin.getline(Ann.fullname, Len);
cout << "Please enter your handicap: ";
cin >> Ann.handicap;
return Ann.fullname == NULL ? 0 : 1;
}
void handicap(golf &Ann, int hc)
{
Ann.handicap = hc;
}
void showgolf(const golf &Ann)
{
cout << Ann.fullname << endl;
cout << Ann.handicap << endl;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/30
From : C++ Primer Plus第五版第9章编程练习 第2题
*******************************************************************************************************************/
#include <iostream>
#include <string>
void strcount(const std::string &s);
int main()
{
std::string s;
while(getline(std::cin, s))
strcount(s);
system("pause");
return 0;
}
void strcount(const std::string &s)
{
static int total = 0;
int len = 0;
for(int i = 0 ; i < (int)s.length() ; ++i)
len += s[i] == ' ' ? 0 : 1;
std::cout << len << std::endl;
total += len;
std::cout << "Total: " << total << std::endl;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/30
From : C++ Primer Plus第五版第9章编程练习 第3题
*******************************************************************************************************************/
// newplace.cpp -- using placement new
#include <iostream>
#include <new> // for placement new
struct chaff
{
char dross[20];
int slag;
};
const int BUF = 512;
const int N = 5;
char Buffer[BUF]; // chunk of memory
int main()
{
using namespace std;
cout << "Calling new and placement new:\n";
chaff *p = new (Buffer) chaff[2];
for(int i = 0 ; i < 2 ; ++i)
{
std::cin.getline(p[i].dross, 20);
std::cin >> p[i].slag; std::cin.get(); //吸收'\n'
}
for(int i = 0 ; i < 2 ; ++i)
std::cout << "#" << i + 1 << std::endl << p[i].dross << std::endl << p[i].slag << std::endl;
delete p;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/30
From : C++ Primer Plus第五版第9章编程练习 第4题
*******************************************************************************************************************/
#include <iostream>
#include "sale.h"
int main()
{
double a[3] = {1.1, 2.2, 3.3};
SALES::Sales s;
SALES::setSales(s, a, 3);
SALES::showSales(s);
SALES::setSales(s);
SALES::showSales(s);
system("pause");
return 0;
}
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);
}
#include <iostream>
#include "sale.h"
namespace SALES
{
void setSales(Sales &s, const double ar[], int n)
{
double max = -1, min = 999999, average = 0;
if(n < 4)
{
for(int i = 0 ; i < 4 ; ++i)
{
if(i < n)
{
s.sales[i] = ar[i];
max = max < s.sales[i] ? s.sales[i] : max;
min = min > s.sales[i] ? s.sales[i] : min;
average += s.sales[i];
}
else
s.sales[i] = 0;
}
s.max = max;
s.min = min;
s.average = average / n;
}
else
{
for(int i = 0 ; i < 4 ; ++i)
{
s.sales[i] = ar[i];
max = max < s.sales[i] ? s.sales[i] : max;
min = min > s.sales[i] ? s.sales[i] : min;
average += s.sales[i];
}
s.max = max;
s.min = min;
s.average = average / 4;
}
}
void setSales(Sales &s)
{
double max = -1, min = 999999, average = 0;
for(int i = 0 ; i < 4 ; ++i)
{
std::cin >> s.sales[i];
max = max < s.sales[i] ? s.sales[i] : max;
min = min > s.sales[i] ? s.sales[i] : min;
average += s.sales[i];
}
s.max = max;
s.min = min;
s.average = average / 4;
}
void showSales(const Sales &s)
{
for(int i = 0 ; i < 4 ; ++i)
std::cout << s.sales[i] << std:: endl;
std::cout << "MAX: " << s.max << std::endl << "MIN: " << s.min << std:: endl << "AVE: " << s.average << std::endl;
}
}