//----------------------------------------------------------------------------- // Copyright (c) 2009 eryar All rights reserved. // // File : Main.cpp // Date : 2009-04-30 21:46 // Author : eryar@163.com // // Description: // 随机出100以内的数的加减法运算, 通过键盘接收解答, // 判断对错, 并返回正确与否文字. 将题目和学生解答保存到文本 // 文件中. //============================================================================= #include <time.h> #include <fstream> #include <iostream> using namespace std; void Swap(int &, int &); int main(int argc, char *argv[]) { bool bFlag = false; char isQuit = 'N'; int iCounter = 1; ofstream oFile("result.txt"); // Seed the random number generator with current time srand((unsigned)time(NULL)); int iNumberOne = 0; int iNumberTwo = 0; int iType = 0; // 内容为0,1,用来判断运算类型 char cType = '+'; int iAnswer = 0; // The answer student replyed. int iResult = 0; // The true result calculated by computer. while (!bFlag) { iNumberOne = rand() % 101; //随机产生0-100中的一个数 iNumberTwo = rand() % 101; iType = rand() % 2; //随机产生0或1 if (iNumberOne < iNumberTwo) { Swap(iNumberOne, iNumberTwo); } iResult = iType ? (iNumberOne + iNumberTwo) : (iNumberOne - iNumberTwo); cType = iType ? '+' : '-'; cout<<iCounter<<". "<<iNumberOne<<cType<<iNumberTwo<<" ="; cin>>iAnswer; oFile<<iCounter<<". "<<iNumberOne<<cType<<iNumberTwo<<" = " <<iResult<<"/t"<<iAnswer<<endl; if (iResult == iAnswer) { cout<<"How clever you are!"<<endl; } else { cout<<"What a pity! Come on!"<<endl; } cout<<"Are you sure to quit? (y/n)"; cin>>isQuit; if (isQuit == 'y' || isQuit == 'Y') { bFlag = true; } iCounter ++; } oFile.close(); // close the open file. return 0; } void Swap(int &a, int &b) { int c = 0; c = a; a = b; b = c; }