描述

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

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

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

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

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

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

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;
}

  

最新文章

  1. Linux主机上使用交叉编译移植u-boot到树莓派
  2. 使用Packet Sniffer抓包和分析(z-stack协议)
  3. cell 的复用机制
  4. nvm诡异的报错
  5. NYOJ 737 石子合并(一)
  6. input lable水平对齐
  7. iOS高仿app源码:纯代码打造高仿优质《内涵段子》
  8. Linux上安装Mysql+Apache+Php
  9. ThinkPHP 3.2版本 , 无法读取$_SESSION[&#39;verify_code&#39;]
  10. css系列教程--color direction line-height letter-spacing
  11. [python]自问自答:python -m参数? (转)
  12. [转] spring事务管理几种方式
  13. RPM基础知识
  14. 19. leetcode 100. Same Tree
  15. Node.js Error: Cannot find module express的解决办法
  16. CF908G New Year and Original Order 数位DP
  17. mac office2016
  18. Django学习手册 - 基于requests API验证(二)
  19. Bugku-CTF之输入密码查看flag
  20. 『TensorFlow』生成式网络中的图片预处理

热门文章

  1. QrCode C#生成二维码 及JavaScript生成二维码
  2. Python内置函数:strip()
  3. Java-API-Package:org.springframwork.transaction.annotation
  4. 转:Oracle下创建ASM磁盘总结
  5. react-router4.x 实用例子(路由过渡动画、代码分割)
  6. SpringBoot外部配置
  7. mongodb用mongoose得到的对象不能增加属性解决
  8. SqlServer——游标
  9. 用命令查看端口占用情况 netstat&#160;-ano
  10. bash: telnet: command not found