C語言(面積計算器)


簡單面積計算器(自定義函數利用)

 調試了好久也沒有從default再次進入switch;
  程序不能輸入負數文章中以規范;
 1 #include<stdio.h>
 2 #include <stdlib.h> 
 3 #include<math.h>
 4 double calculateround(double);
 5 double calculatesan(double,double);
 6 double calculatechang(double,double);
 7 int panduan(double);
 8 int main ()
 9 {
10 int choose;
11  double r,s;
12  double w ,h;
13  printf("*************本應用程序只能執行一次且不能循環執行****************\n");
14  printf("------------------------只有三個選項由---------------------------\n");
15  printf("1、計算圓的面積!\n");
16  printf("2、計算三角形的面積!\n");
17  printf("3、計算長方形的面積!\n");
18  printf("-----------------------------------------------------------------\n");
19  printf("請輸所計算的面積的選項:\n");
20  scanf ("%d",&choose);
21  switch (choose)
22  {
23  case 1:printf("請輸入圓的半徑:\n");
24      do
25      {
26       scanf("%lf",&r);
27       if(!panduan(r))
28       {
29        printf("請輸入正確的數值,本程序只支持正數,請重新輸入:");
30       }
31      }while(!panduan(r));
32      s = calculateround(r);
33   break;
34  case 2:printf("請輸入三角形的寬和高:\n");
35   do
36   {
37    scanf("%lf%lf",&w,&h);
38     if(!panduan(w)||!panduan(h))
39       {
40        printf("請輸入正確的數值,本程序只支持正數,請重新輸入:");
41       }
42       }while(!panduan(w)||!panduan(h));
43   s= calculatesan( w, h);
44   break;
45  case 3:printf("請輸入三角形的寬和高:\n");
46   do
47   {
48    scanf("%lf%lf",&w,&h);
49     if(!panduan(w)||!panduan(h))
50       {
51        printf("請輸入正確的數值,本程序只支持正數,請重新輸入:");
52       }
53   }while(!panduan(w)||!panduan(h));
54   s= calculatechang(w,h);
55   break;
56  default:
57   printf("請輸入正確的選項:");
58  }
59  printf("所需計算的面積為:%.2lf\n",s);
60  
61     return 0;
62  }
63 int panduan(double num)
64 {
65  return num>0;
66 }
67 double calculateround(double r)
68 {
69  double s=3.14 * pow(r,2);
70  return s;
71 }
72 double calculatesan(double w,double h)
73 {
74  double  s = w *h/2;
75  return s;
76 }
77 double calculatechang(double w,double h)
78  {
79  double  s = w *h;
80  return s;
81 }

希望大神給與指點!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM