我是C ++的初学者和完全相当的编程(除了一点 html 和 css )。
我决定开始我的第一个C ++项目。
一位朋友建议我尝试制作一个简单的计算器,所以这是我的第一个镜头。任何指针都会很棒!不确定我究竟缺少什么,如果有的话,但我收到的错误是:
1>------ Build started: Project: CalculatorFinal, Configuration: Debug Win32 ------
1> CalculatorFinal.cpp
1>c:\users\ramee\documents\visual studio 2010\projects\calculatorfinal
\calculatorfinal\calculatorfinal.cpp(32): warning C4102: 'calc' : unreferenced label
1> CalculatorFinal.vcxproj -> c:\users\ramee\documents\visual studio 2010
\Projects\CalculatorFinal\Debug\CalculatorFinal.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
我的代码在下面(如果在这里没有正确格式化,请道歉。这是我的第一篇文章:D
// CalculatorFinal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // Including header
#include <iostream> // Including ioStream
using namespace std; // Namespace
void calc (double x, double y);
double result;
double n1,n2; // Declaring Variables
char q,operation;
int main()
{
cout<<"Welcome to My Calculator" <<endl; // Outputs welcome message
cout<<""<<endl; // Blank Space
cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl; // Outputs instruction
cout<<" EX: 2 + 2" <<endl; // Outputs instruction
cout<<""<<endl; // Blank Space
cout<<"Operators:"<<endl; // Outputs operation header
cout<<"For Addition, select '+'"<<endl // Outputs ADD instruction
cout<<"For Subtraction, select '-'"<<endl; // Outputs SUB instruction
cout<<"For Multiplication, select '*'"<<endl; // Outputs MUL instruction
cout<<"For Division, select '/'"<<endl; // Outputs DIV instruction
cout<<""<<endl; // Blank Space
cout<<"To clear, select 'c'"<<endl; // Outputs clear instruction
cout<<"To quit, select 'q'"<<endl; // Outputs QUIT instruction
cout<<""<<endl; // Blank Space
cout<<"Input a mathmatical equation"<<endl; // Input instructions
cin>>n1>>operation>>n2;
calc:(n1,n2);
cout<<"The answer is:"<<result<<endl;
std::cin>>q; // Input "q" to "quit"
return 0;}
void calc(double x, double y) // Operator function
{ x=n1;
y=n2;
switch(operation) // Operator swtich statement
{case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
cin>>n1>>operation>>n2;
calc (n1,n2);
}
}
答案 0 :(得分:1)
这是我根据你的编写的计算器程序,它更好:
#include <iostream>
using namespace std;
//Function prototype
int solve(int, int, char);
int main()
{
//Declare variables
int solution, num1, num2;
char oper;
//Output
cout << "Calculator\n----------\n" << endl;
cout << "Syntax:\n" << endl;
cout << "1 + 3\n" << endl;
cout << "Operators: +, -, *, /\n" << endl;
cout << "Equation: ";
//Input
cin >> num1 >> oper >> num2;
//Solve and output
solution = solve(num1, num2, oper);
cout << "Answer: " << solution << endl;
//Pause [until enter key] and exit
cin.ignore(); //Enter key from last cin may be passed, ignore it.
cin.get();
return 0;
}
int solve(int num1, int num2, char oper)
{
//Switch oper
switch(oper)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
cout << "\nIncorrect operation! Try again: ";
cin >> num1 >> oper >> num2;
solve(num1, num2, oper);
}
}
从上一个程序中可以注意以下几点: 1)函数原型没有函数名[即void func(int)] 2)使用返回值[即返回结果;] 3)确保你有分号。
[旧帖子: cout&lt;&lt;“For Addition,select'+'”&lt ;; * //输出ADD指令
[没有结束分号]
供参考:
给std :: cin&GT;&GT; Q; //输入“q”到“退出”
std ::这里不需要。 (使用namespace std;)
(删除calc中的冒号:(n1,n2);)
-
您的程序现在可以使用。]
答案 1 :(得分:0)
我不会将其称为C ++程序。这是几乎所有业余C ++程序员都犯的错误。我将其称为C风格的C ++程序。请不要误解我的意思,但你需要开始以面向对象的方式思考,这样才能充分利用C ++的真正力量。
我建议你创建一个名为calculator的C ++类,并在开始编码之前考虑一下类的设计。我会将诸如Add,Subtract,Divide等方法保留为public和其他方法作为private。这也可以让你有机会在将来增强计算器类,例如为它添加内存支持,以便它记住最后的操作或结果。开始以面向对象的方式思考,以避免以后难以管理的意大利面条代码。
答案 2 :(得分:0)
我评论说我已从原始源代码更改了。 我已经检查过这段代码正在使用GCC(G ++)编译器
祝你好运!#include "stdafx.h"
#include <iostream>
using namespace std;
void calc (double _x, double _y); // CHANGED
double result;
double n1,n2;
double x,y; // CHANGED
char q,operation;
int main()
{
cout<<"Welcome to My Calculator" <<endl;
cout<<""<<endl;
cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl;
cout<<" EX: 2 + 2" <<endl;
cout<<""<<endl;
cout<<"Operators:"<<endl;
cout<<"For Addition, select '+'"<<endl;
cout<<"For Subtraction, select '-'"<<endl;
cout<<"For Multiplication, select '*'"<<endl;
cout<<"For Division, select '/'"<<endl;
cout<<""<<endl;
cout<<"To clear, select 'c'"<<endl;
cout<<"To quit, select 'q'"<<endl;
cout<<""<<endl;
cout<<"Input a mathmatical equation"<<endl;
cin>>n1>>operation>>n2;
calc(n1,n2); // CHANGED
cout<<"The answer is:"<<result<<endl;
std::cin>>q;
return 0;
}
void calc(double _x, double _y) // CHANGED
{
x=_x; // CHANGED
y=_y; // CHANGED
switch(operation)
{case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
cin>>x>>operation>>y; // CHANGED
calc (x,y); // CHANGED
}
}
答案 3 :(得分:0)
这是我的代码,它太长但支持更多运营商
#include "stdafx.h"
#include"iostream"
#include"math.h"
#include"iomanip"
#include <string>
#include <sstream>
using namespace std;
double calc(string mystring);
double calc2(string mystring);
double factoriel(double number);
double root(double num1,double num2);
double dowork(int a,int b,string c);
int main(){
cout<<"***************************************************\n";
cout<<"* *\n";
cout<<"* calculator *\n";
cout<<"***************************************************\n\n\n";
string inpstring;
cin >> inpstring;
int length_string=inpstring.length();
double result;
if(abs(calc(inpstring))>abs(calc2(inpstring))){
result=calc(inpstring);
}
else if(abs(calc(inpstring))<=abs(calc2(inpstring))){
result=calc2(inpstring);
}
double s;
s=3.14;
cout<<"\n"<<"\tresult : "<<result<<endl;
system("pause");
}
double calc(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();
string ops;
string myop;
double param1=0;
double param2=0;
double result=0;
string first_inp;
string second_inp;
ops="+-*/^%!R";
int length_ops=ops.length();
for (int i=0;i<=length_string-1;i++){
if (i==0){
if(mystring.substr (0,1)=="-"){
continue;
}
}
for (int j=0;j<=length_ops-1;j++){
if (!(mystring.substr (i,1).compare(ops.substr(j,1)))){
numberofop++;
if (numberofop==1){
myop=ops.substr(j,1);
first_inp = mystring.substr (0,i);
second_inp = mystring.substr (i+1,length_string-1);
stringstream(first_inp) >> param1;
stringstream(second_inp) >> param2;
if (myop=="+"){
a=1;
}
else if(myop=="-"){
a=2;
}
else if(myop=="*"){
a=3;
}
else if(myop=="/"){
a=4;
}
else if(myop=="^"){
a=5;
}
else if(myop=="%"){
a=6;
}
else if(myop=="!"){
a=7;
}
else if(myop=="R"){
a=8;
}
}
}
}
}
switch (a){
case 1:
result=param1+param2;
break;
case 2:
result=param1-param2;
break;
case 3:
result=param1*param2;
break;
case 4:
result=param1/param2;
break;
case 5:
result= pow(param1,param2);
break;
case 6:
result= int(param1)% int(param2);
break;
case 7:
result= factoriel(param1);
break;
case 8:
result= root(param1,param2);
break;
}
return result;
}
double factoriel(double a){
cout<<"enter number \n";
double i=a;
double d=1;
while(i>1){
d=d*i;
i--;
}
return d;
}
double root(double num1,double num2){
double result;
double reverce;
reverce=1/num2;
result=pow(num1,reverce);
return result;
}
double calc2(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();
double pi=3.1415;
double teta;
string ops;
string myop;
double param1=0;
double param2=0;
double result=0;
string first_inp;
string second_inp;
ops="logsincostancot";
int length_ops=ops.length();
for (int i=0;i<=length_string-1;i++){
if (i==0){
if(mystring.substr (0,1)=="-"){
continue;
}
}
for (int j=0;j<=length_ops-1;j++){
if (!(mystring.substr (i,3).compare(ops.substr(j,3)))){
numberofop++;
if (numberofop==1){
myop=ops.substr(j,3);
second_inp = mystring.substr (i+3,length_string-1);
stringstream(second_inp) >> param2;
if (myop=="log"){
a=1;
}
else if(myop=="sin"){
a=2;
}
else if(myop=="cos"){
a=3;
}
else if(myop=="tan"){
a=4;
}
else if(myop=="cot"){
a=5;
}
}
}
}
}
switch (a){
case 1:
result=log(param2);
break;
case 2:
teta=(double(param2)*pi)/180;
result=sin(teta);
break;
case 3:
teta=(double(param2)*pi)/180;
result=cos(teta);
break;
case 4:
teta=(double(param2)*pi)/180;
result=tanf(teta);
break;
case 5:
teta=(double(param2)*pi)/180;
result=1/tanf(teta);
break;
}
return result;
}
double dowork(int a,int b,string c){
string cut;
cut=c.substr(a,b);
double result;
result=calc(cut);
cout<<"\nresult is "<<result;
return result;
}
答案 4 :(得分:0)
我用c ++编写了这个简单的计算器,这样我们就可以根据需要添加,减去,除或多个数。例如:2 + 3 + 4-2 =然后你会得到答案。
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char c;
while(true){
cout << "To solve your math problem you can use this syntex: number(+, -, / or *)number=" << endl;
cout << "you can use as many number as you want." << endl << endl;
int n, ans;
char oper;
cin >> n;
ans = n;
cin >> oper;
while(oper!='='){
cin >> n;
if(oper=='+'){
ans = ans + n;
}
if(oper=='-'){
ans = ans - n;
}
if(oper=='/'){
ans = ans/n;
}
if(oper=='*'){
ans = ans*n;
}
cin >> oper;
}
cout << "answer: " << ans << endl << endl;
cout << "Press esc to exit or press any key to continue." << endl << endl;
c=getch();
if(c==27){
break;
}
}
return 0;
}
答案 5 :(得分:0)
这是我对它的看法,没有上课。来自你的全新。
#include <iostream>
#include <cstdlib>
using namespace std;
double a, b;
char operation;
int main()
{
cout << "Welcome to my calculator program.\n";
cout << "To make a calculation simply use the following operators -> (+ - * /).\n";
cout << "To exit, simply write an operation but replace the operator with 'q'. (eg. 2q3).\n";
while (operation != 'q'){
cin >> a >> operation >> b;
if(std::cin.fail()){
cout << "Input not numerical. Exiting...";
exit(1);
}
switch (operation)
{
case '+':
cout << " = " << a + b << '\n';
break;
case '-':
cout << " = " << a - b << '\n';
break;
case '*':
cout << " = " << a * b << '\n';
break;
case ':':
case '/':
cout << " = " << a / b << '\n';
}
}
return 0;
}