100分求一段代码!
在c:\Complex.txt里面按行读取6个形如a+ib的复数
再分别把这6个复数作为字符串分别赋值给一个数组的头6个元素。
再把复数的实部和虚部分别转化成整数。
谢谢!
问题点数:100、回复次数:4Top
1 楼21st_centry_fox(花不归)回复于 2003-06-07 23:03:38 得分 100
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class Complex
{
int real;
int imag;
public:
Complex(int a, int b) : real(a), imag(b) {}
string toString() const;
friend ostream & operator << (ostream & out, const Complex & c);
};
string Complex::toString() const
{
string temp;
char* c = new char;
temp += itoa(real, c, 10);
temp += " + i";
temp += itoa(imag, c, 10);
return temp;
}
ostream & operator << (ostream & out, const Complex & c)
{
out << c.toString();
return out;
}
int main()
{
ifstream in("Complex.txt");
if (!in.is_open())
{
cout << "File does not exist!\n";
}
string line;
string complex_array[6];
int i = 0;
while (getline(in, line))
{
istringstream sin(line);
char c;
int m, n;
sin >> m >> c >> c >> n;
Complex cp(m, n);
complex_array[i++] = cp.toString();
}
for (int i = 0; i < sizeof(complex_array) / sizeof(*complex_array); i++)
cout << complex_array[i] << endl;
system("pause");
}
你说得不太清楚,不过我尽最大可能揣摩你的意思吧
你所要的是这个吧?Top
2 楼21st_centry_fox(花不归)回复于 2003-06-07 23:06:42 得分 0
在同一目录下保存如下复数(我假设你一行写一个,否则你应该事先告诉我分隔符是什么):
1+i2
2+i3
4+i5
6+i7
8+i9
10+i11
Top
3 楼21st_centry_fox(花不归)回复于 2003-06-07 23:13:39 得分 0
忘了说了
保存的文件名当然就是你说的Complex.txtTop
4 楼qrlvls( 空 气 )回复于 2003-06-07 23:58:52 得分 0
可以up吗?Top




