1.
main.cpp
#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
golf test[5];
golf otherone;
for(int i = 0; i < 5; i++)
{
if(!setgolf(test[i]))
return 0;
}
setgolf(otherone, "hello world!", 3);
showgolf(otherone);
handicap(otheone, 888);
showgolf();
}
golf.h
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
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_H_INCLUDED
golf.cpp
#include <iostream>
#include "golf.h"
#include <cstring>
using namespace std;
void setgolf(golf & g, const char * name, int hc)
{
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g)
{
cout << "Please enter the fullname";
if(cin.getline(g.fullname,40) && strlen(g.fullname) != 0)
{
cout << "Please enter the number";
cin >> g.handicap;
cin.get();
return 1;
}
else
{
cout << "error";
return 0;
}
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
cout << g.fullname << ' ';
cout << g.handicap;
cout << endl;
}
2.#include <iostream>
#include <string>
using namespace std;
void strcount(const string input);
int main()
{
string input;
char next;
cout << "enter a line:\n";
getline(cin, input);
while(cin)
{
strcount(input);
cout << "Enter next line";
getline(cin, input);
}
cout << "bye\n";
return 0;
}
void strcount(const string input)
{
using namespace std;
static int total = 0;
int count = 0;
if(input == "")
cout << "It's blank!!!";
else
{
cout << "\"" << input << "\" contains ";
cout << input.length() << " letters" << endl;
}
}
3.方法1
#include <iostream>
#include <new>
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
char buffer[512];
int main()
{
chaff * test = new(buffer) chaff[2];
for(int i = 0; i < 2; i++)
{
cin.getline(test[i].dross,20);
cin >> test[i].slag;
cin.get();
}
for(int i = 0; i < 2; i++)
cout << test[i].dross << ' ' << test[i].slag << endl
}
方法2:
#include <iostream>
#include <new>
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
int main()
{
chaff * test = new chaff[2];
for(int i = 0; i < 2; i++)
{
cin.getline(test[i].dross,20);
cin >> test[i].slag;
cin.get();
}
for(int i = 0; i < 2; i++)
{
cout << test[i].dross << ' ' << test[i].slag << endl;
}
return 0;
}
4.main.cpp
#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);
return 0;
}
sale.h
#ifndef SALE_H_INCLUDED
#define SALE_H_INCLUDED
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_H_INCLUDED
sale.cpp
#include "sale.h"
#include <iostream>
using namespace std;
namespace SALES
{
void setSales(Sales & s, const double ar[], int n)
{
int sum = 0;
int avg, max ,min;
for(int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += s.sales[i];
}
max = min = s.sales[0];
for(int i = 1; i < n; i++)
{
max = max > s.sales[i] ? max : s.sales[i];
min = min < s.sales[i] ? min : s.sales[i];
}
avg = sum / n;
}
void setSales(Sales & s)
{
double max = 999;
double min = -999;
double avg = 0;
for(int i = 0 ; i < 4 ; ++i)
{
cin >> s.sales[i];
max = max < s.sales[i] ? s.sales[i] : max;
min = min > s.sales[i] ? s.sales[i] : min;
avg += s.sales[i];
}
s.max = max;
s.min = min;
s.average = avg / 4;
}
void showSales(const Sales & s)
{
for(int i = 0 ; i < 4 ; ++i)
cout << s.sales[i] << endl;
cout << "MAX: " << s.max << endl << "MIN: " << s.min << endl << "AVE: " << s.average << endl;
}
}