一切都很好,相反,我想得到前3名学生的分数被显示,但我得到了前3名学生的堆栈。我是个初学者。请帮助添加一些学生(5或6)和他们的分数。当我点击4,它希望它显示前3名学生的分数。我想我错过了一些地方,我需要比较学生的分数,但有一些麻烦。
#include <iostream>
#include <string>
using namespace std;
static std::size_t sizeOfStack { 0 };
class Student
{
public:
string name;
int marks;
Student* nextStudent;
void setName( string setn )
{
name = setn;
}
string getName( )
{
return name;
}
void setMarks( int setm )
{
marks = setm;
}
int getMarks( )
{
return marks;
}
void setNextStudent( Student* setAddress )
{
nextStudent = setAddress;
}
Student* getNextStudent()
{
return nextStudent;
}
};
class Stack
{
public:
Student* headStudent { NULL };
bool isEmpty( )
{
if ( headStudent == NULL )
{
cout << '\n' << "Stack is Empty" << '\n';
return true;
}
else
{
return false;
}
}
void push( )
{
string studentName;
int marks;
Student* newNode = new Student;
cout << "Enter the name of Student: " << '\n';
cin >> studentName;
newNode->setName( studentName );
cout << "Enter the Marks of Student: " << '\n';
cin >> marks;
newNode->setMarks( marks );
newNode->setNextStudent( NULL );
if ( headStudent == NULL )
{
headStudent = newNode;
}
else
{
Student* ptr = headStudent;
while ( ptr->getNextStudent( ) != NULL )
{
ptr = ptr->getNextStudent();
}
ptr->setNextStudent( newNode );
}
cout << '\n' << "Student Data saved successfully" << '\n';
++sizeOfStack;
}
void pop( )
{
if ( !isEmpty( ) )
{
Student* pre = headStudent;
Student* ptr = headStudent;
while ( ptr->getNextStudent() != NULL )
{
pre = ptr;
ptr = ptr->getNextStudent( );
}
if ( ptr == headStudent )
{
headStudent = NULL;
}
else
{
pre->setNextStudent( NULL );
}
delete ptr;
--sizeOfStack;
cout << '\n' << "Student Data Remove Successfully from database" << '\n';
}
}
void display( )
{
if ( !isEmpty( ) )
{
string studentNames[ sizeOfStack ] { };
int studentMarks[ sizeOfStack ] { };
int i { };
int j { };
cout << "*************************" << '\n';
cout << "The Data of All students: " << '\n';
cout << "Name\t\tMarks"<< '\n';
Student* ptr = headStudent;
while ( ptr != NULL )
{
studentNames[i++] = ptr->getName( );
studentMarks[j++] = ptr->getMarks( );
ptr = ptr->getNextStudent( );
}
for ( std::size_t idx = sizeOfStack - 1; idx >= 0; --idx )
{
cout<< studentNames[ idx ] << "\t\t" << studentMarks[ idx ] <<'\n';
}
cout << "*************************" << '\n';
}
}
void shortStack( )
{
for ( std::size_t i = 0; i < sizeOfStack; ++i )
{
Student* ptr = headStudent;
for ( std::size_t j = 0; j < sizeOfStack - 1; ++j )
{
string tmp;
if ( ptr->getMarks( ) < ptr->getNextStudent( )->getMarks( ) )
{
// ptr->setMarks(ptr->getMarks() + ptr->getNextStudent() );
// some statement Goes here
}
ptr = ptr->getNextStudent( );
}
}
}
void top( )
{
if ( !isEmpty( ) )
{
// shortStack();
cout << "\n\n" << "Top Positions:" << '\n';
cout << '\n' << "Name\t\tMarks";
Student* ptr = headStudent;
for( std::size_t idx = 0; idx < 3; ++idx )
{
cout << '\n' << ptr->getName( ) << "\t\t" << ptr->getMarks( );
ptr = ptr->getNextStudent( );
}
cout << "\n\n";
}
}
};
int main( )
{
Stack s;
while ( true )
{
int choice { };
cout << '\n' << "*************************************************" << '\n';
cout << "1. To Add a student in the stack" << '\n';
cout << "2. To Remove a student from stack" << '\n';
cout << "3. Display all students of stack" << '\n';
cout << "4. Display top 3 students of stack" << '\n';
cout << "5. Press 5 or any other key to close the program" << '\n';
cout << "*************************************************" << '\n';
cout << "Enter your choice (1, 2, 3, 4, 5):" << '\n';
cin >> choice;
switch ( choice )
{
case 1:
s.push( );
break;
case 2:
s.pop( );
break;
case 3:
s.display( );
break;
case 4:
s.top( );
break;
default:
exit( 0 );
break;
}
}
return 0;
}发布于 2021-12-13 14:52:14
我在代码中遇到的第一个问题是在显示函数中:
for ( std::size_t idx = sizeOfStack - 1; idx >= 0; --idx )idx,直到它小于0”。不幸的是,由于idx变量的类型.,所以这种情况永远不会发生。
std::size_t是为无符号值定义的,因此如果进一步减少它,它将变成一个非常大的数字,而不是负数,这最终会导致代码中的分段错误。如果您确实需要使用
对于( std::size_t idx = sizeOfStack;idx > 0;-idx){ cout << studentNames idx-1 <<“\t”<< studentMarks idx-1 <<‘n’;}
其次,对堆栈中的元素进行排序可以帮助解决您在帖子中提到的问题。这样你就可以很容易地打印出前3名的分数。
下面是我可以为您提供的排序算法:
void sortStack( )
{
Student* ptr = headStudent;
Student std_array[sizeOfStack];
// copy the elements in the stack
for(std::size_t i = 0; i < sizeOfStack; i++)
{
std_array[i] = *ptr;
ptr = ptr->getNextStudent();
}
// sort elements of the array
for(std::size_t j = 0; j < sizeOfStack-1; j++)
{
for(std::size_t k = j+1; k < sizeOfStack; k++)
{
if(std_array[j].getMarks() < std_array[k].getMarks())
{
Student tmpStd = std_array[j];
std_array[j] = std_array[k];
std_array[k] = tmpStd;
}
}
}
// update the stack according to the sorted values
ptr = headStudent;
for(std::size_t l = 0; l < sizeOfStack; l++)
{
ptr->setName(std_array[l].getName());
ptr->setMarks(std_array[l].getMarks());
ptr = ptr->getNextStudent();
}
}发布于 2021-12-14 14:40:55
我想你是在做虚拟大学作业。在这个任务中,指导员告诉我们只使用堆栈使用链接列表。不会考虑阵列执行的需要
https://stackoverflow.com/questions/70256835
复制相似问题