简单控制台计算器问题 [英] Simple console calculator question.

查看:76
本文介绍了简单控制台计算器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c ++很陌生,我在两天前开始使用它。我编写了一个功能齐全的dos计算器。我现在承认,这可能不是制作计算器的最好方法,请多好。这是代码。我的问题是最底层的评论。 (Visual Studio 2017)



  #include     stdafx.h 
#include < iostream >

// 计算器的添加功能。
int addNumbers( int x, int y)
{
int answer = x + y;
return answer;
}

// Calc的减法功能。
int subtractNumbers( int x, int y)
{
int answer = x - y;
return answer;
}

// Calc的乘法函数。
int multiplyNumbers( int x, int y)
{
int answer = x * y;
return answer;
}

// Calc的除法功能。
int divideNumbers( int x, int y)
{
int answer = x / y;
return answer;
}



int main()
{
// 请求第一个数字 r
std :: cout<< ; 请输入第一个数字:<<的std :: ENDL;
int number1;
std :: cin>> 1号;

// 第二个号码
std :: cout<< 请输入第二个数字:<<的std :: ENDL;
int number2;
std :: cin>> 2号;

// 请求arithmatec操作,并将其定义为操作,以便我们可以调用计算器的函数。
std :: cout<< 现在,请输入运营商。<<的std :: ENDL;
std :: cout<< 1是(+)<<的std :: ENDL;
std :: cout<< 2是( - )<<的std :: ENDL;
std :: cout<< 3是(*)<<的std :: ENDL;
std :: cout<< 4是(/)<<的std :: ENDL;
int 操作;
std :: cin>>操作;

// 将计算器的函数调用到输入的operation变量。这将计算相应的等式。
if (operation == 1
std :: cout<< 两个数字的总和为:<< addNumbers(number1,number2)<<的std :: ENDL;
if (operation == 2
std :: cout<< ; 两个数字的区别在于:<< subtractNumbers(number1,number2)<<的std :: ENDL;
if (operation == 3
std :: cout<< ; 这两个数字的乘积是:<< multiplyNumbers(number1,number2)<<的std :: ENDL;
if (operation == 4
std :: cout<< ; 这两个数字的商是:<< divideNumbers(number1,number2)<<的std :: ENDL;

// 如果无法输入错误信息
if (操作> 4
std :: cout< ;< 操作员错误:请输入1到4之间的数字。<<的std :: ENDL;
if (操作< 1
std :: cout<< ; 操作员错误:请输入1到4之间的数字。<<的std :: ENDL;
return 0 ;
} // (如果除了1,2,3之外的任何内容,将显示错误消息的语句,输入4。)

// 最后的评论我正在努力实现目标。
// 另外:如何制作它连续,所以当你计算一件事时,控制台不会关闭





我尝试过:



  if (操作!=  1  2  3  4 
std :: cout<< Eroror;



else (operation =! 1 2 3 4
std :: cout<< error ...<< std :: endl;

解决方案

可以使用if else解决第一个问题:如果是C ++中的语句 - Cprogramming.com [ ^ ]



第二个问题需要循环:C++循环类型 [ ^ ]



例如你可以使用带退出条件的无限循环然后打破某个数字: break语句 - cppreference.com [ ^ ]



  #include   <   iostream  >  
使用 命名空间标准;

int main(){

for (;;){
// 输入数字代码

if (operation == 1
.....;
else if (operation == ...)
.... ..;
else if (operation == 99
break ;
else
cout<< 操作员错误:请输入1到4之间的数字。<< ENDL;
}

return 0 ;
}





注意使用std命名空间:名称可见性 - C ++教程 [ ^ ]


I'm pretty new to c++, I started with it like two days ago. I coded a fully functional dos calculator. I will admit now that this may not be the best way to make a calculator, pls be nice. Here's the code. My question is on the final comment at the bottom. (Visual Studio 2017)

#include "stdafx.h"
#include <iostream>

//Calculator's adding function.
int addNumbers(int x, int y)
{
	int answer = x + y;
	return answer;
}

//Calc's subtracting function.
int subtractNumbers(int x, int y)
{
	int answer = x - y;
	return answer;
}

//Calc's multiplication function.
int multiplyNumbers(int x, int y)
{
	int answer = x * y;
	return answer;
}

//Calc's division function.
int divideNumbers(int x, int y)
{
	int answer = x / y;
	return answer;
}



int main()
{
	//Requests first number
	std::cout << "Please enter the first number:" << std::endl;
	int number1;
	std::cin >> number1;

	//Second number
	std::cout << "Please enter the second number: " << std::endl;
	int number2;
	std::cin >> number2;

	//Requests an arithmatec operation, and defines it as "operation" so we can call the calculator's functions to it.
	std::cout << "Now, please enter an operator." << std::endl;
	std::cout << " 1 is (+) " << std::endl;
	std::cout << " 2 is (-) " << std::endl;
	std::cout << " 3 is (*) " << std::endl;
	std::cout << " 4 is (/) " << std::endl;
	int operation;
	std::cin >> operation;

	//Calls the calculator's functions to the inputted "operation" variable. This calculates the respective equation.
	if (operation == 1)
		std::cout << "The sum of the two numbers is: " << addNumbers(number1, number2) << std::endl;
	if (operation == 2)
		std::cout << "The difference of the two numbers is: " << subtractNumbers(number1, number2) << std::endl;
	if (operation == 3)
		std::cout << "The product of the two numbers is: " << multiplyNumbers(number1, number2) << std::endl;
	if (operation == 4)
		std::cout << "The quotient of the two numbers is: " << divideNumbers(number1, number2) << std::endl;

	//Error message if input is impossible.
	if (operation > 4)
		std::cout << "Operator error: Please enter a number between 1 and 4." << std::endl;
	if (operation < 1)
		std::cout << "Operator error : Please enter a number between 1 and 4." << std::endl;
		return 0;
}	// (statement that will show error message if anything but 1, 2, 3, 4 is inputted.)

//The last comment there is what I'm trying to accomplish.
//ALSO: How to make it continuous, so when you calculate one thing, the console doesn't close



What I have tried:

if (operation != 1, 2, 3, 4)
    std::cout << "Eroror";

and

else (operation =! 1, 2, 3, 4)
		std::cout << "error..." << std::endl;

解决方案

The first issue may be resolved using if else : If Statements in C++ - Cprogramming.com[^]

The second issue requires a loop: C++ Loop Types[^]

eg you could use an infinite loop with an exit condition and then break on a certain number: break statement - cppreference.com[^]

#include <iostream>
using namespace std;
 
int main () {

   for( ; ; ) {
// Input number code

      if(operation == 1)
          .....;
      else if(operation==...)
          ......;
      else if(operation==99)
         break;
      else
        cout << "Operator error: Please enter a number between 1 and 4." << endl;
   }

   return 0;
}



Note the use of the std namespace: Name visibility - C++ Tutorials[^]


这篇关于简单控制台计算器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆