博客
关于我
文件四则运算
阅读量:157 次
发布时间:2019-02-28

本文共 2105 字,大约阅读时间需要 7 分钟。

    
        #define _CRT_SECURE_NO_WARNINGS        #include 
#include
#include
#include
#include
// 生成四则运算题目 void giveQuestion() { FILE* fp = fopen("test.txt", "w"); if (fp == NULL) { printf("文件打开失败"); return; } srand((size_t)time(NULL)); for (int i = 0; i < 10; i++) { int num1 = rand() % 100 + 1; int num2 = rand() % 100 + 1; char ch = symbol[rand() % 4]; char buf[64] = {0}; sprintf(buf, "%d %c %d=\n", num1, ch, num2); fputs(buf, fp); } fclose(fp); printf("生成成功!\n"); } // 解答四则运算题目 void answerQuestion() { FILE* fp = fopen("test.txt", "r"); if (fp == NULL) { printf("文件打开失败"); } FILE* fp1 = fopen("test1.txt", "w"); if (fp1 == NULL) { printf("文件打开失败"); } while (!feof(fp)) { char buf[32] = {0}; char* result = fgets(buf, sizeof(buf), fp); if (result == NULL) { break; } int num1, num2; char ch; sscanf(buf, "%d %c %d", &num1, &ch, &num2); int ret = 0; if (ch == '+') { ret = num1 + num2; } else if (ch == '-') { ret = num1 - num2; } else if (ch == '*') { ret = num1 * num2; } else if (ch == '/') { ret = num1 / num2; } char buf1[32] = {0}; sprintf(buf1, "%d %c %d=%d\n", num1, ch, num2, ret); fputs(buf1, fp1); } fclose(fp1); fclose(fp); printf("解答成功!\n"); } int main() { giveQuestion(); answerQuestion(); return 0; }

转载地址:http://thwc.baihongyu.com/

你可能感兴趣的文章
Node.js安装和入门 - 2行代码让你能够启动一个Server
查看>>
node.js安装方法
查看>>
Node.js官网无法正常访问时安装NodeJS的方法
查看>>
node.js模块、包
查看>>
node.js的express框架用法(一)
查看>>
Node.js的交互式解释器(REPL)
查看>>
Node.js的循环与异步问题
查看>>
Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
查看>>
nodejs + socket.io 同时使用http 和 https
查看>>
NodeJS @kubernetes/client-node连接到kubernetes集群的方法
查看>>
NodeJS API简介
查看>>
Nodejs express 获取url参数,post参数的三种方式
查看>>
nodejs http小爬虫
查看>>
nodejs libararies
查看>>
nodejs npm常用命令
查看>>
nodejs npm常用命令
查看>>
Nodejs process.nextTick() 使用详解
查看>>
NodeJS yarn 或 npm如何切换淘宝或国外镜像源
查看>>
nodejs 中间件理解
查看>>
nodejs 创建HTTP服务器详解
查看>>