100分求一段代码!

llcnllcn 2003-06-03 09:39:24
在c:\Complex.txt里面按行读取6个形如a+ib的复数
再分别把这6个复数作为字符串分别赋值给一个数组的头6个元素。
再把复数的实部和虚部分别转化成整数。

谢谢!
...全文
60 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qrlvls 2003-06-07
  • 打赏
  • 举报
回复
可以up吗?
21st_centry_fox 2003-06-07
  • 打赏
  • 举报
回复
忘了说了
保存的文件名当然就是你说的Complex.txt
21st_centry_fox 2003-06-07
  • 打赏
  • 举报
回复
在同一目录下保存如下复数(我假设你一行写一个,否则你应该事先告诉我分隔符是什么):
1+i2
2+i3
4+i5
6+i7
8+i9
10+i11
21st_centry_fox 2003-06-07
  • 打赏
  • 举报
回复
#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");
}
你说得不太清楚,不过我尽最大可能揣摩你的意思吧
你所要的是这个吧?

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧