+ -
当前位置:首页 → 问答吧 → C++急问......

C++急问......

时间:2014-01-23

来源:互联网

(1) Alphabetical order (A to Z): Display the student list in alphabetical order (A to Z) of
student names. If there are multiple students having the same name, the one with smaller
student ID is listed first. The list is displayed with the format below:
Student ID Student Name Engineering Science
-----------------------------------------------------------------
12345678A Chan Tai Man, Peter 24 17 (Granted)
12333344A Lee Mei Lai 19 (Rejected) 23
14556678A Ng Fat Tat 19 (Rejected) 17 (Rejected)
13982646A Wong Ling Ling, Mary 22 (Granted) 18 (Rejected)

有无人知道其实系点打个CODE?

作者: 0天疯   发布时间: 2014-01-23

有冇code??

作者: Susan﹏汪汪   发布时间: 2014-01-23

#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<string>
#include<cstring>

using namespace std;
const int MAXSTUD = 1000;
int arraysize[MAXSTUD] = {};
int counta;
int num = 0;

class Student
{
public:
Student ()
{ //initialize the number of subjects student have.
count = 0; //count the no. of subjects student have.
}
void Inputfile(char words[]);

void Showdata(){
for (unsigned int i = 0;i < strlen(name);i++)
cout << name;
cout << '\t';
for (int i = 0;i < 9;i++)
cout << ID;
for (int i = 0;i < count;i++)
{
cout << '\t';
for (int j = 0;j < 6;j++)
cout << subcode[j];
cout << '\t';
for (int j = 0;j < 2;j++)
cout << grade[j];
}
}
void Switchdata()
{
if ( counta == 0)
for (unsigned int i = 0;i < strlen(name);i++)
before = name;
else {
for (unsigned int i = 0;i < strlen(name);i++)
after = name;
int i = 0;
for (; before == after; i++)
;
if (before > after)
{
int hold = 0;
hold = arraysize[counta - 1];
arraysize[counta - 1] = arraysize[counta];
arraysize[counta] = hold;
}
else if (before < after)
;
else
cout << "Error: There are two students having the same name.";

for (int i = 0;i < 30;i++)
before = after;
}
}

void List_name()
{
for (int i = 0; i < 9; i++)
cout << ID;
for (int i = 0; i < 6; i++)
cout << " ";
for (unsigned int i = 0; i < strlen(name); i++)
cout << name;
for (unsigned int i = 0; i < 25 - strlen(name); i++)
cout << " ";
calculate_GPA();
cout << fixed << setprecision(2) << GPA << endl;
}

void calculate_GPA()
{
int Total_Weight = 0;
double Total_marks = 0;
int weight = 0;
for (int i = 0;i < count;i++)
{
if (subcode[2] == '1')
{
if(subcode[5] == '1' || subcode[5] == '2' || subcode[5] == '3' ||
subcode[5] == '4' || subcode[5] == '5')
weight = 1;
Total_Weight += 1;
}
else if (subcode[2] == '2')
{
if (subcode[5] == '1' || subcode[5] == '2' || subcode[5] == '3' ||
subcode[5] == '4' || subcode[5] == '5')
weight = 2;
Total_Weight += 2;
}
else if (subcode[2] == '3')
{
if (subcode[5] == '1' || subcode[5] == '2' || subcode[5] == '3' ||
subcode[5] == '4' || subcode[5] == '5')
weight = 3;
Total_Weight += 3;
}
else
cout << "Error: Subject does not exists.";

if (grade[0] == 'A' && grade[1] == '+')
Total_marks += 4.5 * weight;
else if (grade[0] == 'A' && grade[1] == '\0')
Total_marks += 4 * weight;
else if (grade[0] == 'B' && grade[1] == '+')
Total_marks += 3.5 * weight;
else if (grade[0] == 'B' && grade[1] == '\0')
Total_marks += 3 * weight;
else if (grade[0] == 'C' && grade[1] == '+')
Total_marks += 2.5 * weight;
else if (grade[0] == 'C' && grade[1] == '\0')
Total_marks += 2 * weight;
else if (grade[0] == 'D' && grade[1] == '+')
Total_marks += 1.5 * weight;
else if (grade[0] == 'D' && grade[1] == '\0')
Total_marks += 1 * weight;
else if (grade[0] == 'F' && grade[1] == '\0')
Total_marks += 0 * weight;
else
cout << "Error: there is no such grade.";

}
GPA = Total_marks / Total_Weight;
}
int Getnamelen()
{
return strlen(name);
}
char Getname(int ptr) //get the name to the main program
{
return name[ptr];
}
char GetID(int ptr) //get the ID to the main program
{
return ID[ptr];
}
char Getsubcode(int subno,int ptr) //get the subject code to the main program
{
return subcode[subno][ptr];
}
char Getgrade(int gradeno,int ptr) //get the grade to the main program
{
return grade[gradeno][ptr];
}
int Getcount() //get the number of subjects to the main program
{
return count;
}
double GetGPA() //get the GPA to the main program
{
return GPA;
}
void Changename(char x[],int length)
{
for (int i = 0;i < length;i++)
name = x;
}
void ChangeID(char x[])
{
for (int i = 0;i < 9;i++)
ID = x;
}
void Changesubcode(int subno,int ptr,char x)
{
subcode[subno][ptr] = x;
}
void Changegrade1(int subno,char x[])
{
for (int i = 0;i < 2;i++)
grade[subno] = x;
}
void Changegrade2(int gradeno,int ptr,char x)
{
grade[gradeno][ptr] = x;
}
void Changecount(int no)
{
count = no;
}
void Delete()
{
for (int i = 0;i < 30;i++)
name = '\0';
for (int i = 0;i < 9;i++)
ID = '\0';
for (int i = 0;i < 15;i++)
{
for (int j = 0;j < 6;j++)
subcode[j] = '\0';
for (int j = 0;j < 2;j++)
subcode[j] = '\0';
}
count = 0;
GPA = 0;
}
private:
char name[30]; //Student Name
char ID[9]; //StudentID
char subcode[15][6]; //15 subject code array with 6 character.
char grade[15][2]; //15 subject grade array with 2 character.
int count; //counting the number of subject.
double GPA; //storing the GPA of the student.
};

Student stud[MAXSTUD];

int main()
{

return 0;
}

void Alphabetical_order() //R13
{
system("CLS");
cout << left << endl << endl << setw(13) << "Student ID" << left << setw(23) << "Student Name"
<< left << setw(16) << "Engineering" << left << setw(13) << "Science" << endl;
for (int i = 0; i < 65; i++)
cout << "-";
cout << endl;
for (int i = 0; i < num; i++)
arraysize = i;
for (int j = 0;j < num - 1;j++)
{
for (counta = 0; counta < num ; counta++)
{
stud[arraysize[counta]].Switchdata();
}
counta = 0;
}

for (int i = 0; i < num ; i++)
{
cout << setw(18) << ' ';
stud[arraysize].List_name();
}
cout << endl;




system ("pause");
printARSM();
printARSMinput();

}

作者: 0天疯   发布时间: 2014-01-23

用 [ code ] tag 显示程式码,宜家好难睇。
另外,你遇到乜嘢问题?读唔到资料?输出出错?排序出错?

作者: fitcat07   发布时间: 2014-01-23

void Alphabetical_order() //R13
{
system("CLS");
cout << left << endl << endl << setw(13) << "Student ID" << left << setw(23) << "Student Name"
<< left << setw(16) << "Engineering" << left << setw(13) << "Science" << endl;
for (int i = 0; i < 65; i++)
cout << "-";
cout << endl;
for (int i = 0; i < num; i++)
arraysize = i;
for (int j = 0;j < num - 1;j++)
{
for (counta = 0; counta < num ; counta++)
{
stud[arraysize[counta]].Switchdata();
}
counta = 0;
}

for (int i = 0; i < num ; i++)
{
cout << setw(18) << ' ';
stud[arraysize].List_name();
}
cout << endl;




system ("pause");
printARSM();
printARSMinput();

}

上面果PART...只系出到排序A至Z 跟大细
但系我想要如果个学生名头果一个英文字母系 = 其他学生第一个英文字母既话 就跟ID大细排
E.G CHAN KA CHUN && CHAN XXXX
两个名开首一样 就转用学生ID...
想问一下系咪用IF ELSE STATEMENT...如果系应该点打...唔识...

作者: 0天疯   发布时间: 2014-01-23

太乱

方法一
如果只是排第一个字母
只需要比较第一个char
然后比较ID就是了

方法二
预先按ID排列的话
只需要用Name第一个char做比较...做一次stable sorting

[ 本帖最后由 Susan﹏汪汪 於 2013-11-28 08:12 PM 编辑 ]

作者: Susan﹏汪汪   发布时间: 2014-01-23

似功课?多口一问,比答案好唔好?
Algorithm 同 program,楼主边方面有问题?

睇问题,algorithm应该唔难。冇写开C++,用perl应该几个钟写完。

作者: tonii   发布时间: 2014-01-23

见你用 bubble sort 根据 name 去排序, 其实只要转一转个 sorting key 就掂.

Input:
CHAN SHUI 2013012035
LO DAVID 2013012034
CHAN SIU 2013012033

Output:
CHAN SIU 2013012033
CHAN SHUI 2013012035
LO DAVID 2013012034

Current Algorithm:
sorting_key = name

New Algorithm:
sorting_key = surname + student id (eg. CHAN2013012033)
引用:原帖由 0天疯 於 2013-11-28 13:58 发表
void Alphabetical_order() //R13
{
system("CLS");
cout

作者: a8d7e8   发布时间: 2014-01-23

热门下载

更多