佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1207|回复: 5

帮我看看有什么地方可以改进。

[复制链接]
发表于 8-7-2013 07:16 PM | 显示全部楼层 |阅读模式
  1. #include<stdio.h>

  2. #define PREPAID_BALANCE 10.00

  3. int main()
  4.         //Account Balance Program
  5. {
  6.         const float band1 = 0.50f, band2 = 0.40f, band3 = 0.20f, sms_rate = 0.10f;//calculating rate
  7.         float total_call, balance, sms_cost;
  8.         int min, min1, min2, min3, decide, sms_num, topup_amount;
  9.         int total_sms=0, total_min=0;//for summary
  10.         float total_call_charge=0, total_sms_charge=0, total_topup=0; //for summary

  11.         balance=PREPAID_BALANCE;
  12.         printf("Thank you for using TARMoblie.\n\n");

  13. CALL://done
  14.         {
  15.                 printf(
  16.                         "\n==============================================="
  17.                         "\n\t\tCALL\n"
  18.                         );
  19.                 printf("\nYour balance is RM %.2f\n",balance);//show balance in begin
  20.                 printf("\nESTIMATED Available Talk Time : %.0f minutes",balance/0.51);//estimate call time(inaccurate)
  21.                 printf("\nCall duration (minute) :");//promt user input
  22.                 scanf("%d",&min);
  23.                 fflush(stdin);

  24.                 if (min>10)
  25.                         min1=10;
  26.                 else min1=min;
  27.                 printf("\nBand1 charge rate : %4d minute * RM 0.50 = RM\t%.2f",min1,min1*band1);


  28.                 if(min>10)//show band 2 rate if the call duration is more then 10 minutes
  29.                 {
  30.                         if(min>20)//if call duration is over 20minute, assign 10 into min2
  31.                                 min2=10;
  32.                         else min2=min-10;
  33.                         printf("\nBand2 charge rate : %4d minute * RM 0.40 = RM\t%.2f",min2,min2*band2);
  34.                 }


  35.                 if(min>20)//show band 3 rate if the call duration is more than 20 minutes
  36.                 {
  37.                         min3=min-20;//calculate the band 3 duration usage
  38.                         printf("\nBand3 charge rate : %4d minute * RM 0.20 = RM\t%.2f",min3,min3*band3);
  39.                 }

  40.                 if(min<=10)
  41.                         min2=0;
  42.                 if(min<=20)
  43.                         min3=0;

  44.                 total_call=min1*band1+min2*band2+min3*band3;//calculate this call rate
  45.                 balance=balance-total_call;//calculate balance
  46.                 total_min=min+total_min;//for summary, calculate averall call time
  47.                 total_call_charge=total_call+total_call_charge;//for summary, calculate overall call charge

  48.                 printf("\n\t\t\t\t\t\t-----\n");
  49.                 printf("\t\t\tCharge of this call: RM\t%.2f\n",total_call);//show the charge rate
  50.                 printf("\t\t\t\t\t\t=====\n");

  51.                 printf("\nNew Balance is RM %.2f\n",balance);

  52.                 printf("-----------------------------------------------\n");
  53.                 goto SELECTION;
  54.         }

  55. SMS://done
  56.         {
  57.                 printf(
  58.                         "\n==============================================="
  59.                         "\n\t\tSMS\n"
  60.                         );

  61.                 printf("\nYour balance is RM %.2f\n",balance);
  62.                 printf("\nSMS available : %.0f",(balance-1.90)/0.10);//estimate avalible sms
  63.                 printf("\nHow many SMS do you want to send :");
  64.                 scanf("%d",&sms_num);
  65.                 fflush(stdin);

  66.                 sms_cost=sms_num*sms_rate;//calcalate sms cost
  67.                 printf("SMS charge = RM %.2f\n",sms_cost);

  68.                 balance=balance-sms_cost;//calculate balance
  69.                 printf("\nNew Balance is RM%.2f\n",balance);
  70.                 printf("-----------------------------------------------\n");

  71.                 total_sms_charge=sms_cost+total_sms;//for summary, total sms charge
  72.                 total_sms=sms_num+total_sms;//for summary, total sms count

  73.                 goto SELECTION;
  74.         }

  75. TOPUP://done
  76.         {
  77.                 printf(
  78.                         "\n==============================================="
  79.                         "\n\t\tTOP UP\n"
  80.                         );
  81.                 printf("\nYour balance is RM %6.2f\n",balance);

  82.                 printf("\nTop Up Amount : RM ");//promt for topup amount
  83.                 scanf("%d",&topup_amount);
  84.                 fflush(stdin);

  85.                 balance=topup_amount+balance;//calculate balance after topup
  86.                 total_topup=topup_amount+total_topup;//for summary, calculate overall topup amount

  87.                 printf("\nNew Balance is RM%6.2f\n",balance);
  88.                 printf("-----------------------------------------------\n");

  89.                 goto SELECTION;
  90.         }

  91. SELECTION://done
  92.         {
  93.                 printf("\n\n");

  94.                 printf("(1)Call, (2)SMS, (3)TOP UP, (4)EXIT >>");
  95.                 scanf("%d",&decide);
  96.                 fflush(stdin);

  97.                 if(decide==1 || decide==2)
  98.                 {
  99.                         if(balance>=2.00)//need at least above RM2.00 to make call or sms
  100.                         {
  101.                                 if(decide==1)
  102.                                         goto CALL;
  103.                                 if(decide==2)
  104.                                         goto SMS;
  105.                         }
  106.                         else goto CHECK_BALANCE;
  107.                 }
  108.                 if(decide==3)
  109.                         goto TOPUP;
  110.                 if(decide==4)
  111.                         goto SUMMARY;
  112.                 if(decide>4)
  113.                         goto SELECTION;
  114.         }

  115. CHECK_BALANCE:
  116.         {
  117.                 while(balance<2.00)//loop topup, if amount is less than RM2.00
  118.                 {
  119.                         printf(
  120.                                 "\n==============================================="
  121.                                 "\n\t\tNOTICE\n"
  122.                                 );
  123.                         printf("\nYour balance is RM %.2f\n",balance);
  124.                         printf(
  125.                                 "You need at least RM 2.00 to make call or sms\n"
  126.                                 "How much to top up? >>RM "
  127.                                 );
  128.                         scanf("%d",&topup_amount);//promt for topup amount
  129.                         fflush(stdin);

  130.                         balance=topup_amount+balance;//calculate balance atfer topup
  131.                         total_topup=topup_amount+total_topup;//for summary, calculate overall topup amount

  132.                         printf("\nNew Balance is RM%6.2f\n",balance);
  133.                         printf("-----------------------------------------------\n");
  134.                 }
  135.                 goto SELECTION;
  136.         }

  137. SUMMARY:
  138.         {
  139.                 printf(
  140.                         "\n\n==============================================="
  141.                         "\n\t\tTRANSACTION SUMMARY\n"
  142.                         );

  143.                 printf("\nIn total, you \ttalk \t%d minutes\n",total_min);
  144.                 printf("\t\tsent \t%d SMS's\n", total_sms);
  145.                 printf("\nStarting Balance\t\t : RM %6.2f",PREPAID_BALANCE);
  146.                 printf("\nTotal Amount Topped UP \t\t : RM %6.2f",total_topup);
  147.                 printf("\nTotal Call Charges \t\t : RM %6.2f",total_call_charge);
  148.                 printf("\nTotal SMS Charges \t\t : RM %6.2f", total_sms_charge);
  149.                 printf("\nEnding Balance \t\t\t : RM %6.2f",balance);

  150.                 printf("\n-----------------------------------------------\n");
  151.                 printf("\n\n");
  152.         }
  153.         system("pause");
  154.         return 0;

  155. }
复制代码
我刚学C语言,学到do。。。while 那里罢了。
这是学校给的assignment,已经做完了,但是觉得很长(?)
所以想问下有没有其他方法可以写或者缩短?
另外,因为我有一个朋友说用GOTO很乱水,但是我又不知道有什么其他的方法,请各位指教。
回复

使用道具 举报


ADVERTISEMENT

发表于 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():

  1. char key;
  2. (或者int key)
  3. switch(key)
  4. {
  5.     case 'a' : doAfunction();
  6.         break;
  7.     case 0x33: //do whatever
  8.         break;
  9. }
复制代码
如果不是Numeric enumerable,可以用if() else...

  1. if(str == "I CHOOSE OPTION A")
  2. {
  3.     dothis();
  4. }
  5. else if(str == "I choose PIKACHU")
  6. {
  7.     dothat();
  8. }
  9. ...
复制代码
回复

使用道具 举报

 楼主| 发表于 10-7-2013 08:11 PM | 显示全部楼层
geekman 发表于 9-7-2013 02:20 PM
如果你的Selection Condition是Numeric Enumerable的,你可以用swich():如果不是Numeric enumerable,可以 ...

你好,我把code改成以下了
  1. #include <stdio.h>
  2. <span style="line-height: 1.5;">#define PREPAID_BALANCE 10.00</span>

  3. void Call_Function();
  4. void SMS_Function();
  5. void Topup_Function();
  6. void Check_Balance();
  7. void Summary();

  8. const float band1 = 0.50f, band2 = 0.40f, band3 = 0.20f, sms_rate = 0.10f;
  9. float total_call, balance, sms_cost;
  10. int min,min1,min2,min3,sms_num,topup_amount;
  11. int decide;//for choosing
  12. int total_sms=0, total_min=0;//for summary
  13. float total_call_charge=0, total_sms_charge=0, total_topup=0; //for summary





  14. int main()
  15. {
  16.     balance=PREPAID_BALANCE;
  17.     printf("Thank you for choosing TARMoblie.\n");

  18.     Call_Function();

  19.     do
  20.     {
  21.         printf("\n\n");
  22.         printf("(1)Call, (2)SMS, (3)TOP UP, (4)EXIT >>");
  23.         scanf("%d",&decide);
  24.         fflush(stdin);
  25.         switch(decide)
  26.         {
  27.         case 1:
  28.         case 2:
  29.             if(balance>=2.00)
  30.             {
  31.                 if(decide==1)
  32.                     Call_Function();
  33.                 if(decide==2)
  34.                     SMS_Function();
  35.             }
  36.             else Check_Balance();
  37.             break;
  38.         case 3:
  39.             Topup_Function();
  40.             break;
  41.         case 4:
  42.             Summary();
  43.             break;
  44.         default:
  45.             printf("Please Enter number between 1-4");
  46.         }
  47.     }
  48.     while(decide!=4);
  49.     return 0;
  50. }

  51. void Call_Function()
  52. {
  53.     printf(
  54.         "\n==============================================="
  55.         "\n\t\tCALL\n"
  56.     );
  57.     printf("\nYour balance is RM %.2f\n",balance);//show balance in begin
  58.     printf("\nESTIMATED Available Talk Time : %.0f minutes",balance/0.51);//estimate call time(inaccurate)
  59.     printf("\nCall duration (minute) :");//promt user input
  60.     scanf("%d",&min);
  61.     fflush(stdin);

  62.     if (min>10)
  63.         min1=10;
  64.     else min1=min;

  65.     printf("\nBand1 charge rate : %4d minute * RM 0.50 = RM\t%.2f",min1,min1*band1);
  66.     if(min>10)//will show band 2 rate if the call duration is more then 10 minutes
  67.     {
  68.         if(min>20)
  69.             min2=10;
  70.         else min2=min-10;
  71.         printf("\nBand2 charge rate : %4d minute * RM 0.40 = RM\t%.2f",min2,min2*band2);
  72.     }
  73.     if(min>20)//show band 3 rate if the call duration is more than 20 minutes
  74.     {
  75.         min3=min-20;//calculate the band 3 duration usage
  76.         printf("\nBand3 charge rate : %4d minute * RM 0.20 = RM\t%.2f",min3,min3*band3);
  77.     }

  78.     if(min<=10)
  79.         min2=0;
  80.     if(min<=20)
  81.         min3=0;

  82.     total_call= min1* band1+min2* band2+min3* band3;//calculate this call rate
  83.     total_min+=min;//calculate this call time
  84.     total_call_charge+=total_call;//calculate overall call time
  85.     balance-=total_call;//calculate balance

  86.     printf("\n\t\t\t\t\t\t-----\n");
  87.     printf("\t\t\tCharge of this call: RM\t%.2f\n",total_call);//show the charge rate
  88.     printf("\t\t\t\t\t\t=====\n");

  89.     printf("\nNew Balance is RM %.2f\n",balance);

  90.     printf("-----------------------------------------------\n");
  91. }

  92. void SMS_Function()
  93. {
  94.     printf(
  95.         "\n==============================================="
  96.         "\n\t\tSMS\n"
  97.     );

  98.     printf("\nYour balance is RM %.2f\n",balance);
  99.     printf("\nSMS available : %.0f",(balance-1.90)/0.10);
  100.     printf("\nHow many SMS do you want to send :");
  101.     scanf("%d",&sms_num);
  102.     fflush(stdin);

  103.     sms_cost=sms_num*sms_rate;
  104.     printf("SMS charge = RM %.2f\n",sms_cost);

  105.     balance-=sms_cost;
  106.     printf("\nNew Balance is RM%.2f\n",balance);
  107.     printf("-----------------------------------------------\n");

  108.     total_sms_charge=sms_cost+total_sms;
  109.     total_sms+=sms_num;
  110. }

  111. void Topup_Function()
  112. {
  113.     printf(
  114.         "\n==============================================="
  115.         "\n\t\tTOP UP\n"
  116.     );
  117.     printf("\nYour balance is RM %6.2f\n",balance);

  118.     printf("\nTop Up Amount : RM ");
  119.     scanf("%d",&topup_amount);
  120.     fflush(stdin);

  121.     balance+=topup_amount;
  122.     total_topup+=topup_amount;

  123.     printf("\nNew Balance is RM%6.2f\n",balance);
  124.     printf("-----------------------------------------------\n");
  125. }

  126. void Check_Balance()
  127. {
  128.     while(balance<2.00)
  129.     {
  130.         printf(
  131.             "\n==============================================="
  132.             "\n\t\tNOTICE\n"
  133.         );
  134.         printf("\nYour balance is RM %.2f\n",balance);
  135.         printf(
  136.             "You need at least RM 2.00 to make call or sms\n"
  137.             "How much to top up? >>RM "
  138.         );
  139.         scanf("%d",&topup_amount);
  140.         fflush(stdin);

  141.         balance+=topup_amount;
  142.         total_topup+=topup_amount;

  143.         printf("\nNew Balance is RM%6.2f\n",balance);
  144.         printf("-----------------------------------------------\n");
  145.     }
  146. }

  147. void Summary()
  148. {
  149.     printf(
  150.         "\n\n==============================================="
  151.         "\n\t\tTRANSACTION SUMMARY\n"
  152.     );

  153.     printf("\nIn total, you \ttalk \t%d minutes\n",total_min);
  154.     printf("\t\tsent \t%d SMS's\n", total_sms);
  155.     printf("\nStarting Balance\t\t : RM %6.2f",PREPAID_BALANCE);
  156.     printf("\nTotal Amount Topped UP \t\t : RM %6.2f",total_topup);
  157.     printf("\nTotal Call Charges \t\t : RM %6.2f",total_call_charge);
  158.     printf("\nTotal SMS Charges \t\t : RM %6.2f", total_sms_charge);
  159.     printf("\nEnding Balance \t\t\t : RM %6.2f",balance);

  160.     printf("\n-----------------------------------------------\n");
  161.     printf("\n\n");
  162. }
复制代码
我把code改成以上的会不会比用goto来的好?
应为我觉得这样子做跟用goto好象是一样的东西。。。
想请教一下,有哪些地方可以改进的?



附上flowchart

flowchart 我不是很会做,请问这样就可以了吗,是否要加其他东西?

谢谢
pcd_assignmnet.jpg
回复

使用道具 举报

发表于 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)
  1. case 1:
  2.         case 2:
  3.             if(balance>=2.00)
  4.             {
  5.                 if(decide==1)
  6.                     Call_Function();
  7.                 if(decide==2)
  8.                     SMS_Function();
  9.             }
  10.             else Check_Balance();
  11.             break;
复制代码
多余了。
如果是我会这样做:
  1. case 1:Call_Function();
  2.         break;
  3. case 2: SMS_Function();
  4.         break;
  5. ...
  6. void Call_Function()
  7. {
  8.    Check_Balance();
  9.    
  10.    ...// do whatever call function do
  11. }
  12. void SMS_Function()
  13. {
  14.     // same as above
  15. }
  16. void Check_Balance()
  17. {
  18.     if(balance <= 2)
  19.         Top_Up();
  20.     //else balance is good and nothing need to be done
  21.     //or you may choose to print the balance here.
  22. }
复制代码
应为我觉得这样子做跟用goto好象是一样的东西。。。

随便你,喜欢继续用GOTO就继续用吧,当你进入OOP的区域(C++)的时候,你就会知道滋味了。或者你可以考虑将Assembly当成你的终生以及唯一的编程语言,那个语言倒是不忌讳GOTO(其实C里面的GOTO就是用inline assembler来处理的。
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 21-9-2025 05:27 PM , Processed in 0.141253 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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