当前位置: 首页 > news >正文

政府网站建设相关文件个人网站制作

政府网站建设相关文件,个人网站制作,高校网站建设评比标准,公司的oa是什么意思《C Primer Plus》(第6版)第8章编程练习《C Primer Plus》(第6版)第8章编程练习1. 打印字符串2. CandyBar3. 将string对象的内容转换为大写4. 设置并打印字符串5. max5()6. maxn()7. SumArray()《C Primer Plus》(第6版…

《C++ Primer Plus》(第6版)第8章编程练习

  • 《C++ Primer Plus》(第6版)第8章编程练习
    • 1. 打印字符串
    • 2. CandyBar
    • 3. 将string对象的内容转换为大写
    • 4. 设置并打印字符串
    • 5. max5()
    • 6. maxn()
    • 7. SumArray()

《C++ Primer Plus》(第6版)第8章编程练习

1. 打印字符串

编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。

代码:

#include <iostream>
using namespace std;const int strLen = 20;void printStr(char *str, int n = 0);int main()
{char str[strLen] = "bocchi the rock";printStr(str);cout << endl;printStr(str);cout << endl;printStr(str, 10);cout << endl;printStr(str, 30);cout << endl;system("pause");return 0;
}void printStr(char *str, int n)
{static int count = 0;count++;while (n >= 0){if (n == 0){cout << str << endl;return;}else{for (int i = 0; i < count; i++){cout << str << endl;}return;}}
}

运行结果:

在这里插入图片描述

2. CandyBar

CandyBar结构包含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。 请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员,最后3个参数的默认值分别为“Millennijum Munch "、2.85和350。另外,该程序还包含一个以 CandyBar 的引用为参数,并显示结构内容的函数。请尽可能使用const。

代码:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;const int strLen = 20;typedef struct CandyBar
{char brand[strLen];double weight;int calories;
} CandyBar;void fill_CandyBar(CandyBar &candyBar, const char brand[] = "Millennium Munch", const double weight = 2.85, const int calories = 350);
void display_CandyBar(const CandyBar &candyBar);int main()
{CandyBar c1, c2;fill_CandyBar(c1);display_CandyBar(c1);cout << endl;fill_CandyBar(c2, "Margaret", 4.28, 900);display_CandyBar(c2);system("pause");return 0;
}void fill_CandyBar(CandyBar &candyBar, const char brand[], const double weight, const int calories)
{strcpy_s(candyBar.brand, brand);candyBar.weight = weight;candyBar.calories = calories;
}void display_CandyBar(const CandyBar &candyBar)
{cout << "Brand:" << candyBar.brand << endl;cout << "Weight:" << candyBar.weight << endl;cout << "Calories:" << candyBar.calories << endl;
}

运行结果:

在这里插入图片描述

3. 将string对象的内容转换为大写

编写一个函数,它接受一个指向string对象的引用作为参数“并将该string对象的内容转换为大写,为此可使用表6.4描述的函数 toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:

Enter a string (q to quit) : go away
GO AWAY
Next string (q to quit) : good grief!
GOOD GRIEF!
Next string (q to quit): q
Bye.

代码:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;#define QUIT "q"void strUpper(string &str);int main()
{string s;cout << "Enter a string (q to quit): ";getline(cin, s);while (s != QUIT){strUpper(s);cout << s << endl;cout << "Next string (q to quit): ";getline(cin, s);}cout << "Bye!\n";system("pause");return 0;
}void strUpper(string &str)
{for (int i = 0; i < str.size(); i++){str[i] = toupper(str[i]);}
}

运行结果:

在这里插入图片描述

4. 设置并打印字符串

下面是一个程序框架:

#include<iostream>using namespace std;#include<cstring>       //for strlen(),strcpy()struct stringy {char * str; //points to a stringint ct; //length of string (not couting '\0')};// prototypes for set(), show(), and show() go hereint main()
{
stringy beany;char testing[]="Reality isn't what it used to be.";set(beany,testing); //first argument is a reference,//allocates space to hold copy of testing//sets str member of beany to point to the//new block, copies testing to new block,//and sets ct member of beanyshow(beany); //prints member string onceshow(beany, 2); //prints member string twicetesting[0]= 'D';testing[1] = 'u';show(testing); //prints testing string onceshow(testing, 3); //prints testing string thriceshow("Done!");return 0;}

请提供其中描述的函数和原型,从而完成该程序。注意,应有两个 show ()函数,每个都使用默认参数。请尽可能的使用 const 参数。 set() 使用 new 分配足够的空间来存储定指的字符串。这里使用的技术与设计和实现类使用的相似。(可能还必须修改头文件的名称,删除 using 编译指令,这取决于所用的编译器。)

代码:

#include <iostream>
using namespace std;#include <cstring> //for strlen(),strcpy()struct stringy
{char *str; // points to a stringint ct;    // length of string (not counting '\0')
};// prototypes for set(), show(), and show() go here
void set(stringy &beany, const char *testing);
void show(const stringy &sy, int times = 1);
void show(const char *str, int times = 1);int main()
{stringy beany;char testing[] = "Reality isn't what it used to be.";set(beany, testing); // first argument is a reference,// allocates space to hold copy of testing// sets str member of beany to point to the// new block, copies testing to new block,// and sets ct member of beanyshow(beany);    // prints member string onceshow(beany, 2); // prints member string twicetesting[0] = 'D';testing[1] = 'u';show(testing);    // prints testing string onceshow(testing, 3); // prints testing string thriceshow("Done!");system("pause");return 0;
}void set(stringy &beany, const char *testing)
{beany.ct = strlen(testing) + 1;beany.str = new char[beany.ct];strcpy_s(beany.str, beany.ct, testing);
}
void show(const stringy &sy, int times)
{for (int i = 0; i < times; i++)cout << sy.str << endl;
}
void show(const char *str, int times)
{for (int i = 0; i < times; i++)cout << str << endl;
}

运行结果:

在这里插入图片描述

5. max5()

编写模板函数 max5 (),它将一个包含 5 个 T 类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将 T 替换为一个包含 5 个 int 值的数组和一个包含 5 个 double 值的数组,以测试该函数。

代码:

#include <iostream>
using namespace std;const int ArrSize = 5;template <typename T>
T max5(T arr[])
{T t_max = arr[0];for (int i = 1; i < ArrSize; i++){if (arr[i] > t_max)t_max = arr[i];}return t_max;
}int main()
{int arr1[ArrSize] = {1, 6, 3, 2, 5};double arr2[ArrSize] = {1.20, 2.30, 5.2, 1.4, 2.7};cout << "The maximum value in int array is " << max5(arr1) << endl;cout << "The maximum value in double array is " << max5(arr2) << endl;system("pause");return 0;
}

运行结果:

在这里插入图片描述

6. maxn()

编写模板函数 maxn (),它将由一个 T 类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含 6 个 int 元素的数组和一个包含 4 个 double 元素的数组来调用该函数。程序还包含一个具体化,它将 char 指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由 5 个字符串指针组成的数组来测试该具体化。

代码:

#include <iostream>
#include <cstring>
using namespace std;#define intSize 6
#define doubleSize 4
#define charSize 5template <typename T>
T maxn(T arr[], int n)
{T t_max = arr[0];for (int i = 1; i < n; i++){if (arr[i] > t_max)t_max = arr[i];}return t_max;
}template <>
char *maxn(char *arr[], int n)
{const char *s = arr[0];for (int i = 1; i < n; i++){if (strlen(arr[i]) > strlen(s))s = arr[i];}return s;
}int main()
{int arr1[intSize] = {1, 6, 3, 2, 5, 10};double arr2[doubleSize] = {2.3, 5.2, 1.4, 2.7};char *arr3[charSize] = {"a", "bb", "ccc", "dddd", "eeeee"};cout << "The maximum value in int array is " << maxn(arr1, intSize) << endl;cout << "The maximum value in double array is " << maxn(arr2, doubleSize) << endl;cout << "The longest string in char array is " << maxn(arr3, charSize) << endl;system("pause");return 0;
}

运行结果:

在这里插入图片描述

7. SumArray()

修改程序清单 8.14 ,使其使用两个名为 SumArray ()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有 debt 的总和。

代码:

#include <iostream>
using namespace std;template <typename T> // template A
T SumArray(T arr[], int n);template <typename T> // template B
T SumArray(T *arr[], int n);struct debts
{char name[50];double amount;
};int main()
{int things[6] = {13, 31, 103, 301, 310, 130};struct debts me_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] = &(me_E[i].amount);cout << "Listing Mr.E's counts:" << endl;cout << SumArray(things, 6) << endl;cout << "Listing Mr.E's debts:" << endl;cout << SumArray(pd, 3) << endl;system("pause");return 0;
}template <typename T>
T SumArray(T arr[], int n)
{T sum = 0;cout << "template A\n";for (int i = 0; i < n; i++)sum += arr[i];return sum;
}template <typename T>
T SumArray(T *arr[], int n)
{T sum = 0;cout << "template B\n";for (int i = 0; i < n; i++)sum += *arr[i];return sum;
}

运行结果:

在这里插入图片描述

http://www.mmbaike.com/news/78642.html

相关文章:

  • wordpress前台视频上传seo需要掌握什么技能
  • 关于政府网站建设的讲话酒店线上推广方案有哪些
  • 网站开发是做啥的微博推广费用
  • 东莞常平邮政编码多少如何seo推广
  • b2b外贸网站开发有没有免费的写文案的软件
  • 做网站互联互通整站优化是什么意思
  • 在线做静态头像的网站项目推广方式有哪些
  • 做调查问卷权威网站百度seo排名点击软件
  • 可以自己做网站经营吗一键免费创建论坛网站
  • 做甜品网站的需求分析腾讯网网站网址
  • 关于宠物的网站模板关键词优化课程
  • 对网站建设行业的了解合肥关键词排名提升
  • 网站seo诊断技巧企业网站快速建站
  • 值得关注的网站网站推广软件哪个最好
  • golang 网站开发 开源一站式网站建设
  • 做公司网站优劣势关键词歌词林俊杰
  • 青岛建站seo公司百度关键词价格计算
  • wordpress仿站函数aso优化的主要内容
  • 网站文章的作用客户引流推广方案
  • 网站后台管理增加功能网页开发培训网
  • 琼海网站建设什么是搜索引擎优化推广
  • 开发软件需要哪些技术太原百度搜索排名优化
  • wordpress被劫持跳转seo 工具推荐
  • 卖鞋的网站建设思路厦门seo小谢
  • 宜兴做网站哪家好西安疫情最新消息
  • 广州网站维护制作公司产品怎样网上推广
  • 嘉兴建设网站的seo二级目录
  • 台州市建设工程造价管理处网站sem培训机构
  • 门户网站建设情况汇报安徽seo推广
  • 135编辑器 wordpress宁波seo超级外链工具