在这次作业中其实遇到很多困难,主要就是对vs的使用,还有Github上对于作业的下载保存和提交。在这次作业中也学习到了很多。
因为以前从没用过vs,在这次下载中也遇到一些困难,具体安装教程可以点击这里!大佬写的很详细。 遇到的问题,比如我第一次因为c盘只有十几个G,所以想安装在e盘里,结果之后运行代码一直说找不到文件位置,我就很迷,之后没办法只有卸载了重新下载,然后又发现安装工具那一栏的位置的地址还是没有改,还是e盘,然后只有上网找。 将代码弄进vs之后发现运行直接出错,后面做了些改动,比如在vs里面点击新建文件使用空项目,运行不了,找不到pch.h头文件,后来用控制台才可以出现pch.h的头文件。 之后运行,还是会出错,然后我把Calculator::MakeFormula()函数里的//srand((unsigned int)time(NULL));语句注释掉,将随机数的生成写入了main函数里面,并添加了for循环语句,才可以正常运行; 全代码: 头文件:Calculator.h
#pragma once #pragma once #include "stdlib.h" #include <stack> #include <vector> #include <iostream> #include "stdlib.h" #include <ctime> #include <string> using namespace std; class Calculator { private: string op[4] = { "+", "-", "*", "/" }; public: Calculator(); string MakeFormula(); string Solve(string formula); };源文件ConsoleApplication2.cpp
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include <iostream> #include <stdlib.h> #include <stack> #include <vector> #include <iostream> #include "stdlib.h" #include <ctime> #include <string> #include "Calculator.h" #define random(a,b) (rand()%(b-a+1)+a) using namespace std; Calculator::Calculator() {} string Calculator::MakeFormula() { string formula = ""; //srand((unsigned int)time(NULL)); int count = random(1, 3); int start = 0; int number1 = random(1, 100); formula += to_string(number1); while (start <= count) { int operation = random(0, 3); int number2 = random(1, 100); formula += op[operation] + to_string(number2); start++; } return formula; }; string Calculator::Solve(string formula) { vector<string>* tempStack = new vector<string>(); stack<char>* operatorStack = new stack<char>(); int len = formula.length(); int k = 0; for (int j = -1; j < len - 1; j++) { char formulaChar = formula[j + 1]; if (j == len - 2 || formulaChar == '+' || formulaChar == '-' || formulaChar == '*' || formulaChar == '/') { if (j == len - 2) { tempStack->push_back(formula.substr(k)); } else { if (k < j) { tempStack->push_back(formula.substr(k, j + 1)); } if (operatorStack->empty()) { operatorStack->push(formulaChar); } else { char stackChar = operatorStack->top(); if ((stackChar == '+' || stackChar == '-') && (formulaChar == '*' || formulaChar == '/')) { operatorStack->push(formulaChar); } else { tempStack->push_back(to_string(operatorStack->top())); operatorStack->pop(); operatorStack->push(formulaChar); } } } k = j + 2; } } while (!operatorStack->empty()) { tempStack->push_back(string(1, operatorStack->top())); operatorStack->pop(); } stack<string>* calcStack = new stack<string>(); for (int i = 0; i < tempStack->size(); i++) { string peekChar = tempStack->at(i); if (peekChar != "+" && peekChar != "-" && peekChar != "/" && peekChar != "*") { calcStack->push(peekChar); } else { int a1 = 0; int b1 = 0; if (!calcStack->empty()) { b1 = stoi(calcStack->top()); calcStack->pop(); } if (!calcStack->empty()) { a1 = stoi(calcStack->top()); calcStack->pop(); } if (peekChar == "+") { calcStack->push(to_string(a1 + b1)); } else if (peekChar == "-") { calcStack->push(to_string(a1 - b1)); } else if (peekChar == "*") { calcStack->push(to_string(a1 * b1)); } else if (peekChar == "/") { calcStack->push(to_string(a1 / b1)); } } } return formula + "=" + calcStack->top(); } int main() { srand((unsigned int)time(NULL)); cout<<"请输入您要生成的题目数:"<<endl; int num; cin >> num; for (int i = 1; i <= num; i++) { Calculator* calc = new Calculator(); string question = calc->MakeFormula(); cout << question << " = " << endl; //string ret = calc->Solve(question); //cout << ret << endl; } getchar(); } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门提示: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件具体代码我没有改多少,能运行我就没有改了,运行结果如下 至于写题目的结果,我写了好几次代码都会出错我就放弃了。 之后在github的提交,也好麻烦,甚至没有找到,提交错了一次,将作业提交成评论去了。后面再按着步骤走才找到提交作业的地点。
学习一个新的工具,确实存在一些困难,不停的百度百度,问室友,看别人提交了的博客,慢慢学,慢慢写,最终还是运行起了,总的来说,遇到的困难虽然会很多,但通过很久的时间和努力,还是解决了。最后学的一个新东西,还是挺高兴的吧。
转载于:https://www.cnblogs.com/Drac/p/11564320.html