请问以下排次序既c++程式要点写?

peter022 2008-10-20 10:08:37
输入1:
peng 50 0
lei 100 1
hongwei 100 1
tian 100 1
stu001 -321 1
a 1
//0系不显示姓名,1系显示姓名
//a系以由小到大排序
//1系以姓名字母排序

输出1
Rank Name Mark
1 hongwei 100
2 lei 100
3 ****** 50
4 stu001 -321
5 tian 100

输入2:
peng 50 0
lei 100 1
hongwei 100 1
tian 100 1
stu001 -321 1
d 0
//0系不显示姓名,1系显示姓名
//d系以由大到小排序
//0系以分数排序

输出2
Rank Name Mark
1 lei 100
2 hongwei 100
3 tian 100
4 ****** 50
5 stu001 -321

以下係該程式既骨干

#include <iostream>
#include <string>
#define NUMBER 20

using namespace std;

findPosition(int marks[], int start, int end, bool order)
{
int pos = start;
return pos;
}

void swap(int& val0, int& val1)
{

}


)
{
for(int i=0; i<num-1; i ++)
{int pos;
switch(rankBy)
{}
}}

void getInput(int marks[], string names[], bool flags[], int num)
{
for(int i=0; i<num; i ++)
cin>> names[i] >> marks[i] >> flags[i];
}

void printResult()
{
}

int main()
{
int marks[NUMBER];
string names[NUMBER];
bool flags[NUMBER];

getInput(marks, names, flags, NUMBER);

bool sorting_order;
char temp_chr;
cin >> temp_chr;
if('a' == temp_chr || 'A' == temp_chr) sorting_order = true;
else sorting_order = false;

int sortBy;
cin >> sortBy;
if(1 == sortBy)
{if(sorting_order) selectionSort(marks, names, flags, NUMBER);
else selectionSort(marks, names, flags, NUMBER, 1, false);
}
else
{if(sorting_order) selectionSort(marks, names, flags, NUMBER, 0);
else selectionSort(marks, names, flags, NUMBER, 0, false);

}
printResult(marks, names, flags, NUMBER);
}
...全文
155 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2008-10-21
  • 打赏
  • 举报
回复

//楼主给的框架实在糟糕...下面这个勉强能跑.
#include <iostream>
#include <string>
#define NUMBER 5

using namespace std;

int marks[NUMBER];
string names[NUMBER];
bool flags[NUMBER];

int findPosition(int start, int end, int sortBy, int sorting_order)
{
int pos = start;
for (int i = start; i < end; ++i)
{
if (sortBy)
{
if (sorting_order)
{
if (names[i] < names[pos])
pos = i;
}
else
{
if (names[i] > names[pos])
pos = i;
}
}
else
{
if (sorting_order)
{
if (marks[i] < marks[pos])
pos = i;
}
else
{
if (marks[i] > marks[pos])
pos = i;
}
}
}
return pos;
}

void swap(int val0, int val1)
{
int ti;
string ts;
bool tf;
ti = marks[val0], marks[val0] = marks[val1], marks[val1] = ti;
ts = names[val0], names[val0] = names[val1], names[val1] = ts;
tf = flags[val0], flags[val0] = flags[val1], flags[val1] = tf;
}


void selectionSort(int* marks, string* names, bool* flags, int num, int sortBy, int sorting_order = true)
{
//sortBy是否按名字排序
//sorting_order是否从小到大排序
for(int i=0; i <num-1; i ++)
{
int pos = findPosition(i, num, sortBy, sorting_order);
swap(i, pos);
}

}

void getInput(int marks[], string names[], bool flags[], int num)
{
for(int i=0; i <num; i ++)
cin >> names[i] >> marks[i];
}

void printResult()
{
cout << "rank name mark" << endl;
for (int i = 0; i < NUMBER; ++i)
cout << i+1 << " " << names[i] << " " <<marks[i] << endl;
}

int main()
{


getInput(marks, names, flags, NUMBER);

bool sorting_order;
char temp_chr;
cin >> temp_chr;
if('a' == temp_chr || 'A' == temp_chr) sorting_order = true;
else sorting_order = false;

int sortBy;
cin >> sortBy;
if(1 == sortBy)
{
if(sorting_order)
selectionSort(marks, names, flags, NUMBER, 1, true);
else
selectionSort(marks, names, flags, NUMBER, 1, false);
}
else
{
if(sorting_order)
selectionSort(marks, names, flags, NUMBER, 0, true);
else
selectionSort(marks, names, flags, NUMBER, 0, false);

}
printResult();
}
luofanshan 2008-10-21
  • 打赏
  • 举报
回复
楼主别用广东话啊,用普通话。
luofanshan 2008-10-21
  • 打赏
  • 举报
回复
楼主别用广东话啊,用普通话。
frank_ll 2008-10-21
  • 打赏
  • 举报
回复
文字好抽象
P_ghost 2008-10-21
  • 打赏
  • 举报
回复
这个题目在哪里见过……忘了~~
太乙 2008-10-21
  • 打赏
  • 举报
回复
lz问题解决了??
xuxingok 2008-10-21
  • 打赏
  • 举报
回复
mark
carylin 2008-10-21
  • 打赏
  • 举报
回复
帮顶一下。
peter022 2008-10-21
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#define NUMBER 20

using namespace std;

/*TODO: please fill the return type*/ findPosition(int marks[], int start, int end, bool order)
{
// TODO: if start > end, exit;

int pos = start;

// TODO: find the mininum(ascending) or maximum(descending) data's position in the array
// between the start position and the end position
// Important: the function have to allow the user to choose the
// sort-order by using the parameter: bool order

return pos;
}


// TODO: implement the other overloaded function of findPosition here,
// which takes the string array as parameter instead of integer array


void swap(int& val0, int& val1)
{
// TODO: swap the two variables: val0 and val1
}

// TODO: implement the second overloaded function of findPosition for swaping boolean variables

// TODO: implement the third overloaded function of findPosition for swaping string variables


// Please deduce the default arguments and their order according to
// the given function calls in main function.
// There are six arguments, and the last two of them require
// default arguments.
// Please set the default values as mentioned in the webpage description:
// by default, the data will be sorted by name (value: 1)
// and in ascending order (value: true).

void selectionSort(/* TODO: please fill the arguments here, one of them should be "rankBy" and controls the sorting keyword*/)
{
for(int i=0; i<num-1; i ++)
{
int pos;

switch(rankBy)
{
// TODO: please take both sorting keyword types (by name or by mark) into consideration by calling the function findPosition
// and find the minimum(ascending) or maximum(descending) data's position according the sorting order
}

// TODO: swap the students' records by calling the three overloaded functions: swap
}
}

void getInput(int marks[], string names[], bool flags[], int num)
{
for(int i=0; i<num; i ++)
cin>> names[i] >> marks[i] >> flags[i];
}

void printResult(/* TODO: fill the parameters here*/)
{
// TODO: print the result according to the format in the example
}

int main()
{
int marks[NUMBER];
string names[NUMBER];
bool flags[NUMBER];

// get the input data
getInput(marks, names, flags, NUMBER);

// choose sorting order
bool sorting_order;
char temp_chr;

// set sorting_order to true if the user choose ascending order
// set sorting_order to false if the user choose descending order
cin >> temp_chr;
if('a' == temp_chr || 'A' == temp_chr) sorting_order = true;
else sorting_order = false;

// choose sorting method
// other numbers mean sort the data by name
int sortBy;
cin >> sortBy;

// sort the data according to the parameters
// please pay attention to the default arguments of selectionSort and
// deduce the their values according to the following code
// 1 means sort by name, 0 means sort by mark
// true means ascending order, false means descending order
if(1 == sortBy)
{
if(sorting_order) selectionSort(marks, names, flags, NUMBER);
else selectionSort(marks, names, flags, NUMBER, 1, false);
}
else
{
if(sorting_order) selectionSort(marks, names, flags, NUMBER, 0);
else selectionSort(marks, names, flags, NUMBER, 0, false);

}

// output the result
printResult(marks, names, flags, NUMBER);
}
這個是否詳細一點?
npuhuxl 2008-10-20
  • 打赏
  • 举报
回复
题目好抽象啊,帮忙顶!
帅得不敢出门 2008-10-20
  • 打赏
  • 举报
回复
void swap(int& val0, int& val1)
{
int tmp = val1;
val1 = val0;
val0 = tmp;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧