當前位置:首頁 » 考試成績 » c學生類成績平均分

c學生類成績平均分

發布時間: 2021-01-29 03:14:40

1. 設計一個學生類CStudent,包括姓名和三門課程成績,利用重載運算符「+」將所有學生C++

#include <iostream>
#include <cstring>
#include<IOMANIP>
using namespace std;
class student
{
private:
char a[20]; //不能在類定義中對非static const型變數賦值
int x ,y ,z;
int cnt; //計數器,存放成績對應的人數。默認為1個學生的成績。
public :
student():cnt(1){
a[0]=''
x=y=z=0;
}
student (char *b,int m,int k,int t):cnt(1)
{
strcpy(a,b); //字元串賦值
x=m;
y=k;
z=t;
}
student operator +(const student& s);//const 引用,提高效率
void print()
{
cout<<"course 1#,ave score:="<<fixed<<setprecision(2)<<(float) x/cnt<<endl;
cout<<"course 2#,ave score:="<<fixed<<setprecision(2)<<(float) y/cnt<<endl;
cout<<"course 3#,ave score:="<<fixed<<setprecision(2)<<(float) z/cnt<<endl;
}
};
student student:: operator +(const student& s)
{
student b;
b.x=x+s.x;
b.y=y+s.y;
b.z=z+s.z;
b.cnt=cnt+1;//這里,b中存放的總成績對應人數的計算
return b;
}
int main()
{
student s1("hank",80,80,80),s2("wei",80,90,81),s3, s4("Li",90,90,90);
s3 = s1+ s2 + s4;
s3.print();
}

2. 統計10個學生成績,用c語言編程,輸出優秀,良好,及格,不及格,四類學生人數,用if~else~i

int優秀=0;
int良好=0;
int及格=0;
int不及格=0;
int[]10個人的成績內容=newint{100,50,60...};
for(inti=0;i<10個人的成績.lenght-1;i++){
if(10個人的成績[i]<60){
不及格++;
}elseif(10個人的成績[i]<=80){
及格++;
}lseif(10個人的成績[i]<=90){
良好++;
}lseif(10個人的成績[i]<=100){
優秀++;
}
}
//然後依次輸出就OK啦!

3. 創建一個學生類(student),包含:學號,姓名,性別,Java成績,sql 成績,c語言成績,html成績,總分,平均分。

學生類:
publicclassStudent
{
privateStringstuId;
privateStringname;
privateStringgender;
privatefloatjavaScore;
privatefloatsqlScore;
privatefloatcScore;
privatefloathtmlScore;
privatefloatavgScore;
privatefloatsumScore;

publicStudent(){};

publicStudent(StringstuId,Stringname,Stringgender,floatjavaScore,floatsqlScore,
floatcScore,floathtmlScore)
{
this.stuId=stuId;
this.name=name;
this.gender=gender;
this.javaScore=javaScore;
this.sqlScore=sqlScore;
this.cScore=cScore;
this.htmlScore=htmlScore;
}

publicfloatgetSumScore(floatjavaScore,floatsqlScore,
floatcScore,floathtmlScore)
{
returnjavaScore+sqlScore+cScore+htmlScore;
}

publicStringgetStuId(){
returnstuId;
}

publicvoidsetStuId(StringstuId){
this.stuId=stuId;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetGender(){
returngender;
}

publicvoidsetGender(Stringgender){
this.gender=gender;
}

publicfloatgetJavaScore(){
returnjavaScore;
}

publicvoidsetJavaScore(floatjavaScore){
this.javaScore=javaScore;
}

publicfloatgetSqlScore(){
returnsqlScore;
}

publicvoidsetSqlScore(floatsqlScore){
this.sqlScore=sqlScore;
}

publicfloatgetcScore(){
returncScore;
}

publicvoidsetcScore(floatcScore){
this.cScore=cScore;
}

publicfloatgetHtmlScore(){
returnhtmlScore;
}

publicvoidsetHtmlScore(floathtmlScore){
this.htmlScore=htmlScore;
}

publicfloatgetAvgScore(){
returnavgScore;
}

}

ps:總分和平均分不需要set,缺少了get平均分部分,懶得寫了你仿照總分加上吧,給你個機會
測試類:
publicclassTestStudent{

publicstaticvoidmain(String[]args){
Studentstudent=newStudent("1","name","男",85,74,92,88);
floatsumScore=student.getSumScore(student.getHtmlScore(),
student.getJavaScore(),student.getSqlScore(),student.getcScore());
System.out.println("學生"+student.getName()+"總分為:"+sumScore);
}

}

結果:學生name總分為:339.0

注意確保2個類在一個包下。

4. 求c語言編程學生成績類型

#include"stdio.h"
#define max 50
void main()
{
double degree[max],avg=0.0;
int i=0,j=0,count1=0,count2=0;
printf("");
do{
scanf("%lf",°ree[i]);
while(degree[i]>100){
printf("");
scanf("%lf",°ree[i]);
};
avg+=degree[i];
if(degree[i]>80)
count1++;
if(degree[i]<60)
count2++;
i++;
}while(degree[i-1]>=0)
for(;j<i;j++)
printf("%lf ",degree[j]);
printf("\n");
printf("%d,%d\n",count1,count2);
printf("%lf\n",avg/i);
}
可以改一下輸出語句,讓其看起專來更易懂屬

5. 定義一個學生類CStudent,學生類的數據成員包括姓名、學號和成績等。

#include"Exp19_1.h"
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

class student{ //為了簡化,數據域僅保留學號與姓名
int key;
string name;
public:
void putkey(int k){key=k;}
void putnam(string nam){name=nam;}
void show(){cout<<key<<'\t'<<name<<endl;}//典型的應重載插入運算符
bool operator<(student& stu){return key<stu.key;}
bool operator!=(student& stu){return key!=stu.key;}
};

int main(){
int i,k;
string nam;
Node<student> * P1;
student stu;
List <student>list1,list2,list3;
ifstream infile;
infile.open("EXP19_1.txt",ios::in);
if(!infile){
cout<<"不能打開文件"<<endl;
return 1;
}
for(i=0;i<9;i++){
infile>>k;
stu.putkey(k);
infile>>nam;
stu.putnam(nam);
P1=list1.CreatNode(stu);
list1.InsertFront(P1);//向前生成list1
P1=list1.CreatNode(stu);
list2.InsertRear(P1); //向後生成list2
}
infile.close();
list1.PrintList();
cout<<"list1長度:"<<list1.Length()<<endl;
cout<<endl;
list2.PrintList();
cout<<endl;
cout<<"請輸入一個要求刪除的學號"<<endl;
cin>>k;
stu.putkey(k);
P1=list1.Find(stu);
if(P1!=NULL){
P1=list1.DeleteNode(P1);
delete P1;
list1.PrintList();
cout<<"list1長度:"<<list1.Length()<<endl;
}
else cout<<"未找到"<<endl;
cout<<endl;

list1.MakeEmpty();//清空list1
cout<<"清空後list1長度:"<<list1.Length()<<endl;

infile.open("EXP19_1.txt",ios::in);
infile.clear();//如果文本文件結束前缺回車,無此句會出錯
if(!infile){
cout<<"不能打開文件"<<endl;
return 1;
}
for(i=0;i<9;i++){
infile>>k;
stu.putkey(k);
infile>>nam;
stu.putnam(nam);
P1=list3.CreatNode(stu);
list3.InsertOrder(P1);//升序創建list1
}
infile.close();
list3.PrintList();
cout<<endl;
return 0;
}

此為頭文件:

#include<iostream>
using namespace std;
//首先看結點組織,採用結點類,凡與結點數據和指針操作有關函數作為成員函數
template<typename T>class List;
template<typename T>class Node{
T info; //數據域
Node<T> *link; //指針域
public:
Node(); //生成頭結點的構造函數
Node(const T & data);//生成一般結點的構造函數
void InsertAfter(Node<T>* P); //在當前結點後插入一個結點
Node<T>* RemoveAfter(); //刪除當前結點的後繼結點,返回該結點備用
friend class List<T>;
//以List為友元類,List可直接訪問Node的私有成員,與結構一樣方便,但更安全
};
template <typename T> Node<T>::Node(){link=NULL;}
template <typename T> Node<T>::Node(const T & data){
info=data;
link=NULL;
}
template<typename T>void Node<T>::InsertAfter(Node<T>* p){
p->link=link;
link=p;
}
template<typename T>Node<T>* Node<T>::RemoveAfter(){
Node<T>* tempP=link;
if(link==NULL) tempP=NULL; //已在鏈尾,後面無結點
else link=tempP->link;
return tempP;
}
//再定義鏈表類,選擇常用操作:包括建立有序鏈表、搜索遍歷、插入、刪除、取數據等
template<typename T>class List{
Node<T> *head,*tail;//鏈表頭指針和尾指針
public:
List(); //構造函數,生成頭結點(空鏈表)
~List(); //析構函數
void MakeEmpty(); //清空一個鏈表,只余表頭結點
Node<T>* Find(T data); //搜索數據域與data相同的結點,返回該結點的地址
int Length(); //計算單鏈表長度
void PrintList(); //列印鏈表的數據域
void InsertFront(Node<T>* p); //可用來向前生成鏈表,在表頭插入一個結點
void InsertRear(Node<T>* p); //可用來向後生成鏈表,在表尾添加一個結點
void InsertOrder(Node<T> *p); //按升序生成鏈表
Node<T>*CreatNode(T data); //創建一個結點(孤立結點)
Node<T>*DeleteNode(Node<T>* p); //刪除指定結點
};
template<typename T>List<T>::List(){
head=tail=new Node<T>();
}
template<typename T>List<T>::~List(){
MakeEmpty();
delete head;
}
template<typename T>void List<T>::MakeEmpty(){
Node<T> *tempP;
while(head->link!=NULL){
tempP=head->link;
head->link=tempP->link; //把頭結點後的第一個節點從鏈中脫離
delete tempP; //刪除(釋放)脫離下來的結點
}
tail=head; //表頭指針與表尾指針均指向表頭結點,表示空鏈
}
template<typename T> Node<T>* List<T>::Find(T data){
Node<T> *tempP=head->link;
while(tempP!=NULL&&tempP->info!=data) tempP=tempP->link;
return tempP; //搜索成功返回該結點地址,不成功返回NULL
}
template<typename T>int List<T>::Length(){
Node<T>* tempP=head->link;
int count=0;
while(tempP!=NULL){
tempP=tempP->link;
count++;
}
return count;
}
template<typename T>void List<T>::PrintList(){
Node<T>* tempP=head->link;
while(tempP!=NULL){
tempP->info.show();
tempP=tempP->link;
}
cout<<endl;
}
template<typename T>void List<T>::InsertFront(Node<T> *p){
p->link=head->link;
head->link=p;
if(tail==head) tail=p;
}
template<typename T>void List<T>::InsertRear(Node<T> *p){
p->link=tail->link;
tail->link=p;
tail=p;
}
template<typename T>void List<T>::InsertOrder(Node<T> *p){
Node<T> *tempP=head->link,*tempQ=head; //tempQ指向tempP前面的一個節點
while(tempP!=NULL){
if(p->info<tempP->info)break; //找第一個比插入結點大的結點,由tempP指向
tempQ=tempP;
tempP=tempP->link;
}
tempQ->InsertAfter(p); //插在tempP指向結點之前,tempQ之後
if(tail==tempQ) tail=tempQ->link;
}
template<typename T>Node<T>* List<T>::CreatNode(T data){//建立新節點
Node<T>*tempP=new Node<T>(data);
return tempP;
}
template<typename T>Node<T>* List<T>::DeleteNode(Node<T>* p){
Node<T>* tempP=head;
while(tempP->link!=NULL&&tempP->link!=p) tempP=tempP->link;
if(tempP->link==tail) tail=tempP;
return tempP->RemoveAfter();
}

6. 設計一個類CStudent 類中包含一個學生的基本數據如下: 編號,姓名,性別,年齡,數學成績,計算機成績

/*************************************************

Version: 6.00 Date: 2013.5.14
Description:
設計一個類CStudent,類中包含一個學生的基本數據如下:
編號,姓名,性別,年齡,數學成績,計算機成績,外語成績。
並假設編號為整數,且從1號往後連續編碼;姓名為字元串,性別為字元。如:
1 LiPing m 18 89 98 94
請採用binary文件形式,並使用隨機讀寫處理方式,對自定義CStudent類的對象數據
進行存儲與讀寫處理(即是說,總按具有連續編碼的編號num為"序"來對文件中的各對
象數據進行隨機讀寫處理)。並設計該類的成員函數,而且對輸出運算符"<<"進行重載,
使該運算符能夠完成將一個學生的信息輸出到屏幕上。

Function List: // 主要函數列表,每條記錄應包括函數名及功能簡要說明
(1) 從鍵盤輸入一個學生的有關信息,並將它們存入到數據文件中(按編號來確定寫出位置)。
(2) 按編號對學生信息進行檢索並將檢索結果顯示在屏幕上。
(3) 按姓名對學生信息進行檢索並將檢索結果顯示在屏幕上。
(4) 計算某編號學生的總成績與平均成績。
(5) 列出所有總成績超過n分的性別為s同學的有關信息(n,s由用戶從鍵盤輸入)。
History:
<author> <time> <version > <desc>
*****************************************************/
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int n;
class CStudent
{public:int Num;
char Name[10];
char Sex[10];
int Age;
float Math,Com,Eng,Total,ave;
public: void getdata() //輸入類元素
{cout<<"編號: ";
cin>>Num;
cout<<endl<<"姓名: ";
cin>>Name;
cout<<endl<<"性別: ";
cin>>Sex;
cout<<endl<<"年齡: ";
cin>>Age;
cout<<endl<<"數學成績: ";
cin>>Math;
cout<<endl<<"計算機成績:";
cin>>Com;
cout<<endl<<"外語成績: ";
cin>>Eng;
cout<<endl; }
void outdate() //輸出類元素
{Total=Math+Com+Eng;
ave=(Math+Com+Eng)/3;
cout<<setw(5)<<Num<<setw(5)<<Name<<setw(5)<<Sex<<setw(5)<<Age<<setw(8)<<Math<<setw(10)<<Com<<setw(8)<<Eng<<setw(8)<<Total<<setw(8)<<ave<<endl;
} };
void get() //把類數據輸入到磁碟中
{ ofstream outfile("stud.dat");
CStudent s[50];
int i;
cout<<"輸入數據:"<<endl;
for( i=0;i<n;i++)
{
cout<<"第"<<i+1<<"個學生:"<<endl;
s[i+1].getdata();
outfile.write((char*)&s,sizeof(s));
};
outfile.close(); }
void shaixuan() //把類數據從磁碟中輸出
{
ifstream infile("stud.dat");
CStudent s[50];
int i;
string sex;
float total;
cout<<"請輸入你要篩選同學的性別:"<<" ";
cin>>sex;
cout<<"請輸入你要篩選同學的總成績超過多少分:"<<" ";
cin>>total;
infile.read((char *)&s,sizeof(s));
while(infile)
{
for( i=0;i<50;i++)
{
if((sex==s[i].Sex)&&(total<=s[i].Total))
{ cout<<" 編號 姓名 性別 年齡 數學成績 計算機成績 外語成績 總分 平均分"<<endl;
s[i].outdate();}
}
infile.read((char*)&s,sizeof(s));
};
infile.close();
}
void xingming() //把類數據從磁碟中輸出
{
ifstream infile("stud.dat");
CStudent s[50];
int i;
string str;
cout<<"請輸入你要篩選同學的姓名:"<<" ";
cin>>str;
infile.read((char *)&s,sizeof(s));
while(infile)
{
for( i=0;i<50;i++)
{
if(str==s[i].Name)
{ cout<<" 編號 姓名 性別 年齡 數學成績 計算機成績 外語成績 總分 平均分"<<endl;
s[i].outdate();}
}
infile.read((char*)&s,sizeof(s));
};
infile.close();
}
void xuehao() //把類數據從磁碟中輸出
{
ifstream infile("stud.dat");
CStudent s[50];
int i,m;
cout<<"請輸入你要篩選同學的編號:"<<" ";
cin>>m;
infile.read((char *)&s,sizeof(s));
while(infile)
{
for( i=0;i<50;i++)
{
if(m==s[i].Num)
{ cout<<" 編號 姓名 性別 年齡 數學成績 計算機成績 外語成績 總分 平均分"<<endl;
s[i].outdate();}
}
infile.read((char*)&s,sizeof(s));
};
infile.close();
}
int main()
{
int a;
cout<<"請輸入你想添加多少個學生的信息n:"<<' ';
cin>>n;
do //選擇應執行程序
{ cout<<"如果你輸入數字7,你就可以添加學生信息了;"<<'\n';
cout<<"如果你輸入數字8,你就可以篩選出所有總成績超過n分的性別為sex同學的有關信息了;"<<'\n';
cout<<"如果你輸入數字9,你就可以按編號進行篩選了;"<<'\n';
cout<<"如果你輸入數字10,你就可以按姓名進行篩選了;"<<'\n';
cout<<"如果你輸入數字1,你就可以退出此程序了;"<<'\n';
cin>>a;
switch(a)
{

case 7:get();break;
case 8:shaixuan();break; //輸入數據
case 9:xuehao();break;
case 10:xingming();break; //輸出數據
default:cout<<"error"<<endl;break;
} }
while(a==7||a==8||a==9||a==10);
return 0;
}
哈哈,自己調試一下再改進一下吧!

7. c語言 利用面向對象方法,定義一個學生類,求每名同學的平均分和所有同學的最高分。

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;
string num;
double score;
public:
void SetName(string name);
void SetNum(string num);
void SetScore(double score);
string GetName();
string GetNum();
double GetScore();
};
void Student::SetName(string name)
{
this->name = name;
}
void Student::SetNum(string num)
{
this->num = num;
}
void Student::SetScore(double score)
{
this->score = score;
}
string Student::GetName()
{
return name;
}
string Student::GetNum()
{
return num;
}
double Student::GetScore()
{
return score;
}
int main()
{
Student stu[10];
string name,num;
double score,max = 0.0;
for(int i = 0;i < 3;i++)
{
cout<<"輸入學生"<<i+1<<"姓名:";
cin>>name;
cout<<"輸入"<<i+1<<"學號:";
cin>>num;
cout<<"輸入"<<i+1<<"分數:";
cin>>score;
stu[i].SetName(name);
stu[i].SetNum(num);
stu[i].SetScore(score);
}
for(i=0;i<3;i++)
if (max<stu[i].GetScore())
max=stu[i].GetScore();
cout<<"最大數"<<max<<endl;
score = 0;
for(int i = 0;i < 3;i++)
{
score += stu[i].GetScore();
}
score = score/3;
cout<<"平均分:"<<score<<endl;
system("pause");
return 0;
}

8. 用c語言編程學生成績,分為優秀,良好,及格,不及格四類,用if~else~if結構如何編輯

#include<stdio.h>
int main()
{
int i,b=0,j=0,l=0,y=0,a[10];
printf("請輸來入10個學生的源成績\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
if(a[i]<60)
{
b++;
}
else if(a[i]<70)
{
j++;
}
else if(a[i]<80)
{
l++;
}
else if(a[i]<=100)
{
y++;
}
}
printf("優秀%d\n",y);
printf("良好%d\n",l);
printf("及格%d\n",j);
printf("不及格%d\n",b);
return 0;
}

9. 3.C語言 用結構體類型編一程序,輸入一個學生的學號,姓名及三門課的成績,計算並輸出平均成績

#include<stdio.h>
structstud{
intnum;
charname[30];
floatscore[3];
};
intmain()
{
structstudstu;
inti;
floatavg;
printf("inputnumber:");scanf("%d",&stu.num);
printf("inputname:");scanf("%s",stu.name);
printf("input3score: ");
avg=0;
for(i=0;i<3;i++)
{
scanf("%f",&stu.score[i]);
avg+=stu.score[i];
}
avg/=3;
printf("averagescore:%f ",avg);
return0;
}

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919