//main.cpp #include <iostream> #include <cstdlib> #include <cstdint> #include <string> #include<vector> #include "hypotenuse.h" using namespace std; extern float hypotenuse(float a, float b); namespace hypo { float module_1(); int module_2(); } using namespace hypo; static float hypo::module_1() { int i; int c = 45; float q[3] = { 0,0,0 }; string s = "Enter the length of the two sides of the right triangle:n"; string t = "a="; string u = "b="; string v = "Thus,the hypotenuse c="; string w = "Completed!n"; vector<float> cont(3); cout << s << t; cin >> q[0]; cout << u; cin >> q[1]; q[2] = hypotenuse(q[0], q[1]); cont[0] = q[0]; cont[1] = q[1]; cont[2] = q[2]; cout << v << q[2] << "n" << w << endl; for (i = 0; i < 3; i++) { cout << cont[i] << "n"<<endl; } return EXIT_SUCCESS; } class hypo_class { private: static string a, b, r; public: static int module_3(); }; static int hypo::module_2() { string a = "hypo"; string b = "ten"; string r = "use"; cout << a+b+r<<"n"<< endl; return EXIT_SUCCESS; } int hypo_class::module_3() { cout << "Thanks!n" << endl; return EXIT_SUCCESS; } int main() { hypo::module_1(); hypo::module_2(); hypo_class::module_3(); }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859//hypotenuse.cpp #include <iostream> #include "hypotenuse.h" using namespace std; float hypotenuse(float a, float b) {if (a < 0 || b < 0){abort();}return sqrtf((float)(pow(a, 2) + pow(b, 2))); } 123456789101112
// hypotenuse.h #ifndef _HYPOTENUSE_H__ #define __HYPOTENUSE_H__ #include <iostream> float hypotenuse(float a, float b); #endif 12345678
(跟着PDF自学c++,共花了6小时,边学边写,自己即兴想的一个小项目。自己调开发环境里的问题花了好久,但是我很开心,毕竟自己学,自己调环境,最后成功运行了。这种成就感相对于“跟别人学,让别人帮忙把环境配好再写代码”而言要高许多。)