读书人

求教一个C++的编程小疑点

发布时间: 2012-04-06 12:22:24 作者: rapoo

求教一个C++的编程小问题,顶者有份
C++
/*
编写程序实现一个小型学校人事管理系统。
其中,基类person包含编号、姓名数据成员;派生类学生包含编号、姓名、班号和成绩数据成员;
派生类教师包含编号、姓名、职称和部门数据成员。各个学生以及教师记录按编号递增的顺序分
别存放在学生和教师对象数组中。另外,对于每一类,还需包含信息输入、信息显示、信息修改、
信息删除、信息插入五类功能。
*/


[解决办法]
你总得会点啥吧

[解决办法]
那你是想应付作业呢?还是想学点真本事?
[解决办法]
taodm让他揭贴,分我一半。。。
[解决办法]
那帮不了你了,我现在也不可能再写得出新手级的代码了。不用STL都已经不会写代码了。
[解决办法]
写一个大体的架吧:

class person
{
public:
virtual void MsgInput();
virtual void MsgPrint();
virtual void MsgModify();
virtual void MsgDelete();
virtual void MsgInsert();
protected:
int num;
string name;
}:

class student : public person
{
public:
virtual void MsgInput();
virtual void MsgPrint();
virtual void MsgModify();
virtual void MsgDelete();
virtual void MsgInsert();
private:
int classnum;
float score;
};

class teacher : public person
{
public:
virtual void MsgInput();
virtual void MsgPrint();
virtual void MsgModify();
virtual void MsgDelete();
virtual void MsgInsert();
private:
string department;
string position;
};
[解决办法]
ding
[解决办法]
从互联网上下载 C++入门经典源代码.rar
Project Code文件夹中的代码符合要求。
[解决办法]
/*
***************************************************************
*
*Project.h
*
****************************************************************
*/

// Include files
#include <string>

using std::string;

// Global Const variables. These variables determine
// the maximum lengths of fields and member variables.

const int MAX_FIELD_SIZE= 35;
const int HEADER_SIZE= 15;

const int SURNAME_SIZE= 21;
const int FIRST_NAME_SIZE= 21;
const int ADDRESS_SIZE= 31;
const int CITY_SIZE= 21;
const int STATE_SIZE= 4;
const int ZIP_CODE_SIZE= 7;
const int PHONE_NUMBER_SIZE= 9;

/*
***************************************************************
*
*In this file we declare the member functions and member
*variables of the "Person " base class, as well as it 's
*derived classes.
*
****************************************************************
*/

/*
****************************************************************
*
*The "Person " Class. This is the base class for
*both the Student and Teacher class.
*
****************************************************************
*/

class Person {
private:

// Private member variables
string first_name;
string surname;
string address1;
string address2;
string address3;
string city;
string state;
int zip;
string phone;

// Private virtual member functions
// virtual void set_other_info() = 0;

public:


// Function to get the data for the object
virtual void setup();

// Public Get member functions
string get_first_name() const{ return first_name; }
string get_surname() const { return surname; }
string get_address1() const {return address1; }
string get_address2() const { return address2; }
string get_address3() const { return address3; }
string get_city() const { return city; }
string get_state() const { return state; }
int get_zip() const { return zip; }
string get_phone() const { return phone; }

void display_info() const;
virtual void dump_info(std::ostream& os) const;
virtual void load_info(std::ifstream& is);
bool validate_input(const string& input, bool is_string = true);

// Public Set member functions
void set_first_name();
void set_surname();
void set_address();
void set_city();
void set_state();
void set_zip();
void set_phone();

// Public virtual member functions
virtual void display_other_info() const = 0;
};

/*
****************************************************************
*
*The "Student " Class inherited from the
* "Person " base Class.
*
****************************************************************
*/

class Student : public Person {
private:

// Private member variables
string student_ID;
int grade;

public:
// Function to get the data for the object
virtual void setup();

// Public virtual member functions
virtual void display_other_info() const;
virtual void dump_info(std::ostream& os) const;
virtual void load_info(std::ifstream& is);
};

/*
****************************************************************
*
*The "Teacher " Class inherited from the
* "Person " base Class.
*
****************************************************************
*/

class Teacher: public Person {
private:

// Private member variables
int years_experience;
long salary;

public:
// Function to get the data for the object
virtual void setup();

// Public virtual member functions
virtual void display_other_info() const;
virtual void dump_info(std::ostream& os) const;
virtual void load_info(std::ifstream& is);
};


// Exception class
class create_ex: public std::exception {
private:
string msg;

public:
create_ex(const string& m) { msg = m; }
const char* what() const throw() { return msg.c_str(); }
};

[解决办法]
/*
***************************************************************
*
*Project.cpp
*
****************************************************************
*/

// standard library includes

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <cctype>

// private includes

#include "project.h "
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setw;

/*
***************************************************************
*
*In this file we define and implement the member functions
*of the "Person " class, as well as it 's derived classes.
*
*The derived classes are the "Teacher " class and the "Student "


*class. Only the virtual functions inherited from the
* "Person " class, along with their constructors are defined
*and implemented in this file.
*
****************************************************************
*/

/*
****************************************************************
*
*The "Person " Class Member Functions
*
****************************************************************
*/

// Set member functions for the base Person Class.

void Person::setup() {
// Validate the object, and prompt user to enter values
// for all the attributes.

set_first_name();
set_surname();
set_address();
set_city();
set_state();
set_zip();
set_phone();
}

// This function determines whether the user input should consist of digits,
// alphabetic characters, or a mixture

bool Person::validate_input(const string& input, bool is_string) {
// If is_string is true, we test for characters. If not we test for digits.

if (is_string) {
for (unsigned int i = 0; i < input.length(); i++) {
if (!std::isalpha(input[i])) {
cout < < endl < < "The input contained invalid characters. Try Again. " < < endl;
return false;
}
}
}
else {
for (size_t i = 0; i < input.length(); i++) {
if (!std::isdigit(input[i])) {
cout < < endl < < "The input contained invalid characters. Try Again. " < < endl;
return false;
}
}
}

return true;
}

// Set member functions of the Person Abstract Class

void Person::set_first_name() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (!result) {
cout < < endl < < "Please enter the First Name. " < < endl;
cin > > temp;

if (temp.length() > = FIRST_NAME_SIZE) {
cout < < endl < < "The input must be less than "
< < FIRST_NAME_SIZE < < " characters. " < < endl;
continue;
}

if (validate_input(temp, true))
result = true;
}

// Now we can assign the input variable to the member variable
first_name = temp;
}

void Person::set_surname() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (!result) {
cout < < endl < < "Please enter the Surname. " < < endl;
cin > > temp;

if (temp.length() > = SURNAME_SIZE) {
cout < < endl < < "The input must be less than "
< < SURNAME_SIZE < < " characters. " < < endl;
continue;
}

if (validate_input(temp, true))
result = true;
}

// Now we can assign the input variable to the member variable
surname = temp;
}

void Person::set_address() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (!result) {
cout < < endl < < "Enter line 1 of the street address (or '. ' to exit) " < < endl;
std::ws(cin); // Skip any whitespace left on cin
std::getline(cin, temp, '\n ');

if (temp == ". ")
return;

address1 = temp;
result = true;


}

// Re-initialize result to validate user input
result = false;

while (result == false) {
cout < < endl < < "Enter line 2 of the street address (or '. ' to exit) " < < endl;
std::ws(cin); // Skip any whitespace left on input
std::getline(cin, temp, '\n ');

if (temp == ". ")
return;

address2 = temp;
result = true;
}

// Re-initialize result to validate user input
result = false;

while (result == false) {
cout < < endl < < "Enter line 3 of the street address (or '. ' to exit) " < < endl;
std::ws(cin); // Skip whitepsace on input
std::getline(cin, temp, '\n ');

if (temp == ". ")
return;

address3 = temp;
result = true;
}
}

void Person::set_city() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (result == false) {
cout < < endl < < "Please enter the City. " < < endl;
cin > > temp;

if (temp.length() > = CITY_SIZE ) {
cout < < endl < < "The input must be less than "
< < CITY_SIZE < < " characters. " < < endl;
continue;
}

if (validate_input(temp, true))
result = true;
}

// Now we can assign the input variable to the member variable
city = temp;
}

void Person::set_state() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (result == false) {
cout < < endl < < "Please enter the State. " < < endl;
cin > > temp;

if (temp.length() > = STATE_SIZE ) {
cout < < endl < < "The input must be less than "
< < STATE_SIZE < < " characters. " < < endl;
continue;
}

if(validate_input(temp, true))
result = true;
}

// Now we can assign the input variable to the member variable
state = temp;
}

void Person::set_zip() {
string temp;

// initialize boolean variables to test the user inputs.
bool result = false;

while (result == false) {
cout < < endl < < "Please enter the 6 digit Zip Code. " < < endl;
cin > > temp;

if (temp.length() != ZIP_CODE_SIZE - 1) {
cout < < endl < < "The Zip Code must be 6 digits long. Try Again. " < < endl;
continue;
}

if (validate_input(temp, false))
result = true;
}

// Now we can assign the input variable to the member variable
zip = std::atoi(temp.c_str());
}
[解决办法]
写的太好了。

格式啊,细节啊都值得学习。
[解决办法]
顶啊
[解决办法]
http://community.csdn.net/IndexPage/SmartQuestion.aspx#homework
[解决办法]
又是作业,真行,做作业都知道往这里跑,,
[解决办法]


好难
------解决方案--------------------


我靠,猛人很多阿!
[解决办法]
up
[解决办法]
jf~~up~
[解决办法]
请问你读了这书干吗?
[解决办法]
作业帖,自己做吧。
[解决办法]
自己做~
[解决办法]
路过~~~~~
[解决办法]
和这一篇很像啊!

http://topic.csdn.net/u/20070626/13/fd2a9d94-76fe-4db0-813b-091abaea895e.html
[解决办法]
好难...
[解决办法]
路过
还是得自己学点
以后出来了怎么混饭吃啊?
[解决办法]
帮顶!拿分来
[解决办法]
用STL吧,几个容器就搞定了
[解决办法]
路过!
[解决办法]
路过,看看
[解决办法]
帮顶!拿分来
[解决办法]
从基础做起,应该网上有呢
[解决办法]
顶者有分
[解决办法]
dddddd
[解决办法]
up下
[解决办法]
JF
[解决办法]
用STL,几个容器就搞定??

这么简单为什么在学校里老师还教我们那么麻烦的办法呢??

读书人网 >C++

热点推荐