面向对象程序设计作业 6.1~6.4
面向对象程序设计作业 6.1~6.4
面对对象基础题三道、继承多态一道。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class myDate{
private:
int year,month,day;
public:
myDate(int y,int m,int d) {
year=y;
month=m;
day=d;
}
void display() {
cout << setfill('0') << setw(4) << year << "/" ;
cout << setfill('0') << setw(2) << month << "/" ;
cout << setfill('0') << setw(2) << day << " ";
}
};
class myTime{
private:
int hour,minute;
public:
myTime(int h,int mi) {
hour=h;
minute=mi;
}
void display() {
cout << setfill('0') << setw(2) << hour << ":";
cout << setfill('0') << setw(2) << minute ;
}
};
int main() {
int y,m,d,h,mi,flag=1;
while (cin >> y >> m >> d >> h >> mi, y!=0 || m!=0 || d!=0 || h!=0 || mi!=0) {
if (flag==0) cout << endl;
myDate md(y,m,d);
myTime mt(h,mi);
md.display();
mt.display();
flag=0;
}
}
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
class shares {
private:
int num[9],ssize,left[9],right[9],sum[9];
public:
shares() {
ssize=0;
for (int i=0; i<9; i++) {
num[i]=0;
left[i]=0;
right[i]=0;
sum[i]=0;
}
}
void input(int item) {
num[ssize++]=item;
}
void counts() {
int i;
sum[0]=num[0];
for (i=1; i<7; i++) {
if (sum[i-1]>0) {
sum[i]=sum[i-1]+num[i];
right[i]=i;
left[i]=left[i-1];
}else {
sum[i]=num[i];
left[i]=i;
right[i]=i;
}
}
}
void display() {
int nmax=0,num=0;
for (int i=0; i<ssize; i++) {
if (sum[i]>nmax) {
nmax=sum[i];
num=i;
}
}
if (nmax<=0) cout << "won't buy!";
else cout << sum[num] << " " << left[num]+1 << " " << right[num]+1 ;
}
};
int main () {
int num[9],flag=0;
while (cin >> num[0] >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6],
num[0]!=0 || num[1]!=0 || num[2]!=0 || num[3]!=0 || num[4]!=0 || num[5]!=0 || num[6]!=0) {
if (flag==1) cout << endl;
shares s;
for (int i=0; i<7; i++) {
s.input(num[i]);
}
s.counts();
s.display();
flag=1;
}
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
class Group {
protected:
string name;
int win,lose;
public:
Group(string s) {
name=s;
}
void addnum(int w,int l) {
win += w;
lose += l;
}
virtual void display(){
cout << name;
}
~Group() {}
};
class GroupA:public Group {
public:
GroupA(string s):Group(s) {
win=0;
lose=0;
}
virtual void display() {
cout << name << " A " << 2*win-lose ;
}
~GroupA() {};
};
class GroupB:public Group {
public:
GroupB(string s):Group(s) {
win=0;
lose=0;
}
virtual void display() {
cout << name << " B " << win-lose ;
}
~GroupB () {}
};
int toInt(string num) {
stringstream s;
int number=0;
s << num;
s >> number;
return number;
}
int main() {
string s;
int i,j=0,flag=0;
Group *gp[20];
while (getline(cin,s),s[0]!='0') {
if (flag==1) cout << endl;
flag=1;
string win="",lose="",name="";
for (i=2; s[i]!=' '; i++) {
name+=s[i];
}
if (s[0]=='A') {
gp[j]=new GroupA(name);
for (i=i+1; s[i]!=' '; i++) {
win+=s[i];
}
for (i=i+1; s[i]!='\0'; i++) {
lose+=s[i];
}
gp[j]->addnum(toInt(win),toInt(lose));
gp[j]->display();
}else if (s[0]=='B') {
gp[j]=new GroupB(name);
while (s[i]!='\0') {
win="",lose="";
for (i=i+1; s[i]!=':'; i++) {
win+=s[i];
}
for (i=i+1; s[i]!=' '&&s[i]!='\0'; i++) {
lose+=s[i];
}
gp[j]->addnum(toInt(win),toInt(lose));
}
gp[j]->display();
}
j++;
}
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
int num[12];
class SeqList {
private:
int *data;
int ssize;
public:
SeqList(int n) {
ssize=n;
}
void setnum(int num[12]) {
data=new int[12];
for (int i=0; i<ssize; i++) {
*(data+i)=num[i];
}
}
void insertnum(int num) {
int i=0,j=0;
for (i=0; i<ssize; i++) {
if (*(data+i)>num) {
break;
}
}
ssize++;
for (j=ssize-1; j>i; j--) {
*(data+j)=*(data+j-1);
}
*(data+i)=num;
}
void print() {
cout << "size=" << ssize << ":" ;
for (int i=0; i<ssize; i++) {
if (i!=0) cout << " " ;
cout << *(data+i) ;
}
}
~SeqList() {}
};
void creat(SeqList *p) {
p->setnum(num);
}
void insert(SeqList *p,int item) {
p->insertnum(item);
}
void print(SeqList *p) {
p->print();
}
int main () {
int Case,j,flag=1;
cin >> Case;
while (Case--) {
if (flag==0) cout << endl;
int n,number;
cin >> n;
for (j=0; j<n; j++) {
cin >> num[j];
}
cin >> number;
SeqList *p,sl(n);
p=&sl;
creat(p);
insert(p,number);
print(p);
sl.~SeqList();
flag=0;
}
return 0;
}