C语言--第四周作业

一、题目7-1 计算分段函数[1]
1.代码

#include <stdio.h>
int main ()
{
  float x,result;
  scanf("%f",&x);
  if(x==0)
  {
   result = 0; 
  }
  else
  {
    result = 1/x;
  }
  printf("f(%.1f) = %.1f",x,result);
}

2.设计思路
(a)
第一步:确定x的大小;
第二步:讨论值;
(b)流程图

3.错误

二、题目7-2 A除以B
1.代码

#include <stdio.h>
int main()
{
	int x,y;
	float c;
	scanf("%d %d",&x,&y);
	if (y==0)
	{
		printf("%d/%d=Error",x,y);
	}
	else if(y<0)
	{
		c = 1.0*x/y;
		printf("%d/(%d)=%.2f",x,y,c);
	}
	else
	{
		c = 1.0*x/y;
		printf("%d/%d=%.2f",x,y,c);
	}
	return 0;
}

2.思路
(a)步骤
第一步:确定y的大小
第二步:分步讨论
(b)

3.错误

原因:if后面的括号加了逗号。

三、题目7-6 阶梯电价
1.代码

#include <stdio.h>
int main ()
{
  float x,cost;
  scanf("%f",&x);
  if(x<=50)
  {
    if(x<0)
    {
      printf("Invalid Value!
");
    }
    else
    {
      cost = x*0.53;
      printf("cost = %.2f",cost);
    }
}
	
  else 
   {
      cost = 50*0.53+(x-50)*0.58;
        printf("cost = %.2f",cost);
    }
}

2.思路
(a)
第一步:确定x的范围
第二步:算出cost的值
(b)

错误

原因:应该是cost = xxx,而我直接输入数值。

四、题目7-7 出租车计价
1.代码


#include <stdio.h>
int main()
{
  float a,b;
  int x;
  scanf("%f %d",&a,&x);
  if(a<=3)
  {
    x = 10+b/5*2;
    printf("%.0f",x);
  }
  else if(a>3&&a<=10)
  {
    x = 10+2*(a-3)+b/5*2;
    printf("%.0f",x);
  }
  else
  {
    x = 10+2*7+(a-10)*2*1.5+b/5*2;
    printf("%.0f",x);
  }
}

2.思路
(a)
第一步:读懂题
第二步:确定x的范围
第三步:列出y的式子
(b)流程图

3.错误

五、Git
Git地址:https://git.coding.net/GUANCHENG/TRY.git

六、总结
在本周我们学习了if 条件语句,嵌套等知识。并且进行了相应的练习,收获良多。

七、互评
1.http://www.cnblogs.com/jsjyys/p/7709233.html#3824629
2.http://www.cnblogs.com/xmb1547828350/p/7719018.html
3.http://www.cnblogs.com/yaoshunyux/p/7747467.html

八、学习进度

原文地址:https://www.cnblogs.com/gu-an-cheng-wxm/p/7749010.html