标签:
p296.3
1 #include<iostream> 2 #include<cstdlib> 3 #include<string> 4 #include<cstring> 5 #include<new> 6 using namespace std; 7 struct chaff 8 { 9 char dross[20]; 10 int slag; 11 }; 12 void setarray(chaff *pt); 13 void showarray(chaff *pt); 14 15 int main(){ 16 char buffer[40]; 17 chaff ar[2], *pt; 18 pt = new(buffer)chaff[2]; 19 setarray(pt); 20 showarray(pt); 21 system("pause"); 22 return 0; 23 } 24 25 void setarray(chaff *pt){ 26 int i; 27 char adross[20], pdross[20], ch; 28 cout << "enter a line\n"; 29 cin.get(adross, 20); 30 while (cin){ 31 cin.get(ch); 32 while (ch != ‘\n‘) 33 cin.get(ch); 34 break; 35 } 36 strcpy(pt->dross, adross); 37 cout << "enter another line\n"; 38 cin.get(pdross, 20); 39 while (cin){ 40 cin.get(ch); 41 while (ch != ‘\n‘) 42 cin.get(ch); 43 break; 44 } 45 strcpy((pt + 1)->dross, pdross); 46 cout << "enter two integer\n"; 47 cin >> pt->slag; 48 cin >> (pt + 1)->slag; 49 cin.get(); 50 } 51 52 void showarray(chaff *pt){ 53 cout << pt->dross << " " << 54 pt->slag << endl << 55 (pt + 1)->dross << " " << 56 (pt + 1)->slag << endl; 57 }
p296.4
1 sales.cpp 2 3 #include<iostream> 4 #include"sales.h" 5 using namespace std; 6 7 void SALES::setsales(Sales &s, const double ar[], int n){ 8 int num, i, sum=0; 9 if (n <= QUARTERS) 10 num = n; 11 else num = QUARTERS; 12 for (i = 0; i < num; i++){ 13 s.sales[i] = ar[i]; 14 sum += s.sales[i]; 15 } 16 s.average = sum / num; 17 s.max = s.sales[0]; 18 s.min = s.sales[0]; 19 for (i = 1; i < num; i++){ 20 if (s.max < s.sales[i]) 21 s.max = s.sales[i]; 22 if (s.min>s.sales[i]) 23 s.min = s.sales[i]; 24 } 25 } 26 27 void SALES::setsales(Sales &s){ 28 cout << "enter double into an array\n"; 29 int i; 30 double sum = 0; 31 for (i = 0; i < QUARTERS; i++){ 32 cin >> s.sales[i]; 33 sum += s.sales[i]; 34 } 35 s.average = sum / QUARTERS; 36 s.max = s.sales[0]; 37 s.min = s.sales[0]; 38 for (i = 1; i < QUARTERS; i++){ 39 if (s.max < s.sales[i]) 40 s.max = s.sales[i]; 41 if (s.min>s.sales[i]) 42 s.min = s.sales[i]; 43 } 44 } 45 46 void SALES::showsales(const Sales & s){ 47 for (int i = 0; i < QUARTERS; i++) 48 cout << s.sales[i] << " "; 49 cout << "average is " << s.average << endl << 50 "max is " << s.max << endl << 51 "min is " << s.min << endl; 52 } 53 54 驱动程序 55 56 #include<iostream> 57 #include<cstdlib> 58 #include<string> 59 #include<cstring> 60 #include"sales.h" 61 using namespace std; 62 63 int main(){ 64 cout << "enter a number you want to fill the array\n"; 65 int n, i; 66 cin >> n; 67 double *ar = new double[n+1]; 68 cout << "enter an double array\n"; 69 for (i = 0; i < n; i++) 70 cin >> ar[i]; 71 SALES::Sales ex1, ex2; 72 SALES::setsales(ex1, ar, n); 73 SALES::setsales(ex2); 74 SALES::showsales(ex1); 75 SALES::showsales(ex2); 76 delete[]ar; 77 system("pause"); 78 return 0; 79 } 80 81 82 头文件 83 84 namespace SALES 85 { 86 const int QUARTERS = 4; 87 struct Sales{ 88 double sales[QUARTERS]; 89 double average; 90 double max; 91 double min; 92 }; 93 void setsales(Sales &s, const double ar[], int n); 94 void setsales(Sales &s); 95 void showsales(const Sales&s); 96 }
p332.1
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstring> 4 using namespace std; 5 6 class Bankacount{ 7 char name[30]; 8 char num[20]; 9 double deposit; 10 public: 11 Bankacount(char *pt="nobody", char *ar="0", double balance=0.0); 12 ~Bankacount(); 13 void storage(double ct); 14 void withdraw(double ct); 15 void show()const; 16 }; 17 18 Bankacount::Bankacount(char *pt, char *ar, double balance) 19 { 20 strncpy(name, pt, 29); 21 name[30] = ‘\0‘; 22 strncpy(num, ar, 19); 23 num[20] = ‘\0‘; 24 deposit = balance; 25 } 26 27 Bankacount::~Bankacount(){ 28 29 } 30 31 void Bankacount::storage(double ct){ 32 if (ct < 0) 33 cout << "the deposit must be positive\n"; 34 else deposit += ct; 35 } 36 37 void Bankacount::withdraw(double ct){ 38 if (ct<0) 39 cout << "the deposit must be positive\n"; 40 else deposit -= ct; 41 } 42 43 void Bankacount::show()const{ 44 cout << "name is " << name << endl 45 << "number is " << num << endl 46 << "deposit now is " << deposit << endl; 47 } 48 49 50 int main(){ 51 Bankacount acount1("GE", "asdf", 0.0); 52 Bankacount acount2; 53 Bankacount acount3 = Bankacount("DELL", "34WE", 2.3); 54 acount1.show(); 55 acount2.show(); 56 acount3.show(); 57 acount1.storage(4.5); 58 acount2.withdraw(2.7); 59 acount1.show(); 60 acount2.show(); 61 62 system("pause"); 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/coding-time/p/4530832.html