在我的高级面向对象编程课程中,我真的需要帮助!这是该程序的工作原理;我们正在用 C++ 编写代码,我们将创建一个创建气象站的程序。如果按“1”,则输入温度、风速和风向;如果你按'2'如果会给你当前的天气读数; 如果您按“3”,它将为您提供天气历史;如果按“4”,则退出。此外,它还从文本文件中读取输出并将它们输出到控制台……我让所有这些都正常工作。我遇到的问题是我们要重载流(<< 和 >>)运算符,因此我们不再需要调用 print() 函数。以前,我调用了 print 函数来打印当前的天气读数以及温度历史。我已经排除了我的文件 wind.h 和 wind。cpp,因为我什至还没有开始尝试在那里实现重载;另外,我认为我在一篇文章中发布了足够的代码。另外,我应该提到,如果用户想要英语,该程序还会从english.txt 读取输出,如果他们想要piglatin,则从piglatin.txt 读取piglation。
基本上,我的问题是如何正确实现运算符流的重载?
源.cpp:
#include <iostream>
#include <string>
#include "temp.h"
#include "Wind.h"
#include "Language.h"
#include <fstream>
#define SIZE 10
using namespace std;
int main()
{
Temperature temp1;
win wind1;
//Temperature p;
string windDir[SIZE];
int windSpeed[SIZE];
int temp[SIZE];
//Tem p1(int temp);
int index=0;
int reply;
int reply2;
int lang;
int count=0;
string STRING1, STRING2, STRING3, STRING4, STRING5, STRING6;
fstream myEngFile;
fstream myPigFile;
ofstream out;
//Language Menu
cout<<"**** Welcome to The Michael Weather Station! \n";
cout<<"**** Elcomeway otay Ethay Ichaelmay Eatherway Ationstay! \n";
cout<<"Please enter 1 for english ";
cout<<"orway 2 orfay iglatinpay \n";
cin>>lang;
cin.clear();
switch(lang){
//English Main Menu
case 1:
//for(int count=0; count<21; count++)
for(int index=0; index<SIZE; index++){
bool menu = true;
while (menu != false){
myEngFile.open ("English.txt");
getline(myEngFile, STRING1);
cout<<STRING1<<"\n";
getline(myEngFile, STRING2);
cout<<STRING2<<"\n";
getline(myEngFile, STRING3);
cout<<STRING3<<"\n";
getline(myEngFile, STRING4);
cout<<STRING4<<"\n";
getline(myEngFile, STRING5);
cout<<STRING5<<"\n";
cin>>reply;
cin.clear();
myEngFile.close();
switch(reply){
//Gets data in English
case 1:
temp[count]=temp1.getTemp();
windSpeed[count] =wind1.getSpeed();
windDir[count] = wind1.getDirection();
cout<<" \n";
count++;
break;
//Prints data in English
case 2:
cout<<"--------------------------------\n";
//temp1.printTemp(temp[count-1]);
Temperature p = temp1.getTemp();
//cout<<temp[count-1];
cout<<p;
wind1.printWindMeasurement(windSpeed[count-1], windDir[count-1]);
cout<<" \n";
cout<<" \n";
break;
//Prints History in English
case 3:
cout<<"---------------------------------- \n";
//for(int index=SIZE%count; index<SIZE; index++){
for(int i=0; i<count; i++){
//temp1.printTemp(temp[i]);
cout<<temp[i];
wind1.printWindMeasurement(windSpeed[i], windDir[i]);
cout<<"\n";
}
break;
//Exits...In English?
case 4:
cout<<"Exiting............. \n";
menu= false;
break;
//Cheacks to see that only 1,2,3, or 4 is entered
default:
cout<<"Please enter 1,2,3, or 4 only!!! \n";
cout<<" \n";
} //Switch(reply) in english
} //While loop in english
return 0;
} //For loop in english
break;
//PigLatin Main Menu
case 2:
for(int index=0; index<SIZE; index++){
bool menu = true;
while (menu != false){
myPigFile.open ("PigLatin.txt");
getline(myPigFile, STRING1);
cout<<STRING1<<"\n";
getline(myPigFile, STRING2);
cout<<STRING2<<"\n";
getline(myPigFile, STRING3);
cout<<STRING3<<"\n";
getline(myPigFile, STRING4);
cout<<STRING4<<"\n";
getline(myPigFile, STRING5);
cout<<STRING5<<"\n";
getline(myPigFile, STRING6);
cout<<STRING6<<"\n";
cin>>reply2;
cin.clear();
myPigFile.close();
switch(reply2){
//Gets data in PigLatin
case 1:
temp[count]=temp1.getTemp2();
windSpeed[count] =wind1.getSpeed2();
windDir[count] = wind1.getDirection2();
cout<<" \n";
count++;
break;
//Prints data in PigLatin
case 2:
cout<<"----------------------------------\n";
//temp1.printTemp2(temp[count-1]);
cout<<temp[count-1];
wind1.printWindMeasurement2(windSpeed[count-1], windDir[count-1]);
cout<<" \n";
cout<<" \n";
break;
//Prints History in PigLatin
case 3:
cout<<"------------------------------------ \n";
//for(int index=SIZE%count; index<SIZE; index++){
for(int i=0; i<count; i++){
//temp1.printTemp2(temp[i]);
cout<<temp[i];
wind1.printWindMeasurement2(windSpeed[i], windDir[i]);
cout<<"\n";
}
break;
//Exits...in PigLatin?
case 4:
cout<<"exitingway............. \n";
menu= false;
break;
//Checks to see only 1,2,3, or 4 is entered
default:
cout<<"Easeplay enterway 1,2,3, orway 4 onlyway!!! \n";
cout<<" \n";
} //Switch (Reply2) in piglatin
} //While loop for PigLatin Main Menu
return 0;
} //For loop for PigLatin Main Menu
break;
} //switch(Lang)
} //Main
温度.cpp:
#include "temp.h"
#include "Language.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Lang La;
fstream myEngFile5;
string tempLine5;
string tempLine6;
//string tempLine1; // from language file
/**** Keep the getTemp() method, but get rid of the print method
and overload the operators there*******/
ostream &operator<<( ostream &out, const Temperature &p) //&f
{
//int far=0;
fstream myEngFile1;
string tempLine1;
myEngFile1.open ("English.txt");
myEngFile1.seekg(194);
getline(myEngFile1, tempLine1);
//La.getTempLang();
out<<p.tempLine1<<endl; // What is the Temperature?
// cin>>p; //f
return out;
//myEngFile1.close();
}
/*int Temperature::getTemp()
{
int value=0;
fstream myEngFile1;
string tempLine1;
myEngFile1.open ("English.txt");
myEngFile1.seekg(194);
getline(myEngFile1, tempLine1);
cout<<tempLine1<<"\n"; // What is the Temperature?
cin>>value;
return value;
myEngFile1.close();
}*/
int Temperature::getTemp2()
{
int value=0;
fstream myPigFile1;
string tempLine1;
myPigFile1.open ("PigLatin.txt");
myPigFile1.seekg(254);
getline(myPigFile1, tempLine1);
cout<<tempLine1<<"\n"; //What is the Temperature? in piglatin
cin>>value;
return value;
myPigFile1.close();
}
/*void Tem::printTemp(int temper)
{
fstream myEngFile5;
string tempLine5;
string tempLine6;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
cout<<tempLine5<<temper; //The Temperature is
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
cout<<tempLine6<<"\n"; //F
}*/
ostream &operator<<( ostream &out, const Temperature p)
{
out<<tempLine5<<p.value; //The Temperature is __
out<<tempLine6<<"\n"; //F
return out;
}
istream &operator>>( istream &in, Temperature &p)
{
//in>>p.value;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
in>>tempLine5;
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
in>>tempLine6;
return in;
}
/*void Tem::printTemp2(int temper)
{
fstream myPigFile4;
string tempLine5;
string tempLine6;
myPigFile4.open("PigLatin.txt");
myPigFile4.seekg(445);
getline(myPigFile4, tempLine5);
cout<<tempLine5<<temper; //The temperature is __ (in piglatin)
myPigFile4.seekg(473);
getline(myPigFile4, tempLine6);
cout<<tempLine6<<"\n"; //fay
}*/
温度.h:
#ifndef TEMP
#define TEMP
#include <fstream>
#include <string>
using namespace std;
//int index=0;
class Temperature
{
int value;
//char unit;
public:
string tempLine1;
int getTemp();
int getTemp2();
Temperature()
{
value=0;
//unit='F';
}
Temperature(int v/*, char u*/)
{
value=v;
//unit=u;
}
friend ostream &operator<<(ostream &out, const Temperature &p); // needs 'friend' i think
friend istream &operator>>(istream &in, Temperature &p);
//friend ostream operator<<(ostream out, const Temperature p);
//friend istream operator>>(istream in, Temperature &p);
//void printTemp(int temper);
//void printTemp2(int temper);
};
#endif