冒泡在包含名称和标记的结构中按升序对名称进行排序
Bubble sort names in ascending order in a structure containing name and marks
当我调用函数然后显示它时,输出与输入相同。程序中没有其他错误,这应该意味着函数中存在一些问题。我只是一个初学者,所以任何帮助不胜感激。
#include <iostream>
using namespace std;
struct student
{
char name[30];
int marks;
void getinfo()
{
cout<< "Enter your name:n"; cin>>name;
cout<<"Enter marks:n"; cin>>marks;
}
void showinfo()
{
cout<<"nName: "<<name<<endl;
cout<<"Marks: "<<marks<<endl<<endl;
}
};
void bubsort( student S[] , int N)
{
student Temp;
for(int i=0;i<N-1;i++)
for(int j=0;j<N-1-i;j++)
{
if(S[j].name>S[j+1].name)
{
Temp=S[j];
S[j]=S[j+1];
S[j+1]=Temp;
}
}
}
int main()
{
student A[5];
cout<<" Enter details for 5 students:n";
for( int i=0;i<5;i++)
A[i].getinfo();
bubsort(A,5); //I used the function
cout<<" Sorted information:n";
for( int j=0;j<5;j++)
A[j].showinfo(); //result is in the same order as input
}
数组衰减
为指针,因此您的比较S[j].name>S[j+1].name
是比较指针而不是字符串。
如果要比较字符串,则需要使用std::string
而不是字符数组,或者使用strcmp
。
相关文章:
- 按升序对链表进行排序
- 读取一组用户输入,按升序排序,然后打印结果
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 数组将排序排序为降序而不是升序
- 编写程序,导入三个浮点数a,b和c.按升序排序
- 按升序对列表进行排序,C++使用气泡排序
- 按升序对数组进行排序嵌套循环问题
- 按特定值升序对字符串进行排序.我的实施中出了什么问题?
- C 气泡排序升序 - 不能对所有元素进行排序
- 偶数升序排序,奇数降序排序
- 按升序对矢量的相同元素进行排序
- C 排序升序非零值
- 重载比较符号"<"时,C++升序排序的正确方法
- 按升序对特征向量Xf进行排序
- 确定以下函数是否确定数组是否按升序排序
- 升序排序 - 链表
- STL 按变量排序,然后按升序排序
- 堆排序-使用哪个堆(最小/最大)进行升序排序和降序排序
- 如何对变量ab c进行升序排序
- 编写一个程序,使用冒泡排序对二维数组中的整数按升序排序