注册 登录
编程论坛 C++教室

初学者的请教

也不懂 发布于 2008-03-29 15:58, 771 次点击
浮点A+B
Time Limit:1000MS  Memory Limit:32768K


Description:
小红在数学方面很有天赋,虽然还只有一年级,其他同学还只能计算十以内的加减,可她却能够进行小数的加减了。现在,给你两个正的小数A和B,你的任务是代表小红计算出A+B的值。

Input:
本题目包含多组测试数据,请处理到文件结束。每一组测试数据包含正小数A和B。
Output:
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
Sample Input:
1.1 2.9
1.1111 2.3444
1 1.1
1 2
Sample Output:
4
3.4555
2.1
3

怎么做????    谢谢!!
2 回复
#2
zhangcaike20082008-10-16 20:45
我发现这样的 问题一般都没人回答……
#3
braveboy20082008-10-19 18:37
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    double d;
    string line;
    ifstream in("input.txt");
    ofstream out("output.txt");

    if (!in)                       //文件打开失败
    {
        cerr << "opening file failed!" << endl;
        return 1;
    }
     
    while (!in.eof())
    {   
        vector<double> vec;
        double sum = 0;
        getline(in,line);
        if (!line.empty())
        {
            istringstream stream(line);

            while (stream >> d)
            {
                vec.push_back(d);
            }

            for (vector<double> :: iterator iter=vec.begin(); iter!=vec.end(); ++iter)
            {
                sum += *iter;
            }
            
            out << sum << endl;
        }

    }

    return 0;
}
1