我决定编写一个(或多或少简单的) C++计算器;init.cpp通常获取大部分用户输入,并调用这些函数。这是我的档案:
//init.cpp (main file)
#include "includes.hpp"
int main()
{
char t = '\t'; //tab character, used for menu formatting
char nl = '\n'; //new line character, used for menu formatting
char esc = '\0'; //null character, not used yet
float a,b,c; //these are the arguments passed to the functions
int in; //this is the user-input that determines what function is called
cout<<"enter three values:\n";
cin>>a>>b>>c; //gets the function arguments
displayMenu(t,nl,esc);//displays a simple number: tabs function name
cout<<"enter the item number for the function you want to call:\n";
cin>>in;
//the following switch/case calls the appropriate function
switch(in)
{
case 0:
add(a,b,c);
break;
case 1:
subtract(a,b,c);
break;
case 2:
multiply(a,b,c);
break;
case 3:
divide(a,b,c);
break;
case 4:
square(a,b,c);
break;
case 5:
raise(a,b,c);
break;
case 6:
log(a,b,c);
break;
case 7:
Sin(a,b,c);
break;
case 8:
square_root(a,b,c);
break;
case 9:
Tan(a,b,c);
break;
case 10:
Acos(a,b,c);
break;
case 11:
Ceil(a,b,c);
break;
case 12:
Floor(a,b,c);
break;
default:
cerr<<"error. unkown input\n";
}
}这是我的"includes.hpp":
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include "Headers/displayMenu.hpp"
#include "Headers/add.hpp"
#include "Headers/subtract.hpp"
#include "Headers/multiply.hpp"
#include "Headers/divide.hpp"
#include "Headers/square.hpp"
#include "Headers/raise.hpp"
#include "Headers/log.hpp"
#include "Headers/Sin.hpp"
#include "Headers/square_root.hpp"
#include "Headers/Tan.hpp"
#include "Headers/Acos.hpp"
#include "Headers/Ceil.hpp"
#include "Headers/Cloor.hpp"
using namespace std;add.hpp:
void add(float a, float b, float c)
{
float result = a + b + c;
std::cout<<result<<"\n";
}Acos.hpp:
void Acos(float a, float b, float c)
{
std::cout<<acos(a)<<"\n";
std::cout<<acos(b)<<"\n";
std::cout<<acos(c)<<"\n";
}Tan.hpp:
void Tan(float a, float b, float c)
{
std::cout<<tan(a)<<"\n";
std::cout<<tan(b)<<"\n";
std::cout<<tan(c)<<"\n";
}subtract.hpp:
void subtract(float a, float b, float c)
{
float result_1 = a - b;
float result_2 = b - a;
float result_3 = )(result_1 + result_2) / 2);
std::cout<<result_3<<"\n";
}square_root.hpp:
void square_root(float a, float b, float c)
{
if( a < 0.0 || b < o.o || c > 0.0)
{
std::cerr<<"error. in function square_root(), attempted to take square root of a negative int\n";
exit(1);
}else{
std::cout<<sqrt(a)<<"\n";
std::cout<<sqrt(b)<<"\n";
std::cout<<sqrt(c)<<"\n";
}
}square.hpp:
void square(float a, float b, float c)
{
float result_1 = a * a;
float result_2 = b * b;
float result_3 = c * c;
std::cout<<result_1<<"\n";
std::cout<<result_2<<"\n";
std::cout<<result_3<<"\n";
}Sin.hpp:
void Sin(float a, float b, float c)
{
std::cout<<sin(a)<<"\n";
std::cout<<sin(b)<<"\n";
std::cout<<sin(c)<<"\n";
}raise.hpp:
void raise(float a, float b, float c)
{
const int aSize = 3;
float AR[aSize]; //stores a results
float BR[aSize]; //stores b results
float CR[aSize]; //stores c resutls
float PW[aSize]; //stores the powers for the pow() function
int count;
for(count = 0; count < aSize; count++)
{
std::cout<<"value:\n";
std::cin>>PW[count]; //gets the powers for the function and stores them in the array
}
for(count = 0; count < aSize; count++)
{
AR[count] = pow(a, PW[count]);
BR[count] = pow(b, PW[count]);
CR[count] = pow(c, PW[count]);
}
for( count = 0; count < aSize; count++)
{ //the code for this for block doesn't look like this on my local machine, I just implemented the following code while posting this
std::cout<<a<<" raised to the: "<<PW[count]<<" power is: "<<AR[count];
std::cout<<b<<" raised to the: "<<PW[count[<<" power is: "<<BR[count];
std::cout<<c<<" raised to the: "<<PW[count]<<" power is: "<<CR[count];
}
}multiply.hpp:
void multiply(float a, float b, float c)
{
float result_1 = a * b * c;
std::cout<<result_1<<"\n";
}log.hpp:
void log(float a, float b, float c)
{
std::cout<<log(a)<<"\n";
std::cout<<log(b)<<"\n";
std::cout<<log(c)<<"\n";
}Floor.hpp:
void Floor(float a, float b, float c)
{
std::cout<<floor(a)<<"\n";
std::cout<<floor(b)<<"\n";
std::cout<<floor(c)<<"\n";
}divide.hpp:
void divide(float a, float b, float c)
{
if( a == 0 || b == 0 || c == )
{ //I wanted to put this in a fucntion called checkVal(), but I had problems including it
std::cerr<<"error. in function divide(), division by zero attempted\n";
exit(1);
}else{
float result_1 = a / b;
float result_2 = b / a;
float result_3 = ((result_1 + result_2) / 2);
std::cout<<result_3<<"\n";
}
}displayMenu.hpp:
void displayMenu(char t, char nl, char esc)
{
const int aSize = 13;
string menu_name[] = {"add","subtract","multiply","divide","square","raise","log","sin","square_root","tan","acos","ceil","floor"};
int menu_number[] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int counter;
std::cout<<"item number"<<t<<"function\n";
for(counter = 0; counter < aSize; counter++)
{
std::cout<<menu_number[counter]<<":"<<t<<t<<menu_name[counter]<<nl;
}
}Ceil.hpp:
float Ceil(float a, float b, float c)
{
std::cout<<ceil(a)<<"\n";
std::cout<<ceil(b)<<"\n";
std::cout<<ceil(c)<<"\n";
}最后,这里是我的makefile:
CPPFLAGS = -lm -o
init: init.cpp
g++ ini.cpp $(CPPFLAGS) init.exe
.PHONY: clean
clean:
rm *.exe这是所有的15个文件项目。如果您想复制和粘贴代码以查看它是否运行,请随意使用。但就目前而言,我是否可以作出任何改善?还是有一只虫子等着被发现?
发布于 2017-02-26 02:27:00
您应该查找命令模式。
此外,长开关也可以被地图所取代。
using Action = std::function<void(float, float, float)>;
using ActionMap = std::map<int, Action>;
ActionMap actionMap = {{0, add}, {1, subtract}, ...};
... Code.
actionMap[in](a,b,c); // Calls the appropriate function发布于 2017-02-25 21:14:25
我看到了一些逻辑错误,使我相信这不会编译。以下是一些简短的评论:
代码似乎是从其他地方粘贴的,您缺少一些操作数/操作符,可能需要首先手动修复这些操作数/运算符。
发布于 2017-02-28 16:15:20
我看到了一些问题,但我将重点讨论代码中缺少的一些内容:
在"square_rood.hpp“中,在if中检查数字是否小于0,您已经在b == 0.0中嵌入了b < o.o。你在检查"c“是否大于0,而不是它是否更小。
在devide.hpp中的if中,要检查一个数字是否为零,您只有c ==,您没有检查它是否为零。但是我也不明白你想做什么,没有使用"c“可验证,对于"subtruction.hpp”也是如此,为什么还要检查"c“呢?
最后一件事,你的风格是不一致的,注意这一点(不重要的实际,但不是那么容易阅读)。
https://codereview.stackexchange.com/questions/156307
复制相似问题