c++作业
输入输出流与文件操作
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
//职工
struct Person
{
int m_Id;
string m_Name;
int m_Age;
int m_Wage;
Person(int i, string n, int a, int w): m_Id(i), m_Name(n), m_Age(a), m_Wage(w) { }
void PrintInfo()
{
cout << "===================" << endl;
cout << "号码:\t" << this -> m_Id << endl;
cout << "名字:\t" << this -> m_Name << endl;
cout << "年龄:\t" << this -> m_Age << endl;
cout << "工资:\t" << this -> m_Wage << endl;
cout << "===================" << endl;
}
};
Person *p[3] =
{
new Person(101, "小黄", 23, 3100),
new Person(102, "中黄", 41, 6600),
new Person(105, "大黄", 55, 7000),
};
int main()
{
//对5组数据按ID进行排序
int i, j;
Person *temp;
for(i = 0; i < 3; i++)
{
for(j = i + 1; j < 3; j++)
{
if(p[i] -> m_Id > p[j] -> m_Id)
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
//将排好序的数据存入到文件f.dat
ofstream outfile1("f.dat", ios :: out);
if(!outfile1)
{
cerr << "f.dat 打开错误" << endl;
exit(1);
}
for(i = 0; i < 3; i++)
{
outfile1 << p[i] -> m_Id << " "
<< p[i] -> m_Name << " "
<< p[i] -> m_Age << " "
<< p[i] -> m_Wage << endl;
}
outfile1.close();
//再写入两个职工信息
//示例输入 108 Titus 40 4000 109 Tulius 35 5000
cout << "请输入2个职工信息:";
ofstream outfile2("f.dat", ios :: out | ios :: app);
if(!outfile2)
{
cerr << "f.dat 打开错误" << endl;
exit(1);
}
temp = new Person(0, "", 0, 0);
cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
outfile2 << temp -> m_Id << " "
<< temp -> m_Name << " "
<< temp -> m_Age << " "
<< temp -> m_Wage << endl;
cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
outfile2 << temp -> m_Id << " "
<< temp -> m_Name << " "
<< temp -> m_Age << " "
<< temp -> m_Wage << endl;
outfile2.close();
//输出文件中的全部职工数据
ifstream infile1("f.dat", ios :: in);
if(!infile1)
{
cerr << "f.dat 打开错误" << endl;
exit(1);
}
for(i = 0; i < 5; i++)
{
infile1 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
temp -> PrintInfo();
}
infile1.close();
//输入一个ID检测是否存在
int id;
cout << "请输入一个号码:";
cin >> id;
bool isFound = false;
ifstream infile2("f.dat", ios :: in);
if(!infile2)
{
cerr << "f.dat 打开错误" << endl;
exit(1);
}
for(i = 0; i < 5; i++)
{
infile2 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;
if(temp -> m_Id == id)
{
temp -> PrintInfo();
isFound = true;
break;
}
}
if(!isFound)
{
cerr << "没有找到这个人" << endl;
}
infile2.close();
return 0;
}
类和对象的进阶
- 定义一个复数类,重载运算符“+”、“-”、“”、“/”,使这四个运算符能应用于复数的加减乘除运算。 复数加法:m=a+bi ,n=c+di; m+n=(a+c)+(b+d)i; 复数减法:m=a+bi ,n=c+di; m-n=(a-c)+(b-d)i; 复数乘法:m=a+bi ,n=c+di; mn=(ac-bd)+(bc+ad)i;
复数除法:m=a+bi ,n=c+di; m/n=((ac+bd)+(bc-ad)i)/(c2+d2)。 - 参考教材例题11.1,11.2,11.3,11.5,分别采用公有继承,私有继承和保护继承的方式,在程序运行中,输入num,name,sex,age,addr的值,程序应输出以上五个数据的数值;
- 3.将教材例9.13中的Time类声明为Date的友元类,输出年月日和时分秒。
第一题
#include <iostream>
#include<cstdio>
using namespace std;
class Complex
{
public:
Complex(double x = 0, double y = 0) :real(x), imag(y) {}
friend Complex operator * (Complex &m, Complex &n);
friend Complex operator / (Complex &m, Complex &n);
Complex operator + (Complex &n);
Complex operator - (Complex &n);
void display();
private:
double real;
double imag;
};
Complex operator * (Complex &m, Complex &n)
{
return Complex(m.real*n.real - m.imag*n.imag, m.real*n.imag + m.imag*n.real);
}
Complex operator / (Complex &m, Complex &n)
{
Complex c;
c.real = (m.real*n.real + m.imag*n.imag) / (n.real*n.real + n.imag*n.imag);
c.imag = (m.imag*n.real - m.real*n.imag) / (n.real*n.real + n.imag*n.imag);
return c;
}
Complex Complex ::operator + (Complex &n)
{
return Complex(real + n.real, imag + n.imag);
}
Complex Complex ::operator - (Complex &n)
{
return Complex(real - n.real, imag - n.imag);
}
void Complex::display()
{
cout << "<" << real << "," << imag << "i>" << endl;
}
int main()
{
int a,bi,c,di;
cout<<"请输入m的实部和虚部"<<endl;
cin>>a>>bi;
cout<<"请输入n的实部和虚部"<<endl;
cin>>c>>di;
Complex m(a, bi), n(c, di), c3;
cout<<"为"<<endl;
m.display();
cout<<"n为"<<endl;
n.display();
cout<<"计算结果为"<<endl;
c3 = m + n;
c3.display();
c3 = m - n;
c3.display();
c3 = m * n;
c3.display();
c3 = m / n;
c3.display();
return 0;
}
第二题
#include <iostream>
using namespace std;
class Student
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
void display( )
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private :
int num;
string name;
char sex;
};
class Student1:public Student
{
public:
void get_value1()
{
get_value();
cin>>age>>addr;
}
void display1()
{
display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;
}
private:
int age;
string addr;
};
class Student2:private Student
{
public:
void get_value1()
{
get_value();
cin>>age>>addr;
}
void display1()
{
display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;
}
private:
int age;
string addr;
};
class Student3:protected Student
{
public:
void get_value1()
{
get_value();
cin>>age>>addr;
}
void display1()
{
display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;
}
private:
int age;
string addr;
};
int main()
{
Student1 stud1;
stud1.get_value1();
stud1.display1();
Student2 stud2;
stud2.get_value1();
stud2.display1();
Student3 stud3;
stud3.get_value1();
stud3.display1();
return 0;
}
第三题
#include <iostream>
using namespace std;
class Date;
class Time
{
public:
Time(int h, int m, int s): hour(h), minute(m), sec(s) {};
void display(Date &);
private:
int hour;
int minute;
int sec;
};
class Date
{
public:
friend Time;
Date(int m, int d, int y): month(m), day(d), year(y) {};
private:
int month;
int day;
int year;
};
void Time::display(Date &d)
{
cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Date d1(01, 01, 2019);
Time t1(15, 57, 56);
t1.display(d1);
return 0;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 陈子琦的博客!
评论