


6. 學生信息處理程序
總時間限制: 1000ms 內存限制: 1024kB
描述
實現一個學生信息處理程序,計算一個學生的四年平均成績。
要求實現一個代表學生的類,並且類中所有成員變量都是【私有的】。
補充下列程序中的 Student 類以實現上述功能。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
class Student {
// 在此處補充你的代碼
};
int main() {
Student student; // 定義類的對象
student.input(); // 輸入數據
student.calculate(); // 計算平均成績
student.output(); // 輸出數據
}
輸入
輸入數據為一行,包括:
姓名,年齡,學號,第一學年平均成績,第二學年平均成績,第三學年平均成績,第四學年平均成績。
其中姓名為由字母和空格組成的字符串(輸入保證姓名不超過20個字符,並且空格不會出現在字符串兩端),年齡、學號和學年平均成績均為非負整數。信息之間用逗號隔開。
輸出
輸出一行數據,包括:
姓名,年齡,學號,四年平均成績。
信息之間用逗號隔開。
樣例輸入
Tom Hanks,18,7817,80,80,90,70
樣例輸出
Tom Hanks,18,7817,80
提示
必須用類實現,其中所有成員變量都是私有的。
輸出結果中,四年平均成績不一定為整數。
完整代碼:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
class Student {
private:
char name[20];
int id,age;
float a,b,c,d;
char ch;
public:
void input()
{
cin.get(name, 21, ','); //看補充
cin>>ch>>age>>ch>>id>>ch>>a>>ch>>b>>ch>>c>>ch>>d;
};
float calculate()
{
float sum=a+b+c+d;
return sum/4;
};
bool t()
{
float sum=(a+b+c+d)/4;
int a=sum;
if (a==sum)
return true;
else
return false;
}
void output()
{
float mid=calculate();
cout<<name<<","<<age<<","<<id<<",";
if (t()==false)
{
printf("%.1f",mid);//沒有給iomanip頭文件,只能選擇C語言輸出
}
else
printf("%.0f",mid);
}
};
int main() {
Student student; // 定義類的對象
student.input(); // 輸入數據
student.calculate(); // 計算平均成績
student.output(); // 輸出數據
}
7.奇怪的類復制
描述
程序填空,使其輸出9 22 5
#include <iostream>
using namespace std;
class Sample {
public:
int v;
// 在此處補充你的代碼
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
輸入
無
輸出
9
22
5
樣例輸入
None
樣例輸出
9
22
5
完整代碼:
#include <iostream>
using namespace std;
class Sample {
public:
int v;
Sample(int n=0)
{
v=n;
}
Sample(const Sample &x)
{
v=x.v+2;
}
};
void PrintAndDouble(Sample o) //作為函數參數時候會調用復制構造函數
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
8.超簡單的復數類
描述
下面程序的輸出是:
3+4i
5+6i
請補足Complex類的成員函數。不能加成員變量。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
double r,i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
// 在此處補充你的代碼
};
int main() {
Complex a;
a = "3+4i"; a.Print();
a = "5+6i"; a.Print();
return 0;
}
輸入
無
輸出
3+4i
5+6i
樣例輸入
無
樣例輸出
3+4i
5+6i
完整代碼:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex
{
private:
double r,i;
public:
void Print()
{
cout << r << "+" << i << "i" << endl;
}
Complex() {};
Complex(char x[])
{
r=x[0]-'0';
i=x[2]-'0';
}
};
int main()
{
Complex a;
a = "3+4i";
a.Print();
a = "5+6i";
a.Print();
return 0;
}
9.哪來的輸出
描述
程序填空,輸出指定結果
#include <iostream>
using namespace std;
class A {
public:
int i;
A(int x) { i = x; }
// 在此處補充你的代碼
};
int main()
{
A a(1);
A * pa = new A(2);
delete pa;
return 0;
}
輸入
無
輸出
2
1
樣例輸入
無
樣例輸出
2
1
完整代碼:
#include <iostream>
using namespace std;
class A
{
public:
int i;
A(int x)
{
i = x;
}
~A()
{
cout<<i<<endl;
}
};
int main()
{
A a(1);
A * pa = new A(2);
delete pa;
return 0;
}
