【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
来源:互联网 发布:java课程设计 清华大学 编辑:程序博客网 时间:2024/05/16 14:21
1.
#include <iostream>#include <string>using namespace std;void priStr(char *);void priStr(char *, int n);static int i = 1;int main(){char a[10] = "liujiayu";priStr(a);for (int i = 1; i < 4; i++){priStr(a, i);}cin.get();return 0;}void priStr(char *a){cout << a << endl;}void priStr(char *a, int n){if (n != 0){for (int j = 0; j < i; j++)cout << a << endl;i++;}elsecout << "input error" << endl;}
2.
#include <iostream>#include <string>using namespace std;struct CandyBar{char * brandName;double weight;int cal;};void setCandy(CandyBar &a, char *n="Millennium Munch", double w=2.85, int c=350);void showCandy(const CandyBar &);int main(){CandyBar candy;setCandy(candy, "ljy", 3.8, 400);showCandy(candy);setCandy(candy);showCandy(candy);cin.get();return 0;}void setCandy(CandyBar &a, char *n, double w, int c){a.brandName = n;a.weight = w;a.cal = c;}void showCandy(const CandyBar &a){cout << a.brandName << endl;cout << a.weight << endl;cout << a.cal << endl;}3.
#include <iostream>#include <string>using namespace std;char * l_u(string &);int main(){cout << "Enter a string(q to quit): ";string str;while (getline(cin, str)){if (str != "q"){cout << l_u(str)<<endl;delete [] l_u(str);cout << "Next string(q to quit):";}elsebreak;}cout << "Bye."<<endl;cin.get();return 0;}char * l_u(string &str){int n = str.size();char *s = new char[n+1];for (int i = 0; i < n; i++){s[i] = toupper(str[i]);}s[n] = '\0';return s;}4.
#include <iostream>#include <cstring>using namespace std;struct stringy{char * str;int ct;};void set(stringy &, char *);void show(const stringy &, int n = 1);void show(const char *, int n = 1);int main(){stringy beany;char testing [] = "Reality isn't what it used to be.";set(beany, testing);show(beany);show(beany, 2);testing[0] = 'D';testing[1] = 'u';show(testing);show(testing, 3);show("Done!");cin.get();return 0;}void set(stringy &str, char *ch){int strl = strlen(ch);char *s = new char [strl+1];for (int i = 0; i < strl; i++){s[i] = ch[i];}s[strl] = '\0';str.str = s;str.ct = 2;}void show(const stringy &s, int n){for (int i = 0; i < n; i++){cout << s.str << endl;}}void show(const char *c, int n){for (int i = 0; i < n; i++){cout << c << endl;}}5.
#include <iostream>#include <string>using namespace std;template<typename T>T max5(T *a);int main(){int a[5] = { 0, 1, 2, 3, 4 };double b[5] = { 0.0, 1.0, 2.0, 3.0, 4.9 };cout << max5(a) << endl;cout << max5(b) << endl;cin.get();return 0;}template<typename T>T max5(T *a){T temp=a[0];for (int i = 1; i < 5; i++){if(a[i]>temp) temp = a[i];}return temp;}6.
#include <iostream>#include <string>using namespace std;template<typename T>T maxn(T a[],int n);template <> char* maxn<char *>(char * a[], int n);int main(){int a[6] = { 0, 1, 2, 3, 4,5 };double b[4] = { 0.0, 1.0, 2.0, 3.9 };char* c[5] = { "liu", "jia", "yu", "good", "girl" };cout << maxn(a,6) << endl;cout << maxn(b,4) << endl;cout << maxn(c, 5) << " at " << static_cast<const void *>(maxn(c, 5)) << endl;cin.get();return 0;}template<typename T>T maxn(T *a,int n){T temp=a[0];for (int i = 1; i < n; i++){if(a[i]>temp) temp = a[i];}return temp;}template <> char* maxn<char *>(char * a[], int n){int temp = strlen(a[0]);int l=0;for (int i = 1; i < n; i++){if (strlen(a[i]) > temp){temp = strlen(a[i]);l = i;}}return a[l];}7.
#include <iostream>using namespace std;template <typename T>void SumArray(T arr[], int n);template <typename T>void SumArray(T * arr [], int n);struct debts{char name[50];double amount;};int main(){int things[6] = { 13, 31, 103, 301, 310, 130 };debts mr_E[3] = { { "Ima Wolfe", 2400.0 }, { "Ura Foxe", 1300.0 }, { "Iby Stout", 1800.0 } };double * pd[3];for (int i = 0; i < 3; i++)pd[i] = &mr_E[i].amount;cout << "Counting Mr.E's counts of things:\n";SumArray(things, 6); cout << "Counting Mr.E's debts:\n";SumArray(pd, 3);cin.get();return 0;}template <typename T>void SumArray(T arr [], int n){cout << "Template A\n";T sum=arr[0];for (int i = 1; i < n; i++){sum += arr[i];}cout << sum<<endl;}template <typename T>void SumArray(T * arr [], int n){cout << "Template B\n";T sum = *arr[0];for (int i = 1; i < n; i++){sum += *arr[i];}cout << sum << endl;}
0 0
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- C.Primer.Plus(第六版)第八章 编程练习
- C ++ Primer Plus 第六版 第八章编程练习答案
- 【学习C++】C++ Primer Plus (第六版)第十一章编程练习1-7
- 记录——《C Primer Plus (第五版)》第八章编程练习第六题
- 【学习C++】C++ Primer Plus (第六版)第九章编程练习1-4
- 【学习C++】C++ Primer Plus (第六版)第十章编程练习1-8
- 【学习C++】C++ Primer Plus (第六版)第十二章编程练习1-6
- 【学习C++】C++ Primer Plus (第六版)第十三章编程练习1-4
- C.Primer.Plus(第六版)第六章编程练习
- C ++ Primer Plus 第六版 第六章编程练习答案
- C++primer plus第六章编程练习
- 【学习C++】C++ Primer Plus (第六版)第七章编程练习6-10
- C primer plus(第五版)编程练习第八章
- c++.primer.plus第五版第八章编程练习答案
- C Primer Plus 第八章 编程练习 1-8题
- C primer plus(第五版)编程练习第六章
- c++.primer.plus第五版第六章编程练习答案
- 获取iPhone各个版本屏幕大小
- JDBC预处理出错
- C#中oracleClient操作空间数据库报“遇到不支持的 Oracle 数据类型 USERDEFINED”
- Windows 2012配置故障转移(For SQLServer 2014 AlwaysOn)
- SMW0报错:For selected object type excel no application has been defined Maintain the settings
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- android 4.0 全屏 屏蔽下面的status bar
- MAT
- ViewPager和Fragment简介
- maven 学习二:本地资源库,中央资源库,远程资源库的介绍
- Dev-C++使用注意点
- Asp.net使用placeHolder
- 多线程异常 和 事务(二)
- 怎样用MATLAB将矩阵输出为图像并存到硬盘上-图像保存到硬盘!