如何通过读取文本文件实现栈与队列的元素增删操作?
嘿,你已经搞定栈和队列的基础功能了,这步已经成功一半啦!剩下的文件读取和指令解析其实没那么复杂,我给你拆解成几个清晰的步骤,再附上代码示例,你直接套进去就行~
读取并解析test.dat的完整方案
1. 先搞定文件读取
不管用C还是C++,第一步都是打开文件并逐行读取。一定要记得做文件打开失败的判断,不然程序遇到文件不存在的情况直接崩溃,很不友好。
2. 拆分每行的指令
每一行的指令格式大概是指令 参数,比如add 3或者remove stack。我们需要把指令和参数拆分开:
- C++里用
istringstream来拆分字符串,简单又靠谱; - C语言里用
sscanf或者手动分割字符串(不过sscanf更省心)。
3. 根据指令调用栈/队列的方法
拿到指令和参数后,直接调用你已经实现的push/pop(栈)、enqueue/dequeue(队列)方法就行。还要注意处理异常情况:比如栈/队列为空时执行remove,或者指令格式错误的情况,给点提示信息方便调试。
C++代码示例(假设你已经实现了Stack和Queue类)
#include <iostream> #include <fstream> #include <sstream> #include <string> // 引入你自己的栈和队列头文件 #include "Stack.h" #include "Queue.h" int main() { Stack myStack; Queue myQueue; std::ifstream inputFile("test.dat"); std::string currentLine; // 检查文件是否成功打开 if (!inputFile.is_open()) { std::cerr << "Error: 无法打开test.dat文件,请检查路径是否正确!" << std::endl; return 1; } // 逐行读取并处理指令 while (std::getline(inputFile, currentLine)) { std::istringstream lineStream(currentLine); std::string command; int targetNum; // 读取第一个单词作为指令 lineStream >> command; if (command == "add") { // 读取要添加的整数 if (lineStream >> targetNum) { myStack.push(targetNum); myQueue.enqueue(targetNum); std::cout << "操作成功:将" << targetNum << "压入栈并加入队列" << std::endl; } else { std::cerr << "指令格式错误:" << currentLine << " 正确格式应为add 整数" << std::endl; } } else if (command == "remove") { std::string containerType; lineStream >> containerType; if (containerType == "stack") { if (!myStack.isEmpty()) { int poppedVal = myStack.pop(); std::cout << "操作成功:从栈中弹出" << poppedVal << std::endl; } else { std::cerr << "警告:栈为空,无法执行remove操作!" << std::endl; } } else if (containerType == "queue") { if (!myQueue.isEmpty()) { int dequeuedVal = myQueue.dequeue(); std::cout << "操作成功:从队列中取出" << dequeuedVal << std::endl; } else { std::cerr << "警告:队列为空,无法执行remove操作!" << std::endl; } } else { std::cerr << "未知容器类型:" << containerType << " 可选值为stack或queue" << std::endl; } } else { std::cerr << "未知指令:" << command << " 可选指令为add或remove" << std::endl; } } // 关闭文件 inputFile.close(); return 0; }
C语言代码示例(假设你已经实现了栈和队列的C语言版本)
#include <stdio.h> #include <string.h> // 引入你自己的栈和队列头文件 #include "stack_queue.h" int main() { Stack myStack; Queue myQueue; // 初始化栈和队列(根据你自己的实现调整) initStack(&myStack); initQueue(&myQueue); FILE *file = fopen("test.dat", "r"); if (!file) { fprintf(stderr, "Error: 无法打开test.dat文件,请检查路径是否正确!\n"); return 1; } char lineBuffer[100]; while (fgets(lineBuffer, sizeof(lineBuffer), file)) { // 去掉换行符,避免干扰解析 lineBuffer[strcspn(lineBuffer, "\n")] = '\0'; char command[20]; int targetNum; char containerType[20]; // 尝试解析add指令 if (sscanf(lineBuffer, "%s %d", command, &targetNum) == 2 && strcmp(command, "add") == 0) { push(&myStack, targetNum); enqueue(&myQueue, targetNum); printf("操作成功:将%d压入栈并加入队列\n", targetNum); } // 尝试解析remove指令 else if (sscanf(lineBuffer, "%s %s", command, containerType) == 2 && strcmp(command, "remove") == 0) { if (strcmp(containerType, "stack") == 0) { if (!isEmptyStack(&myStack)) { int poppedVal = pop(&myStack); printf("操作成功:从栈中弹出%d\n", poppedVal); } else { fprintf(stderr, "警告:栈为空,无法执行remove操作!\n"); } } else if (strcmp(containerType, "queue") == 0) { if (!isEmptyQueue(&myQueue)) { int dequeuedVal = dequeue(&myQueue); printf("操作成功:从队列中取出%d\n", dequeuedVal); } else { fprintf(stderr, "警告:队列为空,无法执行remove操作!\n"); } } else { fprintf(stderr, "未知容器类型:%s 可选值为stack或queue\n", containerType); } } // 无效指令处理 else { fprintf(stderr, "无效指令:%s 正确格式为add 整数 或 remove stack/queue\n", lineBuffer); } } // 关闭文件,销毁栈和队列(释放内存) fclose(file); destroyStack(&myStack); destroyQueue(&myQueue); return 0; }
最后提醒你一下:如果你的指令还有其他类型(比如只给remove没指定容器?或者其他操作),只要按照这个思路,在else if里加对应的分支处理就行,逻辑是一样的~
内容的提问来源于stack exchange,提问作者Conner Hill
相关产品推荐
相关产品推荐

