C++面向物件程式設計50道程式設計題(第21題)
阿新 • • 發佈:2018-12-09
C++面向物件程式設計50道程式設計題(第21題)
摘要:C++程式設計實習是為學生提供了一個既動手又動腦,獨立實踐的機會,將課本上的理論知識和實際有機的結合起來,鍛鍊學生的分析問題和解決問題的能力,提高學生運用所學知識解決實際問題的能力。
本專輯為程式設計入門者、高校計算機軟體專業學習或複習提供C++程式設計題庫。
讀者請先獨立思考哦,再與參考程式進行比對檢查。
一、問題描述
二、考察內容
基本面向物件概念,如何建立類、物件,對類私有資料成員和公有成員函式的理解,對物件陣列的理解和應用,對多維資料的處理,簡單的數學運算。
三、難度等級
難度等級:★★★☆☆
四、參考程式
#include <iostream.h> #include <string.h> class Student{ int num,score; char name[9]; public: Student(){num=0;score=0;strcpy(name," ");} int chack_name(Student str[],int n); void Set(int id,char *na,int sc); int get_score(){return score;} void print(){cout<<num<<'\t'<<name<<'\t'<<score<<endl;} }; void Student::Set(int id,char *na,int sc) { num=id; strcpy(name,na); score=sc; } int Student::chack_name(Student str[],int n) { for(int i=0;i<n;i++) if(str[i].num==str[i+1].num) return 1; else return 0; } Student max(Student *s,int n) { Student aa; int t=s[0].get_score(); for(int i=1;i<3;i++) if(t<s[i].get_score()){ t=s[i].get_score(); aa=s[i]; } return aa; } void main() { Student s[3],bb; int n,sc,m=0; char na[9]; cout<<"請輸入第"<<1<<"個學生的學號、成績、姓名:"; cin>>n; cin>>sc; cin>>na; s[0].Set(n,na,sc); for(int i=1;i<3;i++) { if(m){ cout<<"該學號已存在,請重新輸入!\n";i--; } cout<<"請輸入第"<<i+1<<"個學生的學號、成績、姓名:"; cin>>n; cin>>sc; cin>>na; s[i].Set(n,na,sc); m=s[i].chack_name(s,i); } bb=max(s,3); bb.print(); }
五、心得感受
可以在評論處寫下思考和程式設計此題的感受