我如何组织我的代码? [英] How do I organize my code?
问题描述
您好,我被告知我需要:
项目7
更改程序以使用结构温度,风和天气测量。
重构您的程序,以便
的代码 - 温度为2文件(.h - 声明)和(.cpp - implements)
- wind有两个文件(.h - 声明)和(.cpp - implements)< br $>
-WeatherMeasurement有两个文件(.h - 声明)和(.cpp - implements)
- 你的主要是一个文件
我不确定它要我做什么。我需要使用我所做的结构来改变我的最后一个程序,但我不知道如何做到这一点。如果有人可以提供帮助,我会非常感激。
我尝试了什么:
我是编程新手,所以我真的不确定它想要什么。谢谢
++代码:
Hello, I am told that I am needed to:
Project 7
Change the program to use structs for temperature, wind, and weather measurement.
Refactor your program so that the code for
- temperature is in two files (.h – declarations) and (.cpp – implementations)
- wind is in two files (.h – declarations) and (.cpp – implementations)
-WeatherMeasurement is in two files (.h – declarations) and (.cpp – implementations)
- And your main is in one file
I am not sure what it wants me to do. I needed to change my last program using structs which I did, but I have no idea how to do this. If someone could help I will really appreciate it.
What I have tried:
I am new to programming so I am really not sure what it wants. Thank you
++ code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include<string>
#include <stdlib.h>
#include<vector>
#include<sstream>
using namespace std;
struct Weather_Station
{
string name;
double temperature;
double windspeed;
string windDirection;
};
string DisplayMenu(string station_name)
{
string str, temp;
do
{
cout << "*******************WEATHER STATION: " << station_name\
<< " *******************" << endl << endl;
cout << "I. Input a complete weather reading." << endl;
cout << "P. Print the current weather." << "\n";
cout << "H. Print the weather history (from most recent to oldest)." << endl;
cout << "E. Exit the program." << "\n";
cout << "Enter your choice: " << endl;
cin >> str;
temp = str;
for (std::string::size_type i = 0; i < str.length(); ++i)
temp[i] = toupper(str[i]);
str = temp;
} while (!(str == "I" || str == "P" || str == "H" || str == "E"));
return str;
}
double getTemperature()
{
double temp;
string temp_string;
stringstream converter;
cout << "Enter the temperature: ";
cin >> temp_string;
converter << temp_string;
converter >> temp;
return temp;
}
double getWindSpeed()
{
double temp;
string temp_string;
stringstream converter;
//this loop will be iterated continuously untill user enters windspeed which is greater than zero
cout << "Enter Wind speed: ";
cin >> temp_string;
converter << temp_string;
converter >> temp;
if (temp <= 0)
cout << "Wind speed should be always greater than 0(zero)";
}
do {
} while (temp <= 0);
return temp;
}
string getWindDirection()
{
string temp_string, temp;
do {
cout << "Enter the Wind Direction (North,South,East,West): ";
cin >> temp_string;
temp = temp_string;
for (std::string::size_type i = 0; i < temp_string.length(); ++i)
temp[i] = toupper(temp_string[i]);
} while (!(temp == "NORTH" || temp == "SOUTH" || temp == "EAST" || temp == "WEST" || temp == "N" || temp == "S" || temp == "E") || temp == "W");
temp_string = temp;
if (temp_string == "N")
temp_string = "NORTH";
if (temp_string == "S")
temp_string = "SOUTH";
if (temp_string == "W")
temp_string = "WEST";
if (temp_string == "E")
temp_string = "EAST";
return temp_string;
};
void printWeather(Weather_Station ws)
{
cout << "Station Name " << ws.name << endl;
cout << "Temperature " << ws.temperature << endl;
cout << "Wind Direction " << ws.windDirection << endl;
cout << "Wind Speed " << ws.windspeed << endl;
cout << endl;
}
int main()
{
//Have the user provide a name for the weather station upon entry.
vector<Weather_Station> myStation;
Weather_Station myWeather_Details;
string station_name, input_choice;
int histCount = 0;
cout << "Enter the name of Weather Station: ";
getline(cin, station_name);
myWeather_Details.name = station_name;
while (1)
{
//Control loop to perform various actions
input_choice = DisplayMenu(station_name);
if (input_choice == "I")
{
// get the details
myWeather_Details.temperature = getTemperature(); // get temperature
myWeather_Details.windDirection = getWindDirection(); //get wind direction
myWeather_Details.windspeed = getWindSpeed(); //get wind direction
//store the details
myStation.push_back(myWeather_Details);
}
else if (input_choice == "P")
{
cout << "*************Printing Current Weather*************" << endl;
printWeather(myStation.back());
}
else if (input_choice == "H")
{
//this loop will be iterated continuously untill user gives the input count more than 0 and it is not greater than available record count in vector
do {
cout << "Please enter how many records you want" << "\n";
cin >> histCount;
if (histCount <= 0)
cout << "Input record count should always be greater than 0(zero)"<<"\n";
else if (histCount>>myStation.size())
cout << "Input record count shouldn't be more than available record count"<<"\n";
} while (histCount <= 0 || histCount>>myStation.size());
cout << "*************Printing Weather History*************" << endl;
vector<Weather_Station>::reverse_iterator rit;
for (rit = myStation.rbegin(); rit != myStation.rend(); rit++)
printWeather(*rit);
}
else if (input_choice == "E")
{
exit(0);
}
}
return 0;
}
推荐答案
他们想让代码面向对象就像。
将标题视为一个文件,告诉该类的实现应该做什么。使用标头和实现来保持特定类的行为在应用程序中是同构的是一个好主意。有关更多信息,您需要阅读有关面向对象的编程。
至于您的问题,声明会进入.h文件。例如,在wind.h文件中,方法的签名
They want to make the code "Object-oriented" like.
Think of the headers as a file that tells what the implementations of that class are supposed to do. It is a good idea use headers and implementations to keep the behavior of that particular class homogeneous across the application. For more information, you need to read about object-oriented programming.
As for your problem, the declarations go into the .h files. For instance, in wind.h file the "signature" of the methods
string getWindDirection();
和
double getWindSpeed();
。
在wind.CPP文件中,您使用刚刚使用#include指令创建的头文件。然后编写方法的全部内容 getWindSpeed 和< b> getWindDirection 和温度和天气需要做同样的事情。
有关更详细的示例,请访问在哪里。
有很多当你阅读它时你会发现面向对象编程的优点。
祝你好运。
.
In the wind.CPP file you use the the header file you just created using the #include directive.Then write the full body of the methods getWindSpeed and getWindDirection and the same thing you need to do with temperature and weather.
For a more detailed example please visit here.
There are a lot of advantages of object-oriented programming which you get to know when you read about it.
Good luck.
这篇关于我如何组织我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!