Thursday, July 12, 2012
Sample code C++ Score Student Management
You just copy this source code and you can run it:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
const size=100;
class Student{
private:
int id;
char name[25];
char sex[7];
char address[25];
char grade[10];
float mat, phy, khmer;
float total, avg;
int rank;
public:
Student(){
static int autoNum=1;
id =autoNum;
autoNum++;
}
void read(){
cout<<"ID\t"<<getID();cout<<endl<<endl;
cout<<"Name\t"; gets(name);cout<<endl;
cout<<"Sex\t"; gets(sex); cout<<endl;
cout<<"Address\t"; gets(address); cout<<endl;
cout<<"Mat\t"; cin>>mat;cout<<endl;
cout<<"Phy\t";cin>>phy;cout<<endl;
cout<<"Khmer\t";cin>>khmer;cout<<endl;
total=mat+phy+khmer;
avg=total/3.0; grad();
}
void grad(){
int avg;
avg = total/3.0;
if(avg > 90){
strcpy(grade,"A");
}else if(avg > 80){
strcpy(grade,"B");
}else if(avg > 70){
strcpy(grade,"C");
}else if(avg > 60){
strcpy(grade,"D");
}else if(avg > 50){
strcpy(grade,"E");
}else{
strcpy(grade,"F");
}
}
void tableHeader(){
cout<<"ID\tName\tSex\tAddress\tMat\tPhy\tKhmer\tTotal"
<<"\tAverage\tGrade\tRank"<<endl;
}
void display(){
cout<<getID()<<"\t"<<name<<"\t"<<sex<<"\t"<<address<<"\t"<<mat<<"\t"
<<phy<<"\t"<<khmer<<"\t"<<total<<"\t"<<avg<<"\t"
<<grade<<"\t"<<rank<<endl;
}
void setData(Student obj){
this->id=obj.id;
strcpy(this->name, obj.name);
strcpy(this->sex, obj.sex);
strcpy(this->address,obj.address);
stpcpy(this->grade,obj.grade);
this->mat=obj.mat;
this->phy=obj.phy;
this->khmer=obj.khmer;
this->total=obj.total;
this->avg=obj.avg;
}
void insertName(){
cout<<"Input New Name: "; gets(name);
}
void insertSex(){
cout<<"Input New Sex: "; gets(sex);
}
void insertAddress(){
cout<<"Address: ";gets(address);
}
double insertMath(){
cout<<"Math:";cin>>mat;
return mat;
}
double insertPhy(){
cout<<"Phy:";cin>>phy;
return phy;
}
double insertKhmer(){
cout<<"Khmer:";cin>>khmer;
return khmer;
}
void insertScore(int value,int id_num){
double math,ph,kh;
if(value==1 && id==id_num){
math=insertMath();
total=math+phy+khmer;
avg=total/3.0;
grad();
}else if(value==2 && id==id_num){
ph=insertPhy();
total=mat+ph+khmer;
avg=total/3.0;
grad();
}else if(value==3 && id==id_num){
kh=insertKhmer();
total=mat+phy+kh;
avg=total/3.0;
grad();
}
}
void insertScores(){
cout<<"Mat: "; cin>>mat;
cout<<"Phy: "; cin>>phy;
cout<<"Khmer: "; cin>>khmer;
total=mat+phy+khmer;
avg=total/3.0;
grad();
}
void setRank(int n){
rank=n;
}
float getTotal(){
return total;
}
float getAverage(){
return avg;
}
float getID(){
return id;
}
char *getName(){
return name;
}
char *getSex(){
return sex;
}
char *getAddress(){
return address;
}
float getMat(){
return mat;
}
float getPhy(){
return phy;
}
float getKhmer(){
return khmer;
}
char *getGrade(){
return grade;
}
int getRank(){
return rank;
}
};
void insert(Student obj[], int d){
obj[d].read();
}
void rank(Student obj[],int n);
void sortByAverage(Student obj[], int n);
void sortByID(Student obj[], int n);
void sortByName(Student obj[], int n);
void searchByID(Student obj[], int n);
void searchByName(Student obj[], int n);
void searchByRank(Student obj[], int n);
void deleteRecord(Student obj[], int d, int n);
void Update(Student stu[], int n);
void rank(Student obj[],int n){
int rank=1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(obj[i].getTotal()<obj[j].getTotal()){
++rank;
}
}
obj[i].setRank(rank);
rank=1;
}
}
void sortByAverage(Student stu[], int n){
int i, j, ord;
Student stuTemp;
cout<<endl<<"1.Sort by acending"<<endl;
cout<<"2.Sort by descending";cin>>ord;
if(ord == 1) {
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(stu[i].getAverage() < stu[j].getAverage()){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
if(ord==2){
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(stu[i].getAverage() > stu[j].getAverage()){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
}
void sortByID(Student stu[], int n){
int i, j, ord;
Student stuTemp;
cout<<"1.Sort by ascending"<<endl;
cout<<"2.Sort by descending"; cin>>ord;
if(ord == 1) {
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(stu[i].getID() > stu[j].getID()){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
if(ord==2){
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(stu[i].getID() < stu[j].getID()){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
}
void sortByName(Student stu[], int n){
int ord;
Student stuTemp;
cout<<"1.Sort by ascending"<<endl;
cout<<endl<<"2.Sort by descending"; cin>>ord;
if(ord==1){
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(strcmp(stu[i].getName(), stu[j].getName()) > 0){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
if(ord==2){
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(strcmp(stu[i].getName(), stu[j].getName()) < 0){
stuTemp = stu[i];
stu[i] = stu[j];
stu[j] = stuTemp;
}
}
}
void searchByID(Student stu[], int n){
int search; char ch; int j;
cout<<endl<<"Search By ID: "; cin>>search;
for(int i=0; i<n; i++)
if(search==stu[i].getID())
{
ch='a';
cout<<endl; j=0;
if(j==0)
stu[i].tableHeader();
cout<<stu[i].getID()<<"\t"<<stu[i].getName()<<"\t"
<<stu[i].getSex()<<"\t"<<stu[i].getAddress()<<"\t"
<<stu[i].getMat()<<"\t"<<stu[i].getPhy()<<"\t"<<stu[i].getKhmer()
<<"\t"<<stu[i].getTotal()<<"\t"<<stu[i].getAverage()
<<"\t"<<stu[i].getGrade()<<"\t"<<stu[i].getRank()<<endl;
}
if(ch=='a')
cout<<endl<<"Search sucessful! "<<endl;
else cout<<endl<<"No ID in record! "<<endl;
}
void searchByName(Student stu[], int n){
char name[25], ch;
cout<<endl<<"Search By Name: "; gets(name);
cout<<endl;stu[0].tableHeader();
for(int i=0; i<n; i++)
if(strcmp(name, stu[i].getName())==0)
{
ch='b';
cout<<stu[i].getID()<<"\t"<<stu[i].getName()<<"\t"
<<"\t"<<stu[i].getSex()<<"\t"<<stu[i].getAddress()<<"\t"
<<stu[i].getMat()<<"\t"<<stu[i].getPhy()<<"\t"<<stu[i].getKhmer()
<<"\t"<<stu[i].getTotal()<<"\t"<<stu[i].getAverage()
<<"\t"<<stu[i].getGrade()<<"\t"<<stu[i].getRank()<<endl;
}
if(ch=='b')
cout<<endl<<"Search sucessful! "<<endl;
else cout<<endl<<"No Name in record! "<<endl;
}
void searchByRank(Student stu[], int n){
int rank; char ch;
cout<<endl<<"Search By Rank: "; cin>>rank;
cout<<endl;stu[0].tableHeader();
for(int i=0; i<n; ++i)
if(rank==stu[i].getRank()){
ch='d';
cout<<stu[i].getID()<<"\t"<<stu[i].getName()<<"\t"
<<stu[i].getSex()<<"\t"<<stu[i].getAddress()<<"\t"<<stu[i].getMat()<<"\t"
<<stu[i].getPhy()<<"\t"<<stu[i].getKhmer()
<<"\t"<<stu[i].getTotal()<<"\t"<<stu[i].getAverage()
<<"\t"<<stu[i].getGrade()<<"\t"<<stu[i].getRank()<<endl;
}
if(ch=='d')
cout<<endl<<"Search sucessful! "<<endl;
else cout<<endl<<"No Rank in record! "<<endl;
}
void deleteRecord(Student stu[],int d, int n){
d = d - 1;
for(int i=0;i<n;i++){
if(stu[i].getID()==stu[d].getID())
for(int j=i;j<n;j++){
stu[j].setData(stu[j+1]);
}
}
cout<<"delete sucessful!";
}
void Update(Student stu[], int n){
int search,a, b;
cout<<"Update ID: "; cin>>search;
/* if(cin.peek()!=10 || cin.good()!=1)
{
cin.clear();
char ch[100];
cin.getline(ch,100);
} */
for(int i=0; i<n; i++)
if(search==stu[i].getID()){
cout<<endl<<"1.Update name"<<endl;
cout<<"2.Update sex"<<endl;
cout<<"3.Update scores"<<endl;
cout<<endl<<"Please select menu:";cin>>a;
cout<<endl<<endl;
/* if(cin.peek()!=10 || cin.good()!=1)
{
cin.clear();
char ch[100];
cin.getline(ch,100);
} */
switch(a){
case 1:
stu[i].insertName(); break;
case 2:
stu[i].insertSex(); break;
case 3:
clrscr();
cout<<"Subject to Update:"<<endl;
cout<<"--------------------";
cout<<endl<<"1.Update Math:"<<endl;
cout<<"2.Update Phy:"<<endl;
cout<<"3.Update Khmer:"<<endl;
cout<<"4.Update All"<<endl;
cout<<endl<<"Please select menu:";cin>>b;
cout<<endl<<endl;
/*if(cin.peek()!=10 || cin.good()!=1)
{
cin.clear();
char ch[100];
cin.getline(ch,100);
} */
switch(b){
case 1:
stu[i].insertScore(b,search);
break;
case 2:
stu[i].insertScore(b,search);
break;
case 3:
stu[i].insertScore(b,search);
break;
case 4:
stu[i].insertScores();
break;
default:
if(b<=0 || b>=5)
{
//goto again;
}
}
break;
default:
cout<<"Press Try again!";
}
}
}
void main(){
Student stu[size];
int i, x,w; int n=0; int d;
again: clrscr();
gotoxy(35,2); cout<<"Score System Management";
gotoxy(30,3); cout<<"========================";
gotoxy(35,5); cout<<"1. Insert";
gotoxy(35,6); cout<<"2. Udate";
gotoxy(35,7); cout<<"3. Search";
gotoxy(35,8); cout<<"4. delet";
gotoxy(35,9); cout<<"5. sort";
gotoxy(35,10); cout<<"6. display";
gotoxy(35,11); cout<<"7. Exit"<<endl;
cout<<"Please input number(1-7) of menu: "; cin>>x;
cout<<endl;
if(cin.peek()!=10 || cin.good()!=1){
cin.clear();
char ch[100];
cin.getline(ch,100);
goto again;
}
switch(x){
case 1:
clrscr();
if(n>size){
cout<<"Your memory is full! ";
getch(); goto again;
}
insert(stu,n); cout<<endl;
n=n+1; goto again;
case 2:
if(n<=0){
cout<<"Please insert data first!";
getch(); goto again;
}
Update(stu, n);
getch(); goto again;
case 3:
if(n<=0){
cout<<"Please insert data first!";
getch(); goto again;
}
cout<<endl;
menusearch:clrscr();
gotoxy(29,1);cout<<"With one do you want to search"<<endl;
gotoxy(27,2);cout<<"==================================="<<endl;
gotoxy(30,3);cout<<"1.search by ID"<<endl;
gotoxy(30,4);cout<<"2.search by Name"<<endl;
gotoxy(30,5);cout<<"3.search by Rank"<<endl<<endl;
gotoxy(30,6);cout<<"Please select menu: ";cin>>w;
if(cin.peek()!=10 || cin.good()!=1){
cin.clear();
char ch[100];
cin.getline(ch,100);
goto menusearch;
}
switch(w){
case 1:
searchByID(stu, n);
getch(); goto again;
case 2:
searchByName(stu, n);
getch(); goto again;
case 3:
searchByRank(stu, n);
getch(); goto again;
default:
if(w<=0 || w>=5){
goto menusearch;
}
}
case 4:
if(n<=0){
cout<<"Please insert data first!";
getch(); goto again;
}else
{
cout<<"Delete ID: "; cin>>d;
if(d>n || d<=0){
getch(); goto again;
}else{
deleteRecord(stu,d,n);
n=n-1;
getch(); goto again;
}
}
case 5:
if(n<=0){
cout<<"Please insert data first!";
getch(); goto again;
}cout<<endl;
menusort:clrscr();
gotoxy(29,1);cout<<"With one do you want to Sort"<<endl;
gotoxy(27,2);cout<<"==================================="<<endl;
gotoxy(30,3);cout<<"1.Sort by ID"<<endl;
gotoxy(30,4);cout<<"2.Sort by Name"<<endl;
gotoxy(30,5);cout<<"3.Sort by Average"<<endl<<endl;
gotoxy(30,6);cout<<"Please select menu: ";cin>>x;
if(cin.peek()!=10 || cin.good()!=1){
cin.clear();
char ch[100];
cin.getline(ch,100);
goto menusort;
}
switch(x){
case 1:
sortByID(stu, n);
getch(); goto again;
case 2:
sortByName(stu, n);
getch(); goto again;
case 3:
sortByAverage(stu, n);
getch(); goto again;
default:
if(x<=0 || x>=4){
goto menusort;
}
}
case 6:
if(n<=0){
cout<<"Please insert data first!"<<endl;
getch(); goto again;
}else{
stu[0].tableHeader();
for(i=0; i<n; i++){
rank(stu,n);
stu[i].display();
}cout<<"\n\n\tPress any key to continue...";
getch();
goto again;
}
case 7: clrscr();
gotoxy(35,2); cout<<"Member of Group";
gotoxy(30,3); cout<<"========================";
gotoxy(35,5); cout<<"1.VIN SAVRY";
gotoxy(35,7); cout<<"2.PHY SOPEAK";
gotoxy(35,9); cout<<"3.TUN SOMANITA";
gotoxy(35,11); cout<<"4.VIN KIAT";
gotoxy(35,13); cout<<"5.CHIM CHANPUN";
gotoxy(35,15); cout<<"6.CHUM RITEKAN"<<endl;
gotoxy(25,17); cout<<"Press any key to exit program...";
getch();exit(0);
default:
if(x<=0 || x>=8)
{
goto again;
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment