项目组织的最佳方式是什么?
我需要开发一个简单的可移植 C++ 程序 Billing_Unit。 它读取一些参数(电话号码等)并返回通话价格和剩余的免费分钟数。
我决定从标准输入获取 Billing_Unit 的数据,并将结果输出到标准输出。
我开发了两个测试单元:Test_Unit_Source 和 Test_Unit_Destination。
我决定组织我的程序单元的连续执行:
- Test_Unit_Source:从数据库读取数据并将其放入 标准输出;
- Billing_Unit:读取标准输出 上一个单元,计算通话费用和剩余的免费费用 分钟,输出结果。
Test_Unit_Destination:读取调用 费用和剩余的免费分钟数,将其存储到数据库中。
测试单元源 |计费单位 | Test_Unit_Destination
简化的Test_Unit_Source:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
using namespace std;
int main() {
signed char buf;
string Name_File;
ifstream inp_file;
inp_file.open("temp.txt",std::ios::binary);
if (!inp_file) return ERROR_RESULT;
do {
buf=inp_file.get();
cout<<buf;
} while (!inp_file.eof());
return SUCCESS_RESULT;
}
简化的Billing_Unit - 它必须是可移植的:
#include <iostream>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
int main() {
signed char var;
unsigned long res;//cents
signed char next_call;
while (!EOF_USERS) {
std::cin >> input_data;
...
//calculations
...
std::cout << result;
}
std::cout << EOF_USERS;
return SUCCESS_RESULT;
}
简化的Test_Unit_Destination:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
using namespace std;
int main() {
signed char buf;
ofstream out_file;
out_file.open("out.txt",std::ios::binary);
if (!out_file) return ERROR_RESULT;
while (!EOF_USERS) {
cin >> buf;
out_file << buf;
}
return SUCCESS_RESULT;
}
实际上Test_Unit_Source 和Test_Unit_Destination 可以合并在一个程序单元中。这取决于我的决定。
我的项目组织得好吗? 该项目的最佳组织方式是什么?可能最好通过命令行设置 Billing_Unit 的输入参数,但我不知道在这种情况下如何返回结果。
I need to develop a simple portable C++ program Billing_Unit.
It reads some parameters (telefone number, etc.) and returns the price of call and rest of free minutes.
I decided to get data for Billing_Unit from standart input, and output result to standart output.
I developed two test units: Test_Unit_Source and Test_Unit_Destination.
I decided to organize a consecutive performing of my program units:
- Test_Unit_Source: Reads data from database and puts it to
standart output; - Billing_Unit: Reads standart output from
previous unit, Calculates the call costs and the rest of free
minutes, outputs result. Test_Unit_Destination: Reads the call
costs and the rest of free minutes, stores it to database.Test_Unit_Source | Billing_Unit | Test_Unit_Destination
Simplified Test_Unit_Source:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
using namespace std;
int main() {
signed char buf;
string Name_File;
ifstream inp_file;
inp_file.open("temp.txt",std::ios::binary);
if (!inp_file) return ERROR_RESULT;
do {
buf=inp_file.get();
cout<<buf;
} while (!inp_file.eof());
return SUCCESS_RESULT;
}
Simplified Billing_Unit - it must be portable:
#include <iostream>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
int main() {
signed char var;
unsigned long res;//cents
signed char next_call;
while (!EOF_USERS) {
std::cin >> input_data;
...
//calculations
...
std::cout << result;
}
std::cout << EOF_USERS;
return SUCCESS_RESULT;
}
Simplified Test_Unit_Destination:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#define SUCCESS_RESULT 0
#define ERROR_RESULT 1
using namespace std;
int main() {
signed char buf;
ofstream out_file;
out_file.open("out.txt",std::ios::binary);
if (!out_file) return ERROR_RESULT;
while (!EOF_USERS) {
cin >> buf;
out_file << buf;
}
return SUCCESS_RESULT;
}
Actually Test_Unit_Source and Test_Unit_Destination can be united in one program unit. It depends on my decision.
Is it a good organization of my project?
What is the best organization of this project? May be it's better to set input parameters for Billing_Unit through command line, but I don't know how to return the result in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的设计完全适合类 UNIX 语言的模型:通过将程序编写为读取
stdin
并写入stdout
的过滤器,您可以为用户提供轻松的灵活性对数据进行预处理或后处理。例如,正如您所编写的,您可以简单地运行
But 假设输入数据的格式错误。然后你可以运行
并且你可以使用以下命令更改输出格式
这些是简单的示例,但我希望它们向您展示使用像您这样的程序是多么容易。
如果您确实打算像这样直接从 shell 运行程序,请确保将所有错误消息写入
stderr
而不是stdout
。这会将它们与输出数据分开,因此它们不会被下一个命令处理为输入。Your design fits perfectly into the model of unix-like languages: by writing your program as a filter that reads
stdin
and writes tostdout
you're giving users the flexibility to easily pre- or post-process the data.For example, as you've written it, you can simply run
But suppose the input data is in the wrong format. Then you can run
And you can change the output format with
These are simple examples, but I hope they show you how easy it is to work with a program like yours.
If you do plan to run your program directly from the shell like this, be sure that any error messages are written to
stderr
instead ofstdout
. That will separate them from your output data, so they won't be processed as input by the next command.