如何在字符串对象中输入? [英] How to take input in a string object?
本文介绍了如何在字符串对象中输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码没有在字符串对象s1中输入。我写了一个程序,用链接列表存储学生的信息。如何根据学生姓名按字母顺序对列表进行排序?
My code is not taking input in string object s1. I wrote a program that stores information of students using linked list. How do I sort the list in alphabetical order according to student name?
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct node
{
int roll;
int link;
int score;
string name;
};
void traverse();
//void search();
void insert();
//void del();
node abc[50];
int start = -1;
int avail = 0;
main()
{
int choice;
for(int i=0; i<49; i++)
abc[i].link = i+1;
abc[49].link = -1;
do
{
cout<<"\nPress\n1. Insertion\n2. Traversing\n3. Searching\n4. Delete: ";
cin>>choice;
switch(choice)
{
case 1: insert(); break;
case 2: traverse(); break;
//case 3: search(); break;
//case 4: del(); break;
default: cout<<"\nInvalid Input"<<endl;
}
}while(1);
system("pause");
}
void insert()
{
int item1, item2; string s1;
if(avail == -1)
{
cout<<"Overflow!"<<endl;
return;
}
cout<<"Enter Roll Number: "; cin>>item1;
cout<<"Enter Score: "; cin>>item2;
cout<<"Enter student name: "; getline(cin, s1);
//insertion at the beginning
if(start == -1 || s1<abc[start].name)
{
int newnode = avail;
avail = abc[avail].link;
abc[newnode].roll = item1;
abc[newnode].score = item2;
abc[newnode].name = s1;
abc[newnode].link = start;
start = newnode;
return;
}
//insertion somewhere in the mid
int prev = start;
for(int i = abc[start].link; i!=-1; i = abc[i].link)
{
if(s1<abc[i].name)
{
int newnode = avail;
avail = abc[avail].link;
abc[newnode].roll = item1;
abc[newnode].score = item2;
abc[newnode].name = s1;
abc[newnode].link = i;
abc[prev].link = newnode;
return;
}
else
prev = i;
}
//insertion at the end
int newnode= avail;
avail = abc[avail].link;
abc[newnode].roll = item1;
abc[newnode].score = item2;
abc[newnode].name = s1;
abc[newnode].link = -1;
abc[prev].link = newnode;
return;
}
void traverse()
{
if(start ==-1)
{
cout<<"\nList Empty"<<endl;
return;
}
cout<<"Student Name"<<setw(20)<<"Roll No."<<setw(20)<<"Score\n";
for(int i = start; i!=-1; i=abc[i].link)
{
cout<<setw(12)<<abc[i].name;
cout<<setw(20)<<abc[i].roll;
cout<<setw(20)<<abc[i].score;
cout<<"\n";
}
}
推荐答案
您可能会使用(至少两种)方法:
You might use (at least two) approaches:
- Homework 一:选择你最喜欢的排序算法,并实现它(这应该非常简单,可能只需要实现两项的交换操作列表)。
- 实用一:使用
C ++
标准库:std :: list
和std :: sort
应该可以让您快速提供所需的代码。
- Homework one: pick your favourite sorting algorithm, and implement it (this should be pretty straightforward, possibly requiring just to implement the swap operation for two items of the list).
- Practical one: use the
C++
standard library:std::list
andstd::sort
should allow you to quickly provide the required code.
>
这篇关于如何在字符串对象中输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文