【c++ primer 5th 练习3.43】教程文章相关的互联网学习教程文章

c++并发练习---生产者消费者模型【代码】【图】

问题:有一个生产者,多个消费者,生产者每生产一个,放入队列,多个消费者顺序从队列中取出数据,打印最终结果。分析:首先这题,我本意应该设计成如下模型:生产者单开一个线程,向队列中放入数据,而消费者在锁的保护下,从队列中去数据。但是在实际编程中,发现在队列只有100个数的情况,线程不切换,当队列数据多的时候,会发生切换,但是也不是我所想象的那种随机切换,思考至今,也没有一个合理的解释/(ㄒoㄒ)/~~。最后我把...

C++ Lua 交互练习!!!!!!!!【代码】

// text.cpp : Defines the entry point for the console application. // #include <iostream> #include <fstream> #include <string> usingnamespace std;extern"C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } #pragma comment(lib,"lualib.lib") lua_State * L; LUALIB_API int textC(lua_State *L) {if(L == nullptr){return0;}cout<<"This msg from C++"<<endl;return0; } LUALIB_API int textC1...

【C/C++学院】(5)面向对象编程练习--h和cpp分开编写【图】

抽象一个点,一个圆,并判断点与圆的关系。(在圆内还是圆外)h文件进行类的声明;cpp文件为类的实现细节;主要注重的细节为:=================================================================================================================================工程代码如下://MyCircle.h #pragma once #include"MyPoint.h" class MyCircle { public:void setCir(double x, double y, double r);public:char * judge(double ...

蓝桥杯练习——C++输出阶乘的最右边一位非零数【代码】

1 #include<iostream>2 #include<iomanip>3usingnamespace std;4#define M 10000 5#define N 10000 6int p=1; 7int func2(int a);8void func(int *a,int n) 9{ 10int i,j,k; 11for(j=2;j<=n;j++) 12{ 13 k=0; 14for(i=0;i<p;i++) 15 a[i]*=j; 16for(i=0;i<p;i++) 17{ 18 a[i]+=k; 19 k=a[i]/M; 20 a[i]%=M; 21} 22if(k) 23{ 24 p++; 25 a[p-1]+=k; 26} 27} 28//cout<<a[p-1];29int tempnum=0; 30for(int i=0;i<...

C++桶排序练习【代码】

假定一组随机数,然后快速找到第num位置的数#include<iostream> #include<ctime> usingnamespace std;#define number 10 #define num 7int main() {srand((unsigned int)time(NULL));int arr[number];int flag=0;//生成随机数组for (int i = 0; i < number; i++){//生成1~10随机数arr[i]=rand()%number+1;}//显示生成的随机数组for (int i = 0; i < number; i++){cout<<arr[i]<<"\t";flag++;if ((flag%10) == 0){cout<<endl;}}//桶...

C++ Primer第5版 第七章课后练习答案【代码】

练习7.1struct Sales_data {string bookNo;unsigned units_sold = { 0 };double revenue = { 0.0 }; };int main(int argc, char* argv[]) { Sales_data total;if (cin >> total.bookNo >> total.units_sold >> total.revenue) {Sales_data trans;while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {if (total.bookNo == trans.bookNo) {total.revenue += trans.revenue;total.units_sold += trans.units_sold;}...

c++primer 第四章编程练习答案【代码】

4.13.1#include<iostream>struct students {char firstname[20];char lastname[20];char grade;int age; }; int main() {usingnamespace std;students student1;cout << "What is your fistname? ";cin.get(student1.firstname, 20).get();cout << "What is your lastname? ";cin.getline(student1.lastname, 20);cout << "What latter grade do you deserve? ";cin >> student1.grade;cout << "What is your age? ";cin >> studen...

71. C++ 分别用指针数组和二维数组生成二维空间,存储数据并释放。 练习new/delete, new[]/delete[]【代码】【图】

分别用指针数组和二维数组生成二维空间,存储数据并释放。比如,数据如下: //使用了下fgetc() 1 #include <iostream>2 #include <stdio.h>3usingnamespace std;4 5 6int main()7{8#if 0 //生成二维数组存储 9 FILE* fp = fopen("G:\\qtcode\\temp.txt","r"); 1011//char buf[3][10] ={0};12char (*buf)[10] = newchar[3][10]; 1314int i,j,ch; 15for(i = 0;i<3;i++) 16 { 17for(j = 0;j<8;j++) 18 { 19 ...

C++编程基础练习,入门必备练手【图】

练习1,1 从一个简单程序开始1,将string头文件注释掉,重新编译这个程序,会发生什么事? 目前还没有发现会发生什么事。2,将using namespace std注释掉,重新编译,会发生什么事?3,将函数名main()改为my_main(),然后重新编译,有什么结果?练习1.2将上述程序的内容进行扩充(1)要求用户同时输入名字(first name)和姓氏(last name);(2)修改输出结果,同时打印姓氏和名字。C/C++的学习裙【105+302+9869】,无论你是小白还...

基于神经网络的垃圾邮件分类(C++练习题)【代码】

【问题描述】 神经网络在机器学习领域非常流行。神经网络由多层组成,它包含一个输入层可以输入参数x(程序的输入)。然后输入通过多个隐藏层,在最后一层获得一个输出,称为输出层。 我们有一个包含N个隐藏层的非常简单的神经网络,每层包含一个神经元。每个神经元有两个相关的值:wi和bi,分别表示神经元的权重和偏移。給定神经元的一个输入x,将生成一个输出(wix)+bi。 因此,一个输入x被神经网络按照以下方式进行传输。**第一个...

C++数组练习题(三)【代码】

我是计算机专业的一位大一本科生,C++小白,下面是我们学校在学习C++时用的基础练习题,我感觉这些题比较适合初学C++的码友,所以利用空闲时间将这些题整理出来,一是为了让初学C++的码友有所参考,二也是为了复习一下所学过知识。(因准备急促,如有代码错误,还请各位码友指正。) 1.回文字符串 从键盘输入n(n<100)个字符串(每个字符串长度不超过19),输出这组字符串中的回文字符串。其中判断一个字符串是否为回文字符串用函数...

(C++)A+B 输入输出练习V 输入的第一行是一个正数N,表示后面有N行。每一行的第一个数是M,表示本行后面还有M个数。【代码】

#include<cstdio>/* 2 4 1 2 3 4 5 1 2 3 4 5 */int main(){int n,a;scanf("%d",&n);while(n--){int sum = 0,m;scanf("%d",&m);for(int i = 0;i<m;i++){scanf("%d",&a);sum +=a;}printf("%d\n",sum); }return 0;}

<小甲鱼>C++实例练习19—记录猫狗数量(静态变量)【代码】

前言: 参考:' rel='nofollow' target='_blank'>B站UP主鱼C_小甲鱼 代码调试平台:VS2017,调试成功。问题描述: 问题描述:记录猫狗数量(静态变量)。代码实现: //静态变量static #include<iostream> #include <string>using namespace std;class Pet { public:Pet(string theName);~Pet();static int getcount(); protected:string name; private:static int count; }; class Dog:public Pet { public:Dog(string theName); }...

C++ Primer 第十四章操作重载与类型转换 14.8 函数调用运算符 练习和总结【代码】【图】

14.8 函数调用运算符 重载了函数调用运算符的类,它的对象可以做出像函数 一样的行为,因此我们称这样的对象为函数对象。 相对于普通的函数,函数对象可以保存一些状态,这些状态其实就是他的数据成员。 这其实就又和lambda表达式一样了,lambda表达式其实就是一个类的对象。 练习 14.33 零个,一个,多个都可以,而且参数可以有默认值 14.34 struct IfThenElse {int operator()(int a,int b,int c) {return a ? b : c;}; };14.35 ...

<小甲鱼>C++实例练习20—猫狗问题(虚方法)【代码】

前言: 参考:' rel='nofollow' target='_blank'>B站UP主鱼C_小甲鱼 代码调试平台:VS2017,调试成功。问题描述: 问题描述:猫狗问题(虚方法)代码实现: //当在子类中对基类的方法进行覆盖时,使用new对变量进行声明时,调用覆盖的函数, //为了执行更快C++优先读取基类的方法,因此在基类声明时,需要将其方法声明为虚方法 #include <iostream> #include <string>using namespace std;class Pet { public:Pet(string theName);...