Имя пользователя:
Пароль:
 

Название темы: C++ новичок
Показать сообщение отдельно

Новый участник


Сообщения: 35
Благодарности: 0

Профиль | Отправить PM | Цитировать


Из самоучителя "10 минут за урок" программа калькулятор

main.cpp
#include <iostream>

#include "PromptModule.h"
#include "ErrorHandlingModule.h"
using namespace std;

char GetOperator(void)

{char Operator;
cout<<"Operator: ";
cin>> Operator;
return Operator;}

float GetOperand(void)

{float Operand;
cout<<"Operand:";
cin>> Operand;
return Operand;}

float Accumulate(const char theOperator,const float theOperand)
{
static float myAccumulator=0;
switch (theOperator)
{
case '+':myAccumulator=myAccumulator+theOperand;break;
case '-':myAccumulator=myAccumulator-theOperand;break;
case '*':myAccumulator=myAccumulator*theOperand;break;
case '/':myAccumulator=myAccumulator/theOperand;break;
default: throw runtime_error ("Error - Invalid operator - must be one of +,-,* or /");
};
return myAccumulator;
}
int main (int argc,char* argv[])

{ SAMSErrorHandling::Initialize();
do

{
try
{ char Operator =GetOperator();
float Operand=GetOperand();
cout << Accumulate(Operator,Operand)<<endl;}
catch ( runtime_error RuntimeError)
{ SAMSErrorHandling::HandleRuntimeError (RuntimeError);}

catch (...)
{ SAMSErrorHandling::HandleNotANumberError();}
}
while (SAMSPrompt::UserWantsToContinue ("Divide ?"));


return 0;
}


затем ErrorHandlingModule.cpp

#include <iostream>
#include "ErrorHandlingModule.h"
#include <exception>

namespace SAMSErrorHandling
{using namespace std;
void Initialize(void)
{cin.exceptions(cin.failbit);}
int HandleNotANumberError(void)
{cerr <<"INPUT ERROR - NOT A NUMBER?"<<endl;
cin.clear();
char BadInput[4];
cin>>BadInput;
return 1;
};
int HandleRuntimeError(runtime_error theRuntimeError)
{ cerr<<theRuntimeError.what()<<endl;
return 1;
};
}

ErrorHandlingModule.h

#ifndef ErrorHandlingModuleH
#define ErrorHandlingModuleH
#include <exception>

namespace SAMSErrorHandling
{ using namespace std;
void Initialize(void);

int HandleNotANumberError(void);
int HandleRuntimeError(runtime_error theRuntimeError);
};
#endif

есть ещё файлы PromptModule.cpp,PromptModule.h

Отправлено: 18:54, 09-08-2007 | #3

Название темы: C++ новичок