佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 941|回复: 15

[c++] 的問題!!

[复制链接]
发表于 5-8-2008 11:57 PM | 显示全部楼层 |阅读模式




我们真得很需要你们的帮忙,因为那个老师傻的(我们是engineering course 的,他以为我们是programming student )
可能“新官上任三把火”(c++主任)。。。给我们 “够力够力”
问题2,我的group已经决绝了^^
问题4,有三面哟 ,很有挑战性的
回复

使用道具 举报


ADVERTISEMENT

发表于 6-8-2008 09:50 AM | 显示全部楼层

回复 1# demonden02 的帖子

隨便掃過了一下
覺得跟著上面的instruction做就行了
有何難
問題4連你該做些甚麼都說的很清楚下
應該也不是問題才對
加油吧
自己努力得來得成果才寶貴的
回复

使用道具 举报

发表于 6-8-2008 10:53 AM | 显示全部楼层

回复 1# demonden02 的帖子

我当年也是Engineering的(电脑电子工程,主要学习的都是硬体相关的),当时的功课也不比你的简单。其实这些assignment都已经把所有主要的步骤写出来了,你只需要把这些步骤以电脑语言写出来就是了,根本就是按图索骥。你的老师并不傻,傻的是*ahem*。。。
回复

使用道具 举报

 楼主| 发表于 6-8-2008 04:28 PM | 显示全部楼层
原帖由 Squall_Chua 于 6-8-2008 09:50 发表
隨便掃過了一下
覺得跟著上面的instruction做就行了
有何難
問題4連你該做些甚麼都說的很清楚下
應該也不是問題才對
加油吧
自己努力得來得成果才寶貴的


好的,今晚我再试看看
如果有问题,我就要请教你咯。。。
回复

使用道具 举报

 楼主| 发表于 6-8-2008 04:29 PM | 显示全部楼层
原帖由 geekman 于 6-8-2008 10:53 发表
我当年也是Engineering的(电脑电子工程,主要学习的都是硬体相关的),当时的功课也不比你的简单。其实这些assignment都已经把所有主要的步骤写出来了,你只需要把这些步骤以电脑语言写出来就是了,根本就是按图索骥 ...

哈哈。。。。我不是电脑电子工程
我是M&M engineering.....
diploma year2 only....><
回复

使用道具 举报

 楼主| 发表于 6-8-2008 11:06 PM | 显示全部楼层


#include <iostream>
#include <ctime>
#include <cmath>
#include <ctype.h>
using namespace std;

char tryAgain();
void validateProjectileInfo( double &v, double &theta );

int main()
{
        const int MIN_DISTANCE = 200;
        const int MAX_DISTANCE = 500;
        const int OK_LIMIT = 5;
        const double CHEAT_VELOCITY = 999.0;

        const double G = 9.8;
        const double PI = 3.14159;
       
        int nTries = 0;

        char playAgain ='';

        double distance = 0.0;
        double velocity = 0.0;
        double angle = 0.0;
        double range = 0.0;

        srand( time(0) );
       
        do
        {
                nTries=0;
                distance = MIN_DISTANCE+(rand()%(MAX_DISTANCE-MIN_DISTANCE+1));
                cout<< endl;
                cout<< "Distance to target: " << distance << " meters." << endl;

                playAgain='T';
                double radian_angle=0.0;
                double sinus=0.0;

                while(playAgain=='T'){
                nTries++;
                validateProjectileInfo(velocity,angle);
                radian_angle = angle*(PI/180);
                sinus = sin(2 * radian_angle);
                range = ((velocity*velocity)*sinus)/G;
                        if(range<distance-range || range>distance+range){
                                //Failed to hit target
                                if(range<distance-range){
                                        cout<< "Failed to hit target, you fell short." << endl;
                                } else {
                                        cout<< "Failed to hit target, you went long." << endl;
                                }
                        } else {
                                cout<< "Congratulations! You hit the target with " << nTries << " tries." << endl;
                        }
                playAgain = tryAgain();
                }
                playAgain = tryAgain();

        } while ( playAgain == 'Y' );

        system("AUSE";
        return 0;
       
}

        char tryAgain()
        {
                char input='';
                boolean proceed=false;
                boolean gone_through=false;
                while (proceed==false){
                        if(gone_through==true){ cout<< "Error, Please Enter Y/N Only" << endl; }
                        cout<< "Do you want to try again? (Y/N) : ";
                        cin >> input;
                        gone_through=true;
                        if (input.compare("Y"!=0 || input.compare("N"!=0 || input.compare("y"!=0 || input.compare("n"!=0){
                                proceed=true;
                        }
                }
                return toupper(input);
        }

        void validateProjectileInfo(double &v, double &theta)
        {
                boolean proceed=false;
                boolean gone_through=false;
                while (proceed==false){
                        if(gone_through==true){ cout<< "Velocity must be more than zero" << endl; }
                cout<< "lease Enter a velocity : ";
                cin>> v;
                        gone_through=true;
                        if(v>0){
                                proceed=true;
                        }
                }
                gone_through=false;
                proceed=false;
                while (proceed==false){
                        if(gone_through==true){ cout<< "Angle must be between 0 to 90" << endl; }
                cout<< "lease Enter an angle : ";
                cin>> theta;
                        gone_through=true;
                        if(theta>0 && theta<90){
                                proceed=true;
                        }
                }
               
        }


谁可以帮帮忙?
到底那里出错了

[ 本帖最后由 demonden02 于 6-8-2008 11:10 PM 编辑 ]
回复

使用道具 举报

Follow Us
发表于 7-8-2008 10:58 PM | 显示全部楼层
下次贴code 用 【code】【/code】 吧, 懒得将emotion变回code, 就直接写了个sample code 给你参考参考好了。
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <cstdlib>

  5. using namespace std;

  6. //function phototype
  7. char tryAgain();
  8. void validateProjectileInfo( double &v, double &theta );

  9. int main()
  10. {
  11.         //variable declaration      
  12.         const int MIN_DISTANCE = 200;
  13.         const int MAX_DISTANCE = 500;
  14.         const int OK_LIMIT = 5;
  15.         const double CHEAT_VELOCITY = 999.0;

  16.         const double G = 9.8;
  17.         const double PI = 3.14159;
  18.       
  19.         int nTries = 0;

  20.         char playAgain ;

  21.         double distance = 0.0;
  22.         double velocity = 0.0;
  23.         double angle = 0.0;
  24.         double range = 0.0;
  25.         double radian;

  26.         srand( time(0) );
  27.         
  28.         //BODY START
  29.         do
  30.         {
  31.                 nTries=0;
  32.                 distance = MIN_DISTANCE+(rand()%(MAX_DISTANCE-MIN_DISTANCE+1));
  33.                 cout << endl;
  34.                 cout << "Distance to target: " << distance << " meters." << endl;
  35.                
  36.                 range = 0;
  37.                
  38.                 for(nTries = 0; abs(range - distance) > 5; nTries++)
  39.                 {
  40.    
  41.                     validateProjectileInfo( velocity, angle );
  42.                     radian = angle * PI / 180;               
  43.                     range = velocity * velocity * sin( 2 * radian ) / G;
  44.       
  45.                         
  46.                     if ((range - distance) > 5 )
  47.                        cout << "you went long for " << range - distance << " meters" << endl;
  48.                     else if (( distance - range ) > 5 )
  49.                        cout << "you went short for " << distance - range << " meters" << endl;
  50.                     else
  51.                         cout << "Congratulations! You hit the target with " << nTries << " tries." << endl;
  52.                   }
  53.                
  54.                 playAgain = tryAgain();
  55.                
  56.         }while ( playAgain == 'Y' );

  57.         system("PAUSE");
  58.         return 0;
  59.       
  60. }

  61. char tryAgain()
  62. {
  63.      char user_input;  //enter loop
  64.      
  65.      while( toupper(user_input) != 'N' && toupper(user_input) != 'Y' )
  66.      {
  67.             cout << "Play Again? ";
  68.             cin >> user_input;
  69.      }
  70.      
  71.      return toupper(user_input);
  72. }

  73. void validateProjectileInfo( double &v, double &theta )
  74. {
  75.      v = 0;
  76.      theta = 0;
  77.      
  78.      while( v <= 0 )
  79.      {
  80.             cout << "enter value of velocity (m/s) : ";
  81.             cin >> v;
  82.             
  83.             if( v <= 0 )
  84.                 cout << "Velocity must be more than zero" << endl;
  85.      }
  86.      
  87.      while(theta <= 0 || theta > 90 )
  88.      {
  89.             
  90.             cout << "enter value of angle (degree) : ";
  91.             cin >> theta;
  92.             
  93.             if( theta <= 0 || theta > 90 )
  94.                 cout << "Angle must be between 0 to 90" << endl;
  95.             
  96.      }
  97. }   
复制代码

[ 本帖最后由 onlylonly 于 7-8-2008 11:15 PM 编辑 ]
回复

使用道具 举报

发表于 7-8-2008 11:23 PM | 显示全部楼层

回复 6# demonden02 的帖子

对了, c++ syntax 里没有 boolean 的, 只是 bool 罢了。

还有 string.compare 只能用在string

char input ;
input.compare("Y") !=0

这个, 绝对错误。在这里input 是char, 并非string , 除非
string input
input.compare("Y") !=0

记住, char != string, 两者不相同。string 是string, char 是char

[ 本帖最后由 onlylonly 于 7-8-2008 11:33 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 7-8-2008 11:37 PM | 显示全部楼层
谢谢你  onlylonly

好的,下次我会用 [code] 的。。

[ 本帖最后由 demonden02 于 7-8-2008 11:39 PM 编辑 ]
回复

使用道具 举报

发表于 7-8-2008 11:50 PM | 显示全部楼层

回复 9# demonden02 的帖子

这个是我将你的code的syntax error 修改, 并将char 改为string, 不过之间的 logic error 等, 我没修改, 你也可以参考参考。
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <ctype.h>
  5. #include <string>
  6. using namespace std;

  7. string tryAgain();
  8. void validateProjectileInfo( double &v, double &theta );

  9. int main()
  10. {
  11.         const int MIN_DISTANCE = 200;
  12.         const int MAX_DISTANCE = 500;
  13.         const int OK_LIMIT = 5;
  14.         const double CHEAT_VELOCITY = 999.0;

  15.         const double G = 9.8;
  16.         const double PI = 3.14159;
  17.       
  18.         int nTries = 0;

  19.         string playAgain ;

  20.         double distance = 0.0;
  21.         double velocity = 0.0;
  22.         double angle = 0.0;
  23.         double range = 0.0;

  24.         srand( time(0) );
  25.       
  26.         do
  27.         {
  28.                 nTries=0;
  29.                 distance = MIN_DISTANCE+(rand()%(MAX_DISTANCE-MIN_DISTANCE+1));
  30.                 cout<< endl;
  31.                 cout<< "Distance to target: " << distance << " meters." << endl;

  32.                 playAgain='T';
  33.                 double radian_angle=0.0;
  34.                 double sinus=0.0;

  35.                 while(playAgain=="T"){
  36.                 nTries++;
  37.                 validateProjectileInfo(velocity,angle);
  38.                 radian_angle = angle*(PI/180);
  39.                 sinus = sin(2 * radian_angle);
  40.                 range = ((velocity*velocity)*sinus)/G;
  41.                         if(range<distance-range || range>distance+range){
  42.                                 //Failed to hit target
  43.                                 if(range<distance-range){
  44.                                         cout<< "Failed to hit target, you fell short." << endl;
  45.                                 } else {
  46.                                         cout<< "Failed to hit target, you went long." << endl;
  47.                                 }
  48.                         } else {
  49.                                 cout<< "Congratulations! You hit the target with " << nTries << " tries." << endl;
  50.                         }
  51.                 playAgain = tryAgain();
  52.                 }
  53.                 playAgain = tryAgain();

  54.         } while ( playAgain == "Y" );

  55.         system("PAUSE");
  56.         return 0;
  57.       
  58. }

  59.         string tryAgain()
  60.         {
  61.                 string input;
  62.                 bool proceed=false;
  63.                 bool gone_through=false;
  64.                 int i;
  65.                 while (proceed==false){
  66.                         if(gone_through==true){ cout<< "Error, Please Enter Y/N Only" << endl; }
  67.                         cout<< "Do you want to try again? (Y/N) : ";
  68.                         cin >> input;
  69.                         gone_through=true;
  70.                         if (input.compare("Y")!=0 || input.compare("N")!=0 || input.compare("y")!=0 || input.compare("n")!=0){
  71.                                 proceed=true;
  72.                         }
  73.                 }
  74.                
  75.                 //convert string to uppercase
  76.                 for( i = 0; i < input.length() ; i++)
  77.                      input[i] = toupper(input[i]);
  78.                
  79.                 return input;
  80.         }

  81.         void validateProjectileInfo(double &v, double &theta)
  82.         {
  83.                 bool proceed=false;
  84.                 bool gone_through=false;
  85.                 while (proceed==false){
  86.                         if(gone_through==true){ cout<< "Velocity must be more than zero" << endl; }
  87.                 cout<< "lease Enter a velocity : ";
  88.                 cin>> v;
  89.                         gone_through=true;
  90.                         if(v>0){
  91.                                 proceed=true;
  92.                         }
  93.                 }
  94.                 gone_through=false;
  95.                 proceed=false;
  96.                 while (proceed==false){
  97.                         if(gone_through==true){ cout<< "Angle must be between 0 to 90" << endl; }
  98.                 cout<< "lease Enter an angle : ";
  99.                 cin>> theta;
  100.                         gone_through=true;
  101.                         if(theta>0 && theta<90){
  102.                                 proceed=true;
  103.                         }
  104.                 }
  105.                
  106.         }
复制代码

[ 本帖最后由 onlylonly 于 7-8-2008 11:52 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 9-8-2008 11:19 AM | 显示全部楼层
我想问你,为什么你要
#include <cstdlib>??
当我拿走了#include <cstdlib>,你的program还是能走
还有,这个#include <cstdlib>有什么用?


void validateProjectileInfo(double &v, double θ) 和
void validateProjectileInfo(double v, double theta)
有什么分别?

为什么我的C++时常在compile 和 build 的时候 hang机de?


刚才我的朋友发现了一个很有趣的东西
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. main ()
  5. {
  6. double h = 4/10;
  7. cout<<setprecision(10)<<h<<endl;
  8. return 0;
  9. }
复制代码

为什么cout 是0 而不是0.4??
还有,为什么<1 and -ve 都display 不到到?

[ 本帖最后由 demonden02 于 9-8-2008 12:55 PM 编辑 ]
回复

使用道具 举报

发表于 9-8-2008 04:34 PM | 显示全部楼层

回复 11# demonden02 的帖子

include <cstdlib> 是为了 toupper 与 abs 这两个function, 这两个都在cstdlib 里面的。
toupper 是将小写转换成为大写
abs 是absolute, 也就是将 -ve 负数 (negative)转换为正数 + ve (possitive)

2.

void validateProjectileInfo(double &v, double θ) 和
void validateProjectileInfo(double v, double theta)

这个 θ是unicode吧, 最好不要用, 若某些系统不支持unicode的话, 就会出现error了。
double &v, 这个是argument, 而且是pass by reference (有&)
pass by reference 就是说, 将传送过来的 variable(变数)直接修改。(就是calling function 的 variable value 过后完全不同了)

double v, 这个, 并没有(&)这个, 所以是pass by value, pass by value 不会影响你calling function 的variable 的 value ( calling function 的 variable value 依然不变 )
举例:
  1. #include <iostream>

  2. using namespace std;

  3. void pass_by_reference(double &variable_by_reference);
  4. void pass_by_value(double variable_by_value);

  5. int main()
  6. {
  7.     double by_reference_variable = 0,
  8.            by_value_variable = 0;
  9.            
  10.     cout << "In main function ( before calling function ) "
  11.          << "\nvalue of variable by_reference_variable : " << by_reference_variable
  12.          << "\nvalue of variable by_value_variable : " << by_value_variable << endl;
  13.          
  14.     //call function
  15.     pass_by_reference(by_reference_variable);
  16.     pass_by_value(by_value_variable);
  17.    
  18.     cout << "\n\nIn main function ( after calling function ) "
  19.          << "\nvalue of variable by_reference_variable : " << by_reference_variable
  20.          << "\nvalue of variable by_value_variable : " << by_value_variable << endl;
  21.          
  22.    
  23.     cin.get();   
  24.     return 0;
  25. }

  26. void pass_by_reference(double &variable_by_reference)
  27. {
  28.      variable_by_reference = 10;
  29.      
  30.      cout << "\n\nIn variable_by_reference function  "
  31.           << "\nvalue of variable_by_reference : " << variable_by_reference << endl;
  32.          
  33.      return;
  34. }

  35. void pass_by_value(double variable_by_value)
  36. {
  37.      variable_by_value = 10;
  38.      
  39.      cout << "\n\nIn variable_by_value function  "
  40.           << "\nvalue of variable_by_value : " << variable_by_value << endl;
  41.          
  42.      return;
  43. }
复制代码
3.
double h = 4/10;

4 是integer (int)
10 是 integer (int)

integer / integer ( int 除 int ) 得到的, 当然是int, 所以是0

除非( 注意那个小数点)
double h = 4.0/10; 或 double h = 4./10  或
double h = 4/10.0; 或 double h = 4/10. ; 或
double h = 4.0/10.0; 或 double h = 4./10.

解释
4. 等于 4.0  ( 注意那个小数点)

[ 本帖最后由 onlylonly 于 9-8-2008 04:51 PM 编辑 ]
回复

使用道具 举报

发表于 9-8-2008 04:47 PM | 显示全部楼层
还有,为什么<1 and -ve 都display 不到到?

不很明白你的问题,
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.    
  6.     cout << -100 << endl;
  7.    
  8.     cin.get();
  9.    
  10.     return 0;
  11.    
  12. }
复制代码
可以display 啊。。
回复

使用道具 举报

 楼主| 发表于 9-8-2008 09:56 PM | 显示全部楼层
原帖由 onlylonly 于 9-8-2008 16:47 发表
还有,为什么


那时候我的code 是
  1. #include <iostream>
  2. using namespace std;

  3. main()
  4. {
  5.     double h;

  6.     h=-4/7;

  7.     cout<<h<<endl;

  8.     return 0;
  9. }
复制代码

所以display不到。。paisei...

[ 本帖最后由 demonden02 于 9-8-2008 09:59 PM 编辑 ]
回复

使用道具 举报

发表于 10-8-2008 08:47 AM | 显示全部楼层
原帖由 demonden02 于 9-8-2008 09:56 PM 发表


那时候我的code 是#include
using namespace std;

main()
{
    double h;

    h=-4/7;

    cout



所以,你因该试试着个:

  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.     double h;
  6.     h = -4.0 / 7;
  7.     cout << h << endl;
  8.    
  9.     system("pause";
  10.     return 0;
  11. }
复制代码


或者是这个:


  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.     double h;
  6.     h = (double) -4 / 7;
  7.     cout << h << endl;
  8.    
  9.     system("pause");
  10.     return 0;
  11. }
复制代码

[ 本帖最后由 alexander3133 于 10-8-2008 08:50 AM 编辑 ]
回复

使用道具 举报

发表于 10-8-2008 08:56 AM | 显示全部楼层
或是
  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.     double h;
  6.     h = static_cast<double>( -4 ) / 7 ;
  7.     cout << h << endl;
  8.    
  9.     system("pause";
  10.     return 0;
  11. }
复制代码
或 ( 这个比较少人用了)
  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.     double h;
  6.     h = double(4) / 7;
  7.     cout << h << endl;
  8.    
  9.     system("pause");
  10.     return 0;
  11. }
复制代码
常用的都是
(double) -4 / 7 (c style) 与
static_cast<double>( -4 )  / 7 (c++ style )

不过都相同的

因为看得明白,知道是 type casting 中的 static cast

[ 本帖最后由 onlylonly 于 10-8-2008 09:19 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 23-12-2025 04:33 PM , Processed in 0.141907 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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