//question1
// golf.h
#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
// golf.cpp
#include <iostream>
#include "golf.h"
#include <cstring>
void setgolf(golf & g, const char * name, int hc)
{
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g)
{
std::cout << "Please enter the name: ";
std::cin.getline(g.fullname, Len);
if (!strlen(g.fullname))
return 0;
else
{
std::cout << "Please enter the handicap: ";
std::cin >> g.handicap;
std::cin.get();
return 1;
}
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "Name: " << g.fullname << std::endl;
std::cout << "Handicap: " << g.handicap << std::endl;
}
// main.cpp
#include <iostream>
#include "golf.h"
int main()
{
/*
golf ann;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
golf andy;
setgolf(andy);
showgolf(andy);
*/
const int ArrSize = 5;
golf a[5];
int count = 0;
for (int i = 0; i < 5 && setgolf(a[i]); i++, count++)
;
for (int j = 0; j < count; j++)
showgolf(a[j]);
return 0;
}
//question2
#include <iostream>
#include <cstring>
#include <string>
const int ArSize = 10;
void strcount(const std::string * str);
int main()
{
using namespace std;
string input;
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 std::string * str)
{
using namespace std;
static int total = 0;
cout << "\"" << *str << "\" contains ";
int count = str->length();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
//question3
#include <iostream>
#include <new>
#include <cstring>
struct chaff
{
char dross[20];
int slag;
};
const int BUF = 512;
const int N = 2;
char buffer[BUF];
int main()
{
chaff * pd1;
//pd1 = new chaff[N];
chaff * pd2;
pd2 = new (buffer) chaff[2];
char str[20];
for (int i = 0; i < N; i++)
{
std::cout << "Dross: ";
std::cin >> str;
strcpy(pd2[i].dross, str);
std::cout << "Slag: ";
std::cin >> pd2[i].slag;
}
for (int j = 0; j < N; j++)
{
std::cout << "Dross is " << pd2[j].dross << ", slag is " << pd2[j].slag << std::endl;
}
//delete [] pd1;
return 0;
}
//question4
// sale.h
#ifndef SALE_H
#define SALE_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);
}
#endif
// sale.cpp
#include <iostream>
#include "sale.h"
void SALES::setSales(Sales & s, const double ar[], int n)
{
double sum = 0;
double max = ar[0];
double min = ar[0];
if (n <= QUARTERS)
{
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += ar[i];
if (max < ar[i])
max = ar[i];
if (min > ar[i])
min = ar[i];
}
s.average = sum / n;
s.max = max;
s.min = min;
}
else
std::cout << "Wrong n!" << std::endl;
if (n < QUARTERS)
{
for (int j = n; j < QUARTERS; j++)
{
s.sales[j] = 0;
}
}
}
void SALES::setSales(Sales & s)
{
double sum = 0;
std::cout << "Enter the nums: " << std::endl;
for (int i = 0; i < QUARTERS; i++)
{
std::cin >> s.sales[i];
}
double max = s.sales[0];
double min = s.sales[0];
for (int j = 0; j < QUARTERS; j++)
{
sum += s.sales[j];
if (max < s.sales[j])
max = s.sales[j];
if (min > s.sales[j])
min = s.sales[j];
}
s.average = sum / QUARTERS;
s.max = max;
s.min = min;
}
void SALES::showSales(const Sales & s)
{
std::cout << "Sales: " << std::endl;
for (int i = 0; i < QUARTERS; i++)
{
std::cout << s.sales[i] << " ";
}
std::cout << "\n";
std::cout << "Average: " << s.average << std::endl;
std::cout << "Max: " << s.max << std::endl;
std::cout << "Min: " << s.min << std::endl;
}
// main.cpp
#include <iostream>
#include "sale.h"
int main()
{
using namespace SALES;
Sales a, b;
double arr[3] = {1.2, 3.2, 4.5};
setSales(a, arr, 3);
showSales(a);
setSales(b);
showSales(b);
return 0;
}