【发布时间】:2012-01-28 21:07:11
【问题描述】:
我需要开发一个简单的可移植 C++ 程序 Billing_Unit。 它读取一些参数(电话号码等)并返回通话价格和剩余的免费分钟数。
我决定从标准输入获取 Billing_Unit 的数据,并将结果输出到标准输出。
我开发了两个测试单元:Test_Unit_Source 和 Test_Unit_Destination。
我决定组织我的节目单元的连续表演:
- Test_Unit_Source:从数据库中读取数据并将其放入 标准输出;
- Billing_Unit:读取标准输出 上一个单元,计算通话费用和剩余的免费 分钟,输出结果。
-
Test_Unit_Destination:读取调用 成本和剩余的空闲时间,将其存储到数据库中。
Test_Unit_Source |计费单位 | 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设置输入参数可能会更好,但我不知道在这种情况下如何返回结果。
【问题讨论】:
-
对于命令行参数,请尝试
int main (int argc, char *argv[])这将为您提供每个参数。您可以在要求输入之前检查是否没有。 -
那些看起来像功能测试(不是单元测试)
-
克里斯,是的,你说的很对。我知道如何通过命令行获取输入数据,但我不知道如何返回结果。编译器要求我只返回 int 类型。
-
不确定到底是什么问题,但看起来您想要一个具有预期结果的文件,您想要与之进行比较,并返回 ERROR_RESULT 或 SUCCESS_RESULT
-
Billing_Unit - 是一个主要单位。我想将它与数据库的读写分开。我想开发一个“盒子”来接收数据,计算一些东西并返回结果。最好使用标准输入和输出以获得更好的性能。但是,如果这个问题没有其他解决方案,我将不得不将结果写入文件。
标签: c++ c pipe project-organization