提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
题目
`
一、5
#include
using namespace std;
class Complex
{
double real,image;
public:
Complex(double r=0,double i=0)
{
real=r;
image=i;
}
operator double(void)
{
return real;
}
friend istream& operator>>(istream &,Complex &);
friend ostream& operator<<(ostream &,Complex &);
};
istream& operator>>(istream &in,Complex &c)
{
in>>c.real>>c.image;
return in;
}
ostream& operator<<(ostream &out,Complex &c)
{
out<<c.real;
if(c.image>0)
out<<‘+’<<c.image<<‘i’;
else
if(c.image<0)
out<<c.image<<‘i’;
out<<endl;
return out;
}
int main()
{
Complex c1(1,2),c2(3);
double r;
r=c1;
cout<<r<<endl;
cout<<c2<<‘\t’<<c1<<endl;
cin>>c2;
cout<<c2;
return 0;
}
二、
6-8代码
代码如下:
6.#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char p[100];
cin.getline(p,100,'$');
ofstream out;
out.open("article.txt",ios::out);
out<<p<<endl;
out.close();
return 0;
}
7.#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
char ch;
fstream infile("t2.txt",ios::in);
if(!infile)
{
cout<<"Can not open input file"<<endl;
exit(1);
}
fstream outfile("t1.txt",ios::out|ios::app);
if(!outfile)
{
cout<<"Can not open output file"<<endl;
exit(2);
}
while(infile.get(ch))
outfile<<ch;
infile.close();
outfile.close();
return 0;
}
8.#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
int i=0,j=0;
char a[80],b[80];
fstream in1("string1.txt",ios::in);
fstream in2("string2.txt",ios::in);
fstream outf("string3.txt",ios::out);
in1.getline(a,80);
in2.get(b,80);
while(a[i]&&b[j])
{
if(a[i]<b[j]) outf<<a[i++];
else outf<<b[j++];
}
if(a[i]) outf<<b+j;
else outf<<a+i;
in1.close();
in2.close();
outf.close();
return 0;
}
2.文件
文件:
article:
I am a student.
I love you!
t1:
abcdefghijklmn
opqrst
t2:
hijklmn
opqrst
string1:
abbry
string2:
abitxz
string3:
aabbbirtxy
总结
所有的习题到此就结束了。