第1题:
main.cpp
#include
#include "stock.h"
using namespace std;
int main()
{
BankAccount a1= BankAccount("a1", "fad@123", 123);
BankAccount a2 = BankAccount("a2", "fad@456", 456);
a1.show();
a2.show();
a1.deposit(20);
a1.show();
a1.withdraw(66);
a1.show();
return 0;
}
stock.cpp
#include
#include "stock.h"
using std::cout;
using std::endl;
BankAccount::BankAccount(const char* client, const char* num, double bal )
{
strncpy(name,client,39);
name[39] = '\0';
strncpy(acctnum,num,24);
acctnum[24] = '\0';
balance = bal;
}
void BankAccount::show(void) const
{
cout << "name:"<
stock.h
#pragma warning (disable:4996)
#include
class BankAccount
{
private:
char name[40];
char acctnum[25];
double balance;
public:
BankAccount(const char* client,const char*num,double bal=0.0);
void show(void)const;
void deposit(double cash);
void withdraw(double cash);
};
第2题:
main.cpp
#include
#include "stock.h"
using namespace std;
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
two.Show();
cout << endl;
two.FormalShow();
return 0;
}
stock.cpp
#include
#include "stock.h"
using std::cout;
using std::endl;
using std::string;
Person::Person(const string& ln, const char* fn)
{
lname = ln;
strncpy(fname,fn,24);
fname[24] = '\0';
}
void Person::Show() const
{
cout << "first name:" << fname<<"\n";
cout << "last name:" << lname << "\n";
cout << endl;
}
void Person::FormalShow() const
{
cout << "last name:" << lname << "\n";
cout << "first name:" << fname << "\n";
cout << endl;
}
stock.h
#pragma warning (disable:4996)
#include
using std::string;
class Person
{
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
Person()
{
lname = " ";
fname[0] = '\0';
}
Person(const string& ln,const char* fn="Heyyou");
void Show() const;
void FormalShow() const;
};
第3题:
main.cpp
#include
#include "stock.h"
int main()
{
using namespace std;
golf in[5];
for (int i = 0; i < 5; i++)
{
char name[Len];
int hc;
int result = 1;
if (i % 2 == 0)
{
cout << "此为模式2:\n";
result = in[i].setgolf();
}
else
{
cout << "此为模式1:\n请输入姓名:";
cin.get(name, Len);
cin.clear();
cin.ignore((numeric_limits::max)(), '\n');
cout << "请输入hc:";
cin >> hc;
cin.get();
in[i].setgolf(name, hc);
if (strcmp(name, " ") == 0)
{
result = 0;
}
}
if (result == 0)
{
break;
}
if (i == 3)
{
in[i].handicap(7);
}
in[i].showgolf();
}
return 0;
}
//adadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasd
stock.cpp
#pragma warning (disable :4996)
#include
#include "stock.h"
#include
using namespace std;
golf::golf(const char* name, int hc)
{
strcpy(m_fullname, name);
m_fullname[39] = '\0';
m_handicap = hc;
}
golf::golf()
{
strcpy(m_fullname, " ");
m_handicap = -1;
}
void golf::setgolf(const char* name, int hc)
{
strcpy(m_fullname, name);
m_fullname[39] = '\0';
m_handicap = hc;
}
int golf::setgolf()
{
char name[Len];
int hc;
cout << "请输入姓名:";
cin.get(name, Len);
cin.clear();
cin.ignore((numeric_limits::max)(), '\n');
cout << "请输入hc:";
cin >> hc;
cin.get();
if (strcmp(name, " ") == 0)
{
return 0;
}
else
{
*this = golf(name, hc);
return 1;
}
}
void golf::handicap(int hc)
{
m_handicap = hc;
}
void golf::showgolf() const
{
cout << "name:" << m_fullname << "\t";
cout << "handicap:" << m_handicap << endl;
}
stock.h
#pragma warning (disable :4996)
#include
const int Len = 40;
class golf
{
private:
char m_fullname[Len];
int m_handicap;
public:
golf(const char* name, int hc);
golf();
int setgolf();
void setgolf(const char* name, int hc);
void handicap(int hc);
void showgolf() const;
};
第4题:
main.cpp
#include"stock.h"
using namespace std;
using namespace SALES;
int main()
{
Sales s1, s2;
double ar1[2] = { 462.3,580.66 };
s1.setSales(ar1, 2);
s2.setSales();
cout << "s1:" << endl;
s1.showSales();
cout << "s2:" << endl;
s2.showSales();
return 0;
}
stock.cpp
#include "stock.h"
namespace SALES
{
Sales::Sales()
{
for (int i=0;i< QUARTERS;i++)
{
sales[i] = 0.0;
}
m_min = 0.0;
m_max = 0.0;
m_average = 0.0;
}
Sales::Sales(const double ar[], int n)
{
double min, max, aver;
min = ar[0];
max = ar[0];
aver = 0.0;
int i;
for (i = 0; i < n; i++)
{
sales[i] = ar[i];
if (min > ar[i])
{
min = ar[i];
}
if (max < ar[i])
{
max = ar[i];
}
aver += ar[i];
}
while (i < QUARTERS)
{
sales[i] = 0.0;
i++;
}
aver /= n;
m_min = min;
m_max = max;
m_average = aver;
}
void Sales::setSales()
{
std::cout << "请输入s的值:";
double min;
double max;
double aver;
double ar[4];
for (int i = 0; i < 4; i++)
{
std::cin >> ar[i];
}
*this = Sales(ar, 4);
}
void Sales::setSales(const double ar[], int n)
{
*this = Sales(ar,n);
}
void Sales::showSales()const
{
using namespace std;
cout << "sales:" << endl;
for (int i = 0; i < 4; i++)
cout << sales[i] << "\t";
cout << endl;
cout << left << setw(20) << "average:" << m_average << endl;
cout << left << setw(20) << "max:" << setw(20) << m_max << endl;
cout << left << setw(20) << "min:" << setw(20) << m_min << endl;
}
}
stock.h
#pragma warning (disable:4996)
#include
#include
#include
#include
namespace SALES
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double m_average;
double m_max;
double m_min;
public:
Sales(const double ar[], int n);
Sales();
void setSales(const double ar[], int n);
void setSales();
void showSales()const;
};
}
第5题:
main.cpp
#include
#include "stock.h"
using namespace std;
int main()
{
int total=0;
Stack S;
customer temp = {"name1",10};
S.push(temp);
temp = { "name2",456 };
S.push(temp);
S.pop(temp);
total += temp.payment;
cout << temp.fullname << "\t" << total << endl;
S.pop(temp);
total += temp.payment;
cout << temp.fullname<<"\t"<
stock.cpp
#include
#include "stock.h"
using std::cout;
using std::endl;
Stack::Stack()
{
top = 0;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item& item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::pop(Item& item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
stock.h
#pragma warning (disable:4996)
#include
#ifndef STACK_H_
#define STACK_H_
struct customer
{
char fullname[35];
double payment;
};
typedef struct customer Item;
class Stack
{
private:
enum {MAX=10};
Item items[MAX];
int top;
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item & item);
bool pop(Item & item);
};
#endif
第6题:
main.cpp
#include
#include "stock.h"
using namespace std;
int main()
{
Move a = Move{ 23.5,96.32 };
const Move b = Move{45.36,89.65};
a.showmove();
b.showmove();
a=a.add(b);
a.showmove();
a.reset(0.0, 0.0);
a.showmove();
return 0;
}
stock.cpp
#include
#include "stock.h"
using std::cout;
using std::endl;
Move::Move(double a , double b )
{
x = a;
y = b;
}
void Move::showmove() const
{
cout << "x:" << x<x;
temp.y = m.y + this->y;
return temp;
}
void Move::reset(double a , double b )
{
x = a;
y = b;
}
stock.h
#pragma warning (disable:4996)
#include
class Move
{
private:
double x;
double y;
public:
Move(double a = 0,double b=0);
void showmove() const;
Move add(const Move & m)const;
void reset(double a = 0,double b=0);
};
第7题:
main.cpp
#include"stock.h"
using namespace std;
int main()
{
Plorg p1, p2;
p1.show();
p2.show();
p2.setplorg(5);
p1.setci(40);
p1.show();
p2.show();
return 0;
}
stock.cpp
#include "stock.h"
using namespace std;
Plorg::Plorg()
{
cin >> name;
ci = 50;
}
void Plorg::setplorg(int n, const char*iname)
{
strcpy(name,iname);
ci = n;
}
void Plorg::setci(int n)
{
ci = n;
}
void Plorg::show()
{
cout << "name:"<
stock.h
#pragma warning (disable:4996)
#include
#include
#include
#include
class Plorg
{
private:
char name[20];
int ci;
public:
Plorg();
void setplorg(int n,const char* iname = "Plorga");
void setci(int n);
void show();
};
第8题:
main.cpp
#include"list.h"
using namespace std;
void set(Item& n)
{
n = 100;
}
void show(Item & n)
{
cout << n << endl;
}
int main()
{
List L1, L2;
L1.IsEmpty();
L2.IsFull();
Item ar1[5] = {1,2,3,4,5};
Item a = 5;
L1.log(ar1,5);
L2.modify(50,0);
L1.visit(show);
cout << endl;
L2.visit(show);
cout << endl;
L1.visit(set);
L2.visit(set);
L1.visit(show);
cout << endl;
L2.visit(show);
cout << endl;
return 0;
}
list.cpp
#include "list.h"
using namespace std;
List::List()
{
for (int i=0;i<50;i++)
{
stuff[i] = -5;
}
}
void List::log(Item ar[],int n)
{
for (int i=0;i
list.h
#pragma warning (disable:4996)
#include
#include
#include
#include
typedef int Item ;
class List
{
private:
Item stuff[50];
public:
List();
void log(Item ar[], int n);
void modify(Item item, int n);
void IsFull();
void IsEmpty();
void visit(void(*pf)(Item & n));
};