3-4
运行结果1
结果2
结果3
#include<stdio.h> int main(void) { double x,y; printf("Enter x:"); scanf("%lf",&x); if(x<0){ y=0; } else if(x<=15){ y=4*x/3; } else{ y=2.5*x-10.5; } printf("f(%.2f)=%.2f ",x,y); return 0; }
3-5
#include<stdio.h> int main(void) { double value1,value2; char op; printf("Type in an expression:"); scanf("%lf%c%lf",&value1,&op,&value2); if(op=='+') printf("=%.2f ",value1+value2); else if(op=='-') printf("=%.2f ",value1-value2); else if(op=='*') printf("=%.2f ",value1*value2); else if(op=='/') printf("=%.2f ",value1/value2); else printf("Unknown operator "); return 0; }
3-7
#include<stdio.h> int main(void) { int digit,letter,other; char ch; int i; digit=letter=other=0; printf("Enter 10 characters:"); for(i=1;i<=10;i++){ ch=getchar(); if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) letter++; else if(ch>='0'&&ch<='9') digit++; else other++; } printf("letter=%d,digit=%d,other=%d ",letter,digit,other); return 0; }