C语言(面积计算器)
简单面积计算器(自定义函数利用)
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 }
希望大神给与指点!