例4.1 在例3.5的基础上对程序进行改进
#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c,disc,x1,x2,p,q;
scanf("%1f%1f%1f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc<0)
printf("this equation has not real roots\n");
else
{
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;x2=p-q;
printf("real roots:\nxl=%7.2f\nx2=%7.2f\n",x1,x2);
}
return 0;
}
例4.2 输入两个实数,按由小到大的顺序输出这两个数。
#include<stdio.h>
int main( )
{
float a,b,t;
scanf("%f,%f",&a,&b);
if(a>b)
{
//将和上的值互换
t=a;
a=b;
b=t;
}
printf("%5.2f,%5.2f\n",a,b);
return 0;
}
例4.3输入3个数a,b,c,要求按由小到大的顺序输出
include<stdio.h>
int main()
{
float a,b,c,t;
scanf("%f,%f,%f",&a,&b,&c);
if(a>b)
{
t=a;
a=b;
b=t;
}
if(a>c)
{
t=a;
a=c;
c=t;
}
if(b>c)
{
t=b;
b=c;
c=t;
}
printf("%5.2f,%5.2f,%5.2f\n",a,b,c);
return 0;
}
例4.4输入一个字符,判别它是否为大写字母,如果是,将他转换成小写字母,如果不是,不转换,然后输出的得到的字符
#include<stdio.h>
int main( )
{
char ch;
scanf("%c" , &ch);
ch=(ch>='A' && ch<='Z') ? (ch+32) : ch;
printf("%c\n" ,ch);
return 0;
}
例4.5编一个程序,输入一个x值,要求输出相应的y值
# include<stdio.h>
int main( )
{
int x,y;
scanf("%d",&x);
if(x<0)
y=-1;
else
if(x==0) y=0;
else
y=1;
printf(" x= %d,y= %d\n" ,x,y);
return 0;
}
程序2
# include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
if(x>=0)
if(x>0) y=1;
else y=0;
else y=-1 ;
printf("x= %d,y= %d\n",x,y);
return 0;
}
例4.6要求按照考试成绩的等级输出百分制分数段
# include<stdio.h>
int main()
{
char grade;
scanf("%c",&grade);
printf("Your score:");
switch(grade)
{
case 'A' : printf("85~ 100\n" );break;
case 'B' : printf("70~ 84\n" );break;
case 'C' : printf("60~ 69\n") ;break;
case 'D' : printf("<60\n ");break;
default: printf("enter data error!\n");
}
return 0;
}