c ++问题中的变量和范围 [英] variables and scopes in c++ problem

查看:72
本文介绍了c ++问题中的变量和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是家伙?

嗯,你可以看看我的程序吗

what''s up guys?
ummm can u take a look to my program please

//bona.h


class SLList;
class student 
{
	char FullName [100];
	float FinalMark ;
	int ArrayOfWishes[3];
	student *next;
public:
	student();
	student(char[],float,int[]);
	student(const student&);
	~student();
	void set_name(char[]);
	void set_final_mark(float);
	void set_wishes(int,int,int );
	friend class SLList;
	friend void print(student&);
	friend void AccepteInWill(student&,int);
	const char* getName(){return FullName;}
	float getMark(){return FinalMark;}
	int getWish(int a){return ArrayOfWishes[a];}

};
class SLList
{
	student*head;
public:
	SLList();
	~SLList();
	void AddToList(student*);
	bool IsEmpty();
};
void AccepteInWill(student&,int);
void test_vality_of_wish(int,int,int,int,int,int,int,int,int,int);
//************************************************
// bona.cpp
#include"bona.h"
#include < iostream >
#include < cstring >
using namespace std;
student::student()
{
	FullName[100]=NULL;
	FinalMark=NULL;
	ArrayOfWishes[3]=NULL;
	next=NULL;
}
student::student(char name[],float mark,int a[])
{
	set_name(name);
	set_final_mark(mark);
	set_wishes(a[0],a[1],a[2]);
	next=NULL;

}
student::student(const student&s)
{
	strcpy(FullName,s.FullName);
	FinalMark=s.FinalMark;
	ArrayOfWishes[0]=s.ArrayOfWishes[0];
	ArrayOfWishes[1]=s.ArrayOfWishes[1];
	ArrayOfWishes[2]=s.ArrayOfWishes[2];
}
student::~student()
{
	for(int i=0;i<sizeof(fullname);i++)>
		FullName[i]=NULL;
	FinalMark=0;
	for(int i=0;i<3;i++)
		ArrayOfWishes[i]=0;

}
void student::set_name(char name[])
{
	strcpy(FullName,name);}
void student::set_final_mark(float mark)
{
	if(mark>=60&&mark<=100)
		FinalMark=mark;
	else
		throw invalid_argument("invalid mark");
}
void student::set_wishes(int a1,int a2,int a3)
{
	if((a1>=1&&a1<=8)&&(a2>=1&&a2<=8)&&(a3>=1&&a2<=8))
	{
		ArrayOfWishes[0]=a1;
		ArrayOfWishes[1]=a2;
		ArrayOfWishes[2]=a3;
	}
	else throw invalid_argument("wish number 1,wish number 2 or/and wish number 3 are invalid input");
}
SLList::SLList()
{
	head=NULL;
}
SLList::~SLList()
{
	student*i=head;
	while(!IsEmpty())
	{
		i=i->next;
		delete i;
		head=i;
	}

}
bool SLList::IsEmpty()
{
	if(head=NULL)
		return true;
	else
		return false;
}

void SLList::AddToList(student*s)
{
	if(IsEmpty())
		head=s;
	else
	{
		student*i,*j;
		i=head;
		j=i->next;
		while(j!=NULL)
		{
			if(((s->getMark()) >( i->getMark()) )&&((s->getMark()) <( j->getMark())))
			{
				s->next=j;
				i->next=s;
			}
		}
	}
}
void test_vality_of_wish(int u,int a,int b,int c,int d,int e,int f,int g,int h,int q,student& i)
{
	if(u<=3)
	{
		static int j1=1,j2=1,j3=1,j4=1,j5=1,j6=1,j7=1,j8=1,j9=1;
		switch(i.getWish(u))
		{
				case (1):
					if(j1<=a)
					{

							AccepteInWill(i,1);
							j1=j1+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(2):
					if(j2<=b)
					{

							AccepteInWill(i,2);
							j2=j2+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(3):	
					if(j3<=c)
					{

							AccepteInWill(i,3);
							j3=j3+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(4):
						if(j4<=d)
					{

							AccepteInWill(i,4);
							j4=j4+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(5):
					if(j5<=e)
					{

							AccepteInWill(i,5);
							j5=j5+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(6):
					if(j6<=f)
					{

							AccepteInWill(i,6);
							j6=j6+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(7):
					if(j7<=g)
					{

							AccepteInWill(i,7);
							j7=j7+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(8):
					if(j8<=h)
					{

							AccepteInWill(i,8);
							j8=j8+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
	}
	}
}


	
/////****************************************////
//main.cpp
#include<iostream>
#include"bona.h"
using namespace std;
int main()
{

	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,all_student=0;
	student*wishNumber1=new student[a];
	student*wishNumber2=new student[b];
	student*wishNumber3=new student[c];
	student*wishNumber4=new student[d];
	student*wishNumber5=new student[e];
	student*wishNumber6=new student[f];
	student*wishNumber7=new student[g];
	student*wishNumber8=new student[h];
	student*Denied_student=new student[(all_student-a-b-c-d-e-f-g)];
	return 0;
}



我的问题是:你可以看到函数test_vality_of_wish需要三个以上的参数,我试图使用(extern)关键字但它没有工作

我有点失落。

任何人都可以给出任何建议。


my problem is: as u can see the function test_vality_of_wish needs more than three parameters , and i tried to use the (extern) keyword but it didnt work
i''m kinda lost .
can anyone give any advice .

推荐答案

如果您唯一的问题是传递那么多值,为什么不在数组中传递它们? ...或者像 std :: vector std :: array (或任何其他)的智能数组容器容器)? ..如果那不是你的问题,你需要更清楚,因为很难理解你在问什么。
If your only problem is passing that many values, why not pass them in an array? ...or how about a smart array container like std::vector or std::array (or any other container)? ..if that''s not your question, you need to be more clear because it''s hard to understand what you''re asking.


你的问题很多代码很难解释所有。让我们从一组行开始:

There are so many problems in your code that it is hard to explain them all. Let''s start with the group of lines:
student*wishNumber1=new student[a];
student*wishNumber2=new student[b];
student*wishNumber3=new student[c];



您是否意识到您正在分配类型 student 的对象数组而不是单个实例?因为你所有的变量a,b,c,...都是零,你每个都分配零对象。



接下来,让我们来看看学生班的构造者:


Do you realize that you are allocating arrays of objects with type student and not single instances? And as all your variables a, b, c, ... are zero, you are allocating zero objects each.

Next, let''s take a look at the constructor of the student class:

student::student()
{
    FullName[100]=NULL;
    FinalMark=NULL;
    ArrayOfWishes[3]=NULL;
    next=NULL;
}



第一行是为元素FullName [100]分配一个NULL字符代码。数组边界违规,因为高可寻址元素是FullName [99]。你可能想要清除数组,你可以通过


The first line is assigning a character code of NULL to element FullName[100]. An array boundary violation, as the highes addressable element is FullName[99]. You probably wanted to clear the array, which you could have done by either

memset (FullName, 0, sizeof FullName);






or

FullName[0] = ''\0'';



析构函数也有些奇特:


The destructor is also somewhat peculiar:

student::~student()
{
	for(int i=0;i<sizeof(fullname);i++)>
		FullName[i]=NULL;
	FinalMark=0;
	for(int i=0;i<3;i++)
		ArrayOfWishes[i]=0;
 
}



无论如何,对象即将超出范围。所以将FullName,FinalMark和ArrayOfWishes设置为零是绝对没有意义的。



SLList的析构函数不能工作:


The object is about to go out of scope anyhow. So setting FullName, FinalMark, and ArrayOfWishes to zeros is absolutely pointless.

The destructor of SLList can''t work:

SLList::~SLList()
{
	student*i=head;
	while(!IsEmpty())
	{
		i=i->next;
		delete i;
		head=i;
	}
 
}



它应该是probalby读取:


it should probalby read:

SLList::~SLList()
{
    student*i=head;
    while(!IsEmpty())
    {
        i=i->next;
        delete head;
        head=i;
    }

}



IsEmpty函数很遗憾:


The IsEmpty function is a sad affair:

bool SLList::IsEmpty()
{
    if(head=NULL)
        return true;
    else
        return false;
}



单个等号将分配给头而不是检查它。你所要做的就是:


The single equal sign will assign to head instead of checking it. All you had to do was:

bool SLList::IsEmpty()
    {return head != NULL;}



我没有评论AddToList功能,这显然是行不通的。



功能 test_validity_of_whish 实际上很难理解。它的签名是


I don''t comment on the AddToList function, which obviously can''t work.

Function test_validity_of_whish is in fact hard to understand. It''s signature is

void test_vality_of_wish (int u,
              int a, int b, int c, int d, int e, int f, int g, int h, int q,
              student& i)



让我想知道你将如何提供测试结果。我猜你真正想做的是:检查一个学生是否适合他在他的愿望清单中注册的任何课程。参数u是学生希望的数(0 ... 2)并用于递归。 a到q是课程的最大占用率,我是要解决课程作业的学生。



请使用参数和变量的说话名称。您可能会发现使用单个字母名称解释代码是多么困难。



通过递归解决问题并不是最简单的方法。一个简单的循环就足够了。你陷入了使用静态变量跟踪已经预订了多少学生的课程:


which makes me wonder how you are going to deliver the test result. What I guess that you actually wanted to do is: Check whether a student will fit into any of the courses that he has registered for in hiswish list. Argument u is the number of the students wish (0 ...2) and used for recursion. a to q are the maximum occupancies for the courses, and i is the student for which the course assignment is to be solved.

Please use speaking names for parameters and variables. You probably see how difficult it is to interpret your code with single letter names.

Solving that problem via recursion is not the easiest approach. A simple loop would have sufficed. And you fell into the trap of using static variables to keep track of how many students are already booked for which course:

if(u<=3)
{
static int j1=1,j2=1,j3=1,j4=1,j5=1,j6=1,j7=1,j8=1,j9=1;
switch(i.getWish(u))
    {



现在考虑j1 ... j9只在初始化一次你的计划的开始。你没有机会重置它们。



这是我的建议。像这样组织课程分配:


Now consider that j1 ... j9 are initialize just once at the start of your program. You won''t have a chance to reset them.

Here is my suggestion. Organize that course allocation like this:

int maxOccupancies[9];
int curOccupancies[9];
// ... initialize them appropriately

bool courseAssigned = false;
for (int wishIdx = 0; wishIdx < 3; ++wishIdx)
{
    int course = stud.getWish (wishIdx);
    if (curOccupancies[course] < maxOccupancies[course]
    {
         // grant the wish and put student in the course
         courseAssigned = course;
         curOccupancies[course] += 1;
         stud.assignCourse (course);
         break;
    }
}
if (!courseAssigned)
    // ...



这一切都没有真正回答你的问题。但它试图引导你采用正确的编程方式。我建议选择一本好的C ++教科书,从一开始就开始。一旦你掌握了基础知识,事情就会变得容易了。


That all does not actually answer your question. But it tries to lead you on to the right way of programming. I''d suggest to pick up a good C++ text book and start from the very beginning. Once you get the basics correct, things will be much easier for you.


这篇关于c ++问题中的变量和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆