“桌面计算器”从标准输入设备读入一个(数值计算)表达式,计算它的值后从标准输出设备输出;读入的也可以是一个赋值语句:左端是一个符号名,右端是表达式。 表达式中可以有四则运算符、括号、整数/实数值、已经赋值的符号名和预定义的符号常量( pi 和 e ),也可以只有单个的整数/实数值。 发现输入内容与文法不符或将导致非法计算时,则从标准输出设备输出出错提示,并计算出错次数。 写一个这样的完整c++程序

时间: 2024-02-27 21:55:46 浏览: 24
&& S_ISDIR(st.st_mode)) { // 如果是文件夹 move_folder(src_file + strlen(root) + 1, dest_file + strlen(root) + 1); } } closedir(src_dir); rmdir(src_path); } // 删除文件以下是一个简单的 C++ 程序,可以实现你所描述的功能: ```c++ #include <iostream> #include < void delete_file(char* filename) { char path[100]; strcpy(path, root); strcat(path, "/"); strcatstring> #include <map> #include <cmath> using namespace std; // 定义符号常量 const double PI = 3.(path, filename); if (remove(path) == 0) { printf("文件删除成功!\n"); } else { 14159265358979323846; const double E = 2.71828182845904523536; // 定义错误计数 printf("文件删除失败!\n"); } } // 删除文件夹 void delete_folder(char* foldername) { char器 int error_count = 0; // 定义符号表 map<string, double> symbol_table; // 计算表达 path[100]; strcpy(path, root); strcat(path, "/"); strcat(path, foldername); DIR* dir = opendir式的值 double calc_expression(const string& expression) { // TODO: 实现计算表达式的功能 } //(path); if (dir == NULL) { printf("文件夹删除失败!\n"); return; } struct dirent 解析赋值语句 void parse_assignment(const string& statement) { // TODO: 实现解析赋值语句的* entry; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } char file_path[100]; strcpy功能 } int main() { // 循环读入输入,直到遇到 EOF 或者出现 10 次错误 (file_path, path); strcat(file_path, "/"); strcat(file_path, entry->d_name); struct stat st; if while (!cin.eof() && error_count < 10) { // 读入一行输入 string input; getline (stat(file_path, &st) == 0 && S_ISREG(st.st_mode)) { // 如果是文件 delete_file(file(cin, input); // 如果输入为空,则继续下一轮循环 if (input.empty()) { continue; _path + strlen(root) + 1); } else if (stat(file_path, &st) == 0 && S_ISDIR(st } // 如果输入为 quit,则退出程序 if (input == "quit") { break; } // 判断输入.st_mode)) { // 如果是文件夹 delete_folder(file_path + strlen(root) + 1); } } closed是否为赋值语句 if (input.find('=') != string::npos) { parse_assignment(input); } else { ir(dir); rmdir(path); } // 显示文件夹中的内容 void show_folder_content(char* foldername) { char // 否则,计算表达式的值并输出 double result = calc_expression(input); if (error_count > path[100]; strcpy(path, root); strcat(path, "/"); strcat(path, foldername); DIR* dir = opendir 0) { cout << "error: " << error_count << " errors" << endl; error_count = 0; (path); if (dir == NULL) { printf("文件夹打开失败!\n"); return; } struct } else { cout << result << endl; } } } return 0; } double calc_expression(const string& expression dirent* entry; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == ) { // TODO: 实现计算表达式的功能 } void parse_assignment(const string& statement) { // 找0 || strcmp(entry->d_name, "..") == 0) { continue; } char file_path[100]; strcpy(file_path, path); strcat(file_path, "/"); strcat(file_path, entry->d_name); struct stat st; if到等号的位置 size_t pos = statement.find('='); // 分离符号名和表达式 string (stat(file_path, &st) == 0 && S_ISREG(st.st_mode)) { // 如果是文件 printf("文件 symbol = statement.substr(0, pos); string expression = statement.substr(pos + 1); // 去掉符号名和等:%s\n", entry->d_name); } else if (stat(file_path, &st) == 0 && S_ISDIR(st号两端的空格 symbol.erase(0, symbol.find_first_not_of(" \t\n\r\f\v")); symbol.erase.st_mode)) { // 如果是文件夹 printf("文件夹:%s\n", entry->d_name); } } (symbol.find_last_not_of(" \t\n\r\f\v") + 1); expression.erase(0, expression.find_first_not_of(" closedir(dir); } // 查看文件内容 void show_file_content(char* filename) { char path[100]; strcpy \t\n\r\f\v")); expression.erase(expression.find_last_not_of(" \t\n\r\f\v") + 1); //(path, root); strcat(path, "/"); strcat(path, filename); FILE* fp = fopen(path, "r"); if ( 如果符号名非法,则输出错误提示并增加错误计数器 if (symbol.empty() || !isalpha(symbolfp != NULL) { char buffer[1024]; size_t n; while ((n = fread(buffer, 1, sizeof[0])) { cout << "error: invalid symbol name" << endl; error_count++; return; } // 计(buffer), fp)) > 0) { fwrite(buffer, 1, n, stdout); } fclose(fp); } } 算表达式的值 double value = calc_expression(expression); // 如果计算表达式的过程中出现// 修改文件权限 void change_file_permission(char* filename, mode_t mode) { char path[100]; strcpy(path,错误,则直接返回 if (error_count > 0) { return; } // 将符号及其对 root); strcat(path, "/"); strcat(path, filename); if (chmod(path, mode) == 0) { printf("应的值存入符号表中 symbol_table[symbol] = value; } double calc_expression(const string& expression) { 权限修改成功!\n"); } else { printf("权限修改失败!\n"); } } // 搜索文件 // TODO: 实现计算表达式的功能 } ``` 这个程序包含了三个主要函数: void search_file(char* keyword) { DIR* dir = opendir(root); if (dir == NULL) { printf("文件夹打开失败!\n"); return; } struct dirent* entry; while ((entry = readdir(dir)) != NULL)- `calc_expression`: 计算表达式的值; - `parse_assignment`: 解析赋值语句,并将符号及 { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { 其对应的值存入符号表中; - `main`: 主函数,循环读入输入并进行处理。 其中 continue; } char file_path[100]; strcpy(file_path, root); strcat(file_path, "/"); strcat(file `calc_expression` 函数和 `parse_assignment` 函数都是需要你自己实现的,具体的实现方法可以参_path, entry->d_name); struct stat st; if (stat(file_path, &st) == 0 && S_ISREG考下面的步骤。 `calc_expression` 函数的实现步骤: 1. 去掉表达式两端的空(st.st_mode)) { // 如果是文件 char buffer[1024]; FILE* fp = fopen(file_path, "r"); 格; 2. 如果表达式为空,则返回零; 3. 如果表达式为一个整数或实数,则直接 if (fp != NULL) { fread(buffer, 1, sizeof(buffer), fp); fclose(fp); if (strstr返回它的值; 4. 如果表达式为一个符号名,则从符号表中查找它对应的值(buffer, keyword) != NULL) { printf("文件名:%s,路径:%s\n", entry->d_name, file_path); ,如果找到了则返回该值,否则输出错误提示并增加错误计数器; 5. 如果表达式 } } } else if (stat(file_path, &st) == 0 && S_ISDIR(st.st_mode)) { //包含括号,则递归计算括号中的表达式; 6. 如果表达式包含乘方运算 如果是文件夹 search_file_in_folder(file_path, keyword); } } closedir(dir); } // 在文件夹中搜索文件 void search_file_in_folder(char* folder_path, char* keyword) { DIR* dir = opendir(folder_path); 符,则计算乘方; 7. 如果表达式包含乘除运算符,则计算乘除; 8. 如果 if (dir == NULL) { return; } struct dirent* entry; while ((entry = readdir(dir)) != NULL表达式包含加减运算符,则计算加减; 9. 如果表达式中还有其他字符,则输出) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)错误提示并增加错误计数器。 `parse_assignment` 函数的实现步骤: 1. 找到等号的位置 { continue; } char file_path[100]; strcpy(file_path, folder_path); strcat(file_path, "/"); ; 2. 分离符号名和表达式; 3. 去掉符号名和等号两端的空格; strcat(file_path, entry->d_name); struct stat st; if (stat(file_path, &st) == 0 && S4. 如果符号名非法,则输出错误提示并增加错误计数器; 5. 计算表达式的值_ISREG(st.st_mode)) { // 如果是文件 char buffer[1024]; FILE* fp = fopen(file_path, "; 6. 如果计算表达式的过程中出现错误,则直接返回; 7. 将符号及其对r"); if (fp != NULL) { fread(buffer, 1, sizeof(buffer), fp); fclose(fp); if应的值存入符号表中。 这个程序还需要使用 `map` 来实现符号表,并定义了两 (strstr(buffer, keyword) != NULL) { printf("文件名:%s,路径:%s\n", entry->d_name, file个符号常量 `PI` 和 `E`。在 `main` 函数中,程序通过循环读入输入,并根_path); } } } else if (stat(file_path, &st) == 0 && S_ISDIR(st.st_mode)) { // 如果是文件夹 search_file_in_folder(file_path, keyword); } } closedir(dir); } int据输入的内容分别调用 `calc_expression` 函数和 `parse_assignment` 函数。如果输入的内容为空,则程序继 main() { init_root(); while (1) { char command[100]; printf("\n请输入命令:"); 续下一轮循环;如果输入的内容为 quit,则程序退出。在输出结果时,如果计算过程中出 scanf("%s", command); if (strcmp(command, "create_file") == 0) { char filename[100],现了错误,则输出错误提示并增加错误计数器;否则,直接输出计算结果。当错误计数器达到 10 时,程序退出。

相关推荐

最新推荐

recommend-type

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx
recommend-type

华为OD机试D卷 - 机场航班调度程序 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

基于FPGA读取设计的心电图代码源码+全部资料齐全.zip

【资源说明】 基于FPGA读取设计的心电图代码源码+全部资料齐全.zip基于FPGA读取设计的心电图代码源码+全部资料齐全.zip 【备注】 1、该项目是高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip

【资源说明】 基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

华为OD机试D卷 - 数的分解 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。