C++PrimerPlus学习——第九章编程练习

前两天有事情去了,没有好好学,之后要补回来
9-1
main.cpp

#include <string.h>
#include <iostream>
#include"golf.h"const int GolfSize = 3;
int main()
{golf ann;setgolf(ann, "Ann Birdfree", 24);showgolf(ann);golf andy;setgolf(andy);golf g[GolfSize];int j = 0;for (int i = 0; i < GolfSize; i++){j = setgolf(g[i]);if (j == 1)showgolf(g[i]);else if (j == 0)break;}return 0;
}

golf.h

const int Len = 40;struct golf
{char fullname[Len];int handicap;
};
void setgolf(golf& g, const char* name, int hc);int setgolf(golf& g);void handicap(golf& g, int hc);void showgolf(const golf& g);

golf.cpp

#include <string.h>
#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 std::cout;using std::cin;cout << "Please enter fullname: \n";cin.getline(g.fullname, 50);if (!strcmp(g.fullname, ""))return 0;cout << "Please enter handicap: \n";cin >> g.handicap;cin.get();return 1;
}
void handicap(golf& g, int hc)
{g.handicap = hc;
}void showgolf(const golf& g)
{using std::cout;cout << "fullname: " << g.fullname << "\n";cout << "handicap: " << g.handicap << "\n";
}

9-2
需要用getline才能获取空行

#include <iostream>
#include<string>
using namespace std;void strcount(const string str);int main()
{string input;char next;cout << "Enter a line:\n";getline(cin, input);while (input != ""){strcount(input);cout << "Enter the next line (empty line to quit):\n";getline(cin, input);}cout << "Bye\n";return 0;
}void strcount(const string str)
{static int total = 0;int count = 0;cout << "\"" << str << "\" contains ";count = str.size();total += count;cout << count << " characters\n";cout << total << " characters total\n";
}

9-3
同样需要stcpy_s
(1)

#include <iostream>
#include <string>struct chaff
{char dross[20];int slag;
};int main()
{using namespace std;chaff* cf = new chaff[2];strcpy_s(cf[0].dross, "gugugu");cf[0].slag = 6;strcpy_s(cf[1].dross, "biubiubiu");cf[1].slag = 9;for (int i = 0; i < 2; i++){cout << "dross, slag: " << cf[i].dross << ", " << cf[i].slag << endl;}//delete cf;<buffer>指定的是静态内存,不需要用delete释放return 0;
}

(2)

#include <iostream>
#include <string>struct chaff
{char dross[20];int slag;
};
const int BUF = 512;
const int N = 2;
char buffer[BUF];int main()
{using namespace std;chaff* cf = new (buffer) chaff[N];strcpy_s(cf[0].dross, "gugugu");cf[0].slag = 6;strcpy_s(cf[1].dross, "biubiubiu");cf[1].slag = 9;for (int i = 0; i < 2; i++){cout << "dross, slag: " << cf[i].dross << ", " << cf[i].slag << endl;}//delete cf;<buffer>指定的是静态内存,不需要用delete释放return 0;
}

9-4
main.cpp

#include <iostream>
#include"Sales.h"
using namespace SALES;int main()
{Sales a, b;double arr[3] = { 1.1, 22, 4.4 };setSales(a, arr, 3);showSales(a);setSales(b);showSales(b);return 0;
}

Sales.cpp

#include <iostream>
#include"Sales.h"
using namespace std;void SALES::setSales(Sales& s, const double ar[], int n)
{double sum = 0;double max = ar[0];double min = ar[0];if (n < QUARTERS){for (int i = 0; i < n; i++){s.sales[i] = ar[i];sum += ar[i];if (max < ar[i])max = ar[i];else if (min > ar[i])min = ar[i];}s.average = sum / n;s.max = max;s.min = min;for (int i = n; i < QUARTERS; i++)s.sales[i] = 0;}elsecout << "too long\n";
}
void SALES::setSales(Sales& s)
{double sum = 0;cout << "Enter four numbers:\n";for (int i = 0; i < QUARTERS; i++){std::cin >> s.sales[i];}double max = s.sales[0];double min = s.sales[0];for (int i = 0; i < QUARTERS; i++){sum += s.sales[i];if (max < s.sales[i])max = s.sales[i];else if (min > s.sales[i])min = s.sales[i];}s.average = sum / QUARTERS;s.max = max;s.min = min;
}
void SALES::showSales(const Sales& s)
{cout << "Sales" << "\n";cout << "Average: " << s.average << "\n";cout << "max: " << s.max << "\n";cout << "min: " << s.min << "\n";
}

Sales.h

#ifndef SALES_h_
#define SALES_h_
namespace SALES
{const int QUARTERS = 4;struct Sales{double sales[QUARTERS];double average;double max;double min;};void setSales(Sales& s, const double ar[], int n);void setSales(Sales& s);void showSales(const Sales& s);
}
#endif

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/550512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++PrimerPlus学习——第十一章编程练习

11-1 应该是修改list11.15&#xff0c;当当官方店买的&#xff0c;难道是盗版书吗。。。 打开file之后&#xff0c;操作跟cout类似 vect.h #ifndef VECT_h_ #define VECT_h_ #include <iostream> namespace VECTOR {class Vector{public:enum Mode { RECT, POL };privat…

C++PrimerPlus学习——第十三章编程练习

13-1 注意char*前面加const&#xff0c;不然就会报错 Classis.h #ifndef CLASSIC_H_ #define CLASS_H_ #include <string> class Cd { private:char performers[50];char label[20];int selections;double playtime; public:Cd(const char* s1, const char* s2, int n, …

C++PrimerPlus学习——第十四章编程练习

14-1 参考14.19 winec.h #ifndef WINEC_H_ #define WINEC_H_ #include <string> #include <valarray> using std::string;template<class T1, class T2> class Pair { private:T1 year;T2 bottles; public:Pair() {};Pair(const T1 y, const T2 b) :year(y)…

C++PrimerPlus学习——第十七章编程练习

17-1 不知道有没有理解错题意&#xff0c;参考list17.14 #include <iostream>int main() {using std::cout;using std::cin;using std::endl;char ch;int count 0;while (cin.peek() ! $){cin.get(ch);count;cout << ch;}cout << "\nThere are "…

数字图像处理基础与应用学习,第二章

计算灰度直方图和RGB三个通道的灰度直方图 Tips 1.计算灰度 cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate ]]) imaes:输入的图像 channels:选择图像的通道 mask:掩膜&#xff0c;是一个大小和image一样的np数组&#xff0c;其中把需要处理的部分…

数字图像处理基础与应用 第四章

3-1 (1) 感觉就是图像模糊了&#xff0c;并没有去噪 from cv2 import cv2 import numpy as np import randomdef spNoise(img,prob):# 添加椒盐噪声,prob:噪声比例 output np.zeros(img.shape,np.uint8)thres 1 - prob for i in range(img.shape[0]):for j in range(img.sha…

数字图像处理基础与应用 第五章

5-1感觉这些方法主体都差不多&#xff0c;就是微分算子不同&#xff0c;懒得一个个写了 from cv2 import cv2 import numpy as np import randomdef singleDirectionsharpen(img, N3):p N // 2img_shape np.shape(img)out np.zeros(img_shape)for i in range(img_shape[0])…

新版scipy中的imread,imsave,imresize被弃用解决方法

阅读文献代码时发现新版scipy中的imread,imsave,imresize被弃用报错 搜索了一下发现可以用imageio中的imread和imsave代替原有的&#xff0c;用numpy的reshape来代替imresize 试了一下&#xff0c;不太行&#xff0c;文献中imread有mode‘L’&#xff0c;即读取灰度图&#xff…

anaconda中tensorflow-estimator版本应与tensorflow-gpu版本相同

把tensorflow升级到2.1.0版本是发现import tensorflow as tf出错 发现是anaconda安装的tensorflow-estimator版本是2.2.0&#xff0c;将版本回退到2.1.0后解决了问题

tf.contrib在tf2中无法使用

在尝试文献中代码时发现tf.comtrib无法使用 官方文档中说 It is still possible to run 1.X code, unmodified (except for contrib), in TensorFlow 2.0: import tensorflow.compat.v1 as tf tf.disable_v2_behavior()除了contrib应该都用能两行代码解决问题,contrib则用kera…

发现了imageio文档中有代替scipy.misc的说明

原文&#xff1a;https://imageio.readthedocs.io/en/latest/scipy.html?highlightimread imageio.imread可以代替scipy.misc.imread 用pilmode代替mode 用as_gray代替flatten pilmode类型&#xff1a; ‘L’ (8-bit pixels, grayscale) ‘P’ (8-bit pixels, mapped to an…

fastai学习笔记——安装

虽然说是推荐linux&#xff0c;windows可能有bug&#xff0c;但是我还是没办法只用linux win10anaconda python3.7 安装很简单 conda install -c fastchan fastai anaconda 好了也没发现有啥问题 测试torch是否可用 import torch cuda.test.is_available()True

fastai学习——第一个bug

跟着视频学习&#xff0c;在运行第一段测试代码的时候出现问题 from fastai.vision.all import * path untar_data(URLs.PETS)/imagesdef is_cat(x): return x[0].isupper() dls ImageDataLoaders.from_name_func(path, get_image_files(path), valid_pct0.2, seed42,label_…

fastai学习:01_intro Questionnaire

fastAI Questionnaire 感觉还挺多的&#xff0c;怪不得说每一课要额外8小时进行学习。 1.Do you need these for deep learning? Lots of math T / F Lots of data T / F Lots of expensive computers T / F A PhD T / F F F F F 2.Name five areas where deep learning is …

fastai学习——第二个问题

第二节课需要使用bing image search api获取bing图片搜索中的熊图片&#xff0c;此时发现获取api需要注册azure&#xff0c;卡在绑定卡上很久&#xff0c;想了想还要去弄一张带visa的卡&#xff0c;还是算了&#xff0c;就用猫狗大战数据集实验吧&#xff0c;按照与学习视频中类…

fastai学习:02_production Questionnaire

1.Where do text models currently have a major deficiency? Deep learning is currently not good at generating correct responses! We don’t currently have a reliable way to, for instance, combine a knowledge base of medical information with a deep learning m…

fastai学习:04_mnist_basics Questionnaire

1.How is a grayscale image represented on a computer? How about a color image? 灰度图&#xff1a;单通道&#xff0c;0-256 彩色图&#xff1a;三通道RGB或HSV&#xff0c;0-256 2.How are the files and folders in the MNIST_SAMPLE dataset structured? Why? 分为…

fastai学习:05_pet_breeds Questionnaire

1.Why do we first resize to a large size on the CPU, and then to a smaller size on the GPU? 首先&#xff0c;在训练模型时&#xff0c;我们希望能够将图片的尺寸统一&#xff0c;整理为张量&#xff0c;传入GPU&#xff0c;我们还希望最大限度地减少执行不同增强计算的…

fastai学习:06_multicat Questionnarie

1.How could multi-label classification improve the usability of the bear classifier? 可以对不存在的熊进行分类 2.How do we encode the dependent variable in a multi-label classification problem? One-hot encoding: Using a vector of zeros, with a one in each…

【论文阅读笔记】Detecting Camouflaged Object in Frequency Domain

1.论文介绍 Detecting Camouflaged Object in Frequency Domain 基于频域的视频目标检测 2022年发表于CVPR [Paper] [Code] 2.摘要 隐藏目标检测&#xff08;COD&#xff09;旨在识别完美嵌入其环境中的目标&#xff0c;在医学&#xff0c;艺术和农业等领域有各种下游应用。…