目录
4.1
4.2
4.3
4.4
4.5.1
4.5.2
4.6
4.7
4-8-1
4-8-2
4-8-3
4.9
4.10
#include
#include // 程序中要调用求平方根函数sqrt
int main ( )
{double a,b,c,disc,x1,x2,p,q; // disc是判别式sqrt(b*b-4ac)
scanf("%lf%lf%lf",&a,&b,&c); // 输入双精度浮点型变量的值要用格式声明"%lf"
disc=b*b-4*a*c;
if (disc<0)
printf("This equation hav't real roots\n");
else
{ p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;x2=p-q; // 求出方程的两个根
printf("real roots:\nx1=%10.6f\nx2=%10.6f\n",x1,x2); // 输出方程的两个根
}
return 0;
}
#include
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;
}
#include
int main()
{
float a,b,c,t;
scanf("%f,%f,%f",&a,&b,&c);
if(a>b)
{
t=a;
a=b;
b=t;
} // 实现a和b的互换
if(a>c)
{
t=a;
a=c;
c=t;
} // 实现a和c的互换
if(b>c)
{
t=b;
b=c;
c=t;
} // 实现b和c的互换
printf("%5.2f,%5.2f,%5.2f\n",a,b,c);
return 0;
}
#include
int main()
{
char ch;
scanf("%c",&ch);
ch=(ch>='A' && ch<='Z') ? (ch+32) : ch;
printf("%c\n",ch);
return 0;
}
#include
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;
}
#include
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;
}
#include
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("data error!\n");
}
return 0;
}
#include
int main()
{
void action1(int,int),action2(int,int);
char ch;
int a=15,b=23;
ch=getchar();
switch(ch)
{
case 'a':
case 'A': action1(a,b);break;
case 'b':
case 'B': action2(a,b);break;
default: putchar('\a');
}
return 0;
}
void action1(int x,int y)
{
printf("x+y=%d\n",x+y);
}
void action2(int x,int y)
{
printf("x*y=%d\n",x*y);
}
#include
int main()
{
int year,leap;
scanf("%d",&year);
if (year%4==0)
{
if(year%100==0)
{
if(year%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if (leap)
printf("%d is ",year);
else
printf("%d is not ",year);
printf("a leap year.\n");
return 0;
}
#include
int main()
{
int year,leap;
scanf("%d",&year);
if(year%4!=0)
leap=0;
else if (year%100!=0)
leap=1;
else if(year%400!=0)
leap=0;
else
leap=1;
if (leap)
printf("%d is ",year);
else
printf("%d is not ",year);
printf("a leap year.\n");
return 0;
}
#include
int main()
{
int year,leap;
printf("enter year:");
scanf("%d",&year);
if((year%4==0 && year%100!=0) || (year%400==0))
leap=1;
else
leap=0;
if (leap)
printf("%d is ",year);
else
printf("%d is not ",year);
printf("a leap year.\n");
return 0;
}
#include
#include
int main()
{
double a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%lf,%lf,%lf",&a,&b,&c);
printf("The equation ");
if(fabs(a)<=1e-6)
printf("is not a quadratic\n");
else
{
disc=b*b-4*a*c;
if(fabs(disc)<=1e-6)
printf("has two equal roots:%8.4f\n",-b/(2*a));
else
if(disc>1e-6)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2);
}
else
{
realpart=-b/(2*a);
imagpart=sqrt(-disc)/(2*a);
printf(" has complex roots:\n");
printf("%8.4f+%8.4fi\n",realpart,imagpart);
printf("%8.4f-%8.4fi\n",realpart,imagpart);
}
}
return 0;
}
#include
int main()
{
int c,s;
float p,w,d,f;
printf("please enter price,weight,discount:"); // 提示输入的数据
scanf("%f,%f,%d",&p,&w,&s); // 输入单价、重量、距离
if(s>=3000)
c=12; // 3000km以上为同一折扣
else
c=s/250; // 3000km以下各段折扣不同,c的值不相同
switch(c)
{
case 0: d=0; break; // c=0,代表250km以下,折扣d=0
case 1: d=2; break; // c=1,代表250到500km以下,折扣d=2%
case 2:
case 3: d=5; break; // c=2和3,代表500到1000km以下,折扣d=5%
case 4:
case 5:
case 6:
case 7: d=8; break; // c=4-7,代表1000到2000km以下,折扣d=8%
case 8:
case 9:
case 10:
case 11: d=10; break; // c=8-11,代表2000KM到3000km以下,折扣d=10%
case 12: d=15; break; // c12,代表3000km以上,折扣d=15%
}
f = p * w * s * (1 - d / 100); // 计算总运费
printf("freight=%10.2f\n",f); // 输出总运费,取两位小数
return 0;
}