1淆九、僅供學習交流參考恐仑。
#include<stdio.h>
#include<math.h>
#include<stdio.h>
#include<math.h>
#define PI 3.14159//對第二題的應用
void huashi()//1. 華氏度與攝氏度相互轉(zhuǎn)換
{
float f,c;
printf("\n");
printf("請輸入華氏:\n");
scanf("%f",&f);
c=(5.0/9)*(f-32);
printf("\n");
printf("c=%.2f\n",c);
printf("\n");
}
void mianji()//2. 求圓的面積
{
float r,s;
printf("\n");
printf("請輸入圓的半徑:");
scanf("%f",&r);
s=PI*r*r;
printf("\n");
printf("面積為%.2f\n",s);
printf("\n");
}
void zijieshu()//3. 測試不同數(shù)據(jù)類型的字節(jié)數(shù)
{
printf("\n");
printf("輸出各數(shù)據(jù)類型字節(jié)數(shù)\n");
printf("整形數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(int));
printf("短型數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(short));
printf("單精數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(float));
printf("雙精數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(double));
printf("字符數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(char));
printf("\n");
}
void runnian()//4. 判斷閏年
{
int a;
printf("\n");
printf("請輸入年份:");
scanf("%d",&a);
if(a%4==0 && a%100!=0 || a%400==0)
{
printf("%d 是閏年\n",a);
}
else
printf("%d 不是閏年\n",a);
printf("\n");
}
void sanjiaoxing()//5. 根據(jù)三角形的邊長,計算三角形面積
{
int a,b,c;
double d,s=0;
printf("\n");
printf("請輸入三角形邊長:");
printf("例如輸入:1追葡,2吐绵,3 按回車即可\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a+b>c && a+c>b && b+c>a)
{
d=(a+b+c)/2;
s=sqrt((d-a)*(d-b)*(d-c)*d);
printf("s=%.2f平方米\n",s);
}
else
printf("不能構(gòu)成三角形,請重新輸入\n");
printf("\n");
}
void shuru()//6. 分段函數(shù)的計算
{
int x,y;
printf("\n");
printf("請輸入x的值:");
scanf("%d",&x);
if(x>10)
{
y=3*x-11;
printf("y=%d\n",y);
}
else
if(x>1)
{
y=2*x-1;
printf("y=%d\n",y);
}
else
printf("y=%d\n",x);
printf("\n");
}
void shuijin()//7. 所得稅計算
{
float a,b;
printf("\n");
printf("請輸入工資:a=");
scanf("%f",&a);
if(a<3000)
b=0;
else
if(a<5000)
b=(a-3000)*0.03;
else
b=(a-5000)*0.1;
printf("需要交納稅b=%.2f\n",b);
printf("\n");
}
void yunshuanfu()//8. 實現(xiàn)兩個數(shù)的算術運算
{
float x,y;
char p;
printf("\n");
printf("輸入要計算的數(shù)值:\n");
printf("例如輸入:1-2,按回車\n");
scanf("%f%c%f",&x,&p,&y);
switch (p)
{
case '+':printf("%.2f+%.2f=%.2f\n",x,y,x+y);break;
case '-':printf("%.2f-%.2f=%.2f\n",x,y,x-y);break;
case '*':printf("%.2f*%.2f=%.2f\n",x,y,x*y);break;
case '/':printf("%.2f/%.2f=%.2f\n",x,y,x/y);break;
case '%':printf("%.2f%c%.2f=%d\n",x,p,y,(int)x%(int)y);break;
}
printf("\n");
}
void zongshu()//9. 人口數(shù)量計算
{
int n=2000;
double p=0.078,m=14;
printf("\n");
while (n<=2020)
{
m=m*(1+p);
n=n+1;
}
printf("2020年的人口數(shù)量為%.2f億\n",m);
printf("\n");
}
void jiecheng()//10. 求階乘
{
int a=1,b=1,n=20;
printf("\n");
printf("請輸小于20的數(shù)字n:");
scanf("%d",&n);
while(b<=n)
{
a=a*b;
b=b+1;
}
printf("%d的階乘計算結(jié)果為:%d\n",n,a);
printf("\n");
}
void sushu()//11. 判斷素數(shù)
{
int n,k,i;
printf("\n");
printf("請輸入一個正整數(shù):");
scanf("%d",&n);
{
k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0)break;
if(i>=k+1)
{
printf("%d是素數(shù)\n",n);
}
else
printf("%d不是素數(shù)\n",n);
}
printf("\n");
}
void shulie()//12. 求斐波那契數(shù)列
{
int i;
int a[20]={1,1};
printf("\n");
printf("正在輸出非波納契前20項\n");
for(i=2;i<20;i++)
a[i]=a[i-2]+a[i-1];
for(i=0;i<20;i++)
{
if(i%5==0)
printf("\n");
printf("%12d",a[i]);
}
printf("\n");
}
void defen()//13. 計算選手得分
{
float score[7];
float min,max,ave;
float sum=0.0;
int i;
printf("\n");
printf("請分別輸入七個人員的分數(shù):\n");
for(i=0;i<7;i++) scanf("%f",&score[i]);
printf("\n");
min=score[0];
max=score[0];
for(i=0;i<7;i++){
sum=sum+score[i];
if(score[i]>max) max=score[i];
if(score[i]<min) min=score[i];
}
ave=(sum-max-min)/5;
printf("最高分=%5.2f\n",max);
printf("最低分=%5.2f\n",min);
printf("平均分=%5.2f\n",ave);
printf("\n");
}
void chazhao()//14. 查找電話號碼
{
int i;
char n[20];
struct call
{
char name[20];
char num[20];
}
a[4]={{"zhao","11111111111"},{"qian","22222222222"},{"sun","33333333333"},
{"li","44444444444"}};
printf("可查詢?nèi)藛T名字列表\n");
printf("zhao\n");
printf("qian\n");
printf("sun\n");
printf("li\n");
printf("請輸入姓名\n");
scanf("%s",n);
for(i=0;i<4;i++)
if(strcmp(a[i].name,n)==0)
{
printf("%s\n",a[i].num);
break;
}
if(i>=4)
printf("查無此人\n");
}
void main()
{
int p;
while(1)
{
printf("1. 華氏度與攝氏度相互轉(zhuǎn)換 \n");
printf("2. 求圓的面積\n");
printf("3. 測試不同數(shù)據(jù)類型的字節(jié)數(shù)\n");
printf("4. 判斷閏年\n");
printf("5. 根據(jù)三角形的邊長,計算三角形面積\n");
printf("6. 分段函數(shù)的計算\n");
printf("7. 所得稅計算\n");
printf("8. 實現(xiàn)兩個數(shù)的算術運算\n");
printf("9. 人口數(shù)量計算\n");
printf("10.求階乘\n");
printf("11.判斷素數(shù)\n");
printf("12.求斐波那契數(shù)列 \n");
printf("13.計算選手得分\n");
printf("14.查找電話號碼\n");
printf("0. 退出\n");
printf("請選擇0-14:");
scanf("%d",&p);
if(p==0)
break;
switch(p)
{
case 1:huashi();break;
case 2:mianji();break;
case 3:zijieshu();break;
case 4:runnian();break;
case 5:sanjiaoxing();break;
case 6:shuru();break;
case 7:shuijin();break;
case 8:yunshuanfu();break;
case 9:zongshu();break;
case 10:jiecheng();break;
case 11:sushu();break;
case 12:shulie();break;
case 13:defen();break;
case 14:chazhao();break;
default:printf("請重新選擇:\n");break;
}
}
}
#define PI 3.14159//對第二題的應用
void huashi()//1. 華氏度與攝氏度相互轉(zhuǎn)換
{
float f,c;
printf("\n");
printf("請輸入華氏:\n");
scanf("%f",&f);
c=(5.0/9)*(f-32);
printf("\n");
printf("c=%.2f\n",c);
printf("\n");
}
void mianji()//2. 求圓的面積
{
float r,s;
printf("\n");
printf("請輸入圓的半徑:");
scanf("%f",&r);
s=PI*r*r;
printf("\n");
printf("面積為%.2f\n",s);
printf("\n");
}
void zijieshu()//3. 測試不同數(shù)據(jù)類型的字節(jié)數(shù)
{
printf("\n");
printf("輸出各數(shù)據(jù)類型字節(jié)數(shù)\n");
printf("整形數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(int));
printf("短型數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(short));
printf("單精數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(float));
printf("雙精數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(double));
printf("字符數(shù)據(jù)所占字節(jié)數(shù)%d\n",sizeof(char));
printf("\n");
}
void runnian()//4. 判斷閏年
{
int a;
printf("\n");
printf("請輸入年份:");
scanf("%d",&a);
if(a%4==0 && a%100!=0 || a%400==0)
{
printf("%d 是閏年\n",a);
}
else
printf("%d 不是閏年\n",a);
printf("\n");
}
void sanjiaoxing()//5. 根據(jù)三角形的邊長,計算三角形面積
{
int a,b,c;
double d,s=0;
printf("\n");
printf("請輸入三角形邊長:");
printf("例如輸入:1溺职,2芭概,3 按回車即可\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a+b>c && a+c>b && b+c>a)
{
d=(a+b+c)/2;
s=sqrt((d-a)*(d-b)*(d-c)*d);
printf("s=%.2f平方米\n",s);
}
else
printf("不能構(gòu)成三角形赛不,請重新輸入\n");
printf("\n");
}
void shuru()//6. 分段函數(shù)的計算
{
int x,y;
printf("\n");
printf("請輸入x的值:");
scanf("%d",&x);
if(x>10)
{
y=3*x-11;
printf("y=%d\n",y);
}
else
if(x>1)
{
y=2*x-1;
printf("y=%d\n",y);
}
else
printf("y=%d\n",x);
printf("\n");
}
void shuijin()//7. 所得稅計算
{
float a,b;
printf("\n");
printf("請輸入工資:a=");
scanf("%f",&a);
if(a<3000)
b=0;
else
if(a<5000)
b=(a-3000)*0.03;
else
b=(a-5000)*0.1;
printf("需要交納稅b=%.2f\n",b);
printf("\n");
}
void yunshuanfu()//8. 實現(xiàn)兩個數(shù)的算術運算
{
float x,y;
char p;
printf("\n");
printf("輸入要計算的數(shù)值:\n");
printf("例如輸入:1-2,按回車\n");
scanf("%f%c%f",&x,&p,&y);
switch (p)
{
case '+':printf("%.2f+%.2f=%.2f\n",x,y,x+y);break;
case '-':printf("%.2f-%.2f=%.2f\n",x,y,x-y);break;
case '*':printf("%.2f*%.2f=%.2f\n",x,y,x*y);break;
case '/':printf("%.2f/%.2f=%.2f\n",x,y,x/y);break;
case '%':printf("%.2f%c%.2f=%d\n",x,p,y,(int)x%(int)y);break;
}
printf("\n");
}
void zongshu()//9. 人口數(shù)量計算
{
int n=2000;
double p=0.078,m=14;
printf("\n");
while (n<=2020)
{
m=m*(1+p);
n=n+1;
}
printf("2020年的人口數(shù)量為%.2f億\n",m);
printf("\n");
}
void jiecheng()//10. 求階乘
{
int a=1,b=1,n=20;
printf("\n");
printf("請輸小于20的數(shù)字n:");
scanf("%d",&n);
while(b<=n)
{
a=a*b;
b=b+1;
}
printf("%d的階乘計算結(jié)果為:%d\n",n,a);
printf("\n");
}
void sushu()//11. 判斷素數(shù)
{
int n,k,i;
printf("\n");
printf("請輸入一個正整數(shù):");
scanf("%d",&n);
{
k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0)break;
if(i>=k+1)
{
printf("%d是素數(shù)\n",n);
}
else
printf("%d不是素數(shù)\n",n);
}
printf("\n");
}
void shulie()//12. 求斐波那契數(shù)列
{
int i;
int a[20]={1,1};
printf("\n");
printf("正在輸出非波納契前20項\n");
for(i=2;i<20;i++)
a[i]=a[i-2]+a[i-1];
for(i=0;i<20;i++)
{
if(i%5==0)
printf("\n");
printf("%12d",a[i]);
}
printf("\n");
}
void defen()//13. 計算選手得分
{
float score[7];
float min,max,ave;
float sum=0.0;
int i;
printf("\n");
printf("請分別輸入七個人員的分數(shù):\n");
for(i=0;i<7;i++) scanf("%f",&score[i]);
printf("\n");
min=score[0];
max=score[0];
for(i=0;i<7;i++){
sum=sum+score[i];
if(score[i]>max) max=score[i];
if(score[i]<min) min=score[i];
}
ave=(sum-max-min)/5;
printf("最高分=%5.2f\n",max);
printf("最低分=%5.2f\n",min);
printf("平均分=%5.2f\n",ave);
printf("\n");
}
void chazhao()//14. 查找電話號碼
{
int i;
char n[20];
struct call
{
char name[20];
char num[20];
}
a[4]={{"zhao","11111111111"},{"qian","22222222222"},{"sun","33333333333"},
{"li","44444444444"}};
printf("可查詢?nèi)藛T名字列表\n");
printf("zhao\n");
printf("qian\n");
printf("sun\n");
printf("li\n");
printf("請輸入姓名\n");
scanf("%s",n);
for(i=0;i<4;i++)
if(strcmp(a[i].name,n)==0)
{
printf("%s\n",a[i].num);
break;
}
if(i>=4)
printf("查無此人\n");
}
void main()
{
int p;
while(1)
{
printf("1. 華氏度與攝氏度相互轉(zhuǎn)換 \n");
printf("2. 求圓的面積\n");
printf("3. 測試不同數(shù)據(jù)類型的字節(jié)數(shù)\n");
printf("4. 判斷閏年\n");
printf("5. 根據(jù)三角形的邊長,計算三角形面積\n");
printf("6. 分段函數(shù)的計算\n");
printf("7. 所得稅計算\n");
printf("8. 實現(xiàn)兩個數(shù)的算術運算\n");
printf("9. 人口數(shù)量計算\n");
printf("10.求階乘\n");
printf("11.判斷素數(shù)\n");
printf("12.求斐波那契數(shù)列 \n");
printf("13.計算選手得分\n");
printf("14.查找電話號碼\n");
printf("0. 退出\n");
printf("請選擇0-14:");
scanf("%d",&p);
if(p==0)
break;
switch(p)
{
case 1:huashi();break;
case 2:mianji();break;
case 3:zijieshu();break;
case 4:runnian();break;
case 5:sanjiaoxing();break;
case 6:shuru();break;
case 7:shuijin();break;
case 8:yunshuanfu();break;
case 9:zongshu();break;
case 10:jiecheng();break;
case 11:sushu();break;
case 12:shulie();break;
case 13:defen();break;
case 14:chazhao();break;
default:printf("請重新選擇:\n");break;
}
}
}