1、
// golf.h
const int Len = 40; struct golf { char fullname[Len]; int handicap; }; // non-ineractive version: // function sets golf structure to provided name, handicap // using values passed as arguments to the function void setgolf(golf& g, const char* name, int hc); // interactive version: // function solicits name and handicap from user // and sets the member of g to the values entered // returns 1 if name is entered, 0 if name is empty string int setgolf(golf& g); // function resets handicap to new value void handicap(golf& g, int hc); // function display contents of golf structure void showgolf(const golf& g);
// golf.cpp
#include<iostream> #include"golf.h" void setgolf(golf& g, const char* name, int hc) { strcpy_s(g.fullname, name); g.handicap = hc; } int setgolf(golf& g) { using namespace std; cout << "Enter the full name: "; cin.getline(g.fullname, Len); cout << "Enter the handicap: "; cin >> g.handicap; cin.clear(); while (cin.get() != '\n'); if (strlen(g.fullname) == 0) { cout << "The fullname is empty!\n"; return 0; } else { return 1; } } void handicap(golf& g, int hc) { g.handicap = hc; } void showgolf(const golf& g) { using std::cout; using std::endl; cout << "fullname: " << g.fullname; cout << ", handicap: " << g.handicap << endl; }
// main.cpp
#include<iostream> #include<string> #include"golf.h" const int N = 10; const int hcLimit = 5; int main() { using namespace std; srand(time(0)); int i; golf golfers[N]; int hc; for (int i = 0; i < N; i++) { char id[5]; _itoa_s(i, id, 10); char name[Len] = "golfer#"; strcat_s(name, id); hc = rand() % hcLimit; setgolf(golfers[i], name, hc); } for (int i = 0; i < N; i++) showgolf(golfers[i]); for (i = 0; i < N; i++) { if (!setgolf(golfers[i])) break; } for (int j = 0; j < i; j++) showgolf(golfers[j]); }
2、
#include<iostream> #include<string> using namespace std; void strcount(const string& s); int main() { string input; cout << "Enter a line:\n"; getline(cin, input); while (input != "") { strcount(input); cout << "Enter next line (empty to quit):\n"; getline(cin, input); } cout << "Bye.\n"; } void strcount(const string& str) { static int total = 0; int count = 0; count += str.length(); total += str.length(); cout << "\"" << str << "\" contains " << count <<" characters. " << total<<" characters total.\n"; }
3、
#include<iostream> const int BUF = 48; const int N = 10; const int S = 100; struct chaff { char dross[20]; int slag; }; char buffer[BUF]; void setchaff(chaff* pcha, int n); void showchaff(const chaff* pcha, int n); int main() { // 使用静态数组作为缓冲区 chaff* pcha = new (buffer) chaff[2]; setchaff(pcha, 2); showchaff(pcha, 2); // 使用new运算符分配缓冲区 chaff* pcha1 = new chaff[2]; setchaff(pcha, 2); showchaff(pcha, 2); delete[] pcha1; } void showchaff(const chaff* pcha, int n) { using namespace std; for (int i = 0; i < n; i++) { cout << "dross: " << pcha[i].dross << ", slag: " << pcha[i].slag << endl; } } void setchaff(chaff* pcha, int n) { using namespace std; srand(time(0)); for (int i = 0; i < n; i++) { char id[N]; _itoa_s(i, id, 10); char dross[20] = "chaff#"; strcat_s(dross, id); strcpy_s(pcha[i].dross, dross); pcha[i].slag = rand() % S; } }
4、
// sales.h
namespace SALES { const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; // copies the lesser of 4 or n items from the array ar // to the sales number of s and computers and sotores the // average, maximum, and minimum values of the entered items; // remaining elements of sales, if any, set to 0 void setSales(Sales& s, const double ar[], int n); // gathers sales for 4 quaters interactively, stores them // in the sales member of s and computes and stores the //average, maximum, and minimum values void setSales(Sales& s); // display all information in structure s void showSales(const Sales& s); }
/ sales.cpp
#include<iostream> #include"sales.h" namespace SALES { void setSales(Sales& s, const double ar[], int n) { int i; for (i = 0; i < n; i++) s.sales[i] = ar[i]; while (i < 4) s.sales[i++] = 0; double max=s.sales[0], min=s.sales[0], total=s.sales[0]; for (i = 1; i < 4; i++) { total += s.sales[i]; if (s.sales[i] > max) max = s.sales[i]; if (s.sales[i] < min) min = s.sales[i]; } s.average = total / 4; s.max = max; s.min = min; } void setSales(Sales& s) { using namespace std; for (int i = 0; i < 4; i++) { cout << "sales #" << i + 1 << " quarter: "; cin >> s.sales[i]; } double max = s.sales[0], min = s.sales[0], total = s.sales[0]; for (int i = 1; i < 4; i++) { total += s.sales[i]; if (s.sales[i] > max) max = s.sales[i]; if (s.sales[i] < min) min = s.sales[i]; } s.average = total / 4; s.max = max; s.min = min; } void showSales(const Sales& s) { using namespace std; for (int i = 0; i < 4; i++) cout << "quarter #" << i + 1 << ": " << s.sales[i] << endl; cout << "max: " << s.max << ", min: " << s.min << ", average: " << s.average << endl; } }
// main.cpp
#include<iostream> #include"sales.h" int main() { using namespace SALES; Sales s; double sales[3] = { 1234.2, 2345.3, 3456.4 }; setSales(s, sales, 3); showSales(s); setSales(s); showSales(s); }
原文地址:http://www.cnblogs.com/xinmind/p/16927152.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性