查看: 1207|回复: 5
|
帮我看看有什么地方可以改进。
[复制链接]
|
|
- #include<stdio.h>
- #define PREPAID_BALANCE 10.00
- int main()
- //Account Balance Program
- {
- const float band1 = 0.50f, band2 = 0.40f, band3 = 0.20f, sms_rate = 0.10f;//calculating rate
- float total_call, balance, sms_cost;
- int min, min1, min2, min3, decide, sms_num, topup_amount;
- int total_sms=0, total_min=0;//for summary
- float total_call_charge=0, total_sms_charge=0, total_topup=0; //for summary
- balance=PREPAID_BALANCE;
- printf("Thank you for using TARMoblie.\n\n");
- CALL://done
- {
- printf(
- "\n==============================================="
- "\n\t\tCALL\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);//show balance in begin
- printf("\nESTIMATED Available Talk Time : %.0f minutes",balance/0.51);//estimate call time(inaccurate)
- printf("\nCall duration (minute) :");//promt user input
- scanf("%d",&min);
- fflush(stdin);
- if (min>10)
- min1=10;
- else min1=min;
- printf("\nBand1 charge rate : %4d minute * RM 0.50 = RM\t%.2f",min1,min1*band1);
- if(min>10)//show band 2 rate if the call duration is more then 10 minutes
- {
- if(min>20)//if call duration is over 20minute, assign 10 into min2
- min2=10;
- else min2=min-10;
- printf("\nBand2 charge rate : %4d minute * RM 0.40 = RM\t%.2f",min2,min2*band2);
- }
- if(min>20)//show band 3 rate if the call duration is more than 20 minutes
- {
- min3=min-20;//calculate the band 3 duration usage
- printf("\nBand3 charge rate : %4d minute * RM 0.20 = RM\t%.2f",min3,min3*band3);
- }
- if(min<=10)
- min2=0;
- if(min<=20)
- min3=0;
- total_call=min1*band1+min2*band2+min3*band3;//calculate this call rate
- balance=balance-total_call;//calculate balance
- total_min=min+total_min;//for summary, calculate averall call time
- total_call_charge=total_call+total_call_charge;//for summary, calculate overall call charge
- printf("\n\t\t\t\t\t\t-----\n");
- printf("\t\t\tCharge of this call: RM\t%.2f\n",total_call);//show the charge rate
- printf("\t\t\t\t\t\t=====\n");
- printf("\nNew Balance is RM %.2f\n",balance);
- printf("-----------------------------------------------\n");
- goto SELECTION;
- }
- SMS://done
- {
- printf(
- "\n==============================================="
- "\n\t\tSMS\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);
- printf("\nSMS available : %.0f",(balance-1.90)/0.10);//estimate avalible sms
- printf("\nHow many SMS do you want to send :");
- scanf("%d",&sms_num);
- fflush(stdin);
- sms_cost=sms_num*sms_rate;//calcalate sms cost
- printf("SMS charge = RM %.2f\n",sms_cost);
- balance=balance-sms_cost;//calculate balance
- printf("\nNew Balance is RM%.2f\n",balance);
- printf("-----------------------------------------------\n");
- total_sms_charge=sms_cost+total_sms;//for summary, total sms charge
- total_sms=sms_num+total_sms;//for summary, total sms count
- goto SELECTION;
- }
- TOPUP://done
- {
- printf(
- "\n==============================================="
- "\n\t\tTOP UP\n"
- );
- printf("\nYour balance is RM %6.2f\n",balance);
- printf("\nTop Up Amount : RM ");//promt for topup amount
- scanf("%d",&topup_amount);
- fflush(stdin);
- balance=topup_amount+balance;//calculate balance after topup
- total_topup=topup_amount+total_topup;//for summary, calculate overall topup amount
- printf("\nNew Balance is RM%6.2f\n",balance);
- printf("-----------------------------------------------\n");
- goto SELECTION;
- }
- SELECTION://done
- {
- printf("\n\n");
- printf("(1)Call, (2)SMS, (3)TOP UP, (4)EXIT >>");
- scanf("%d",&decide);
- fflush(stdin);
- if(decide==1 || decide==2)
- {
- if(balance>=2.00)//need at least above RM2.00 to make call or sms
- {
- if(decide==1)
- goto CALL;
- if(decide==2)
- goto SMS;
- }
- else goto CHECK_BALANCE;
- }
- if(decide==3)
- goto TOPUP;
- if(decide==4)
- goto SUMMARY;
- if(decide>4)
- goto SELECTION;
- }
- CHECK_BALANCE:
- {
- while(balance<2.00)//loop topup, if amount is less than RM2.00
- {
- printf(
- "\n==============================================="
- "\n\t\tNOTICE\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);
- printf(
- "You need at least RM 2.00 to make call or sms\n"
- "How much to top up? >>RM "
- );
- scanf("%d",&topup_amount);//promt for topup amount
- fflush(stdin);
- balance=topup_amount+balance;//calculate balance atfer topup
- total_topup=topup_amount+total_topup;//for summary, calculate overall topup amount
- printf("\nNew Balance is RM%6.2f\n",balance);
- printf("-----------------------------------------------\n");
- }
- goto SELECTION;
- }
- SUMMARY:
- {
- printf(
- "\n\n==============================================="
- "\n\t\tTRANSACTION SUMMARY\n"
- );
- printf("\nIn total, you \ttalk \t%d minutes\n",total_min);
- printf("\t\tsent \t%d SMS's\n", total_sms);
- printf("\nStarting Balance\t\t : RM %6.2f",PREPAID_BALANCE);
- printf("\nTotal Amount Topped UP \t\t : RM %6.2f",total_topup);
- printf("\nTotal Call Charges \t\t : RM %6.2f",total_call_charge);
- printf("\nTotal SMS Charges \t\t : RM %6.2f", total_sms_charge);
- printf("\nEnding Balance \t\t\t : RM %6.2f",balance);
- printf("\n-----------------------------------------------\n");
- printf("\n\n");
- }
- system("pause");
- return 0;
- }
复制代码 我刚学C语言,学到do。。。while 那里罢了。
这是学校给的assignment,已经做完了,但是觉得很长(?)
所以想问下有没有其他方法可以写或者缩短?
另外,因为我有一个朋友说用GOTO很乱水,但是我又不知道有什么其他的方法,请各位指教。
|
|
|
|
|
|
|
|
发表于 8-7-2013 11:12 PM
|
显示全部楼层
你的朋友说得没错,使用GOTO并不是个好习惯。C是Structured Language,使用GOTO会打断你的程式的流程,变得Non-Structured,并破坏C语言的Code independence。同时这也会在将来让你变成同事间的怪胎,同时被同事们所厌恶,因为你的Code将会让他们很难Keep track整个程式的flow。
解决方法?细心的理解整个程式的概念,并仔细的设计整个程式的流程,善用Flow Charts,善用Functions。
我想你陷入所有初学Programming的人的误区了。Program并不是写的越短就越厉害。代码应该清晰,明了,格式整齐才是你目前应该学习的正确方向。超级压缩的代码虽然看起来简短,好像很厉害,但是却是所有人都讨厌的编程方式,因为其他接手你的代码的人必须花费额外的心思才能解读你的代码。
Code Optimization则是另外一回事,那是必须累计了一定的知识和实战经验才能做到的,那不是你现在应该烦恼的,等你Final Year时再说吧。
顺带一提,我目前在做着的工作的其中一个Project就有一百九十多万行代码,其中我亲自手打的代码就有三万八千多行(24个Unit Files/Forms),长吗?
Program不是看长短,而是看你的Code能有多自律,你能不能在没有Memory Leak,没有Access Violations,没有Errors,没有Warning 的情形下完成你的工作。
本帖最后由 geekman 于 8-7-2013 11:27 PM 编辑
|
|
|
|
|
|
|
|

楼主 |
发表于 9-7-2013 01:55 PM
|
显示全部楼层
想问下,遇到selection的时候要怎么做?除了goto之外有什么可以用的?
或者有什么例子可以让我看一下? |
|
|
|
|
|
|
|
发表于 9-7-2013 02:20 PM
|
显示全部楼层
如果你的Selection Condition是Numeric Enumerable的,你可以用swich():
- char key;
- (或者int key)
- switch(key)
- {
- case 'a' : doAfunction();
- break;
- case 0x33: //do whatever
- break;
- }
复制代码 如果不是Numeric enumerable,可以用if() else...
- if(str == "I CHOOSE OPTION A")
- {
- dothis();
- }
- else if(str == "I choose PIKACHU")
- {
- dothat();
- }
- ...
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 10-7-2013 08:11 PM
|
显示全部楼层
geekman 发表于 9-7-2013 02:20 PM 
如果你的Selection Condition是Numeric Enumerable的,你可以用swich():如果不是Numeric enumerable,可以 ...
你好,我把code改成以下了- #include <stdio.h>
- <span style="line-height: 1.5;">#define PREPAID_BALANCE 10.00</span>
- void Call_Function();
- void SMS_Function();
- void Topup_Function();
- void Check_Balance();
- void Summary();
- const float band1 = 0.50f, band2 = 0.40f, band3 = 0.20f, sms_rate = 0.10f;
- float total_call, balance, sms_cost;
- int min,min1,min2,min3,sms_num,topup_amount;
- int decide;//for choosing
- int total_sms=0, total_min=0;//for summary
- float total_call_charge=0, total_sms_charge=0, total_topup=0; //for summary
- int main()
- {
- balance=PREPAID_BALANCE;
- printf("Thank you for choosing TARMoblie.\n");
- Call_Function();
- do
- {
- printf("\n\n");
- printf("(1)Call, (2)SMS, (3)TOP UP, (4)EXIT >>");
- scanf("%d",&decide);
- fflush(stdin);
- switch(decide)
- {
- case 1:
- case 2:
- if(balance>=2.00)
- {
- if(decide==1)
- Call_Function();
- if(decide==2)
- SMS_Function();
- }
- else Check_Balance();
- break;
- case 3:
- Topup_Function();
- break;
- case 4:
- Summary();
- break;
- default:
- printf("Please Enter number between 1-4");
- }
- }
- while(decide!=4);
- return 0;
- }
- void Call_Function()
- {
- printf(
- "\n==============================================="
- "\n\t\tCALL\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);//show balance in begin
- printf("\nESTIMATED Available Talk Time : %.0f minutes",balance/0.51);//estimate call time(inaccurate)
- printf("\nCall duration (minute) :");//promt user input
- scanf("%d",&min);
- fflush(stdin);
- if (min>10)
- min1=10;
- else min1=min;
- printf("\nBand1 charge rate : %4d minute * RM 0.50 = RM\t%.2f",min1,min1*band1);
- if(min>10)//will show band 2 rate if the call duration is more then 10 minutes
- {
- if(min>20)
- min2=10;
- else min2=min-10;
- printf("\nBand2 charge rate : %4d minute * RM 0.40 = RM\t%.2f",min2,min2*band2);
- }
- if(min>20)//show band 3 rate if the call duration is more than 20 minutes
- {
- min3=min-20;//calculate the band 3 duration usage
- printf("\nBand3 charge rate : %4d minute * RM 0.20 = RM\t%.2f",min3,min3*band3);
- }
- if(min<=10)
- min2=0;
- if(min<=20)
- min3=0;
- total_call= min1* band1+min2* band2+min3* band3;//calculate this call rate
- total_min+=min;//calculate this call time
- total_call_charge+=total_call;//calculate overall call time
- balance-=total_call;//calculate balance
- printf("\n\t\t\t\t\t\t-----\n");
- printf("\t\t\tCharge of this call: RM\t%.2f\n",total_call);//show the charge rate
- printf("\t\t\t\t\t\t=====\n");
- printf("\nNew Balance is RM %.2f\n",balance);
- printf("-----------------------------------------------\n");
- }
- void SMS_Function()
- {
- printf(
- "\n==============================================="
- "\n\t\tSMS\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);
- printf("\nSMS available : %.0f",(balance-1.90)/0.10);
- printf("\nHow many SMS do you want to send :");
- scanf("%d",&sms_num);
- fflush(stdin);
- sms_cost=sms_num*sms_rate;
- printf("SMS charge = RM %.2f\n",sms_cost);
- balance-=sms_cost;
- printf("\nNew Balance is RM%.2f\n",balance);
- printf("-----------------------------------------------\n");
- total_sms_charge=sms_cost+total_sms;
- total_sms+=sms_num;
- }
- void Topup_Function()
- {
- printf(
- "\n==============================================="
- "\n\t\tTOP UP\n"
- );
- printf("\nYour balance is RM %6.2f\n",balance);
- printf("\nTop Up Amount : RM ");
- scanf("%d",&topup_amount);
- fflush(stdin);
- balance+=topup_amount;
- total_topup+=topup_amount;
- printf("\nNew Balance is RM%6.2f\n",balance);
- printf("-----------------------------------------------\n");
- }
- void Check_Balance()
- {
- while(balance<2.00)
- {
- printf(
- "\n==============================================="
- "\n\t\tNOTICE\n"
- );
- printf("\nYour balance is RM %.2f\n",balance);
- printf(
- "You need at least RM 2.00 to make call or sms\n"
- "How much to top up? >>RM "
- );
- scanf("%d",&topup_amount);
- fflush(stdin);
- balance+=topup_amount;
- total_topup+=topup_amount;
- printf("\nNew Balance is RM%6.2f\n",balance);
- printf("-----------------------------------------------\n");
- }
- }
- void Summary()
- {
- printf(
- "\n\n==============================================="
- "\n\t\tTRANSACTION SUMMARY\n"
- );
- printf("\nIn total, you \ttalk \t%d minutes\n",total_min);
- printf("\t\tsent \t%d SMS's\n", total_sms);
- printf("\nStarting Balance\t\t : RM %6.2f",PREPAID_BALANCE);
- printf("\nTotal Amount Topped UP \t\t : RM %6.2f",total_topup);
- printf("\nTotal Call Charges \t\t : RM %6.2f",total_call_charge);
- printf("\nTotal SMS Charges \t\t : RM %6.2f", total_sms_charge);
- printf("\nEnding Balance \t\t\t : RM %6.2f",balance);
- printf("\n-----------------------------------------------\n");
- printf("\n\n");
- }
复制代码 我把code改成以上的会不会比用goto来的好?
应为我觉得这样子做跟用goto好象是一样的东西。。。
想请教一下,有哪些地方可以改进的?
附上flowchart
flowchart 我不是很会做,请问这样就可以了吗,是否要加其他东西?
谢谢
|
-
|
|
|
|
|
|
|
发表于 10-7-2013 09:53 PM
|
显示全部楼层
1)Top Up 和 Check Balance的功能重复了。C语言基本教科书都会这么说:“A function should do only what it is meant to do.” 一个Function只需做它应该做的事。Check Balance却跑去做Top Up的事情,过界了喂。
2)- case 1:
- case 2:
- if(balance>=2.00)
- {
- if(decide==1)
- Call_Function();
- if(decide==2)
- SMS_Function();
- }
- else Check_Balance();
- break;
复制代码 多余了。
如果是我会这样做:- case 1:Call_Function();
- break;
- case 2: SMS_Function();
- break;
- ...
- void Call_Function()
- {
- Check_Balance();
-
- ...// do whatever call function do
- }
- void SMS_Function()
- {
- // same as above
- }
- void Check_Balance()
- {
- if(balance <= 2)
- Top_Up();
- //else balance is good and nothing need to be done
- //or you may choose to print the balance here.
- }
复制代码应为我觉得这样子做跟用goto好象是一样的东西。。。
随便你,喜欢继续用GOTO就继续用吧,当你进入OOP的区域(C++)的时候,你就会知道滋味了。或者你可以考虑将Assembly当成你的终生以及唯一的编程语言,那个语言倒是不忌讳GOTO(其实C里面的GOTO就是用inline assembler来处理的。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|