#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void getName (string first1_name, string last1_name, string first2_name, string last2_name);
string getLectureSection ();
void getLabSection (string lab_section1, string lab_section2);
void printIdInfo (ostream&out, string first1_name, string last1_name, string first2_name,
string last2_name, string CLASS, string lecture_section,
string lab_section1, string lab_section2, string DUE_DATE);
void readExpression (ifstream&, double operand1, char operatr, double operand2);
void echoExpression (ostream&out, double operand1, char operatr, double operand2);
void evaluateExpression (ostream&out, double operand1, char operatr, double operand2);
void writeFileLocation ();
int main()
{
const string CLASS = "C.S.1428",
DUE_DATE = "11/15/17";
string first1_name,
last1_name,
first2_name,
last2_name,
lecture_section,
lab_section1,
lab_section2;
double operand1,
operand2;
char operatr;
ifstream fin;
fin.open("prog5_002in.txt");
if(!fin)
{
cout << "Program Terminated." << endl
<< "Input file failed to open!" << endl;
return 1;
}
ofstream fout;
fout.open("prog5_002out.txt");
if(!fout)
{
cout << "Program Terminated." << endl
<< "Output file failed to open!" << endl;
return 2;
}
getName (first1_name, last1_name, first2_name, last2_name);
lecture_section = getLectureSection ();
getLabSection (lab_section1, lab_section2);
printIdInfo (fout, first1_name, last1_name, first2_name, last2_name,
CLASS, lecture_section,
lab_section1, lab_section2, DUE_DATE);
readExpression (fin, operand1, operatr, operand2);
while (operatr != '?')
{
echoExpression (fout, operand1, operatr, operand2);
evaluateExpression (fout, operand1, operatr, operand2);
readExpression (fin, operand1, operatr, operand2);
}
cout << endl << endl;
printIdInfo (cout, first1_name, last1_name, first2_name, last2_name,
CLASS, lecture_section, lab_section1, lab_section2, DUE_DATE);
writeFileLocation ();
fin.close();
fout.close();
system("PAUSE>NUL");
return 0;
}
void getName(string first1_name, string last1_name, string first2_name, string last2_name)
{
cout << "Enter your first name: ";
cin >> first1_name;
cout << endl << "Enter your last name: ";
cin >> last1_name;
cout << endl << endl;
cout << "Enter your first name: ";
cin >> first2_name;
cout << endl << "Enter your last name: ";
cin >> last2_name;
cout << endl << endl;
}
string getLectureSection()
{
string lecture_section;
cout << "Enter your three digit lecture section: ";
cin >> lecture_section;
cout << endl << endl;
return lecture_section;
}
void getLabSection(string lab_section1, string lab_section2)
{
cout << "Enter your two digit lab section number: ";
cin >> lab_section1;
cout << endl;
cout << "Enter your two digit lab section number: ";
cin >> lab_section2;
}
void printIdInfo (ostream&out, string first1_name, string last1_name, string first2_name,
string last2_name, string CLASS,string lecture_section,string lab_section1,
string lab_section2,string DUE_DATE)
{
out << first1_name << " " << last1_name << " & "
<< first2_name << " " << last2_name << endl
<< CLASS << lecture_section << endl
<< "Lab Section: " << "L" << lab_section1
<< "L" << lab_section2 << endl
<< DUE_DATE << endl << endl;
}
void readExpression (ifstream &fin, double operand1, char operatr, double operand2)
{
fin >> operand1, operatr, operand2;
}
void echoExpression(ostream&fout, double operand1,char operatr, double operand2)
{
fout << operand1 << operatr << operand2;
}
void evaluateExpression(ostream&fout, double operand1, char operatr, double operand2)
{
double expression;
switch (operatr)
{
case '+' :
expression = (operand1 + operand2);
fout << " = " << expression;
break;
case '-' :
expression = (operand1 - operand2);
fout << " = " << expression;
break;
case '*' :
expression = (operand1 * operand2);
fout << " = " << expression;
break;
case '/' :
if (operand2 == 0)
{
fout << endl << "Division by zero produces an "
<< "undefined result." << endl;
}
else
{
expression = (operand1/operand2);
fout << " = " << setprecision(1) << fixed << expression;
}
break;
default :
fout << operand1 << " " << operatr << " " << operand2;
fout << endl << "Encountered unknown operator.";
}
}
void writeFileLocation()
{
cout << "Program results have been written to Prog5_002out.txt";
}
私は一般的に特にC++でコーディングするのが非常に新しく、これは私のクラスで追加のクレジット割り当てのために取り組んでいるプログラムです。ほとんどの場合、実行するようになっていますが、printIdInfo関数からステートメントを表示する方法はわかりません。なぜなら、それをオープンにしておく方法がわからないからです。だから私は叫び声の呼び出しを働かせることができませんでしたが、半分はfoutで動作します。C++ operand1 operandr、operand2の不明な値
また、私はreadExpressionの関数呼び出しで何か遠隔から正しいことをしているとは思わないが、私が持っている入力ファイルからoperand1、operatr、およびoperand2を正しく見つけることはできないようだ。現時点では、出力ファイルはループしています(無期限に?)$記号の場合に欲しい、遭遇した不明な演算子を表示するだけです。しかし、私は本当に間違っていたのか分かりません。
これは本当に「期限切れ」ではありませんが、金曜日までにボーナスポイントを受け取ることができます。私はどこかにぶち壊されていると私はそれを突破しただけだと思うが、任意のポインタが評価されるだろう。ありがとうございました。
(Input File)
123.5 + 59.3
198.7/-26
125 $ 28
89.3 * 2.5
178.9 - 326.8
198.7/0
34.0 ? 10
はあなたのデバッガでこれを実行してみましたがありますか? 'readExpression'にはいくつかの問題があります。 – 1201ProgramAlarm