标签:
p333.3
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstring> 4 #include<string> 5 using namespace std; 6 7 class Golf{ 8 private: 9 static const int Len = 40; 10 char fullname[Len]; 11 int handicap; 12 public: 13 Golf(char name[Len], int bt); 14 void showgolf() const; 15 }; 16 17 void setgolf(char *name, int extent, int &bt){ 18 cout << "enter a name\n"; 19 cin.get(name, extent); 20 while (cin.get() != ‘\n‘) 21 continue; 22 cout << "enter a number\n"; 23 cin >> bt; 24 } 25 26 Golf::Golf(char name[Len], int bt){ 27 strncpy(fullname, name, Len); 28 handicap = bt; 29 } 30 31 void Golf::showgolf()const{ 32 cout << "fullname is " << fullname << endl 33 << "handicap is " << handicap; 34 } 35 36 int main(){ 37 Golf one = Golf("asxc", 5); 38 int ct; 39 char name[40]; 40 setgolf(name, 40, ct); 41 Golf two = Golf(name, ct); 42 one.showgolf(); 43 cout << endl; 44 two.showgolf(); 45 46 system("pause"); 47 return 0; 48 }
p333.4
1 //头文件 2 namespace SALES 3 { 4 class Sales{ 5 private: 6 enum {QUARTERS=4}; 7 double sales[QUARTERS]; 8 double average, max, min; 9 public: 10 Sales(); 11 Sales(const double *ar, int n); 12 void showsales(); 13 }; 14 } 15 16 //方法 17 #include<iostream> 18 #include"sales.h" 19 using namespace std; 20 21 SALES::Sales::Sales(){ 22 cout << "enter four number\n"; 23 double sum = 0; 24 for (int i = 0; i < QUARTERS; i++){ 25 cin >> sales[i]; 26 sum += sales[i]; 27 } 28 average = sum / QUARTERS; 29 max = sales[0]; 30 min = sales[0]; 31 for (int i = 1; i < QUARTERS; i++){ 32 if (max < sales[i]) 33 max = sales[i]; 34 if (min>sales[i]) 35 min = sales[i]; 36 } 37 } 38 39 SALES::Sales::Sales(const double *ar, int n){ 40 double sum = 0; 41 for (int i = 0; i < n; i++){ 42 sales[i] = ar[i]; 43 sum += sales[i]; 44 } 45 average = sum / QUARTERS; 46 max = sales[0]; 47 min = sales[0]; 48 for (int i = 1; i < QUARTERS; i++){ 49 if (max < sales[i]) 50 max = sales[i]; 51 if (min>sales[i]) 52 min = sales[i]; 53 } 54 } 55 56 void SALES::Sales::showsales(){ 57 cout << "average is " << average << endl 58 << "max is " << max << endl 59 << "min is " << min; 60 } 61 62 //驱动 63 #include<iostream> 64 #include"sales.h" 65 using namespace std; 66 67 int main(){ 68 using namespace SALES; 69 cout << "enter four number\n"; 70 double ar[4]; 71 for (int i = 0; i < 4; i++) 72 cin >> ar[i]; 73 Sales market1(ar, 4); 74 market1.showsales(); 75 Sales market2; 76 cout << endl; 77 market2.showsales(); 78 79 system("pause"); 80 return 0; 81 }
p333.5
1 //头文件: 2 #ifndef _STACK_H_ 3 #define _STACK_H_ 4 5 struct customer{ 6 char fullname[35]; 7 double payment; 8 }; 9 10 typedef customer Item; 11 12 class Stack{ 13 private: 14 enum{max=10}; 15 Item items[max]; 16 int top; 17 public: 18 Stack(); 19 bool isfull()const; 20 bool isempty()const; 21 void push(const Item &); 22 bool pop(const Item &); 23 }; 24 25 #endif 26 27 //方法 28 #include<iostream> 29 #include"stack.h" 30 using namespace std; 31 32 Stack::Stack(){ 33 top = 0; 34 } 35 36 bool Stack::isfull()const{ 37 if (top == max) 38 return true; 39 else return false; 40 } 41 42 bool Stack::isempty()const{ 43 if (top == 0) 44 return true; 45 else return false; 46 } 47 48 void Stack::push(const Item &temp){ 49 if (isfull()) 50 cout << "the stack has already full\n"; 51 else items[top++] = temp; 52 } 53 54 bool Stack::pop(const Item &temp){ 55 if (isempty()){ 56 cout << "the stack has already empty\n"; 57 return false; 58 } 59 else { 60 items[--top] = temp; 61 return true; 62 } 63 } 64 65 //驱动: 66 #include<iostream> 67 #include"stack.h" 68 using namespace std; 69 void get_customer(Item &); 70 71 int main(){ 72 Item temp; 73 Stack test; 74 double payments=0; 75 cout << "enter A to push, enter P to pop or" 76 <<"enter Q to quit\n"; 77 char ch; 78 while (cin >> ch && (ch != ‘Q‘ && ch != ‘q‘)){ 79 while (cin.get() != ‘\n‘) 80 continue; 81 switch (ch){ 82 case ‘A‘: 83 case ‘a‘: 84 get_customer(temp); 85 test.push(temp); 86 break; 87 case ‘P‘: 88 case ‘p‘: 89 if (test.pop(temp)) 90 payments += temp.payment; 91 cout << "now total payments is " << 92 payments << endl; 93 default: 94 cout << "please enter A/P or Q\n"; 95 } 96 cout << "enter A to push, enter P to pop or" 97 << "enter Q to quit\n"; 98 } 99 system("pause"); 100 return 0; 101 } 102 103 void get_customer(Item &temp){ 104 cout << "enter a line\n"; 105 cin.getline(temp.fullname, 35); 106 cout << "enter a number\n"; 107 cin >> temp.payment; 108 cin.get(); 109 }
标签:
原文地址:http://www.cnblogs.com/coding-time/p/4530846.html