佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1345|回复: 17

c++问题。救命。6题,那两题最容易啊?

[复制链接]
发表于 30-6-2013 11:54 PM | 显示全部楼层 |阅读模式
[tr][/tr]
http://www.scribd.com/doc/154330181/2013-c

1(B)的话,用if else 和infinite loop 最简单对吗?

只需要做2题。

    最容易的是第一和第二题对吗?

第4题虽然没有(b),但是我完全想不到怎么做。卡着了。






本帖最后由 plouffle0789 于 27-7-2013 04:26 PM 编辑

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 27-7-2013 04:28 PM | 显示全部楼层
快来人啊。。。。。
回复

使用道具 举报

 楼主| 发表于 28-7-2013 02:23 PM | 显示全部楼层
没人学???????
回复

使用道具 举报

发表于 3-8-2013 01:31 AM | 显示全部楼层
刚做完功课,有点无聊上了中学原地...
碰巧看你问问题,就花点时间写完了....
如果有任何错误,有请见谅... 也请指教
献丑了
  1. //Question 4
  2. #include<iostream>

  3. using namespace std;

  4. int main()
  5. {
  6.         int k, store_number[9999];
  7.         int sum = 0, average, largest = 0, smallest = 9999999999, counter = 0;

  8.         cout<< "How many total numbers you want to enter?  ";
  9.         cin>> k;

  10.         for(int i = 0; i < k; i++)
  11.         {
  12.                 cout<< "Enter number: ";
  13.                 cin>> store_number[i];
  14.         }

  15.         cout<< "\nYou have entered: ";
  16.         for(int i = 0; i < k; i++)
  17.         {
  18.                 cout<< store_number[i] <<  "   " ;

  19.                 sum += store_number[i];

  20.                 if(store_number[i] > largest)
  21.                 {
  22.                         largest = store_number[i];
  23.                 }
  24.                
  25.                 if(store_number[i] < smallest)
  26.                 {
  27.                         smallest = store_number[i];
  28.                 }
  29.         }

  30.         average = sum / k;

  31.         for(int i = 0; i < k; i++)
  32.         {
  33.                 if(store_number[i] > average)
  34.                 {
  35.                         counter++;
  36.                 }
  37.         }

  38.         cout<< "\nThe average of the series numbers is: " << average;
  39.         cout<< "\nThe largest of the series numbers is: " << largest;
  40.         cout<< "\nThe smallest of the series numbers is: " << smallest;
  41.         cout<< "\nThe total number greater than average is: " << counter << "\n\n";

  42.         return 0;
  43. }
复制代码
本帖最后由 数学神 于 3-8-2013 01:34 AM 编辑

回复

使用道具 举报

发表于 3-8-2013 01:46 AM | 显示全部楼层
然后也写了 Question 1 (a)
  1. //Question 1 (a)
  2. #include<iostream>
  3. #include<iomanip>

  4. using namespace std;

  5. int main()
  6. {
  7.         double purchase_price, depreciation_rate, min_value;
  8.         double depreciation_amount = 0, end_year_value, accumulated_depreciation = 0;
  9.         int year = 0;

  10.         cout<< "This program computes and displays the values of your vehicle from the year purchased until the value drops below a minimun value.\n\n";

  11.         cout<< "The purchase price (RM)\t\t: ";
  12.         cin>> purchase_price;
  13.         cout<< "The depreciation rate (%)\t: ";
  14.         cin>> depreciation_rate;
  15.         cout<< "The minimum value (RM)\t\t: ";
  16.         cin>> min_value;

  17.         end_year_value = purchase_price;

  18.         cout.setf(ios::fixed);
  19.         cout<< setprecision(2);

  20.         cout<< "\n\n YEAR\tDEPRECIATION\tEND OF YEAR\t ACCUMULATED\n";
  21.         cout<< "\t   AMOUNT\t   VALUE\tDEPRECIATION\n";
  22.         cout<< "\t   (RM)\t\t   (RM)\t\t    (RM)\n";
  23.         cout<< "-------------------------------------------------------\n\n";
  24.         cout<< setw(3) << year << setw(15) << depreciation_amount << setw(16) << end_year_value << setw(17) << accumulated_depreciation << "\n";

  25.         while(end_year_value > min_value && min_value > 0)
  26.         {
  27.                 year++;
  28.                 depreciation_amount = end_year_value*depreciation_rate/100;
  29.                 end_year_value -= depreciation_amount;
  30.                 accumulated_depreciation += depreciation_amount;

  31.                 cout<< setw(3) << year << setw(15) << depreciation_amount << setw(16) << end_year_value << setw(17) << accumulated_depreciation << "\n";
  32.         }

  33.         cout<< "\nIt will take " << year << " years for your vehicle to drop below RM " << min_value << "\n\n";

  34.         return 0;
  35. }
复制代码
回复

使用道具 举报

发表于 3-8-2013 02:04 AM | 显示全部楼层
又发现到我喜欢的 星星题目~
这类似的题目,蛮有趣的~
主要是锻炼 for loop 的逻辑思考
  1. //Question 3 (a)
  2. #include<iostream>

  3. using namespace std;

  4. int main()
  5. {
  6.     for(int triangle = 1; triangle <= 3; triangle++)
  7.     {
  8.         for(int i = 1; i <= 8; i++)
  9.         {
  10.             for(int j = 1; j <= 8 - i; j++)
  11.             {
  12.                      cout<< " ";
  13.             }

  14.             for(int k = 1; k <= (2*i)-1; k++)
  15.             {
  16.                      cout<< "*";
  17.             }
  18.                         
  19.             cout<< "\n";
  20.         }
  21.     }

  22.     return 0;
  23. }
复制代码
本帖最后由 数学神 于 3-8-2013 02:08 AM 编辑

回复

使用道具 举报

Follow Us
发表于 3-8-2013 02:46 AM | 显示全部楼层
Question 5 貌似很好玩
  1. //Question 5
  2. #include<iostream>

  3. using namespace std;

  4. int main()
  5. {
  6.     char seat[7][4], point;
  7.     char col_seat, decision;
  8.     int row, col;
  9.     bool quit = false;

  10.     for(int i = 0; i < 7; i++)
  11.     {
  12.         point = 'A';

  13.         for(int j = 0; j < 4; j++)
  14.         {
  15.                 seat[i][j] = point;
  16.                 point++;
  17.         }
  18.     }

  19.     do
  20.     {
  21.         fflush(stdin);

  22.         cout<< "Enter a row: ";
  23.         cin>> row;
  24.         cout<< "Enter a seat: ";
  25.         cin>> col_seat;

  26.         switch(col_seat)
  27.         {
  28.                 case 'a':
  29.                 case 'A': col = 1;
  30.                         break;
  31.                 case 'b':
  32.                 case 'B': col = 2;
  33.                         break;
  34.                 case 'c':
  35.                 case 'C': col = 3;
  36.                         break;
  37.                 case 'd':
  38.                 case 'D': col = 4;
  39.                         break;
  40.         }

  41.         if(seat[row-1][col-1] == 'X')
  42.         {
  43.                         cout<< "The seat you chosen is occupied.\nPlease select another seat...\n\n";
  44.         }
  45.         else
  46.         {
  47.                         seat[row-1][col-1] = 'X';

  48.                         cout<< "\n";
  49.                         for(int i = 0; i < 7; i++)
  50.             {
  51.                 cout<< i+1;

  52.                 for(int j = 0; j < 4; j++)
  53.                 {
  54.                                         cout<< " " << seat[i][j];
  55.                 }
  56.                 cout<< "\n";
  57.                         }

  58.                         cout<< "\nEnter another seat? (y/n): ";
  59.             cin>> decision;

  60.             switch(decision)
  61.             {
  62.                                 case 'y':
  63.                                 case 'Y':
  64.                                                 quit = false;
  65.                                                 cout<< "\n";
  66.                                         break;
  67.                                 case 'n':
  68.                                 case 'N':
  69.                                 default:
  70.                                                 quit = true;
  71.                                                 cout<< "Thanks you!!\n\n";
  72.                                         break;
  73.                         }
  74.         }
  75.     }while(!quit);

  76.     return 0;
  77. }
复制代码
本帖最后由 数学神 于 3-8-2013 03:01 AM 编辑

回复

使用道具 举报

发表于 3-8-2013 03:02 AM | 显示全部楼层
应该就这些吧....
其他题目太罗嗦,题目太长,我懒惰去读...
可能以后才做吧


备注:以上所有 code,都是本人原作
希望你不要 copy blindly... 自己要去思考
programming 这东西,不是死读的,要多做

另外,以上所有 coding,我都只是完成题目的基本要求而已
没有去做 validation
也就是说,如果那是 integer,我 expect user 会 只是 key in integer
不会无聊乱 key in 其他东西 (例如乱放 character)... 否则整个 process 就会有影响....

要做 validation 是没问题的... 不过我懒,不想写太长
就完成基本的就好

希望可以帮到你吧!
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 3-8-2013 05:48 PM | 显示全部楼层
数学神 发表于 3-8-2013 01:46 AM
然后也写了 Question 1 (a)

end of year value为什么要放=0啊??

你读着diploma?
回复

使用道具 举报

发表于 3-8-2013 06:14 PM | 显示全部楼层
plouffle0789 发表于 3-8-2013 05:48 PM
end of year value为什么要放=0啊??

你读着diploma?

我有放 end_year_value = 0 吗?
哪行我放啊?

回复

使用道具 举报

 楼主| 发表于 3-8-2013 07:21 PM | 显示全部楼层
数学神 发表于 3-8-2013 06:14 PM
我有放 end_year_value = 0 吗?
哪行我放啊?

看错,如果我放,也是跑得到对吗?
回复

使用道具 举报

发表于 3-8-2013 07:34 PM | 显示全部楼层
plouffle0789 发表于 3-8-2013 07:21 PM
看错,如果我放,也是跑得到对吗?

原则上是没问题的,不过不需要咯

因为我在 line 22 有 initialize end_year_value = purchase_price;
所以你一开始的 end_year_value = 0; 可有可无咯

回复

使用道具 举报

 楼主| 发表于 3-8-2013 08:53 PM | 显示全部楼层
数学神 发表于 3-8-2013 07:34 PM
原则上是没问题的,不过不需要咯

因为我在 line 22 有 initialize end_year_value = purchase_pric ...
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<string>
  4. using namespace std;

  5. int main()
  6. {   // Declare variables

  7.         int year=0;
  8.         double purchase_p ,depreciation_r,end=0,min_value,depreciation_amount = 0,end_of_year_value,accumulated_d = 0;
  9.         
  10.         
  11.         cout<<"This program computes and displays the values of your vehicle"
  12.                <<" from the year\t"<<"purchased until the value drops below a minimum value."<<endl<<endl;
  13.         
  14.         cout<<"Enter the purchase price: RM ";
  15.         cin>>purchase_p;
  16.         cout<<"Enter the depreciation rate(%): ";
  17.         cin>>depreciation_r;
  18.         cout<<"Enter the minimum value: RM ";
  19.         cin>>min_value;
  20.         cout<<endl<<endl;


  21.         cout<<"YEAR\t"<<"DEPRECIATION\t"<<"END OF YEAR\t"<<"ACCUMULATED"<<endl
  22.                 <<"\t"<<"   AMOUNT\t"<<"   VALUE\t"<<"DEPRECIATION"<<endl
  23.                 <<"\t   <RM>"<<"\t\t   <RM>"<<"\t\t   <RM>"<<endl;
  24.         cout<<string(80,'-')<<endl;
  25.         
  26.         do
  27.         {
  28.                 if (year == 0)
  29.                         depreciation_amount = 0;
  30.                 else

  31.                         depreciation_amount=(purchase_p * depreciation_r/100);
  32.         
  33.                         end_of_year_value = purchase_p - depreciation_amount;
  34.                         purchase_p-= depreciation_amount;
  35.                         accumulated_d += depreciation_amount;
  36.                         
  37.                         cout<<year<<"\t"<<setw(8)<<fixed<<setprecision(2)<<depreciation_amount<<"\t"<<setw(9)<<setprecision(2)<<end_of_year_value<<"\t"<<setw(9)<<setprecision(2)<<accumulated_d<<endl;
  38.                         year++;

  39.         }        while ( purchase_p > min_value );


  40.         year -= 1;
  41.         cout<<"\n"<<"It will take "<<year<<"years for your vehicle to drop below RM 2500.00"<<endl;
  42.         
  43.         
  44. return 0;
  45. }
复制代码
这是我的code.请问为什么我不要让end_of_year_value等于0,也可以RUN得到?我怕老师VIVA时,我不会。他很喜欢找毛病,要求你当场modify.做vertification. 本帖最后由 plouffle0789 于 3-8-2013 08:54 PM 编辑

回复

使用道具 举报

发表于 3-8-2013 09:35 PM | 显示全部楼层
plouffle0789 发表于 3-8-2013 08:53 PM
这是我的code.请问为什么我不要让end_of_year_value等于0,也可以RUN得到?我怕老师VIVA时,我不会。他很喜 ...

因为 end_of_year_value 你在做 calculation 时,会 assign 一个 value 进去
所以你有没有 initialize = 0 也没关系 (不重要)

还有要提醒你,如果全部 有小数点的,你都要 2 decimal places,可以参考我的 line 25 直接写
没必要在 cout 里 每一项 注明 setprecision(2) ,因为可能有些人看了会 complaint 太难读

另外,根据 Waterfall Model,有一个过程是 System Test
在这个过程,测试员会不断 “攻击” 你的 program,哪怕是小小的毛病,也会想办法找出来
我经历过不少了

所以你老师的做法,并没有问题
你必须确保自己的 program 是可以经得起挑战的

小小建议:如果这是 assignment,建议你不要单单只是完成题目的要求
尝试加点东西进去,例如做 validation
也就是说,user key-in 后,你可以 check 看他 key in 的 是否是 号码(integer),如果不是,要求他再 key in 过

例子:
cout<< "The purchase price (RM)\t\t: ";
cin>> purchase_price;
如果 user 随便 key in "ABCDEFG"(不是号码),就 show error message 给他,并要求他 key in 正确号码

你如果没有做 validation,这也许算是一种可以被攻击的毛病

本帖最后由 数学神 于 3-8-2013 09:37 PM 编辑

回复

使用道具 举报

 楼主| 发表于 3-8-2013 11:11 PM | 显示全部楼层
数学神 发表于 3-8-2013 09:35 PM
因为 end_of_year_value 你在做 calculation 时,会 assign 一个 value 进去
所以你有没有 initialize = ...

对,我就是要做你说的,可是做不到。

if (purchase_p = char)
     cout << "non numerical data!" << endl;
      else


为什么CHAR有红线呢?
我还在一开始写了char purchase_p; 呢。
回复

使用道具 举报

发表于 4-8-2013 12:48 AM | 显示全部楼层
plouffle0789 发表于 3-8-2013 11:11 PM
对,我就是要做你说的,可是做不到。

if (purchase_p = char)

题外话:我可以知道你是 form 几学生,拿着什么 subject 要学 programming?

回正题:
在写 coding时,如果发现任何一个 part下面有红色线,意思就是有 error
可能是 syntax error,也有可能你用的 variable 还没有 declare(compiler 不认识)
待会儿你 run,compiler 可能会有 complain...

就好像我们用 Microsoft Work 同样道理啦
有问题的词句,下面都有红色线

这里我要提醒你
= 是用在 assign value
== 是用在 comparison
而 if() 里面,都是要 compare 某些东西,所以应该用 ==,而不是 =
注意下

另外,你写 if (purchase_p = char)
assume 你要表达 if (purchase_p == char),用这招来 compare 看 purchase_p 是不是 character....
我想问你,这样的 checking 是正确的吗?可以直接用 variable 跟 data type compare??老师是这么说?
我不清楚,因为我不曾这么做

假设你的概念没有错,可以接受.... 但我认为不是最好的方法
因为你只是 check 看是不是 character 而已.... what if user key in string like "haha", "school"??你的program 还能认得出吗?
我不确定,因为没做试验
你自己去 try

本帖最后由 数学神 于 4-8-2013 12:51 AM 编辑

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 4-8-2013 01:07 AM | 显示全部楼层
数学神 发表于 4-8-2013 12:48 AM
题外话:我可以知道你是 form 几学生,拿着什么 subject 要学 programming?

回正题:

我是diploma.
mechanical engineering.所以有这科。

我明白红线的意思。

我只做得到         if (!(cin >> purchase_p))     cout << "non numerical data!" << endl<<endl;
                 else
    ,你讲的STRING,我还想不到,你会做吗?可以教我吗?

我的code.
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<string>
  4. using namespace std;

  5. int main()
  6. {   // Declare variables

  7.         int year=0;
  8.         double purchase_p ,depreciation_r,end=0,min_value,depreciation_amount = 0,end_of_year_value,accumulated_d = 0;
  9.        
  10.        
  11.         cout<<"This program computes and displays the values of your vehicle"
  12.                <<" from the year\t"<<"purchased until the value drops below a minimum value."<<endl<<endl;
  13.        
  14.         cout<<"Enter the purchase price: RM ";
  15.         cin>>purchase_p;

  16.                  if (!(cin >> purchase_p))
  17.      cout << "non numerical data!" << endl<<endl;
  18.                  else
  19.    


  20.         cout<<"Enter the depreciation rate(%): ";
  21.         cin>>depreciation_r;

  22.          if (!(cin >> depreciation_r))
  23.      cout << "non numerical data!" << endl<<endl;
  24.                  else

  25.         cout<<"Enter the minimum value: RM ";
  26.         cin>>min_value;
  27.         cout<<endl<<endl;

  28.          if (!(cin >> min_value))
  29.      cout << "non numerical data!" << endl<<endl;
  30.                  else
  31.        
  32.     return 0;

  33.         cout<<"YEAR\t"<<"DEPRECIATION\t"<<"END OF YEAR\t"<<"ACCUMULATED"<<endl
  34.                 <<"\t"<<"   AMOUNT\t"<<"   VALUE\t"<<"DEPRECIATION"<<endl
  35.                 <<"\t   <RM>"<<"\t\t   <RM>"<<"\t\t   <RM>"<<endl;
  36.         cout<<string(80,'-')<<endl;
  37.        
  38.         do
  39.         {
  40.                 if (year == 0)
  41.                         depreciation_amount = 0;
  42.                 else

  43.                         depreciation_amount=(purchase_p * depreciation_r/100);
  44.        
  45.                         end_of_year_value = purchase_p - depreciation_amount;
  46.                         purchase_p-= depreciation_amount;
  47.                         accumulated_d += depreciation_amount;
  48.                        
  49.                         cout<<year<<"\t"<<setw(8)<<fixed<<setprecision(2)<<depreciation_amount<<"\t"<<setw(9)<<setprecision(2)<<end_of_year_value<<"\t"<<setw(9)<<setprecision(2)<<accumulated_d<<endl;
  50.                         year++;

  51.         }        while ( purchase_p > min_value );


  52.         year -= 1;
  53.         cout<<"\n"<<"It will take "<<year<<"years for your vehicle to drop below RM 2500.00"<<endl;
  54.        
  55.        
  56.        
  57. return 0;
  58. }
复制代码
本帖最后由 plouffle0789 于 4-8-2013 01:10 AM 编辑

回复

使用道具 举报

发表于 4-8-2013 01:25 AM | 显示全部楼层
plouffle0789 发表于 4-8-2013 01:07 AM
我是diploma.
mechanical engineering.所以有这科。

原来 diploma 的啊~ 那你怎么会想到来中学原地问 programming 问题呢?
你应该去其他版块,如 “软件开发” 问比较妥当吧
(那边很多高手,他们的意见肯定比我的好
因为 中学 level 拿 programming 的学生,少之又少啊~
应该只有 SPM 的 Fundamentals of Programming(FOP)有学吧
STPM 的不清楚

erm.. 我一贯的做法
与其一个个去 compare 看不是不 character or string 那么麻烦
倒不如直接 check 看 user key in 的是否符合 你 declare 的 data type 来的简单吧~
试看用 cin.fail 或其他 cin 的 property 来解决看看吧
(如果不清楚,可以 google)

应该是没问题的... 我之前有 try 过
回复

使用道具 举报

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

本版积分规则

 

所属分类: 欢乐校园


ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 30-3-2026 11:50 PM , Processed in 0.119015 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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