導航:首頁 > 源碼編譯 > 定義學生類型演算法

定義學生類型演算法

發布時間:2022-05-02 06:58:05

⑴ VC++,定義一個學生類Student

#include<iostream>
using namespace std;
class Student
{
private:
int age;
char *name;
public:
static int count;
public:
Student(int m, char *n);
Student();
~Student( );
void Print( ) const;
};
int Student::count = 0;
Student::Student(int m, char *n)
{
age = m;
name = n;
count++;
}
Student::Student()
{
age = 0;
name = "NoName";

⑵ 定義一個學生類,包含學號、姓名、平時成績和考核成績四個數據成員和以下成員方法(用python語言):

花了不少時間寫的,挺詳細的,希望採納。


#引入operator模塊,用於給集合排序
importoperator


#單行注釋用"#",多行注釋用'''注釋內容'''
#定義一個學生類,類名用駝峰命名法
classStudent:

#構造方法,可用來創建對象格式def__init__(self,參數)參數個數自已定義,類型系統自動識別
def__init__(self,stu_no,name,base_score,exam_score):
self.stu_no=stu_no#對象屬性賦值
self.name=name
self.base_score=base_score
self.exam_score=exam_score

#定義計算總評函數定義函數格式def函數名(self,參數),self代表本對象
defget_last_score(self):
#return指定返回值,無return表示此函數無返回值
returnself.base_score*0.3+self.exam_score*0.7

#類似toString方法,列印對象時,調用對象的該方法
def__str__(self):
return'學號:'+self.stu_no+'姓名:'+self.name+",平時成績:"+str(self.base_score)+",考核成績:"+str(self.exam_score)


#定義函數,將對象集合寫到文件,上面三個函數有縮進,屬於Student類的函數,本函數屬於全局函數
defprint_to_file(path,stu_arr):
#打開文件,操作完成後自動關閉文件
withopen(path,'w')asfile:
#調用operator給集合排序
sort_attr=operator.attrgetter('stu_no')#指定排序屬性
stu_arr.sort(key=sort_attr)#排序
forstuinstu_list:
str=stu.__str__()#將對象轉換為字元串
file.write(str+' ')#將字元串寫入文件


#主函數,運行的入口
if__name__=='__main__':
#創建幾個學生對象,按__init__的順序輸入參數
s1=Student('1001','zhangsan',31,69)
s2=Student('1003','wangwu',28,32)
s3=Student('1004','zhaoliu',77,78)
s4=Student('1002','lisi',19,89)
#創建集合
stu_list=[s1,s2,s3,s4]

#文件路徑
f='d:\aaa.txt'
print_to_file(f,stu_list)

java程序設計,定義一個表示學生的類Student

public class Student { //定義一個學生類
private int StuNum; //學號
private int Class; //班級
private char Gender; //性別
private int Age; //年齡
public Student(int StuNum, int Class, char Gender, int Age){//構造函數
this.stuNum = StuNum;
this.class = Class;
this.gender = Gender;
this.age = Age;
}
public int getStuNum() { //獲得學號
return StuNum;
}

public int getClass() { //獲得班級號
return Class;
}

public char getGender() { //獲得性別
return Gender;
}
public void setGender(char Gender) { //修改性別
this.Gender = Gender;
}
public int getAge() { //獲得年齡
return Age;
}
public void setAge(int Age) { //修改年齡
this.Age = Age;
}

public class Pupil extends Student //小學生
{
//...(由於沒說派生後要新加什麼東西,所以這里寫了省略號,如果想在
// 新派生出來的類里加點什麼特殊的東西,直接在省略號位置加就行了
// 下同)
}

public class MidSchoolStu extends Student //中學生
{
//...
}

public class UnderGraate extends Student //大學生
{
//...
}

public class PostGraate extends Student //研究生
{
//...
}

public class FreshStudent extends UnderGraate //一年級學生
{
//...
}

public class Sophomore extends UnderGraate //二年級學生
{
//...
}

public class Junior extends UnderGraate //三年級學生
{
//...
}

public class Senior extends UnderGraate //四年級學生
{
//...
}

public class Master extends PostGraate //碩士生
{
//...
}

public class Doctor extends PostGraate //博士生
{
//...
}

⑷ c++編程題定義一個學生類

#include<iostream>
#include<iomanip>
using namespace std;
class Student{
private:
int num;
char name[10];
int age;
double score;
public:
Student(int,char[],int,double);
~Student(){
cout<<"Destruct is called"<<endl;
}
void display(){
cout<<setiosflags(ios::left);
cout<<setw(5)<<num<<setw(8)<<name<<setw(5)<<age<<setw(5)<<score<<endl;
}
};
Student::Student(int n,char str[],int a,double s){
num=n;
for(int k=0;str[k]!='\0';k++)
name[k]=str[k];
name[k]='\0';
age=a;
score=s;
}
void main(){
Student stu(1,"linda",23,735);
stu.display();
return;
}
注:大致上類體就是這樣的,至於主要數可以自己編寫,類的成員函數也可以根據需要增加.

⑸ 在java中編寫程序,定義一個學生類

代碼如下:

package exam2;

import java.util.ArrayList;

import java.util.List;

/**

編寫一個Java應用程序,該程序包括3個類:

定義一個學生類,Student有姓名,學號,選學的課程列表

定義一個課程類:課程名稱,課程分數

(1)初始化一個學生,選擇了三門課程,並添加到學生的屬性中

(2)實現統計學生的總分功能

1.正確編寫2個類(5分)

2.統計學生的總分功能(5分)

*/

public class Test {

public static void main(String[] args) {

List<Course> courses = new ArrayList<>();

// 初始化3門課程及分數

Course course1 = new Course("java", 80);

Course course2 = new Course("Math", 60);

Course course3 = new Course("English", 90);

// 課程對象添加到集合

courses.add(course1);

courses.add(course2);

courses.add(course3);

// 初始化學生對象

Student student = new Student("tom", "2015101", courses);

// student.setStuid("2015101");

// student.setStuname("tom");

// student.setCourses(courses);

// System.out.println(student);

// (2)實現統計 學生 的總分功能

int sum = sumScore(courses);

System.out.println(student.getStuname() + "學生總分:" + sum);

}

private static int sumScore(List<Course> courses) {

int sum = 0;

// 用課程對象,來獲取課程的分數

for (Course course : courses) {

sum += course.getScore();

}

return sum;

}

}

package exam2;

import java.util.List;

public class Student {

// 定義一個學生類,Student有姓名,學號,選學的課程列表

private String stuname;

private String stuid;

private List<Course> courses;

public Student() {

}

public Student(String stuname, String stuid, List<Course> courses) {

this.stuname = stuname;

this.stuid = stuid;

this.courses = courses;

}

public String getStuname() {

return stuname;

}

public void setStuname(String stuname) {

this.stuname = stuname;

}

public String getStuid() {

return stuid;

}

public void setStuid(String stuid) {

this.stuid = stuid;

}

public List<Course> getCourses() {

return courses;

}

public void setCourses(List<Course> courses) {

this.courses = courses;

}

@Override

public String toString() {

return "學生姓名=" + stuname + ",學號=" + stuid + ", 課程=" + courses;

}

}

package exam2;

public class Course {

//定義一個課程類:課程名稱,課程分數

private String cname;

private int score;

public Course() {

}

public Course(String cname, int score) {

this.cname = cname;

this.score = score;

}

public String getCname() {

return cname;

}

public void setCname(String cname) {

this.cname = cname;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

@Override

public String toString() {

return "[課程名稱=" + cname + ", 課程分數=" + score + "]";

}

}

/*

運行:

tom學生總分:230

*/

(5)定義學生類型演算法擴展閱讀:

Public語句說明

1.Public語句聲明的變數在所有應用程序的所有沒有使用OptionPrivate Mole的模塊的任何過程中都是可用的;若該模塊使用了OptionPrivate Mole,則該變數只是在其所屬工程中是公用的。

2.使用Public語句可以聲明變數的數據類型。例如,下面的語句聲明了一個Integer類型的變數。

3.Public NumberOfEmployees As Integer 也可以使用Public語句來聲明變數的對象類型。下面的語句為工作表的新實例聲明了一個變數。

參考資料:網路-Public 語句



⑹ C++題目,有大佬過來看看么 定義學生類。 (1)類名:STUDENT;

預備知識:

c++中我們cpp文件和.h文件的區別是,cpp文件是需要編譯的文件,成為一個獨立的編譯單元,而h文件從來是不需要編譯,只是用於預處理。

通常我們在cpp文件中,完成函數的實現,然後在h中則是對於函數的聲明,由於默認情況下,全局變數和全局函數存儲類型都是extern類型的,所以我們不需要顯示的使用extern

這樣,我們其他的cpp文件,只要#include .h文件,則在cpp中實現的函數,就能在其他cpp中使用,因為我們已經用.h文件,完成了extern函數聲明的操作。

c++類的定義,其實就是定義一個類型。

class A{

public:

void fun(int n);

int fun1(void);

public:

int num;

};

其實就是定義了一種類型。和我們通常所說的定義不一樣。

類的定義,是不能重復定義的,在同一個編譯單元中,只能定義類一次。如果重復定義,會出錯。同時類聲明和類定義都是內部鏈接。只是為當前編譯單元所用。

因此,把類的定義,放在.h文件中,類的實現放在專門的cpp中。這樣包含.h的其他cpp,就可以使用cpp中實現的函數。。

同時注意:類的實現cpp文件的編譯,必須依賴於類的定義文件.h,所以我們在類實現文件cpp中必須#include< ........h>,用於編譯,否則會出錯。這是不同於普通的函數。

//student.h(這是頭文件,在此文件中進行類的聲明)
classStudent//類聲明
{public:
voiddisplay();
private:
intnum;
charname[20];
charsex;
};
//student.cpp//在此文件中進行函數的定義
#include<iostream>
#include″student.h″
voidStudent∷display()
{cout<<″num:″<<num<<endl;
cout<<″name:″<<name<<endl;
cout<<″sex:″<<sex<<endl;
}
//main.cpp主函數模塊
#include<iostream>
#include″student.h″
intmain()
{Studentstud;
stud.display();
return0;
}

類中只有static成員變數。具有外部鏈接特性。所以這要求,static成員變數的初始化一定要在.Cpp文件中。如果在h文件中。那麼多個cpp文件#include,則發生多次重復定義的錯誤。

類定義和類實現分離的好處:

1/快編譯速度

當然可以啊。相反,如果你把類的所有代碼都內聯定義到頭文件中,那麼所有需要用到這個類的CPP文件實際上都包含了更多的代碼,編譯器編譯每個這樣的CPP文件時都編譯了這些代碼。而分開定義,這些代碼就只被編譯了一遍,也就是在編譯實現那個類的CPP文件時。

在特殊情況下確實可以的
假如我有一個類a被幾百個cpp同時包含,如果定義和聲明放在一起,只要我對a進行任何修改,那幾百個文件都必須被重新編譯。而如果頭文件只包含聲明,修改a的實現不會導致那些文件被重新編譯,有時可以極大的提高速度

在C++中,類的定義方法如下:

class 類名{
訪問范圍說明符:
成員變數1
成員變數2
成員函數聲明1
成員函數聲明2

訪問范圍說明符:
更多成員變數
更多成員函數聲明
...
};

類的定義要以;結束。

「訪問范圍說明符」一共有三種,分別是 public、private 和 protected。三者的區別後面會詳細介紹,目前暫且都用 public。「訪問范圍說明符」可以出現任意多次。

「成員變數」的寫法與普通的變數定義相同。稱其為成員變數,是因為這些變數是一個類的成員。

同樣地,「成員函數聲明」的寫法也與普通的函數聲明相同。

一個類的成員函數之間可以互相調用。類的成員函數可以重載,也可以設定參數的默認值。

以前所學的函數不是任何類的成員函數,可稱為「全局函數」。

成員變數就代表對象的「屬性」,成員函數就代表對象的「方法」。成員變數和成員函數出現的先後次序沒有規定。

成員函數的實現可以位於類的定義之外,格式如下:

返回值類型 類名:函數名()
{
函數體
}


定義類之後,就可以定義對象了。定義對象的基本方法如下:

類名 對象名;

此處,「對象名」的命名規則和普通變數相同。對象也可以看作「類變數」

類的示常式序剖析

下面來看一個用面向對象的方法進行 C++ 程序設計的例子。

例題:編寫一個程序,輸入矩形的寬度和高度,輸出其面積和周長。

這個程序比較簡單,實際上根本不需要用面向對象的方法進行設計,這里只是為了使讀者更容易理解類和對象的概念。

首先要做的事情是分析問題中有哪些「對象」。比較明顯,只有「矩形」這種對象。然後要進行「抽象」,即概括「矩形」這種對象的共同特點。

矩形的屬性就是寬度和高度。因此需要兩個變數,分別代表寬度和高度。

一個矩形可以有哪些方法(即可以對矩形進行哪些操作)呢?在本程序中,矩形可以有設置寬度和高度、計算面積和計算周長三種行為,這三種行為可以各用一個函數實現,它們都會用到寬度和高度這兩個變數。

「抽象」完成後,就可以用 C++ 提供的語法特性寫出一個「矩形類」,將矩形的屬性和方法「封裝」在一起。程序如下:

#include <iostream>
using namespace std;
class CRectangle
{
public:
int w, h; //成員變數,寬和高
void init( int w_,int h_ ); //成員函數,設置寬和高
int area(); //成員函數, 求面積
int perimeter(); //成員函數,求周長
}; //必須有分號
void CRectangle::init( int w_,int h_ )
{
w = w_; h = h_;
}
int CRectangle::area()
{
return w * h;
}
int CRectangle::perimeter()
{
return 2 * ( w + h);
}
int main( )
{
int w,h;
CRectangle r; //r是一個對象
cin >> w >> h;
r.init( w,h);
cout << "It's area is " << r.area() << endl;
cout << "It's perimeter is " << r. perimeter();
cout << sizeof(CRectangle) << endl;
return 0;
}

⑺ 設計Java程序,定義一個表示「學生」的類Student

public class Student{
public String name;
public double ordinary;
public double attendance;

// public Student(String name){
// this.name = name;
// }
// public Student(String name, double ordinary, double attendance){
// this.name = name;
// this.ordinary = ordinary;
// this.attendance = attendance;
// }

// public void setName(String name){
// this.name = name;
// }
// public String getName(){
// return name;
// }
// public void setOrdinary(double ordinary){
// this.ordinary = ordinary;
// }
// public double getOrdinary(){
// return ordinary;
// }
// public void setAttendance(double attendance){
// this.attendance = attendance;
// }
// public double getAttendance(){
// return attendance;
// }

public boolean qualified(){
//如果attendence存放的直接是百分比對應值改成>=0.6
if(this.ordinary >= 60 && this.attendance >= 60){
return true;
}
return false;
}

public String toString(){
return "姓名:" + this.name + " 平時成績:" + this.ordinary + " 出勤率:" + this.attendance;
}
}

public class test{
public static void main(String[] args){
//直接賦值(需要對應屬性許可權修飾符為public)
Student student = new Student();
student.name = "姓名";
student.ordinary = 90;
student.attendance = 80;
// //set方法
// Student student = new Student();
// student.setName("姓名");
// student.setOrdinary(90);
// student.setAttendance(80);
// //構造方法
// Student student = new Student("姓名", 90, 80);
//這里直接用他的qualified判斷是否有考試資格
if(student.qualified()){
System.out.println("這名學生擁有考試資格");
}else{
System.out.println("這名學生沒有考試資格");
}
System.out.println(student.toString());
}
}

⑻ 如何定義一個學生的類.

大略寫了一個不知道合不合適:
public class Student {
private int id;
private String name;
private int sex;
private int age;
private int score1;
private int score2;
private int score3;
public Student(int id,String name,int sex,
int age,int score1,int score2,int score3)
{
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}

public class TestStudent {
public static void main(String[] args) {
Student []stu=new Student[5];
stu[0]=new Student(1,"stu1",1,19,90,84,50);
stu[1]=new Student(2,"stu2",0,19,83,87,91);
stu[2]=new Student(3,"stu3",0,20,70,77,60);
stu[3]=new Student(4,"stu4",1,19,88,82,84);
stu[4]=new Student(5,"stu5",1,21,80,97,93);
int temp1=stu[0].getScore1();
int temp2=stu[0].getScore2();
int temp3=stu[0].getScore3();
int total1=temp1;
int total2=temp2;
int total3=temp3;
int maxScore1=temp1;
int maxScore2=temp2;
int maxScore3=temp3;
int minScore1=temp1;
int minScore2=temp2;
int minScore3=temp3;
for(int i=1;i<5;i++)
{
temp1=stu[i].getScore1();
temp2=stu[i].getScore2();
temp3=stu[i].getScore3();
total1+=temp1;
total2+=temp2;
total3+=temp3;
maxScore1=maxScore1>temp1?maxScore1:temp1;
maxScore2=maxScore2>temp2?maxScore2:temp2;
maxScore3=maxScore3>temp3?maxScore3:temp3;
minScore1=minScore1>temp1?temp1:minScore1;
minScore2=minScore2>temp2?temp2:minScore2;
minScore3=minScore3>temp3?temp3:minScore3;
}
System.out.println("average1 = "+(float)total1/5);
System.out.println("average2 = "+(float)total2/5);
System.out.println("average3 = "+(float)total3/5);
System.out.println("maxScore1 = "+maxScore1);
System.out.println("maxScore2 = "+maxScore2);
System.out.println("maxScore3 = "+maxScore3);
System.out.println("minScore1 = "+minScore1);
System.out.println("minScore2 = "+minScore2);
System.out.println("minScore3 = "+minScore3);
}
}

⑼ 定義一個學生類類型,包含學號(num),分數(score)兩個數據成員,

class Student
{
private:
int num;
int score;
public:
//修改學生學號
void changeNum(int newNum)
{
num=newNum;
}
//修改分數
void changeScore(int newScore)
{
score=newScore;
}

//顯示學號
int displayNum()
{
return num;
}
//顯示分數
int displayScore()
{
return score;
}
};

⑽ 定義一個學生類Student,包括如下屬性:學生學號、姓名、年齡、專業、年級等,

1、首先,定義一個數據結構student,包含學生的各信息。

閱讀全文

與定義學生類型演算法相關的資料

熱點內容
噴油螺桿製冷壓縮機 瀏覽:569
python員工信息登記表 瀏覽:369
高中美術pdf 瀏覽:153
java實現排列 瀏覽:505
javavector的用法 瀏覽:974
osi實現加密的三層 瀏覽:225
大眾寶來原廠中控如何安裝app 瀏覽:906
linux內核根文件系統 瀏覽:235
3d的命令面板不見了 瀏覽:520
武漢理工大學伺服器ip地址 瀏覽:141
亞馬遜雲伺服器登錄 瀏覽:517
安卓手機如何進行文件處理 瀏覽:65
mysql執行系統命令 瀏覽:923
php支持curlhttps 瀏覽:136
新預演算法責任 瀏覽:437
伺服器如何處理5萬人同時在線 瀏覽:244
哈夫曼編碼數據壓縮 瀏覽:418
鎖定伺服器是什麼意思 瀏覽:378
場景檢測演算法 瀏覽:611
解壓手機軟體觸屏 瀏覽:342