1.用函数实现pow功能(不含形参)
#include <stdio.h> #include <stdlib.h> double power(); int main() { double result = power(); printf("结果是:%.2lf\n", result); return 0; } double power() { double num1; int num2; int i; double result; printf("请输入底数:"); scanf("%lf", &num1); printf("\n请输入指数:"); scanf("%d", &num2); result = 1; for (i = 0; i < num2; i++) { result = result * num1; } return result; }
2.简单面积的计算(含形参)
#include <stdio.h> #include <stdlib.h> #include <math.h> double Area(double,double,double,int); int validata(); int main() { double radius,length,highth; int i; printf("请选择要计算的图形面积:\n1.圆形\n2.长方形\n3.三角形\n"); scanf("%d",&i); switch(i) { case 1: printf("请输入圆的半径:"); do { scanf("%lf",&radius); if(!validata(radius)) { printf("您输入的数字不规范,请重新输入:"); } }while(!validata(radius)); printf("\n圆的面积为:%.2lf",Area(radius,0,0,1)); break; case 2: printf("请输入长方形的长、宽:"); do { scanf("%lf%lf",&length,&highth); if(!validata(length)||!validata(highth)) { printf("您输入的数字不规范,请重新输入:"); } }while(!validata(length)||!validata(highth)); printf("\n长方形的面积为:%.2lf",Area(0,length,highth,2)); break; case 3: printf("请输入圆的半径:"); do { scanf("%lf%lf",&length,&highth); if(!validata(length)||!validata(highth)) { printf("您输入的数字不规范,请重新输入:"); } }while(!validata(length)||!validata(highth)); printf("\n三角形的面积为:%.2lf",Area(0,length,highth,3)); break; default: printf("请输入正确的数字形式:"); } return 0; } double Area(double radius,double length,double highth,int i) { double s; switch(i) { case 1: s=3.14*pow(radius,2); return s; break; case 2: s=length*highth; return s; break; case 3: s=length*highth/2; return s; break; } return 0; } int validata(double num) { return num>0?1:0; }