查看: 856|回复: 9
|
[求助]C++的难题了!!
[复制链接]
|
|
我真不会做。。。请教各位教教我。。。十万火急!!谢谢。。。
Write a C++ program that asks the user to enter two integer values for the variables n and X respectively. Then, it should evaluate the following expression:

The values for First, Middle and Last can be calculated by the given equations.
 |
|
|
|
|
|
|
|
发表于 20-3-2006 02:12 PM
|
显示全部楼层
1. Final function (return value)
2. First function (return value)
3. Middle function (return value)
4. Last function (return value)
input is X and n
void main(){
...
...
//get input X and get input n
cout>>final(X,n);
}
int final(int a, int b){
x=final(a,b);
y=middle(a,b);
z=middle(a,b);
...
...
...
return result;
}
int first(int a, int b){
...
...
...
return result;
}
int middle(int a, int b){
...
...
...
return result;
}
int last(int a, int b){
...
...
...
return result;
} |
|
|
|
|
|
|
|

楼主 |
发表于 20-3-2006 03:19 PM
|
显示全部楼层
这个
1. Final function (return value)
2. First function (return value)
3. Middle function (return value)
4. Last function (return value)
怎么用? |
|
|
|
|
|
|
|

楼主 |
发表于 20-3-2006 03:26 PM
|
显示全部楼层
前部分是这样,对吗?
#include <iostream>
#include <cmath>
using namespace std;
input is X and n
void main(){
int x;
int n;
double ansFirst = 0;
double ansMiddle = 0;
double ansLast = 0;
double ansFinal = 0;
cout<<"Enter a value for x : "; //input x and n
cin>>x;
cout<<"Enter a value for n : ";
cin>>n;
cout<<"Your input for x is : "<<x<<endl;
cout<<"Your input for n is : "<<n<<endl;
}
int final(int a, int b){ |
|
|
|
|
|
|
|

楼主 |
发表于 20-3-2006 03:39 PM
|
显示全部楼层
真的麻烦各位大大帮帮小弟了!等下5点就要交了,怎么做都做不来! |
|
|
|
|
|
|
|

楼主 |
发表于 20-3-2006 10:17 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 20-3-2006 10:18 PM
|
显示全部楼层
#include <iostream>
#include <cmath>
using namespace std;
double factoria(int n)
{
if(n==0)
return 1;
else return(n*factoria(n-1));
}
double last(int n)
{
double i=1,hold=0;
bool check=1;
for(int counter=1;counter<=n;counter++)
{
if(check)
{ hold+=(pow(i,counter)/factoria(counter));
check=false;}
else
{ hold-=(pow(i,counter)/factoria(counter));
check=true;}
i+=0.5;
}
return hold;
}
double first(int x,int n)
{
return (factoria(x)-pow(double(x),n));
}
double middle(int x, int n)
{
int i,j;
double hold=0;
if((first(x,n))<0) i=1;
else if((first(x,n))>0) i=2;
else i=3;
switch(i){
case 1: for(j=1;j<=n;j++)
hold+=factoria(j);break;
case 2: for(j=1;j<=x;j++)
hold+=factoria(j);break;
case 3: for(j=1;j<=x;j++)
hold+=pow(double(x),j);break;
}
return hold;
}
int main()
{
int n, x;
cout<<"Enter x: ";
cin>>x;
cout<<"Enter n: ";
cin>>n;
double final, First, Middle, Last;
First=first(x,n);
Middle=middle(x,n);
Last=last(n);
final=(sqrt(First)/(Middle-6))+Last;
cout<<"First: "<<First<<endl;
cout<<"Middle: "<<Middle<<endl;
cout<<"LastL : "<<Last<<endl;
cout<<"Final : "<<final<<endl;
system("pause");
return 0;
} |
|
|
|
|
|
|
|
发表于 21-3-2006 01:50 AM
|
显示全部楼层
原帖由 vwyk 于 20-3-2006 10:18 PM 发表
double last(int n)
{
double i=1,hold=0;
bool check=1;
for(int counter=1;counter<=n;counter++)
{
if(check)
{ hold+=(pow(i,counter)/factoria(counter));
check=false;}
else
{ hold-=(pow(i,counter)/factoria(counter));
check=true;}
i+=0.5;
}
return hold;
}
//Yours can work but i will do as below...
double last(int n){
double hold=0;
int i;
for(i=0;i<n;i++){
if(i%2==0){
hold +=((i+2)//2)//factorial(i+1);
}
else{
hold -=((i+2)//2)//factorial(i+1);
}
}
return hold;
}
double middle(int x, int n)
{
int i,j;
double hold=0;
if((first(x,n))<0) i=1;
else if((first(x,n))>0) i=2;
else i=3;
switch(i){
case 1: for(j=1;j<=n;j++)
hold+=factoria(j);break;
case 2: for(j=1;j<=x;j++)
hold+=factoria(j);break;
case 3: for(j=1;j<=x;j++)
hold+=pow(double(x),j);break;
}
return hold;
}
double middle(int x, int n)
{
int i;
double hold=0;
if(first(x,n)<0)
for(i=1;i<=n;i++)
hold +=factorial(i);
else if(first(x,n)>0)
for(i=1;i<=x;i++)
hold +=factorial(i);
else
for(i=1;i<=x;i++)
hold +=pow(x,i);
}
return hold;
} |
|
|
|
|
|
|
|

楼主 |
发表于 21-3-2006 09:48 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 21-3-2006 07:18 PM
|
显示全部楼层
不乱用variable, 和control statement. |
|
|
|
|
|
|
| |
本周最热论坛帖子
|