佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 766|回复: 9

[求助]C++的难题了!!

[复制链接]
发表于 20-3-2006 01:32 PM | 显示全部楼层 |阅读模式
我真不会做。。。请教各位教教我。。。十万火急!!谢谢。。。
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.
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
刚才下午做了,不知道对不对!
回复

使用道具 举报

Follow Us
 楼主| 发表于 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;
}
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 21-3-2006 09:48 AM | 显示全部楼层
可以告诉我,为何你这么做吗?
两者之间有何不同?
回复

使用道具 举报

发表于 21-3-2006 07:18 PM | 显示全部楼层
不乱用variable, 和control statement.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-4-2024 11:37 PM , Processed in 0.057619 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表