码迷,mamicode.com
首页 > 其他好文 > 详细

作业一

时间:2017-09-24 10:48:49      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:文件操作   i++   文件   create   wro   can   char   统计   答案   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int Param_Check(char * param1, char * param2)
{
if (!strcmp(param1, "-n"))
{
int n = atoi(param2);
if (n <= 0)
{
printf("参数输入错误!\n");
return 0;
}
}
else if (!strcmp(param1, "-o")) {
return 1;
}
return 1;
}

void Array_Num_Create(int * array)
{
for (int i = 0; i < 4; i++) {
array[i] = rand()%10;
if ((i == 1 || i == 3)&&(array[i] == 0)) {
while(!array[i]) {
array[i] = rand()%10;
}
}
else {
continue;
}
}
}

void Question_CreateAndPrint(int count, float * answer)
{
srand(time(NULL));
for(int i = 0; i < count; i++)
{
//生成数值
int array[4];
Array_Num_Create(array);

int tmp = rand()%4;
switch (tmp)
{
case 0:
{
printf("%d. %d/%d+%d/%d\n", i+1, array[0], array[1], array[2], array[3]);
answer[i] = (float)array[0]/array[1]+(float)array[2]/array[3];
break;
}

case 1:
{
printf("%d. %d/%d-%d/%d\n", i+1, array[0], array[1], array[2], array[3]);
answer[i] = (float)array[0]/array[1]-(float)array[2]/array[3];
break;
}

case 2:
{
printf("%d. %d/%d*%d/%d\n", i+1, array[0], array[1], array[2], array[3]);
answer[i] = (float)array[0]/array[1]*(float)array[2]/array[3];
break;
}

case 3:
{
printf("%d. %d/%d/%d/%d\n", i+1, array[0], array[1], array[2], array[3]);
answer[i] = (float)array[0]/array[1]/(float)array[2]/array[3];
break;
}
default: break;
}
}
}

float FourOutFiveIn(float fSrc)
{
float fTemp = 0;
int nTemp = 0;
fTemp = fSrc*100;
fTemp += 0.5;
nTemp = fTemp;
fTemp = nTemp/100;
return fTemp;
}

//输入答案并校验
void Answer_Check(float * answers, int ncount, FILE * fp)
{
int nCorrect = 0;
int nWrong = 0;
int * tmpArray = (int*)malloc(sizeof(int)*ncount);
memset(tmpArray, 0, sizeof(int)*ncount);

//校验答案,统计正确错误总数
for (int i = 0; i < ncount; i++) {
printf("请输入第%d道的答案(四舍五入,保留俩位小数):", i+1);
float inputNum = 0;
scanf("%f", &inputNum);
if (FourOutFiveIn(inputNum) == FourOutFiveIn(answers[i])) {
printf("correct,next one!\n");
tmpArray[i] = 1;
nCorrect++;
}
else {
printf("wrong,the answer is %0.2f!\n", FourOutFiveIn(answers[i]));
tmpArray[i] = 0;
nWrong++;
}
}

//正确错误题号
int * CArray = (int*)malloc(sizeof(int)*nCorrect);
int * WArray = (int*)malloc(sizeof(int)*nWrong);
int x = 0, y = 0;
for (int i = 0; i < ncount; i++) {
if (tmpArray[i] == 1) {
CArray[x++] = i+1;
}
else if (tmpArray[i] == 0) {
WArray[y++] = i+1;
}
}

//写入文件
char buf[200] = "";
sprintf(buf, "correct:%d ", nCorrect);
fseek(fp, 0, SEEK_END);
fwrite(buf, sizeof(buf), 1, fp);
fflush(fp);

memset(buf, 0, 200);
sprintf(buf, "wrong:%d ", nWrong);
fseek(fp, 0, SEEK_END);
fwrite(buf, sizeof(buf), 1, fp);
fflush(fp);

fclose(fp);
}

//文件操作
FILE * fOpen(char * filename)
{
FILE * pFile = fopen(filename, "r+");
if (pFile == NULL)
{
pFile = fopen(filename, "w+");
}
return pFile;
}

int main(int argc, char *argv[])
{
//输入参数检查
if (argc < 5) {
printf("请输入足够的参数:-n 个数, -o 目标文件\n");
return 0;
}
if (!Param_Check(argv[1], argv[2])) {
return 0;
}

//打开文件
char* filename = argv[4];
FILE * fp = fOpen(filename);

//题目生成,打印,并生成答案
int ncount = atoi(argv[2]);
float * answers = (float *)malloc(sizeof(float)*ncount);
Question_CreateAndPrint(ncount, answers);

//答案输入并检验(同时将结果写入文件)
Answer_Check(answers, ncount, fp);

return 0;
}

作业一

标签:文件操作   i++   文件   create   wro   can   char   统计   答案   

原文地址:http://www.cnblogs.com/helloc14/p/7586363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!