|
查看: 1345|回复: 17
|
c++问题。救命。6题,那两题最容易啊?
[复制链接]
|
|
|
本帖最后由 plouffle0789 于 27-7-2013 04:26 PM 编辑
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2013 04:28 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-7-2013 02:23 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 01:31 AM
|
显示全部楼层
刚做完功课,有点无聊上了中学原地...
碰巧看你问问题,就花点时间写完了....
如果有任何错误,有请见谅... 也请指教 
献丑了  - //Question 4
- #include<iostream>
- using namespace std;
- int main()
- {
- int k, store_number[9999];
- int sum = 0, average, largest = 0, smallest = 9999999999, counter = 0;
- cout<< "How many total numbers you want to enter? ";
- cin>> k;
- for(int i = 0; i < k; i++)
- {
- cout<< "Enter number: ";
- cin>> store_number[i];
- }
- cout<< "\nYou have entered: ";
- for(int i = 0; i < k; i++)
- {
- cout<< store_number[i] << " " ;
- sum += store_number[i];
- if(store_number[i] > largest)
- {
- largest = store_number[i];
- }
-
- if(store_number[i] < smallest)
- {
- smallest = store_number[i];
- }
- }
- average = sum / k;
- for(int i = 0; i < k; i++)
- {
- if(store_number[i] > average)
- {
- counter++;
- }
- }
- cout<< "\nThe average of the series numbers is: " << average;
- cout<< "\nThe largest of the series numbers is: " << largest;
- cout<< "\nThe smallest of the series numbers is: " << smallest;
- cout<< "\nThe total number greater than average is: " << counter << "\n\n";
- return 0;
- }
复制代码 本帖最后由 数学神 于 3-8-2013 01:34 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 01:46 AM
|
显示全部楼层
然后也写了 Question 1 (a)  - //Question 1 (a)
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main()
- {
- double purchase_price, depreciation_rate, min_value;
- double depreciation_amount = 0, end_year_value, accumulated_depreciation = 0;
- int year = 0;
- 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";
- cout<< "The purchase price (RM)\t\t: ";
- cin>> purchase_price;
- cout<< "The depreciation rate (%)\t: ";
- cin>> depreciation_rate;
- cout<< "The minimum value (RM)\t\t: ";
- cin>> min_value;
- end_year_value = purchase_price;
- cout.setf(ios::fixed);
- cout<< setprecision(2);
- cout<< "\n\n YEAR\tDEPRECIATION\tEND OF YEAR\t ACCUMULATED\n";
- cout<< "\t AMOUNT\t VALUE\tDEPRECIATION\n";
- cout<< "\t (RM)\t\t (RM)\t\t (RM)\n";
- cout<< "-------------------------------------------------------\n\n";
- cout<< setw(3) << year << setw(15) << depreciation_amount << setw(16) << end_year_value << setw(17) << accumulated_depreciation << "\n";
- while(end_year_value > min_value && min_value > 0)
- {
- year++;
- depreciation_amount = end_year_value*depreciation_rate/100;
- end_year_value -= depreciation_amount;
- accumulated_depreciation += depreciation_amount;
- cout<< setw(3) << year << setw(15) << depreciation_amount << setw(16) << end_year_value << setw(17) << accumulated_depreciation << "\n";
- }
- cout<< "\nIt will take " << year << " years for your vehicle to drop below RM " << min_value << "\n\n";
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 02:04 AM
|
显示全部楼层
又发现到我喜欢的 星星题目~
这类似的题目,蛮有趣的~
主要是锻炼 for loop 的逻辑思考  - //Question 3 (a)
- #include<iostream>
- using namespace std;
- int main()
- {
- for(int triangle = 1; triangle <= 3; triangle++)
- {
- for(int i = 1; i <= 8; i++)
- {
- for(int j = 1; j <= 8 - i; j++)
- {
- cout<< " ";
- }
- for(int k = 1; k <= (2*i)-1; k++)
- {
- cout<< "*";
- }
-
- cout<< "\n";
- }
- }
- return 0;
- }
复制代码 本帖最后由 数学神 于 3-8-2013 02:08 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 02:46 AM
|
显示全部楼层
Question 5 貌似很好玩  - //Question 5
- #include<iostream>
- using namespace std;
- int main()
- {
- char seat[7][4], point;
- char col_seat, decision;
- int row, col;
- bool quit = false;
- for(int i = 0; i < 7; i++)
- {
- point = 'A';
- for(int j = 0; j < 4; j++)
- {
- seat[i][j] = point;
- point++;
- }
- }
- do
- {
- fflush(stdin);
- cout<< "Enter a row: ";
- cin>> row;
- cout<< "Enter a seat: ";
- cin>> col_seat;
- switch(col_seat)
- {
- case 'a':
- case 'A': col = 1;
- break;
- case 'b':
- case 'B': col = 2;
- break;
- case 'c':
- case 'C': col = 3;
- break;
- case 'd':
- case 'D': col = 4;
- break;
- }
- if(seat[row-1][col-1] == 'X')
- {
- cout<< "The seat you chosen is occupied.\nPlease select another seat...\n\n";
- }
- else
- {
- seat[row-1][col-1] = 'X';
- cout<< "\n";
- for(int i = 0; i < 7; i++)
- {
- cout<< i+1;
- for(int j = 0; j < 4; j++)
- {
- cout<< " " << seat[i][j];
- }
- cout<< "\n";
- }
- cout<< "\nEnter another seat? (y/n): ";
- cin>> decision;
- switch(decision)
- {
- case 'y':
- case 'Y':
- quit = false;
- cout<< "\n";
- break;
- case 'n':
- case 'N':
- default:
- quit = true;
- cout<< "Thanks you!!\n\n";
- break;
- }
- }
- }while(!quit);
- return 0;
- }
复制代码 本帖最后由 数学神 于 3-8-2013 03:01 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 03:02 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 ... - #include<iostream>
- #include<iomanip>
- #include<string>
- using namespace std;
- int main()
- { // Declare variables
- int year=0;
- double purchase_p ,depreciation_r,end=0,min_value,depreciation_amount = 0,end_of_year_value,accumulated_d = 0;
-
-
- cout<<"This program computes and displays the values of your vehicle"
- <<" from the year\t"<<"purchased until the value drops below a minimum value."<<endl<<endl;
-
- cout<<"Enter the purchase price: RM ";
- cin>>purchase_p;
- cout<<"Enter the depreciation rate(%): ";
- cin>>depreciation_r;
- cout<<"Enter the minimum value: RM ";
- cin>>min_value;
- cout<<endl<<endl;
- cout<<"YEAR\t"<<"DEPRECIATION\t"<<"END OF YEAR\t"<<"ACCUMULATED"<<endl
- <<"\t"<<" AMOUNT\t"<<" VALUE\t"<<"DEPRECIATION"<<endl
- <<"\t <RM>"<<"\t\t <RM>"<<"\t\t <RM>"<<endl;
- cout<<string(80,'-')<<endl;
-
- do
- {
- if (year == 0)
- depreciation_amount = 0;
- else
- depreciation_amount=(purchase_p * depreciation_r/100);
-
- end_of_year_value = purchase_p - depreciation_amount;
- purchase_p-= depreciation_amount;
- accumulated_d += depreciation_amount;
-
- 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;
- year++;
- } while ( purchase_p > min_value );
- year -= 1;
- cout<<"\n"<<"It will take "<<year<<"years for your vehicle to drop below RM 2500.00"<<endl;
-
-
- return 0;
- }
复制代码 这是我的code.请问为什么我不要让end_of_year_value等于0,也可以RUN得到?我怕老师VIVA时,我不会。他很喜欢找毛病,要求你当场modify.做vertification. 本帖最后由 plouffle0789 于 3-8-2013 08:54 PM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 3-8-2013 09:35 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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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.- #include<iostream>
- #include<iomanip>
- #include<string>
- using namespace std;
- int main()
- { // Declare variables
- int year=0;
- double purchase_p ,depreciation_r,end=0,min_value,depreciation_amount = 0,end_of_year_value,accumulated_d = 0;
-
-
- cout<<"This program computes and displays the values of your vehicle"
- <<" from the year\t"<<"purchased until the value drops below a minimum value."<<endl<<endl;
-
- cout<<"Enter the purchase price: RM ";
- cin>>purchase_p;
- if (!(cin >> purchase_p))
- cout << "non numerical data!" << endl<<endl;
- else
-
- cout<<"Enter the depreciation rate(%): ";
- cin>>depreciation_r;
- if (!(cin >> depreciation_r))
- cout << "non numerical data!" << endl<<endl;
- else
- cout<<"Enter the minimum value: RM ";
- cin>>min_value;
- cout<<endl<<endl;
- if (!(cin >> min_value))
- cout << "non numerical data!" << endl<<endl;
- else
-
- return 0;
- cout<<"YEAR\t"<<"DEPRECIATION\t"<<"END OF YEAR\t"<<"ACCUMULATED"<<endl
- <<"\t"<<" AMOUNT\t"<<" VALUE\t"<<"DEPRECIATION"<<endl
- <<"\t <RM>"<<"\t\t <RM>"<<"\t\t <RM>"<<endl;
- cout<<string(80,'-')<<endl;
-
- do
- {
- if (year == 0)
- depreciation_amount = 0;
- else
- depreciation_amount=(purchase_p * depreciation_r/100);
-
- end_of_year_value = purchase_p - depreciation_amount;
- purchase_p-= depreciation_amount;
- accumulated_d += depreciation_amount;
-
- 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;
- year++;
- } while ( purchase_p > min_value );
- year -= 1;
- cout<<"\n"<<"It will take "<<year<<"years for your vehicle to drop below RM 2500.00"<<endl;
-
-
-
- return 0;
- }
复制代码 本帖最后由 plouffle0789 于 4-8-2013 01:10 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2013 01:25 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|