我决定处理模块化编程,通过头文件和cpp文件创建常规项目。
比如我拿这段代码 ///tabular.h 文件///
#pragma once
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
float pi = 3.14159;
void print(float, float, float, float (*)(float), char[]);
///tabular.h 文件///
#include "tabular.h"
using namespace std;
void print(float a, float b, float h, float (*func)(float), char s[]) {
float x;
cout << "==================" << endl;
cout << " x | "<< s << endl;
cout << "==================" << endl;
x = a;
while (x<b) {
printf("%6.2f |", x);
printf("%8.4f\n", (*func)(x));
x+=h;
}
}
/// app.cpp 文件(主)///
#include "tabular.h"
using namespace std;
float lower, upper, step;
int main() {
cout << "Enter lower, upper bounds and step, multiple of pi:";
cin >> lower >> upper >> step;
lower *= pi; upper *= pi; step *= pi;
char name[] = "sin(x)";
print(lower, upper, step, sin, name);
char name2[] = "cos(x)";
print(lower, upper, step, cos, name2);
}
仅当我编写这些命令时,这对我在 Visual Studio Code 中有效
PS C:\Users\User\Deskto