用迭代器求vector<double>中元素的和,while语句写循环

本文介绍了一个使用C++实现的简单程序,该程序通过读取用户输入的一系列浮点数并将其存储在一个std::vector容器中,然后计算这些数值的总和并输出结果。程序展示了如何使用迭代器来遍历容器以及如何处理基本的输入输出操作。
#include <iostream>
#include<vector>
using namespace std;
double sum(vector<double>::iterator begin,vector<double>::iterator end )
{
    double result=0;
    while(begin!=end)
        result+=*begin++;
    return result;
}


int main()
{
    vector<double>ivec1;
    cout<<"please input double type for vector(ctrl+z to end):"<<endl;
    double val;
    while(cin>>val){
        ivec1.push_back(val);
    }
    cout<<sum(ivec1.begin(),ivec1.end())<<endl;
    return 0;
}
帮我改对#include<iostream> #include<fstream> #include<string> #include<vector> #include <algorithm> using namespace std; enum Grade { E=0,D,C,B,A }; class Student { public: int number; string name; double score; }; ostream& operator<<(ostream& out, Student& p) { return out; } bool compare(const Student& a, const Student& b) { return a.number < b.number; } void printVector(vector<Student>&students) { for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector<Student>students; printVector(students); } int main() { // 学生信息导入 ifstream infile; infile.open("student.txt",ios::in); if (!infile.is_open()) { cout << "文件打开失败" << endl; } vector<Student> students(27); char buf[] = { 0 }; while (infile>>buf) { cout << buf << endl; } infile.close(); // 成绩查询 int choice = 0; while (choice != 4) { cout << "请输入您要进行的操作:\n"; cout << "1. 按学号查询\n"; cout << "2. 按姓名查询\n"; cout << "3. 统计班级成绩\n"; cout << "4. 退出程序\n"; cin >> choice; if (choice == 1) { int number; cout << "请输入学号:\n"; cin >> number; auto it = find_if(students.begin(), students.end(), [number](const Student& s) { return s.number == number; }); if (it != students.end()) { cout << "学号\t姓名\t成绩\n"; cout << it->number << "\t" << it->name << "\t" << it->score << "\n"; } else { cout << "查无此人!\n"; } } else if (choice == 2) { string name; cout << "请输入姓名:\n"; cin >> name; auto it = find_if(students.begin(), students.end(), [name](const Student& s) { return s.name == name; }); if (it != students.end()) {
05-30
检查错误并补全代码: #include <bits/stdc++.h> #include <windows.h> #include <ctime> #include <filesystem> using namespace std; typedef long long ll; typedef string str; typedef __int128 int128; typedef double dou; typedef long double ldou; str dir_path = “D:\user”; ifstream fin; ofstream fout; vector<str> todo; // 清屏函数 void clear() { #ifdef _WIN32 system(“cls”); #else system(“clear”); #endif } // 等待函数 void wait(dou second) { Sleep(second * 1000); } // 暂停函数 void stop() { cout << “Press any key to continue…”; cin.ignore(); cin.get(); } // 快速幂运算函数(支持负指数) ldou qp(ldou a, ll b) { if (b == 0) return 1.0L; if (b < 0) { // 处理负指数 a = 1.0L / a; b = -b; } ldou res = 1.0L; while (b > 0) { if (b % 2 == 1) res *= a; a *= a; b /= 2; } return res; } // 输入输出模板函数 template<typename T> T ioput(const string& prompt) { T input; cout << prompt; cin >> input; return input; } // 简单计算函数 template<typename T> dou calculate(T num1, char op, T num2) { switch (op) { case ‘+’: return num1 + num2; case ‘-’: return num1 - num2; case ‘*’: return num1 * num2; case ‘/’: if (num2 == 0) { cout << “错误:除数不能为零!” << endl; return 0; } return num1 / num2; case ‘^’: return qp(num1, num2); // 使用改进的快速幂函数 default: cout << “错误:无效的运算符!” << endl; return 0; } } // 开始界面 void begin() { system((“color 09”).c_str()); if (!filesystem::exists(dir_path)) { // 检查目录是否存在 filesystem::create_directory(dir_path); // 创建目录 } filesystem::current_path(dir_path); // 切换工作目录 cout << “欢迎使用便携备忘录0.0.0” << endl; wait(0.5); } // 加载任务函数 void load_tasks(const str& user_name) { str file_name = user_name + “_tasks.txt”; if (filesystem::exists(file_name)) { ifstream fin(file_name.c_str()); // 使用 .c_str() 转换为 const char* str line; while (getline(fin, line)) { todo.push_back(line); } fin.close(); } } // 登录界面 str login() { str user; system(("cd " + dir_path).c_str()); // 使用 .c_str() 转换为 const char* while (true) { clear(); user = ioput<str>(“请输入用户ID:”); str file_name = user + “_password.txt”; fin.open(file_name.c_str(), ios::in); // 使用 .c_str() 转换为 const char* if (!fin) { // 如果文件不存在 fin.close(); fout.open(file_name.c_str(), ios::out); // 使用 .c_str() 转换为 const char* str password = ioput<str>(“输入密码来创建账户:”); fout << password; fout.close(); clear(); cout << “账户创建成功!” << endl; break; } else { // 文件已存在 str password; fin >> password; fin.close(); str temp_password = ioput<str>(“请输入密码登录:”); clear(); if (temp_password == password) { cout << “登录成功!” << endl; load_tasks(user); // 加载任务 break; } else { cout << “密码错误,请重新登录” << endl; wait(0.5); } } } return user; } //输出任务列表 void output() { if(todo.size()==0) { cout<<“当前没有任务\n”; return; } else { for(int i=0;i<todo.size();i++) { cout<<i+1<<“、”<<todo[i]<<endl; } } } // 主功能菜单 void start() { while (true) { clear(); str s = ioput<str>("放入任务队列(1)\n进行简单计算(2)\n退出(3)\n请选择: "); clear(); while (s == “1” || s == “管理任务列表”) { clear(); str choose = ioput<str>("1、添加任务\n2、删除任务\n3、任务优先级排序\n4、退出\n请输入选项: "); clear(); if(choose==“1”||choose==“添加任务”) { str task=ioput<str>(“请输入任务:\n”); clear(); todo.push_back(task); cout<<“任务添加成功\n”; stop(); } else if(choose==“2”||choose==“删除任务”) { int number=ioput<int>(“请输入要删除的任务编号”); clear(); str do_=ioput<str>(“确定删除吗:Y/N”); if(do_==“N”) { continue; } todo.erase(todo+number-1); cout<<“删除成功\n”; stop(); } else if(choose==“3”||choose==“任务优先级排序”) { cout<<“请安要输入编号:\n”; str first=ioput<str>(“操作任务:”); str second=ioput<str>(“被操作任务:”); int tool=ioput<int>(“操作方式\n:1-移动到被操作任务前\n2-交换\n”); clear(); if(tool==1) { } else if() { } else { cout<< } cout<<"操作成功"; stop(); } else if(choose=="4"||choose=="退出") { } clear(); } if (s == "2" || s == "进行简单计算") { dou num1 = ioput<dou>("请输入第一个数字: "); char op = ioput<char>("请输入运算符 (+, -, *, /, ^): "); dou num2 = ioput<dou>("请输入第二个数字: "); dou result = calculate(num1, op, num2); if (result != 0) { // 如果没有发生错误,则显示结果 cout << "计算结果: " << result << endl; } stop(); } else if (s == "3" || s == "退出") { clear(); cout << "感谢使用便携备忘录,再见!" << endl; stop(); break; } else { cout << "未知的指令,请重新输入" << endl; stop(); } } } // 保存函数 void save(const str& user_name) { clear(); system(("cd " + dir_path).c_str()); // 使用 .c_str() 转换为 const char* str file_name = user_name + “_tasks.txt”; fout.open(file_name.c_str(), ios::out); // 使用 .c_str() 转换为 const char* for (const auto& task : todo) { fout << task << endl; } fout.close(); cout << “任务已保存” << endl; wait(0.1); clear(); } // 主函数 int main() { begin(); str user_name = login(); start(); save(user_name); // 保存任务列表 return 0; }
最新发布
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值