博客
关于我
文件四则运算
阅读量: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/

你可能感兴趣的文章
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI格式设置
查看>>
Npp删除选中行的Macro录制方式
查看>>
NR,NF,FNR
查看>>
nrf开发笔记一开发软件
查看>>
NS3 IP首部校验和
查看>>
NSDateFormatter的替代方法
查看>>
NSError 的使用方法
查看>>
nsis 安装脚本示例(转)
查看>>
NSJSON的用法(oc系统自带的解析方法)
查看>>
nslookup 的基本知识与命令详解
查看>>
NSOperation基本操作
查看>>