C++ 多文件项目&;C+中的函数+;
我正在尝试一起练习多文件项目(创建.cpp文件和.h文件)和函数。我有一些用于不同形状的函数,这些形状在不同的.cpp文件和.h文件(my functions.h)中,具有这些函数的原型。我创建了另一个.cpp文件(tools.cpp),其中包含所有函数(如-getInt、getFloat、getBool、getBoundedInt、getWidth等),这样我也可以在我的其他项目中使用该文件,也可以用于该tools.cpp文件。我有带有原型的tools.h文件。 现在,我想制作另一个函数(allFunctionController),它将处理所有这些不同类型的函数(例如-获取用户输入、检查输入是否有效、为用户提供帮助函数、调用形状函数等)。还有一个函数允许用户重复所有这些事情,我的主函数只调用这个函数。我提供的屏幕截图将更清晰易懂 [屏幕截图] tools.cpp:-C++ 多文件项目&;C+中的函数+;,c++,function,header-files,C++,Function,Header Files,我正在尝试一起练习多文件项目(创建.cpp文件和.h文件)和函数。我有一些用于不同形状的函数,这些形状在不同的.cpp文件和.h文件(my functions.h)中,具有这些函数的原型。我创建了另一个.cpp文件(tools.cpp),其中包含所有函数(如-getInt、getFloat、getBool、getBoundedInt、getWidth等),这样我也可以在我的其他项目中使用该文件,也可以用于该tools.cpp文件。我有带有原型的tools.h文件。 现在,我想制作另一个函数(al
// Tools.cpp
#include"Tools.h"
#include<string>
#include<iostream>
using namespace std;
namespace tools
{
int width( int value )
{
bool isNegative = value < 0;
int spaceForSign;
if ( isNegative )
{
spaceForSign = 1;
value = -value;
}
else
spaceForSign = 0;
int digitCount = 0;
int digits = value;
do
{
// pull one digit off and count it;
++digitCount;
// if I wanted to look at the digit before throwing away:
int digit = digits % 10;
digits = digits / 10; // remove one digit
} while ( digits > 0 );
return digitCount;
}
char getChar(string prompt)
{
while (true)
{
char userInput;
cout << prompt;
cin >> userInput;
cin.ignore(999,'\n');
if ( !cin.fail() ) return userInput;
cin.clear();
cin.ignore(999,'\n');
cout << "try again" << endl;
}
}
int getInt(string prompt)
{
while (true)
{
int userInput;
cout << prompt;
cin >> userInput;
cin.ignore(999,'\n');
if ( !cin.fail() ) return userInput;
cout << "input failure, try again";
cin.clear();
cin.ignore(999,'\n');
}
}
int getBoundedInt( string prompt, int lowBound, int highBound )
{
while (true)
{
int userInput = getInt(prompt);
if ( lowBound <= userInput && userInput <= highBound )
return userInput;
cout << "must be in range " << lowBound << " to "
<< highBound << ", try again" << endl;
}
}
float getFloat(string prompt)
{
while (true)
{
float userInput;
cout << prompt;
cin >> userInput;
cin.ignore(999,'\n');
if ( !cin.fail() ) return userInput;
cout << "input failure, try again";
cin.clear();
cin.ignore(999,'\n');
}
}
string getString(string prompt)
{
while (true)
{
string userInput;
cout << prompt;
cin >> userInput;
cin.ignore(999,'\n');
if ( !cin.fail() ) return userInput;
cout << "input failure, try again";
cin.clear();
cin.ignore(999,'\n');
}
}
string getLine(string prompt)
{
while (true)
{
string userInput;
cout << prompt;
getline(cin, userInput);
if ( !cin.fail() ) return userInput;
cout << "input failure, try again";
cin.clear();
cin.ignore(999,'\n'); // whatever caused fail
}
}
bool isLowerCase( char c )
{
return 'a' <= c && c <= 'z';
}
bool isUpperCase( char c )
{
return 'A' <= c && c <= 'Z';
}
bool isLetter( char c )
{
return isLowerCase(c) || isUpperCase(c);
}
char getLetter(string prompt)
{
while (true)
{
char userInput = getChar(prompt);
if ( isLetter(userInput) )
return userInput;
cout << "letters only, please" << endl;
}
}
bool getBool(string prompt)
{
while (true)
{
switch ( getChar(prompt) )
{
case 'y': case 'Y': return true;
case 'n': case 'N': return false;
}
cout << "y or n please" << endl;
}
}
}
我只是在这里添加空心和实心矩形代码。但是在我的项目中有更多的形状函数
hollowRectangle.cpp:-
#include"My functions.h"
#include<iostream>
using namespace std;
namespace myFunctions
{
void hollowRectangle (int size)
{
int row, column;
for (row = 1; row <= size; row++)
{
if (row > 1 && row < size)
{
cout << "*";
for (column = 2; column < size; column++)
{
cout << ' ';
}
cout << "*";
}
else
{
for (column = 1; column <= size; column++)
{
cout << "*";
}
}
}
cout << endl;
cout << endl;
cout << "Please press enter to finish...";
cin.ignore(999,'\n');
return;
}
}
// Program for the solid rectangle
#include"My functions.h"
#include<iostream>
using namespace std;
namespace myFunctions
{
int solidRectangle (int size)
{
int row, column;
for (row = 1; row <= size; row++)
{
for (column = 1; column <= size; column++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
cout << "Please press enter to finish...";
cin.ignore(999,'\n');
return 0;
}
}
#包括“My functions.h”
#包括
使用名称空间std;
名称空间myFunctions
{
空心空心矩形(整数大小)
{
int行,列;
用于(行=1;行1&&row 你到底想问什么?我想问我的allFunctionController如何才能正确运行程序?包括你的头文件,只调用你编写的函数(就像内置头文件一样)如果你想知道一个有用的东西来实现和理解的是,C++包含的只是H文件内容的复制粘贴。没有隐藏的魔法。这就是为什么你需要包括保护定义。
#include"My functions.h"
#include<iostream>
using namespace std;
namespace myFunctions
{
void hollowRectangle (int size)
{
int row, column;
for (row = 1; row <= size; row++)
{
if (row > 1 && row < size)
{
cout << "*";
for (column = 2; column < size; column++)
{
cout << ' ';
}
cout << "*";
}
else
{
for (column = 1; column <= size; column++)
{
cout << "*";
}
}
}
cout << endl;
cout << endl;
cout << "Please press enter to finish...";
cin.ignore(999,'\n');
return;
}
}
// Program for the solid rectangle
#include"My functions.h"
#include<iostream>
using namespace std;
namespace myFunctions
{
int solidRectangle (int size)
{
int row, column;
for (row = 1; row <= size; row++)
{
for (column = 1; column <= size; column++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
cout << "Please press enter to finish...";
cin.ignore(999,'\n');
return 0;
}
}
// This function will control all other functions.
#include"My functions.h"
#include"Tools.h"
#include<iostream>
using namespace std;
void allFunctionController ()
{
using namespace tools;
{
int size = getBoundedInt ("Please enter the size", 1, 75);
}
}
using namespace myFunctions;
{
void getHelp ()
{
int size,userInput;
cin >> userInput;
switch (userInput)
{
case 1:
solidRectangle (size);
}
}
}