第二次作业：熟悉使用工具

GIT地址 git地址
GIT用户名 believe001

2，代码设计的思路：

（1），设计一个calculator类，类中私有数据成员包括

int Quenumbers;//出题数量

void calculator();//设置输入题目数量
void showQuestions();//将题目内容显示在屏幕中
void showResult();//将题目结果显示在屏幕上
bool Is_int(float a);//判断结果是否为整数

（2），代码如下：

Calculator.h：

//calculator.h的实现
#ifndef CALCULATOR_H_
#define CALCULATOR_H_
#include "pch.h"
class Calculator {
public:
Calculator() {};
void calculator();//设置输入题目数量
void showQuestions();//将题目内容显示在屏幕中
void showResult();//将题目结果显示在屏幕上
bool Is_int(float a);//判断结果是否为整数
private:
int Quenumbers;
};
#endif

Calculator.cpp：

// calculator.cpp的实现
#include "pch.h"
#include"calculator.h"
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>

std::ofstream OutFile;
using namespace std;
//输入题目数量
void Calculator::calculator()

{
cout << "Please input how many quetions you want to set:";
cin >> Quenumbers;
}

//将题目内容显示在屏幕中
void Calculator::showQuestions()
{
int a, b, c, d;//参与运算的数字
int key;//用于控制随机式子
srand((unsigned)time(NULL));

for (int i = 0; i < Quenumbers; i++)
{
do
{
a = rand() % 101;
b = rand() % 101;
c = rand() % 101;
d = rand() % 101;
key = rand() % 10 + 1;
switch (key)
{
case 1:
answers[i] = a + b * c;
{
cout << a << "+" << b << "*" << c << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "+" << b << "*" << c << "=" << endl;
OutFile.close();
}
break;
case 2:
answers[i] = a + b - c;
{
cout << a << "+" << b << "-" << c << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "+" << b << "-" << c << "=" << endl;
OutFile.close();
}
break;
case 3:
answers[i] = a * 1.0*b / c;
{
cout << a << "*" << b << "÷" << c << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "*" << b << "÷" << c << "=" << endl;
OutFile.close();
}
break;
case 4:
answers[i] = a - 1.0*b / c;
{
cout << a << "-" << b << "÷" << c << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "-" << b << "÷" << c << "=" << endl;
OutFile.close();
}

break;
case 5:
answers[i] = a * b - c;
{
cout << a << "*" << b << "-" << c << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "*" << b << "-" << c << "=" << endl;
OutFile.close();
}
break;
case 6:
answers[i] = a * b - c + d;
{
cout << a << "*" << b << "-" << c << "+" << d << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "*" << b << "-" << c << "+" << d << "=" << endl;
OutFile.close();
}
break;
case 7:
answers[i] = a + b * c - d;
{
cout << a << "+" << b << "*" << c << "-" << d << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "+" << b << "*" << c << "-" << d << "=" << endl;
OutFile.close();
}
break;
case 8:
answers[i] = a * b + c - d;
{
cout << a << "*" << b << "+" << c << "-" << d << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "*" << b << "+" << c << "-" << d << "=" << endl;
OutFile.close();
}
break;
case 9:
answers[i] = a - 1.0*b / c + d;
{
cout << a << "-" << b << "÷" << c << "+" << d << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "-" << b << "÷" << c << "+" << d << "=" << endl;
OutFile.close();

}
break;
case 10:
answers[i] = 1.0*a / b - c * d;
{
cout << a << "÷" << b << "-" << c << "*" << d << "=" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << a << "÷" << b << "-" << c << "*" << d << "=" << endl;
OutFile.close();
}
break;

}
}

}

//判断结果是否为整数
bool Calculator::Is_int(float a)
{
return a == static_cast<int>(a);
}

//将题目结果显示在屏幕上
void Calculator::showResult()
{

cout << "The answers of these questions are as follows:" << endl;
OutFile.open("subject.txt", ios::app);
OutFile << "The answers of these questions are as follows:" << endl;
OutFile.close();

for (int i = 0; i < Quenumbers; i++)
{
cout << "Question" << i + 1 << ":" << answers[i] << endl;
OutFile.open("subject.txt", ios::app);
OutFile << "Question" << i + 1 << ":" << answers[i] << endl;
OutFile.close();
}
}

源.cpp：

#include<iostream >

#include"calculator.h"
//#include "pch.h"
using namespace std;
int main()
{
Calculator a;
a.calculator();
a.showQuestions();
a.showResult();
return 0;
}

（3）运行结果截图：

1，在 https://github.com/join申请注册一个 Github 账号，申请成功后在 https://github.com/login 进行登录。
2，成功登录后，输入阿超仓库的网址 https://github.com/ChildishChange/Calculator ，点击右上角的 Fork，将阿超的四则运算库拷贝到自己的同名仓库中，如下图所示：

git push 成功后如下

4.我所遇到的问题:

1）提交代码过程我是参照[2019BUAA软工助教]第0次代码作业经行操作的，我说一下在这个过程中所遇到的问题吧：

，这是怎么回事呢，原来我并没有根据下面命令配置个人邮箱与用户名 ，如下图：

#include "stdafx.h"
#include "CppUnitTest.h"
#include"../Calculator/Calculator.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace CalculatorUnitTest
{
TEST_CLASS(UnitTest1)
{
public:

TEST_METHOD(TestMethod1)
{
// TODO: 在此输入测试代码
Calculator c;
Assert::AreEqual(true,c.Is_int(3));
}

};
}

posted @ 2019-09-21 17:35  --believe  阅读(393)  评论(2编辑  收藏  举报