日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/7/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }

  

?

轉載于:https://www.cnblogs.com/ruruozhenhao/p/10116186.html

總結

以上是生活随笔為你收集整理的C:简单的学生信息处理程序实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。