练习8.13 1 #include <iostream>2 #include <fstream>3 #include <sstream>4 #include <vector>5 6usingnamespace std;7 8struct PersonInfo9{
10string name;
11 vector<string> phones;
12};
1314string format(string)
15{
16return"format";
17}
1819bool valid(string s)
20{
21if (s != "")
22returntrue;
23else24returnfalse;
25}
2627int main()
28{
29 ifstream in_file("people.txt");
30if (!in_file)
31 {...
字符串类(String),熟悉内存管理与拷贝控制类定义#include <iostream>
//#include <cstring>using std::cout;
using std::cin;class String{using iterator = char *;friend std::ostream &operator<< (std::ostream &, const String &);friend std::istream &operator>> (std::istream &, String &);friend String operator + (const String &, const String &);friend String operator + (const String &, constchar *); ...
1.对CD类的派生练习。基类CD类存储作者和作品号等信息,派生类Classic额外增加一格“主要作品”的信息。主函数使用拷贝构造函数、按引用传递参数的函数和指针来测试基类和派生类的功能。注意继承类和基类的权限关系、初始化成员列表的使用。class.h#ifndef _CLASS_H_
#define _CLASS_H_#include <iostream>
using std::cin;
using std::cout;class CD
{
private:char performers[20] = {}; //初始化为空,不然输出一堆烫char la...
练习7.16没有,一个类可以有0个或多个访问说明符,而且对于某个访问说明符能出现多少次也没有严格限定;定义在public说明符之后的应该是程序的接口,即是成员函数的一些声明,还有一些构造函数;定义在private后的应该是数据成员和某些成员函数的定义;练习7.17有,class和struct的区别在于两者默认的访问权限不同,class默认的访问权限是private,struct则相反;练习7.18封装的意义在于将一些成员函数的定义和数据成员隐藏起来,只...
#include <iostream>int main(int argc, char** argv) {int i = 1;int *n ;n=&i;<span style="white-space:pre"> </span>//*n=i 这种赋值方式也可以std::cout<<"i为:"<<i<<std::endl;std::cout<<"n为:"<<n<<std::endl;std::cout<<"*n为:"<<*n<<std::endl; return 0;
}调试截图:从调试结果可以看出,n是地址值,*是取,*n是取值;&是取,&i是取地址值。原文:http://blog.csdn.net/qingbowen/article/details/46469095
https://www.cnblogs.com/DouglasLuo/p/13221356.html这道练习题要求我们使用C++统计输入的字符串中a, e, i, o, u这五个字符及其大写的数量。这里其实不难,使用switch语句对输入的字符串挨个进行判断,即可得出算出这五个字符的数量。 后面练习题要求不仅要统计a, e, i, o, u这五个字符及其大写的数量。练习题还要求统计出空格,制表符, 换行符的数量。这里我百度搜了很久都没有搜到符合我心意的答案。这道题难的地方在于,我...
写一个复数类,实现基本的运算,目的熟悉封装与数据抽象。类的定义#include <iostream>
#include <vector>
usingnamespace std;
class Complex{friend ostream & operator << (ostream & os, const Complex &); //重载输出标识符friend Complex operator + (const Complex&, const Complex &);friend Complex operator - (const Complex&, const Complex &);friend Complex operator * (const Complex&, const Complex &);frien...
保护继承protected:保护继承保护继承:当保护继承时,基类的公有和保护成员变成派生类的保护成员,私有成员不可直接访问! 1 #include <iostream>2 3usingnamespace std;4 5class info {6public:7void setter(int a,int b){8 num = a;9 age = b;
10 }
1112int getter(void) {
13int a = age;
14return a;
15 }
1617void pri(void) {
18 cout << num << endl;
19 cout << age << endl;
20 ...
汇集1000道C语言和C++语言的练习题 (例题、笔试题、编程题、算法设计题),由我亲自配上答案(点此查看)。 语法题我就不出了,那个没意思,看书就可以了。本贴汇集50题 (0000-0050),持续更新...0001. 抽签题
从10个数中随机抽取5个数。(相当于双色球抽签问题)0002. 插入排序
已有10个按增序排列好的整数1,3,5,7,9,11,13,15,17,19。要求把一个整数n插到数组中,保持增序排列。0003. 数学公式类计算题
已经圆柱体的底半...
练习14.9 1 istream &operator >> (istream &is, Sales_data &item)2{3double price;4is >> item.bookNo >> item.units_sold >> price;5if (is)6 item.revenue = item.units_sold * price;7else 8 item = Sales_data();9returnis;
10 }练习14.10a)将一个Sales_data的对象逐一赋值;b)类型不对,类型无法转换导致输入发生错误,Sales_data对象被重置为初始状态;练习14.11有错误,没有给出当输入错误时程序应该给...
练习11.1map他是一个有序的且元素不重样的关联容器,他存放元素的方式是以键值对的方式存放的;vector容器没有什么特别的要求练习11.2list适用于在在任何地方添加删除元素,因为他是个双向的链表;vector适用于需要随机访问的程序,只能在尾后添加元素,在其他位置添加或删除元素效率会很低;代价过大deque适用于在容器首尾添加删除元素且随机访问的程序;map是键值对的集合,他能在查找的同时进行高效的访问;set就是关键字的简单...
第四题 单利和复利// 单利投资和复利投资 本金为100元int count = 1;// 本金int principal = 100;// A的第一年的收获float a_alone = principal * 0.1;// B第一年的收获 利息float b_double = principal * 0.05;// b 收获的总和float b_Allhavest = b_double;while (b_Allhavest < a_alone * count){b_double += b_double * 0.05;b_Allhavest += b_double;cout << "b的利息-------" << b_double << endl;count++;}int year =...
练习6.541int fun(int, int);
2 typedef int (*pf) (int, int);
3 vector<pf> a; 1int funAdd(int i, int j)2{3return i + j;4}5int funDel(int i, int j)6{7return i - j;8}9int funMul(int i, int j)
10{
11return i * j;
12}
13int funDiv(int i, int j)
14{
15return i / j;
16}
171819int main()
20{
21 typedef int (*pf) (int, int);
22 vector<pf> a(2);
23 a[0] = funAdd;
24 a[1] = funDel;
25 a[2]...
问题:有一个生产者,多个消费者,生产者每生产一个,放入队列,多个消费者顺序从队列中取出数据,打印最终结果。分析:首先这题,我本意应该设计成如下模型:生产者单开一个线程,向队列中放入数据,而消费者在锁的保护下,从队列中去数据。但是在实际编程中,发现在队列只有100个数的情况,线程不切换,当队列数据多的时候,会发生切换,但是也不是我所想象的那种随机切换,思考至今,也没有一个合理的解释/(ㄒoㄒ)/~~。最后我把...
// 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...