C++primer plus第六版课后编程题答案 6.6

6.6
#include <iostream>
#include <string>
using namespace std;

struct donation{
	string name;
	double num;
};

void main66()
{
	cout<<"Please enter the number of people:";
	//cin>>(const int number);
	int number;
	int belowW=0; //计算小于10000的人数
	cin>>number;
	donation *p=new donation[number];//动态分配结构数组
	donation *q=p;
	int i=0;
	while(i<number)
	{
		cout<<"\nEnter the "<<i+1<<"  name:";
		cin>>q->name;
		cout<<"\nEnter the num:";
		cin>>q->num;
		if(q->num<10000)
			belowW++;
		i++;
		q++;
	}
	//q=p; //重置指针
	//测试,遍历数组是否正确
	/**
	int j=0;
	while(j<number)
	{
		cout<<q->name<<": "<<q->num<<endl;
		j++;
		q++;
	
	}*/
	//输出大于10000的
	donation *ss=new donation[belowW];//存放小于10000的捐款人信息
	donation *s=ss;
	q=p;
	int k=0;
	cout<<"Grand Patrons:"<<endl;
	while(k<number)
	{
		if(q->num>10000)
			cout<<q->name<<": "<<q->num<<endl;
		else	//小于10000的存到另一个数组
		{
			s->name=q->name;
			s->num=q->num;
			s++;
		}
		q++;
		k++;
	}
	//显示剩余的人,或者你也可以向上面一样,改一下条件即可
	
	int h=0;
	s=ss;//重置指针
	cout<<"\n\nPatronns"<<endl;
	while(h<belowW)
	{
		cout<<s->name<<": "<<s->num<<endl;
		h++;
		s++;
	}
	 
	cin.get();
	delete []p; //释放内存,仅需要释放使用New分配的内存
	delete []ss; //释放内存,
	system("pause");









}

posted @ 2014-03-31 14:27  天下纵横C++  阅读(257)  评论(0编辑  收藏  举报