输入任意数量双精度数存入容器并对双精度数容器内的数求和
时间:2010-10-23 来源:幻魇
#include<iostream> #include<vector> #include<cstdlib> using std::vector; using std::cin; using std::cout; using std::istream; vector<double> a1; double sum1(vector<double>::iterator t1,vector<double>::iterator t2); double sum2(vector<double>& vecc); int main() { double tmp_lon; char test_state; cin.unsetf(istream::skipws); while (cin&&cin>>tmp_lon) { cin>>test_state; if (test_state=='cr'||test_state=='\n') cin.setstate(istream::eofbit); a1.push_back(tmp_lon); } if (a1.begin()==a1.end()) cout<< 0 <<std::endl; else { vector<double>::iterator iter1=a1.begin(),iter2=a1.end(); cout<<std::endl<<sum1(iter1,iter2)<<std::endl; cout<<sum2(a1)<<std::endl; } system("pause"); return 0; } double sum1(vector<double>::iterator t1,vector<double>::iterator t2) { double tm_sum=0; while (t1!=t2) { tm_sum+=*t1; ++t1; } return tm_sum; } double sum2(vector<double>& vecc) { vector<double>::iterator tmp_iter=vecc.begin(); double tmp_sum=0; while (tmp_iter!=vecc.end()) { tmp_sum+=*tmp_iter; ++tmp_iter; } return tmp_sum; }
相关阅读 更多 +