C:简单的学生信息处理程序实现

描述

在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的。

(注:评测系统无法自动判断变量是否私有。我们会在结束之后统一对作业进行检查,请同学们严格按照题目要求完成,否则可能会影响作业成绩。)

输入姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。

其中姓名、学号为字符串,不含空格和逗号;年龄为正整数;成绩为非负整数。

各部分内容之间均用单个英文逗号","隔开,无多余空格。输出一行,按顺序输出:姓名,年龄,学号,四年平均成绩(向下取整)。

各部分内容之间均用单个英文逗号","隔开,无多余空格。样例输入

Tom,18,7817,80,80,90,70

样例输出

Tom,18,7817,80

 

Analysis:

how to let the string become stream, and split the stream with ',':

 

this is my first OOP's code, At the first time I think it will be easy, but when i was trying to solve it I found it is so difficute. The main question is that I am not familar with Object-Oriented Programming.

this is my code:

#include<bits/stdc++.h>
#include<iostream>

using namespace std;

class Student {
    string name;
    int age;
    int number;
    int grade[4];
    int averageSocre;

    public:
        void calculateAverageSocre() {
            int sum = 0;
            for (int i = 0; i < 4; ++i) {
                sum += grade[i];
            }
            averageSocre = (sum / 4);
        }

        int print() {
            cout << name << ',' << age << ',' << number << ',' << averageSocre << endl;
        }

        void init(string str) {
            stringstream ss;
            ss << str;
            string token;
            getline(ss, token, ',');
            name = token;
            getline(ss, token, ',');
            age = stoi(token);
            getline(ss, token, ',');
            number = stoi(token);
            int index = 0;
            while (getline(ss, token, ',')) {
                grade[index++] = stoi(token);
            }
       }
};

int main() {
    Student A;

    string str;

    cin >> str;

    A.init(str);
    A.calculateAverageSocre();
    A.print();

    return 0;
}

  

The below code are more better:

/* by Guo Wei
 个人习惯:类名和函数名首字母大写,变量名第一个单词小写,后面的单词首字母大写
 */
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
class Student
{
private:
    static const int GRADES = 4; //只和Student相关的常量,写在 CStudent类内部比较好
    string name;
    int id;
    int age;
    int score[GRADES];
public:
    int Average();
    string GetName() { return name; }
    //返回值不要设成 string & ,哪怕是 const string & 也不太好,因为这样等于向外暴露了 name 属性,“隐藏”的效果不好了
    //虽然效率低了点,但面向对象的思想本来就会用程序的运行效率换取工程实现的效率以及可维护性,可重用性等。
    int GetId() { return id; }
    int GetAge() { return age; }
    void SetName( const string & name_) { name = name_; }
    void SetAge( int age_) { age = age_; }
    void SetId( int id_) { id = id_; }
    void SetScore(int score_[]) {
        memcpy(score,score_,sizeof(score));
    }
    void Init(const char *);
};
void Student::Init(const char * line)
{
	const char * p = strchr(line,','); //p指向line中的第一个  ','
	string s = line;
	name = s.substr(0,p-line); // substr(i,l)取从下标i开始,长度为 l 的子串
	sscanf(p + 1, "%d,%d,%d,%d,%d,%d",&age,&id,score,score+1,score+2,score+3); // p + 1 指向后面的年龄,学号等的字符串
}
int Student::Average() {
	int sum = 0;
	for( int i = 0;i < Student::GRADES; ++i )
		sum += score[i];
	return sum / Student::GRADES;
}
int main()
{
	Student stu;
	char line[200];
	gets(line);
	stu.Init(line);
	printf("%s,%d,%d,%d",stu.GetName().c_str(),stu.GetAge(),stu.GetId(),stu.Average());
	return 0;
}

  

 

posted @ 2018-12-13 20:25  Veritas_des_Liberty  阅读(862)  评论(0编辑  收藏  举报