这个程序调用我的两个函数,并将给定的x值插入每个函数的方程中,然后将它们分配给变量g和h,并将它们相乘。为什么我的程序没有正确调用函数?一切都在编译,但我只得到5 0&#39的输出。
#include <cmath>
#include <math.h>
#include <iostream>
using namespace std;
float g;
float h;
float fx = (g * h);
float coss(float);
float sinn(float);
int main(){
// calls coss and sinn functions, and gets a value from both, and multiplies them.
for(float x = 0; x <= 6; x++){
g = coss(x) ;
h = sinn(x) ;
cout << fx << endl;
}
}
// plugs x value into equation, recieves a number and assigns to a variable, h
float coss(float x){
((4000 * 3.14159 * x + 3.14159/3) - ((pow((4000 * 3.14159 * x + 3.14159/3),3))/(3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),5))/(5 * 4 * 3 * 2 * 1)) - ((pow((4000 * 3.14159 * x + 3.14159/3),7))/(7 * 6 * 5 * 4 * 3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),9))/(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
//float h = coss(x);
return 0;
}
// plugs x value into equation, recieves a number and assigns to a variable, g
float sinn(float x){
((1) - (pow(x,2)/(2*1)) + (pow(x,4)/(4 * 3 * 2 * 1)) - (pow(x,6)/(6 * 5 * 4 * 3 * 2 * 1)) + (pow(x,8)/(8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
//float g = (5 * sinn(x));
return 0;
}
更新的代码:
#include <cmath>
#include <math.h>
#include <iostream>
using namespace std;
float result2;
float result;
float coss(float);
float sinn(float);
int main(){
for(float x = 0; x <= 6; x++){
float fx = (result * result2);
cout << fx << endl;
}
}
float coss(float x){
((4000 * 3.14159 * x + 3.14159/3) - ((pow((4000 * 3.14159 * x + 3.14159/3),3))/(3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),5))/(5 * 4 * 3 * 2 * 1)) - ((pow((4000 * 3.14159 * x + 3.14159/3),7))/(7 * 6 * 5 * 4 * 3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),9))/(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
float result = ((4000 * 3.14159 * x + 3.14159/3) - ((pow((4000 * 3.14159 * x + 3.14159/3),3))/(3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),5))/(5 * 4 * 3 * 2 * 1)) - ((pow((4000 * 3.14159 * x + 3.14159/3),7))/(7 * 6 * 5 * 4 * 3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),9))/(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
return result;
}
float sinn(float x){
((1) - (pow(x,2)/(2*1)) + (pow(x,4)/(4 * 3 * 2 * 1)) - (pow(x,6)/(6 * 5 * 4 * 3 * 2 * 1)) + (pow(x,8)/(8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
float result2 = ((1) - (pow(x,2)/(2*1)) + (pow(x,4)/(4 * 3 * 2 * 1)) - (pow(x,6)/(6 * 5 * 4 * 3 * 2 * 1)) + (pow(x,8)/(8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
return result2;
}
答案 0 :(得分:1)
您在循环开始之前分配float x = (g * h);
,当g
和h
都未初始化且未更新时,您的输出中会出现垃圾。您需要在fx = g * h;
和for
语句后的g = ...
循环中添加h = ...
。此外,当您想要返回计算结果时,return 0;
。
答案 1 :(得分:0)
你需要返回系列,而不是零。
另外,也许您可能希望#define fx (g*h)
代替?使用fx=g*h
并不意味着每次更改fx
或g
时都会更新h
- 这只表示fx
设置为g*h
一次。作为类比,如果我标记一个篮子&#34; apple&#34;,然后我将橙子放入其中,标签不会自动更改为&#34; orange&#34 ;,我需要手动更改标签
这是您的代码正确无误的原因:
#include <cmath>
#include <iostream>
using namespace std;
float coss(float);
float sinn(float);
int main(){
for(float x = 0; x <= 6; x++){
float fx = (coss(x) * sinn(x));
cout << fx << endl;
}
return 0;
}
float coss(float x){
float result = ((4000 * 3.14159 * x + 3.14159/3) - ((pow((4000 * 3.14159 * x + 3.14159/3),3))/(3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),5))/(5 * 4 * 3 * 2 * 1)) - ((pow((4000 * 3.14159 * x + 3.14159/3),7))/(7 * 6 * 5 * 4 * 3 * 2 * 1)) + ((pow((4000 * 3.14159 * x + 3.14159/3),9))/(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
return result;
}
float sinn(float x){
float result2 = ((1) - (pow(x,2)/(2*1)) + (pow(x,4)/(4 * 3 * 2 * 1)) - (pow(x,6)/(6 * 5 * 4 * 3 * 2 * 1)) + (pow(x,8)/(8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)));
return result2;
}
请注意,我删除了您的<math.h>
。
或者,在main
中,您可以(但不一定):
for(float x=0; x<=6; x++) {
float g = coss(x);
float h = sinn(x);
float fx = g * h;
cout << fx << endl;
}