005 编程填空:学生信息处理程序(2)

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;

class Student {
string name,temp;
    int stno, age, sc1, sc2, sc3, sc4;
    float avgSc;
public:
    void input() {
        getline(cin, temp);
        char* p = strtok(const_cast<char *>(temp.c_str()), ",");
        name = p;
        p = strtok(NULL, ",");
        age = stoi(p);
        p = strtok(NULL, ",");
        stno = stoi(p);
        p = strtok(NULL, ",");
        sc1 = stoi(p);
        p = strtok(NULL, ",");
        sc2 = stoi(p);
        p = strtok(NULL, ",");
        sc3 = stoi(p);
        p = strtok(NULL, ",");
        sc4 = stoi(p);

    }
    void calculate() {
        avgSc = (sc1 + sc2 + sc3 + sc4) / 4.0;
    }
    void output() {
        cout << name << "," << age << "," << stno << "," << avgSc;
    }
};

int main() {
	Student student;        // 定义类的对象
	student.input();        // 输入数据
	student.calculate();    // 计算平均成绩
	student.output();       // 输出数据
}

posted @ 2022-02-19 22:38  icefield817  阅读(70)  评论(0编辑  收藏  举报