⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 结构.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
//**************************
//**      ch10_1.cpp      **
//**************************
#include <iostream.h>

struct Weather
{
	double temp;
	double wind;
};

void main()
{
	Weather today;
	today.temp=30.5;
	today.wind=10.1;

	cout<<"Today's temp: "<<today.temp<<endl
		<<"Today's wind: "<<today.wind<<endl;
}

//**************************
//**      ch10_2.cpp      **
//**************************
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

Person pr1={"Frank Voltaire",12345678,3.35};

void main()
{
	Person pr2;
	pr2=pr1;
	cout<<pr2.name<<"	"
		<<pr2.id<<"	"
		<<pr2.salary<<endl;
}

//**************************
//**      ch10_3.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

void main()
{
	Person pr1;
	Person* prPtr;
	prPtr=&pr1;
	strcpy(prPtr->name,"David Marat");
	prPtr->id=987654321;
	prPtr->salary=335.0;
	
	cout<<prPtr->name<<"	"
		<<prPtr->id<<"	"
		<<prPtr->salary<<endl;
}

//**************************
//**      ch10_4.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

void main()
{
	Person pr1;
	Person* prPtr;
	prPtr=&pr1;
	strcpy((*prPtr).name,"David Marat");
	(*prPtr).id=987654321;
	(*prPtr).salary=335.0;
	
	cout<<(*prPtr).name<<"	"
		<<(*prPtr).id<<"	"
		<<(*prPtr).salary<<endl;
}

//**************************
//**      ch10_5.cpp      **
//**************************
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

Person allone[6]=
{{"Jone",12345,339.0},
{"david",13916,449.0},
{"marit",27519,311.0},
{"jasen",42876,623.0},
{"peter",23987,400.0},
{"yoke",12335,511.0}};

void main()
{
	Person temp;
	for(int i=0;i<6;i++)
	{
		for(int j=0;j<5-i;j++)
		{
			if(allone[j].salary>allone[j+1].salary)
			{
				temp=allone[j];
				allone[j]=allone[j+1];
				allone[j+1]=temp;
			}
		}
	}

	for(int k=0;k<6;k++)
	{
		cout<<allone[k].name<<"	"
			<<allone[k].id<<"	"
			<<allone[k].salary<<endl;
	}
}

//**************************
//**      ch10_6.cpp      **
//**************************
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

Person allone[6]=
{{"Jone",12345,339.0},
{"david",13916,449.0},
{"marit",27519,311.0},
{"jasen",42876,623.0},
{"peter",23987,400.0},
{"yoke",12335,511.0}};

void main()
{
	Person* pA[6]={&allone[0],&allone[1],&allone[2],&allone[3],&allone[4],&allone[5]};
	Person* temp;
	for(int i=0;i<6;i++)
	{
		for(int j=0;j<5-i;j++)
		{
			if((*pA[j]).salary>(*pA[j+1]).salary)
			{
				temp=pA[j];
				pA[j]=pA[j+1];
				pA[j+1]=temp;
			}
		}
	}

	for(int k=0;k<6;k++)
	{
		cout<<(*pA[k]).name<<"	"
			<<(*pA[k]).id<<"	"
			<<(*pA[k]).salary<<endl;
	}
}

//**************************
//**      ch10_7.cpp      **
//**************************
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

Person allone[6]=
{{"Jone",12345,339.0},
{"david",13916,449.0},
{"marit",27519,311.0},
{"jasen",42876,623.0},
{"peter",23987,400.0},
{"yoke",12335,511.0}};

void Print(Person pr)
{
	cout<<pr.name<<"	"
		<<pr.id<<"	"
		<<pr.salary<<endl;
}

void main()
{
	for(int i=0;i<6;i++)
		Print(allone[i]);
	cout<<endl;
}

//**************************
//**      ch10_8.cpp      **
//**************************
//change one part of ch10_7.cpp listed below:
void Print(Person &pr)        //here, the reference improve the efficiency of program
{
	cout<<pr.name<<"	"
		<<pr.id<<"	"
		<<pr.salary<<endl;
}

//**************************
//**      ch10_9.cpp      **
//**************************
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

Person GetPerson()
{
	Person temp;
	cout<<"please enter a name for one person:"<<endl;
	cin>>temp.name;
	cout<<"please enter one's id number and his salary:"<<endl;
	cin>>temp.id>>temp.salary;

	return temp;
}

void Print(Person &pr)
{
	cout<<pr.name<<"	"
		<<pr.id<<"	"
		<<pr.salary<<endl;
}

void main()
{
	Person employee[3];
	for(int i=0;i<3;i++)
	{
		employee[i]=GetPerson();
		Print(employee[i]);
		cout<<endl;
	}
	cout<<endl;
}

//***************************
//**      ch10_10.cpp      **
//***************************
//the charm of using reference
#include <iostream.h>

struct Person
{
	char name[20];
	unsigned long id;
	float salary;
};

void GetPerson(Person &p)
{
	cout<<"please enter a name for one person:"<<endl;
	cin>>p.name;
	cout<<"please enter one's id number and his salary:"<<endl;
	cin>>p.id>>p.salary;
}

void Print(Person &pr)
{
	cout<<pr.name<<"	"
		<<pr.id<<"	"
		<<pr.salary<<endl;
}

void main()
{
	Person employee[3];
	for(int i=0;i<3;i++)
	{
		GetPerson(employee[i]);
		Print(employee[i]);
		cout<<endl;
	}
}

//***************************
//**      ch10_11.cpp      **
//***************************
//链表的创建与显示,增加节点与删除节点
#include <iostream.h>

struct Student{
	long number;
	float score;
	Student* next;
};

Student* head;

Student* Creat()
{
	Student* pS;       //creat node pointer
	Student* pEnd;     //creat trail pointer
	pS=new Student;
	cin>>pS->number>>pS->score;  //1
	head=NULL;
	pEnd=pS;

	while(pS->number!=0)
	{
		if(head==NULL)
			head=pS;
		else
			pEnd->next=pS;

		pEnd=pS;  //tmp store
		pS= new Student;  //2,3...
		cin>>pS->number>>pS->score;
	}

	pEnd->next=NULL;
	delete pS;
	return(head);
}

void ShowList(Student* head)
{
	cout<<"now the item of list are"<<endl;

	while(head)
	{
		cout<<head->number<<","<<head->score<<endl;
		head=head->next;
	}
}

void main()
{
	ShowList(Creat());
}

//*******************************//
//---------Delete module---------//
//若参数用Student* head,则这里的head是形参,所以如果真的链表首指针被删的话,那链表的首地址就真的找不到了,所以这里必须用引用真正修改head的值,也即修改全局变量head,而不是形参

void Delete(Student* (&head), long number)  //note: here the using of head is reference  
{
	Student* p;
	if(!head)
	{
		cout<<endl<<"List null!"<<endl;
		return;
	}
	if(head->number==number)  //the node to be delete id the fisrt node
	{
		p=head;
		head=head->next;
		delete p;
		cout<<number<<" the head of list have been deleted."<<endl;
		return;
	}

	for(Student* pGuard=head;pGuard->next;pGuard=pGuard->next)
	{
		if(pGuard->next->number==number)
		{
			p=pGuard->next;   //the next node which to be deleted
			pGuard->next=p->next;
			delete p;
			cout<<number<<" have been deleted."<<endl;
			return;
		}
	}
	cout<<number<<"not found!"<<endl;
}

//*******************************//
//---------Insert module---------//
void Insert(Student* (&head), Student* stud)  //此处用引用的道理同delete module
{
	if(head==NULL)
	{
		head=stud;
		stud->next=NULL;
		return;
	}
	if(head->number>stud->number)
	{
		stud->next=head;
		head=stud;
		return;
	}

	Student* pGuard=head;
	while(pGuard->next&&pGuard->next->number<stud->number)
		pGuard=pGuard->next;
	stud->next=pGuard->next;
	pGuard->next=stud;
}

//********************************
//**          Jose.cpp          **
//**      (second edition)      **
//********************************
#include <iostream.h>
#include <iomanip.h>

struct jose
{
	int code;
	jose* next;
};

void main()
{
	//initialize
	int numOfBoys,interval;
	cout<<"please input the number of boys,"<<endl
		<<"           interval of counting:"<<endl;
	cin>>numOfBoys>>interval;

	//creat the child structure array
	jose* pJose=new jose[numOfBoys];
	jose* pCurrent=pJose;

	//initialize the child array, number them, and output the number
	int itemsInLine=0;
	for(int i=1;i<=numOfBoys;i++)
	{
		pCurrent->next=pJose+i%numOfBoys;  //good!
		pCurrent->code=i;
		pCurrent=pCurrent->next;
		if(itemsInLine++ % 10 == 0)
			cout<<endl;
		cout<<setw(4)<<i;
	}
	itemsInLine=0;

	jose* pivot;    //the guard
	pCurrent=&pJose[numOfBoys-1]; //the next is the first element: pJose[0]

	while(pCurrent->next!=pCurrent)  //point to itself, means that the linked list exits only element
	{
		for(int j=0;j<interval;j++)
		{
			pivot=pCurrent;
			pCurrent=pivot->next;
		}

		if(itemsInLine++ % 10 == 0)
			cout<<endl;
		cout<<setw(4)<<pCurrent->code;
		pivot->next=pCurrent->next;  //here, the pivot point to the previous node, serve as a guard
		pCurrent=pivot;              //this step delete the current node  
	}

	cout<<endl<<endl<<"the winner is "
		<<pCurrent->code<<endl;

	delete []pJose;
}

若要实现从任意个小孩开始数,则应加一个参数int start,再修改一个语句
pCurrent=&pJose[(numOfBoys-2+start)%numOfBoys];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -