PDA

View Full Version : sừa giúp minh bài về lớp bạn



macnhuocphi
24-04-2009, 20:25
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
int n;
class nguoi
{
protected:
char ten[30];
char dc[30];
char dt[30];
public:
void into1(char *s1,char *s2,char *s3)
{
strcpy(ten,s1);
strcpy(dc,s2);
strcpy(dt,s3);
}
void show1()
{
cout<<ten<<" "<<dc<<" "<<dt;
}
};
class sv:public nguoi
{
char ma[30];
char lop[30];
char khoa[30];
public:
void into2(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6)
{
into1(s1,s2,s3);
strcpy(ma,s4);
strcpy(lop,s5);
strcpy(khoa,s6);
}
void show2()
{
show1();
cout<<" "<<ma<<" "<<lop<<" "<<khoa;
}
friend class diem;
};
class diem
{
private:
sv a;
float diem;
char loai[30];
public:
void into3(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6)
{
a.into2(s1,s2,s3,s4,s5,s6);
}
void show3()
{
a.show2();
}
void intod(float d)
{
diem=d;
xeploai();
}
void xeploai()
{
if(diem<5) strcpy(loai,"yeu");
else if(diem<6) strcpy(loai,"tb");
else if(diem<8) strcpy(loai,"kha");
else strcpy(loai,"gioi");
}
}ds[100];

void nhap()
{
fstream fl("sv.DAT", ios::out|ios::binary);
char s1[30],s2[30],s3[30],s4[30],s5[30],s6[30];
cout<<"nhap so sv can quan ly";
cin>>n;
fl.write((char*)(&n),sizeof(n));
for(int i=0;i<n;i++)
{
cout<<"nhap ten "; cin.ignore(); cin.getline(s1,30);
cout<<"nhap dia chi"; cin.getline(s2,30);
cout<<"nhap dien thoai"; cin.getline(s3,30);
cout<<"nhap ma "; cin.getline(s4,30);
cout<<"nhap lop"; cin.getline(s5,30);
cout<<"nhap khoa"; cin.getline(s6,30);
ds[i].into3(s1,s2,s3,s4,s5,s6);
fl.write((char*)(&ds[i]),sizeof(ds));
}
fl.close();
}
void diemsv()
{
fstream fl("sv.DAT", ios::in|ios::binary);
fstream fi("bd.DAT", ios::out|ios::binary);
float d;
for(int i=0;i<n;i++)
{
ds[i].show3();
cout<<"nhap diem cua sv thu "<<i+1;
cin>>d;
ds[i].intod(d);
}
}
void main()
{
clrscr();
nhap(); diemsv();
}


sửa giúp mình bài này với lúc ấn ctrf f9 nó hiện ra mấy cãi lỗi ji ý

binprolab
25-04-2009, 14:24
bài này của bạn có khá nhiều lỗi nhưng chủ yếu là lỗi đọc và ghi file. Không biết bạn dùng trình biên dịch gì để code bài này. Bạn nên sử dụng dev_c++. Mình đang cố gắng sửa cho bạn một số lỗi. Sẽ post lên sau

[=========> Bổ sung bài viết <=========]

bạn có thể tham khảo bài sau:


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Student {
int code;
string firstname;
string lastname;
int gender;
string address;
int n;
int grade[6];
string subject;
double avg;
public:
friend class ClassMan; // help class
friend class GradeMan; // help class
void inputByUser() {
cout << "Student code: ";
cin >> code;
cin.ignore(256,'\n');
cout << "First name: ";
getline(cin,firstname);
cout << "Last name: ";
getline(cin,lastname);
cout << "Gender: ";
cin >> gender;
cin.ignore(256,'\n');
cout << "Address: ";
getline(cin,address);
}
// doc thong tin sinh vient u file
void inputByStream(ifstream &fi) {
fi >> code;
getline(fi,firstname);
getline(fi,firstname);
getline(fi,lastname);
fi >> gender;
getline(fi,address);
getline(fi,address);
}
// hien thi thong tin sinh vien ra man hinh
void display() {
cout << "code : " << code << endl;
cout << "firstname : " << firstname << endl;
cout << "lastname : " << lastname << endl;
cout << "gender : " << gender << endl;
cout << "address : " << address << endl;
cout << endl;
}
// ghi thong tin sinh vien ra file
void outputToStream(ofstream &fo) {
fo << code << endl;
fo << firstname << endl;
fo << lastname << endl;
fo << gender << endl;
fo << address << endl;
fo << endl;
}
// operator
bool operator<(const Student &b) const {
return code < b.code;
}
Student& operator=(const Student &b) {
code = b.code;
firstname = b.firstname;
lastname = b.lastname;
gender = b.gender;
address = b.address;
return *this;
}
// nhap diem cho sinh vien theo mon hoc
void inputGradeBySubject(char &S) {
int a;
for (int i=0; i<6; i++)
if (subject[i] == S) {
cout << firstname << ": ";
cin >> a;
grade[i] = a;
}
}
// nhap diem cho sinh vien theo ma so
void inputGradeByStudent() {
int g;
for (int i=0; i<6; i++) {
cout << subject[i] << ": ";
cin >> grade[i];
}
}
// ghi diem sinh vien ra file
void outputgrade(ofstream &fo) {
for (int i=0; i<6; i++)
fo << subject[i] << " " << code << " " << grade[i] << endl;
}
// hien thi diem sinh vien ra man hinh
void displayGrade() {
cout << setfill(' ') << setw(3) << right << code
<< " " << setfill(' ') << setw(10) << left << firstname
<< " " << setfill(' ') << setw(20) << left << lastname;
getAVG();
for(int i=0;i<6;i++)
cout << setfill(' ') << setw(5) << right << grade[i];
cout << setw(6) << right << setprecision(3) << avg;
cout << endl;
}
// doc thong tin diem sinh vien tu` file
void inputGradeFromStream(ifstream &fi, int c) {
int temp_code;
char temp_subject;
int g;
int x = c*6;
for (int i=0; i<6; i++)
setGrade(subject[i],-1);
for (int i=0;i<x; i++) {
fi >> temp_subject >> temp_code >> g;
if(code==temp_code)
setGrade(temp_subject,g);
}
}
// set mon hoc
void setSubject() {
subject = "MPCGHL";
}

// default constructor
Student() {
setSubject();
}
// set diem
void setGrade(char a, int b) {
for (int i=0; i<6; i++)
if (a==subject[i])
grade[i]=b;
}
// tinh diem trung bunh
void getAVG() {
int temp, total;
avg=0;
temp = 0;
total = 0;
for (int i=0;i<6;i++)
if (grade[i]>0) {
total = total + grade[i];
temp++;
}
if (temp!=0)
avg = (float)total/temp;
else
avg = 0;
}
};
class ClassMan {
Student *students;
string classname;
int n;
public:
ClassMan(){ // default constructor
classname = "";
}
ClassMan(string &s){ // assignment constructor
classname = string("list-") + s + ".txt";
}
void menu() { // Menu lua chon
input();
int choice;
do {
system("cls");
cout << " 1. List student" << endl;
cout << " 2. Add student" << endl;
cout << " 3. Edit student" << endl;
cout << " 4. Exit" << endl;
cout << "Please 1,2,3 or 4 to exit: ";
cin >> choice;
switch (choice) {
case 1: {
system("cls");
for (int i=0; i<n; i++)
students[i].display();
system("pause");
break;
}
case 2: {
Student *old = students;
n++;
students = new (nothrow) Student[n];
for(int i=0; i<n-1; i++)
students[i] = old[i];
students[n-1].inputByUser();
delete [] old;
break;
}
case 3: {
int k,temp,m;
cout << "please enter student code to edit: ";
cin >> temp;
for (int i=0; i<n; i++)
if (students[i].code == temp) k=i;
students[k].inputByUser();
break;
}
case 4: {
output();
break;
}
}
} while (choice !=4);
}
void input(){
ifstream fi(classname.c_str());
n = 0;
fi >> n;
students = new (nothrow) Student[n];
for (int i=0;i<n;i++)
students[i].inputByStream(fi);
fi.close();
}
void output(){
ofstream fo(classname.c_str());
fo << n << endl;
sort(students, students+n);
for (int i=0;i<n;i++)
students[i].outputToStream(fo);
fo.close();
}
friend class GradeMan;
virtual ~ClassMan() {
delete [] students;
}
};
class GradeMan {
string classname;
string classfilename;
ClassMan Cl;
int n;
public:
friend class ClassMan;
GradeMan() {
classname = '\0';
classfilename = '\0';
}
GradeMan(string &s) : Cl(s) {
Cl.input();
n = Cl.n;
classname = string("grade-")+s+".txt";
}
void menu() {
char choice;
input();
do {
system("cls");
cout << "1. List grade by class" << endl;
cout << "2. Input grade by subject" << endl;
cout << "3. Input grade by student" << endl;
cout << "4. Exit" << endl;
cout << " please 1, 2, 3, or 4 to exit: ";
cin >> choice;
switch (choice) {
case '1': {
for (int i=0; i<n; i++)
Cl.students[i].displayGrade();
system("pause");
break;
}
case '2': {
inputBySubject();
break;
}
case '3': {
inputByStudent();
break;
}
case '4':
output();
break;
}
}
while (choice!='4');
}
void input() {
for (int i=0; i<n; i++) {
ifstream fi(classname.c_str());
Cl.students[i].inputGradeFromStream(fi,n);
fi.close();
}
}
void inputBySubject() {
char s;
cout << "Nhap diem cho mon : ";
cin >> s;
for (int j=0; j<n; j++)
Cl.students[j].inputGradeBySubject(s);
}
void inputByStudent() {
int k;
cout << "Please enter student code: ";
cin >> k;
for (int i=0; i<n; i++)
if(Cl.students[i].code == k)
Cl.students[i].inputGradeByStudent();
}
void output() {
ofstream fo(classname.c_str());
for (int i=0; i<n; i++)
Cl.students[i].outputgrade(fo);
fo.close();
}
};
int main() {
char choice;
string clname;
do {
system("cls");
cout << "========Main Menu========" << endl;
cout << " 1. Class manager" << endl;
cout << " 2. Grade manager" << endl;
cout << " 3. Exit" << endl << endl;
cout << " Please 1,2 or 3 to exit: ";
cin >> choice;
switch(choice) {
case '1': {
cin.ignore(256,'\n');
cout << "Please enter the class's name: " << endl;
cin >> clname;
string filename = string("list-") + clname + ".txt";
ClassMan A(clname);
fstream filestr;
filestr.open(clname.c_str());
if(filestr.is_open() == false) {
cout << "chua co khoi tao file" << endl;
system("pause");
ofstream fo(clname.c_str());
fo.close();
}
A.menu();
break;
}
case '2': {
cin.ignore(256,'\n');
cout << "Please enter the class's name: " << endl;
cin >> clname;
GradeMan A(clname);
A.menu();
break;
}
case '3':
break;
}
} while (choice != '3');
cout << endl;
}



[=========> Bổ sung bài viết <=========]

tuy bài này nhìn có vẻ khác bài của bạn một chút nhưng hầu như không khác gì là mấy. Bài trên gồm 3 class Student: quản lý sinh viên, GradeMan: quản lý điểm. Hi vọng bạn sẽ vận dụng được bài của mình vào bài của bạn. Chúc bạn thành công!

macnhuocphi
25-04-2009, 19:55
đọc ghi file là kiểu nhị phân

dell1400
18-07-2009, 00:34
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
int n;
class nguoi