1.改错
#include
using namespace std;
class Date{
int year;
int month;
int day;
public:
// ERROR **********found**********
Date(int yyyy, int mm, int dd):year(yyyy),month(mm),day(dd){}
bool isLaterThan(Date dt)const{ //当前日期晚于日期dt时返回true
if(year!=dt.year) return year>dt.year;
if(month!=dt.month) return month>dt.month;
return day>dt.day;
}
bool isEarlyThan(Date dt)const{ //当前日期早于日期dt时返回true
if(year!=dt.year) return year
using namespace std;
const int MAXNUM = 100;
class Set {
private:
int num; // 元素个数
char setdata[MAXNUM]; // 字符数组,用于存储集合元素
public:
Set(char *s); // 构造函数,用字符串s构造一个集合对象
bool InSet(char c); // 判断一个字符c是否在集合中,若在,返回true,否则返回false
void Print() const; // 输出集合中所有元素
};
Set::Set(char *s)
{
num = 0;
while (*s){
//**********found**********
if (InSet(*s)==false) // TODO: 添加代码,测试元素在集合中不存在
//**********found**********
setdata[num++]=*s; // TODO: 添加一条语句,加入元素至集合中
s++;
}
}
bool Set::InSet(char c)
{
for (int i = 0; i < num; i++)
//**********found**********
if (c==setdata[i]) // TODO: 添加代码,测试元素c是否与集合中某元素相同
//**********found**********
return true; // TODO: 添加一条语句,进行相应处理
return false;
}
void Set::Print() const
{
cout << "Set elements: " << endl;
for(int i = 0; i < num; i++)
cout << setdata[i] << ' ';
cout << endl;
}
int main()
{
char s[MAXNUM];
cin.getline(s, MAXNUM-1); // 从标准输入中读入一行
Set setobj(s); // 构造对象setobj
setobj.Print(); // 显示对象setobj中内容
return 0;
}
3
#include
#include
using namespace std;
class IntArray {
public:
IntArray(unsigned int n)
{
size = n;
data = new int[size];
}
~IntArray() { delete [] data; }
int getSize() const { return size; }
int& operator[](unsigned int i) const { return data[i]; }
void swap(int i, int j)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
void sort();
friend ostream& operator<< (ostream &os, const IntArray &array)
{
for (int i = 0; i < array.getSize(); i++)
os << array[i] << ' ';
return os;
}
private:
int *data;
unsigned int size;
};
void readFromFile(const char*, IntArray&);
void writeToFile(char *, const IntArray &);
#include
#include "IntArray.h"
void IntArray::sort()
{
//********333********
for(int i=0;idata[j])
swap(data[i],data[j]);
//********666********
}
void readFromFile(const char* f, IntArray& m)
{
ifstream infile(f);
if (infile.fail()) {
cerr << "打开输入文件失败!";
return;
}
int i = 0;
while (!infile.eof()) {
infile >> m[i++];
}
}
int main()
{
IntArray a1(3), a2(7), a3(1000);
a1[0] = 3, a1[1] = 1, a1[2] = 2;
a2[0] = 5, a2[1] = 2, a2[2] = 7, a2[3] = 4, a2[4] = 1, a2[5] = 6, a2[6] = 3;
readFromFile("in.dat", a3);
cout << "--- 排序前 ---
";
cout << "a1 = " << a1 << endl;
cout << "a2 = " << a2 << endl << endl;
a1.sort();
a2.sort(