2017-11-13 6 views
0
#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 
+0

はあなたのデバッガでこれを実行してみましたがありますか? 'readExpression'にはいくつかの問題があります。 – 1201ProgramAlarm

答えて

0

あなたreadExpression機能がfinから正しく読み込むが、結果で何もしません。 (&で)その引数への参照を渡すことによって

void readExpression(std::ifstream &, double, char, double); 

あなたはstd::ifstreamで正しいことをやっている:あなたはとしてreadExpressionの署名を定義します。しかし、最後の3つの引数を値で渡しています。これは基本的に、各引数の一時コピーが関数内で使用するために作成されていることを意味します。したがって、finから読み込み、関数が終了するときに消去される一時変数に結果を入れます。関数で最後の3つの引数の内容を変更する場合は(std::ifstreamの変更方法と同様)、参照渡しします。署名を変更します。

void readExpression(std::ifstream &, double &, char &, double &); 

その後readExpression(fin, operand1, operatr, operand2)を呼び出した後、あなたはoperand1operatr、およびoperand2はあなたがそれらを含むように期待含まれていることを見つける必要があります。

参照の詳細については、this Wikipedia articleを参照してください。

P.S.、Try to avoid using namespace std whenever possible.

関連する問題