使用定义的C ++值进行操作

时间:2017-04-22 01:04:48

标签: c++

我最近加入了C ++并且已经制作了一个相对基础但重载的计算器来学习c ++中的新概念。我处于这个问题不可忽视的地步。我希望拥有它,以便用户可以使用超过2个值进行操作,例如3+3+3

这可能吗?如果是,是否有指南或网站了解有关此方法的更多信息?提前谢谢。

以下是我的计算器代码:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <fstream>
#include <string>

using namespace std;

void display_history()
{
        string getcontent;
        ifstream calchistory("calchistory.txt");
        if(calchistory.is_open())
        {
            while (! calchistory.eof())
            {
                    getline(calchistory, getcontent);
                    cout << getcontent <<endl;
            }
    }else cout << "could not display history, does calchistory.txt exist? do you have read permissions?"<< endl;

}

int main()
{
ofstream calcHistory;
calcHistory.open("calchistory.txt", ios::app);
const long double PI = 3.141592653589793238L;
long double a, b, answer, r, area, circum;
int choice, i = 0;
char again, clear;
time_t _tm =time(NULL );

struct tm * curtime = localtime ( &_tm );
calcHistory <<asctime(curtime) << endl;
do{
cout << "What would you like to do?" << endl;
cout << "1.Addition" << endl;
cout << "2.subtraction" << endl;
cout << "3.multiplication" << endl;
cout << "4.divison" << endl;
cout << "5.powers" << endl;
cout << "6.circle calculator" << endl;
cout << "7.view history" << endl;
cout << "8.clear history" << endl;
cout << "9.to quit" << endl;
cout << "choice: ";
cin >> choice;


switch(choice){
    case 1:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a + b;
    cout << "the answer is: " << a << " + " << b << " = " << answer << endl;
calcHistory << i<<": " << a <<" + " <<b <<" = "<< answer << endl<<endl;
    break;
    case 2:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a - b;
    cout << "the answer is: " << a << " - " << b << " = " << answer << endl;
    calcHistory << i<<": "<<a <<" - " <<b <<" = "<< answer << endl<<endl;
    break;
    case 3:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a * b;
    cout << "the answer is: " << a << " * " << b << " = " << answer << endl;
    calcHistory <<i<<": "<<a <<" * " <<b <<" = "<< answer << endl<<endl;
    break;
    case 4:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a / b;
    cout << "the answer is: " << a << " / " << b << " = " << answer << endl;
    calcHistory <<i<<": "<<a <<" / " <<b <<" = "<< answer << endl<<endl;
    break;
    case 5:
i = i+1;
    cout << "please enter two numbers you want to find the power of (1st number is base 2nd number is exponent)" << endl;
    cout << "be sure to press enter after each number" << endl;
    cin >> a >> b;
    answer = pow(a, b);
    cout << "the power of " << a << " and " << b << " is " << answer << endl;
    calcHistory <<i<<": "<<a <<" ^ " <<b <<" = "<< answer << endl<<endl;
    break;
case 6:
i = i+1;
    cout<<"Enter the radius of the circle :";
    cin>>r;
    area=PI*r*r;
    circum=2*PI*r;
    cout<<"Area of the circle = "<<area<<"\nCircumference of the circle = "<<circum<<"\n";
    calcHistory <<i<<": "<< "Area of the circle = "<<area<<"\nCircumference of the circle = "<<circum<<"\n"<<endl;
break;
    case 7:
    display_history();
    break;
case 8:
cout << "are you sure you want to do this? this action cannot be undone. (y/n) ";
    cin >> clear;
    if(clear == 'Y'||clear =='y')
    {
    if(calcHistory.is_open())
        {
         calcHistory.close();
         calcHistory.open("calchistory.txt", ios::trunc);
         calcHistory.close();
         cout << "history cleared!" << endl;
        }
}
break;
    case 9:
    if (calcHistory.is_open())
    {
    cout << "session saved in file calchistory.txt" << endl;
    }else cout << "could not write to file, do you have write permissions?" << endl; 
    cout << "exiting...." << endl << endl;
    cin.get();
    calcHistory.close();
    return 0;
    break;
default:
    cout << choice <<" is not a valid option" << endl;
    break;
}
cout << "would you like to continue? (y or n) ";
cin >> again;
cout << endl;
}while(again == 'y' ||again == 'Y');

cin.get();
if (calcHistory.is_open())
{
cout << "session saved in file calchistory.txt" << endl;
}else cout << "could not write to file calchistory.txt, do you have write permissions?" << endl;
cout << "exiting...."<< endl << endl;
calcHistory.close();
return 0;
}

PS:提高我的代码效率或使用更少行的提示?

1 个答案:

答案 0 :(得分:0)

我记得当我对c ++有点新的时候。

我也试过制作计算器程序,这可能需要许多操作用户。

好消息,我总是将代码保存到Dropbox。

搜索了10分钟后,我终于发现了它,

我知道我的代码并不整洁,因为它是在一年前刚刚编程的时候制作的,

但是,你可以参考它。

 #include<iostream>
int main() {
float number;
char symbol;
float solution = 0;
std::cin >> solution;
while (true) {
    std::cin >> symbol;
    if (symbol == '=') {
        std::cout << solution;
        break;
    }
    std::cin >> number;
    if (symbol == '+') {
        solution += number;
    }
    if (symbol == '-') {
        solution -= number;
    }
    if (symbol == '*') {
        solution *= number;
    }
    if (symbol == '/') {
        solution /= number;
    }

}
return 0;
}