#include<iostream> #include<cstring> using namespace std; class String{ public: char Str[20]; friend istream &operator>>(istream &in, String &s); friend ostream &operator<<(ostream &out, String &s); }; istream &operator>>(istream &in,String &s){ cin>>s.Str; return in; } ostream &operator<<(ostream &out,String &s){ cout<<s.Str; return out; } template<class TNo, class TScore, int num>//TNo和TScore为参数化类型 class Student{ private: TNo StudentID; //参数化类型,存储姓名 TScore score[num]; //参数化类型数组,存储num门课程的分数 public: void Input();//数据的录入 TScore MaxScore(); //查找score的最大值并返回该值 void Update(TScore sscore,int i);//更新学生的第i门课程成绩为sscore void SelectSort(); //采用选择排序法对学生成绩进行升序排列 void Print(); //输出所有学生的信息 }; template<class TNo, class TScore, int num> void Student<TNo,TScore,num>::Input(){ for(int i=0;i<3;i++){ cin>>score[i]; } } template<class TNo, class TScore, int num> TScore Student<TNo,TScore,num>::MaxScore(){ TScore max=score[0]; for(int i=1;i<num;i++){ if(score[i]>max)max=score[i]; }return max; } template<class TNo, class TScore, int num> void Student<TNo,TScore,num>::Update(TScore sscore,int i) { cin>>i>>sscore; score[i-1]=sscore; } template<class TNo, class TScore, int num> void Student<TNo,TScore,num>::Print(){ cout<<MaxScore()<<endl; cout<<StudentID<<" "; for(int i=0;i<num;i++){ cout<<score[i]<<" "; } } template<class TNo, class TScore, int num> void Student<TNo,TScore,num>::SelectSort(){ for(int i=0;i<num-1;i++){ int minindex=i; for(int j=i+1;j<num;j++){ if(score[j]<score[minindex]) minindex=j; } if(minindex!=i){ double tmp=score[minindex]; score[minindex]=score[i]; score[i]=tmp; } } } int main(){ Student<String,float,3>student; student.Input(); double sscore;int i; student.Update(sscore,i); student.SelectSort(); student.Print(); return 0; }为什么更改分数输入不进去
时间: 2024-03-02 13:48:06 浏览: 64
在主函数中,您需要先输入更改的分数和对应的课程编号 i,然后再调用 `student.Update(sscore,i)` 函数来更新学生的分数。但是在主函数中,您没有先输入更改的分数和对应的课程编号 i,因此更改分数输入不进去。您可以尝试修改主函数如下:
```c++
int main(){
Student<String,float,3>student;
student.Input();
float sscore; int i;
cin >> sscore >> i; // 先输入更改的分数和对应的课程编号 i
student.Update(sscore,i);
student.SelectSort();
student.Print();
return 0;
}
```
这样就可以先输入更改的分数和对应的课程编号 i,再调用 `student.Update(sscore,i)` 函数来更新学生的分数了。
相关问题
#include <iostream> #include <cstdio> #include <cstring> using namespace std; class STRING{ char* pstr; public: STRING(const char* s="");//已实现 ~STRING();//已实现 friend istream& operator>>(istream& in, STRING& s); friend ostream& operator<<(ostream& os, const STRING& s); }; int main() { STRING s; cin >> s; cout << s << endl; return 0; } /* 请在这里填写答案,,你只需要实现输入输出运算符(>>、<<)的重载即可 */
可以这样实现输入输出运算符的重载:
```c++
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class STRING{
char* pstr;
public:
STRING(const char* s="");
~STRING();
friend istream& operator>>(istream& in, STRING& s);
friend ostream& operator<<(ostream& os, const STRING& s);
};
STRING::STRING(const char* s) {
pstr = new char[strlen(s)+1];
strcpy(pstr, s);
}
STRING::~STRING() {
delete []pstr;
}
istream& operator>>(istream& in, STRING& s) {
char buffer[1024];
in.getline(buffer, 1024);
s.pstr = new char[strlen(buffer)+1];
strcpy(s.pstr, buffer);
return in;
}
ostream& operator<<(ostream& os, const STRING& s) {
os << s.pstr;
return os;
}
int main() {
STRING s;
cin >> s;
cout << s << endl;
return 0;
}
```
在输入运算符中,我们使用`getline()`函数来读取输入的字符串,然后动态分配空间并将字符串拷贝到对象的数据成员中。
在输出运算符中,我们直接输出对象的数据成员。
注意,在使用动态分配空间时,需要在析构函数中释放空间。
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
好的,下面是填充后的代码,每个 TODO 已经被替换成相应的实现:
```c++
#include <iostream>
#include <cstring>
#include "ourstring.h"
#include "strlib.h"
using namespace std;
OurString::OurString(){
str = new char[1];
str[0] = '\0';
}
OurString::OurString(const char *str){
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
OurString::OurString(const OurString &dstr){
this->str = new char[dstr.length()+1];
strcpy(this->str, dstr.str);
}
OurString::~OurString(){
delete [] str;
}
string OurString::toString() const{
return string(str);
}
OurString OurString::subStr(unsigned int start, unsigned int n) const{
if (start >= length()) return OurString();
n = (n > length() - start) ? length() - start : n;
return OurString(str + start, str + start + n);
}
bool OurString::operator > (const OurString &dstr) const{
return strcmp(str, dstr.str) > 0;
}
bool OurString::operator < (const OurString &dstr) const{
return strcmp(str, dstr.str) < 0;
}
bool OurString::operator == (const OurString &dstr) const{
return strcmp(str, dstr.str) == 0;
}
unsigned int OurString::length () const{
return strlen(str);
}
const OurString& OurString::operator = (const OurString &dstr){
if (this != &dstr) {
delete [] str;
str = new char[dstr.length()+1];
strcpy(str, dstr.str);
}
return *this;
}
const OurString& OurString::operator = (const char *str){
if (this->str != str) {
delete [] this->str;
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
return *this;
}
char& OurString::operator[](int index){
return str[index];
}
const OurString OurString::operator + (const OurString &dstr) const{
OurString s;
s.str = new char[length() + dstr.length() + 1];
strcpy(s.str, str);
strcat(s.str, dstr.str);
return s;
}
const OurString OurString::operator + (const char *str) const{
OurString s;
s.str = new char[length() + strlen(str) + 1];
strcpy(s.str, this->str);
strcat(s.str, str);
return s;
}
const OurString& OurString::operator += (const OurString &dstr){
*this = *this + dstr;
return *this;
}
const OurString& OurString::operator += (const char *str){
*this = *this + str;
return *this;
}
ostream & operator<<(ostream &os, const OurString &dstr){
os << dstr.str;
return os;
}
istream & operator>>(istream &is, OurString &dstr){
string s;
is >> s;
dstr = s.c_str();
return is;
}
```
阅读全文