oj上一个c++,有关类的题目

qq_34660207 2016-05-06 01:53:47

这个是题目。
输入的时候怎么这样子??查了好久查不出来,求大神指点

#include <iostream>
#include <iomanip>
using namespace std;
class book
{
public:
book () {}
book(int t,int b)
{
type = t;bno = b;
}
int type;//类型
int bno;//编号
};

class lbook:public book
{
public:
lbook () {}
lbook(int t,int b,int s):book(t,b)
{
storage = s;
}
void set(int t,int b,int s)
{
storage = s;
}
int storage;
};
class pbook :public book
{
public:
pbook () {}
pbook(int t,int b,double p):book(t,b)
{
price = p;
}
void set(int t,int b,double p)
{
price = p;
}

double price;
};

int main()
{
int storge_total = 0;
double price_total = 0;
int count_pbook = 0;
int l = 0,p = 0;
book* lb[10000],* pb[1000];
int type,bno;
double st_pri;
cin >> bno >> type >> st_pri;

while(cin)
{
if (type == 0)
{
pb[p] = new pbook(type,bno,st_pri);
count_pbook ++;
price_total += st_pri;
p++;
}
if (type == 1)
{
lb[l] = new lbook(type,bno,st_pri);
storge_total += st_pri;
l++;
}


cin >> bno >> type >> st_pri;
}

for (int j = 0;j <= l;j++)
{
if (lb[j] -> type == 1)//文学
cout << lb[j] -> bno << " " << lb[j] -> type << " " << ((lbook*)lb[j]) -> storage << endl;
else continue;
}
for (int t = 0;t <= p;t++)
{
if (pb[t] -> type == 0)//理学
cout << pb[t] -> bno << " " << pb[t] -> type << " " << ((pbook*)pb[t]) -> price << endl;
else continue;
}
cout << storge_total << endl;
cout.setf(ios::fixed);
cout << setprecision(1);
cout << price_total / count_pbook << endl;

return 0;
}
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-05-06
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,搬手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
小灸舞 2016-05-06
  • 打赏
  • 举报
回复
for (int j = 0;j <= l;j++)和for (int t = 0;t <= p;t++)都应该去掉= 多循环了一次导致越界崩溃

#include <iostream>
#include <iomanip>
using namespace std;
class book
{
public:
	book () {}
	book(int t,int b) 
	{ 
		type = t;bno = b;
	}
	int type;//类型
	int bno;//编号
};

class lbook:public book
{
public:
	lbook () {}
	lbook(int t,int b,int s):book(t,b)
	{
		storage = s;
	}
	void set(int t,int b,int s)
	{
		storage = s;
	}
	int storage;
};
class pbook :public book
{
public:
	pbook () {}
	pbook(int t,int b,double p):book(t,b)
	{
		price = p;
	}
	void set(int t,int b,double p)
	{
		price = p;
	}

	double price;
};

int main()
{
	int storge_total = 0;
	double price_total = 0;
	int count_pbook = 0;
	int l = 0,p = 0;
	book* lb[10000],* pb[1000];
	int type,bno;
	double st_pri;
	cin >> bno >> type >> st_pri;

	while(cin)
	{
		if (type == 0)
		{
			pb[p] = new pbook(type,bno,st_pri);
			count_pbook ++;
			price_total += st_pri;
			p++;
		}
		if (type == 1)
		{
			lb[l] = new lbook(type,bno,st_pri);
			storge_total += st_pri;
			l++;
		}


		cin >> bno >> type >> st_pri;
	}

	for (int j = 0;j < l;j++)
	{
		if (lb[j] -> type == 1)//文学
			cout << lb[j] -> bno << " " << lb[j] -> type << " " << ((lbook*)lb[j]) -> storage << endl;
		else continue;
	}
	for (int t = 0;t < p;t++)
	{
		if (pb[t] -> type == 0)//理学
			cout << pb[t] -> bno << " " << pb[t] -> type << " " << ((pbook*)pb[t]) -> price << endl;
		else continue;
	}
	cout << storge_total << endl;
	cout.setf(ios::fixed);
	cout << setprecision(1);
	cout << price_total / count_pbook << endl;

	return 0;
}
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧