C++ <getline及atoi>

本文演示了如何使用C++中的atoi函数将C风格的字符串转换为整数,并展示了简单的加法运算。此外,还介绍了如何利用C++标准库中的string类进行输入输出操作。
#include <iostream>
#include <string>  // 用于C++ string类的操作
#include <cctype> // 用于C字符串的拷贝、比较、追加 等,strcpy(), strcmp(), strcat()
#include <cstdlib> // 用于exit(EXIT_FAILURE)及atoi即:alpha to interge 的缩写

int main() {
    using std::cout;
    using std::cin;
    using std::string;

    /* 7890 + 943210 = 951100
     * atoi 函数只能用C类型字符串转数字,不能对string类操作
     * 因为字符串类型最后一位要存放结束符'\0'所以定义数组大小时应考虑为存放'\0'预留空间
     */
    char str1[5] = "7890";
    char str2[7] = "943210";
    int n1 = atoi(str1);
    int n2 = atoi(str2);
    int sum = n1+ n2;
    cout << n1 << " + " << n2 << " = " << sum << '\n';

    /* Enter your name: may be you are right
     * But i can't belive it.
     * may be you are right
     * But
     */
    string a, b;
    cout << "Enter your name: ";
    getline(cin, a) >> b;
    cout << a << '\n' << b << '\n';

    /* Are you ok?
     * I am not sure, maybe i need rest a monments.
     * I am not s    
     */
    string c;
    cout << "Are you ok?\n";
    getline(cin, c, 'u');
    cout << c << '\n';
}
#include <iostream> #include <fstream> #include <string> #include <cstdlib> const int MAX = 100; // 定义书籍结构体 struct Book { std::string name; std::string author; // 添加作者名称成员变量 int price; std::string number; }; // 定义书籍数组结构体 struct Arraybooks { struct Book book[MAX]; int length; }; // 判断书籍是否存在 int isExist(Arraybooks* abs, std::string name) { for (int i = 0; i < abs->length; ++i) { if (abs->book[i].name == name) { return i; } } return -1; } // 添加书籍 void addBooks(Arraybooks* abs) { if (abs->length >= MAX) { std::cout << "书籍已满" << std::endl; } else { std::string name; std::cout << "请输入书籍的名字: " << std::endl; std::cin >> name; abs->book[abs->length].name = name; std::string author; std::cout << "请输入书籍的作者: " << std::endl; std::cin >> author; abs->book[abs->length].author = author; // 保存作者名称 int price; std::cout << "请输入书的价格: " << std::endl; std::cin >> price; abs->book[abs->length].price = price; std::string num; std::cout << "请输入书的编号: " << std::endl; std::cin >> num; abs->book[abs->length].number = num; abs->length++; std::cout << "添加成功" << std::endl; } } // 显示书籍 void showbooks(Arraybooks* abs) { if (abs->length == 0) { std::cout << "书架为空" << std::endl; } else { for (int i = 0; i < abs->length; ++i) { std::cout << "书籍名字 : " << abs->book[i].name << "\t"; std::cout << "书籍作者 : " << abs->book[i].author << "\t"; // 显示作者名称 std::cout << "书籍价格 : " << abs->book[i].price << "\t"; std::cout << "书籍编号 : " << abs->book[i].number << std::endl; } } } // 查找书籍 void CheckBook(Arraybooks* abs) { std::cout << "输入你要查找的书籍" << std::endl; std::string name; std::cin >> name; int ret = isExist(abs, name); if (ret != -1) { std::cout << "书名为:" << abs->book[ret].name << "\t"; std::cout << "书籍作者 : " << abs->book[ret].author << "\t"; // 显示作者名称 std::cout << "书价格为:" << abs->book[ret].price << "\t"; std::cout << "书编号为:" << abs->book[ret].number << std::endl; } else { std::cout << "查无此书" << std::endl; } } // 修改书籍信息 void changebooks(Arraybooks* abs) { std::cout << "输入你要修改的图书" << std::endl; std::string name; std::cin >> name; int ret = isExist(abs, name); if (ret != -1) { std::cout << "请输入名字: " << std::endl; std::string newName; std::cin >> newName; abs->book[ret].name = newName; std::cout << "请输入作者: " << std::endl; std::string newAuthor; std::cin >> newAuthor; abs->book[ret].author = newAuthor; // 修改作者名称 std::cout << "请输入价格: " << std::endl; int price; std::cin >> price; abs->book[ret].price = price; std::cout << "请输入编号: " << std::endl; std::string num; std::cin >> num; abs->book[ret].number = num; std::cout << "修改成功" << std::endl; } else { std::cout << "查无此书" << std::endl; } } // 删除书籍 void deletebooks(Arraybooks* abs) { std::cout << "输入你要删除的图书" << std::endl; std::string name; std::cin >> name; int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->length - 1; ++i) { abs->book[i] = abs->book[i + 1]; } abs->length--; std::cout << "删除成功" << std::endl; } else { std::cout << "查无此书" << std::endl; } } // 保存书籍信息到文件 void saveBooks(Arraybooks* abs) { std::ofstream file("books.txt"); if (file.is_open()) { for (int i = 0; i < abs->length; ++i) { file << abs->book[i].name << "," << abs->book[i].author << "," << abs->book[i].price << "," << abs->book[i].number << std::endl; } file.close(); } else { std::cout << "无法打开文件保存图书信息。" << std::endl; } } // 从文件加载书籍信息 void loadBooks(Arraybooks* abs) { std::ifstream file("books.txt"); if (file.is_open()) { std::string line; while (std::getline(file, line)) { size_t pos1 = line.find(','); size_t pos2 = line.find(',', pos1 + 1); size_t pos3 = line.find(',', pos2 + 1); if (pos1 != std::string::npos && pos2 != std::string::npos && pos3 != std::string::npos) { abs->book[abs->length].name = line.substr(0, pos1); abs->book[abs->length].author = line.substr(pos1 + 1, pos2 - pos1 - 1); // 加载作者名称 abs->book[abs->length].price = atoi(line.substr(pos2 + 1, pos3 - pos2 - 1).c_str()); abs->book[abs->length].number = line.substr(pos3 + 1); abs->length++; } } file.close(); } } // 主函数 int main() { Arraybooks abs = { {}, 0 }; loadBooks(&abs); int select = 0; while (true) { std::cout << "1. 添加书籍" << std::endl; std::cout << "2. 显示书籍" << std::endl; std::cout << "3. 查找书籍" << std::endl; std::cout << "4. 修改书籍信息" << std::endl; std::cout << "5. 删除书籍" << std::endl; std::cout << "0. 退出" << std::endl; std::cout << "请输入你的选择:" << std::endl; std::cin >> select; switch (select) { case 1: addBooks(&abs); break; case 2: showbooks(&abs); break; case 3: CheckBook(&abs); break; case 4: changebooks(&abs); break; case 5: deletebooks(&abs); break; case 0: saveBooks(&abs); std::cout << "欢迎下次使用" << std::endl; return 0; default: std::cout << "输入有误,请重新输入" << std::endl; } } return 0; } 在此基础上添加持久性存储图书功能
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值