问题描述:
按工资高低纳税,已知不同工资s的税率p如下: s<1000 p=0%
1000≤s<2000 p=5%
2000≤s<3000 p=8%
3000≤s<5000 p=10%
5000≤s p=15%
编一程序,输入工资数,求纳税款和实得工资数,
要求:(1)改错; (2)用if多分支形式重写程序。
main()
{ float s,p,t,s1
int m
scanf(“%d”,&s)
m=s%1000
if s>5000 m=5
switch (m)
{ case 0: p=0
case 1: p=0.05break
case 2: p=0.08break
case 3:
case 4: p=0.1break
case 5: p=0.15break
}
t=s*ps1=s-t
printf(“%.2f,%.2f” ,t, s1)
}
解析:
错误有两个:
第一.
m=s%1000
不能用求余,要用除,而且要强制转化
第二.
case 0: p=0这一行少一个break
第三.
if s>5000 m=5
应该加括号
不足有一个.
scanf(“%d”,&s)
应该为 %f
程序修改为:
——-——--
main()
{
float s,p,t,s1
int m
scanf("%f",&s)
m=(int)s/1000
if (s>5000) m=5
switch (m)
{ case 0: p=0break
case 1: p=0.05break
case 2: p=0.08break
case 3:
case 4: p=0.1break
case 5: p=0.15break
}
t=s*ps1=s-t
printf("%.2f,%.2f" ,t, s1)
}
用If 为
main()
{
float s,p,t,s1
int m
scanf("%f",&s)
m=(int)s/1000
if (s>5000) m=5
if (m==0)
p=0
else if (m==1)
p=0.05
else if (m==2)
p=0.08
else if (m==3 || m==4)
p=0.1
else if (m==5)
p=0.15
t=s*ps1=s-t
printf("%.2f,%.2f" ,t, s1)
}
#include<stdio.h>//缺少重要
头文件
#include
<string.h>#include
<stdlib.h>
int
SIZEstruct
student{
char
name[20]
int
xh
int
yw
//语文
int
sx
//数学
int
yy
//英语}*stud
void
save()int
srsj()int
cxsj()int
main()int
gzcx()int
gzcx(){
int
i,a,b
char
name[10]={0}
FILE
*fp
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("无法打开此文件\n")
}
printf("\t\t\t<学生信息查询>\n\n\n\t\t\t查看姓名,
\n")
for(i=0i<SIZEi++)
{
fread(&stud[i],sizeof(struct
student),1,fp)
printf("\t\t%s\n\t\t%d\n\t\t%d\n\t\t%d",stud[i].name,stud[i].xh,stud[i].sx,stud[i].yw,stud[i].yy)
}
printf("\n\t\t***************************************\n")
printf("\t\t请输入数字:1:按学号查询\n
2:按姓名查询\n")
printf("\t\t***************************************\n")
printf("你输入的数字:")
scanf("%d\n",&b)
switch(b)
case
1:
{
printf("请输入工号,查看工资清单\n\n")
printf("工号:")
scanf("%d\n",&a)
printf("\t\t<学生信息查询>\n")
for(i=0i<SIZEi++)
{
if(stud[i].xh==a)
{
printf("\t\t%s\n\t\t%d\n",stud[i].name,stud[i].xh)
}
}
break
case
2:
{printf("请输姓中文姓名,查看学生信息查询\n\n")
printf("姓名:")
scanf("%s",name)
for(i=0i<SIZEi++)
{
if(strcmp(name,stud[i].name)==0)
{
printf("\t\t%s\n\t\t%d\n",stud[i].name,stud[i].xh)
}
}
}
}
printf("\n")
printf("\n\n\t\t***************************************\n")
printf("\t\t注意:但无法查询时,请按要求 *** 作!或者是无此数据!\n")
printf("\t\t***************************************\n\n\n\n")
fclose(fp)
main()
return
0}
void
save(){
FILE
*fp
int
i
if((fp=fopen("stu.dat","wb"))==NULL)
{
printf("cannot
open
file
\n")
return
}
for(i=0i<SIZEi++)
if(fwrite(&stud[i],sizeof(struct
student),1,fp)!=1)
fclose(fp)}
int
srsj(){
int
i
int
SIZE
printf("输入学生格式:")
scanf("%d",&SIZE)
stud=new
struct
student[SIZE]
for(i=0i<SIZEi++)
scanf("%s%d%d%d%d",stud[i].name,&stud[i].xh,&stud[i].sx,&stud[i].yw,&stud[i].yy)
save()
return
0}
int
cxsj(){
int
i
FILE
*fp
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("cannot
open
file\n")
}
for(i=0i<SIZEi++)
{fread(&stud[i],sizeof(struct
student),1,fp)
printf("\t\t姓名:%s\t\t学号:%d\n",stud[i].name,stud[i].xh)
}
fclose(fp)
return
0}int
main() {
int
a
printf("\t\t1:进入
输入数据
\n\t\t2:查询数据\n\t\t3:查询\n")
printf("输入数字:")
scanf("%d",&a)
if(a==1)
srsj()
if(a==2)
cxsj()
if(a==3)
gzcx()
return
0//缺少返回}
abstract class Employee {public abstract double earnings()
}
class YearWorker extends Employee {
double x
public YearWorker(double x) {
super()
this.x = x
}
public double earnings() {
return x
}
}
class MonthWorker extends Employee {
double y
public MonthWorker(double y) {
super()
this.y = y
}
public double earnings() {
return (12 * y)
}
}
class WeekWorker extends Employee {
double z
public WeekWorker(double z) {
super()
this.z = z
}
public double earnings() {
return (48 * z)
}
}
class Company {
Employee[] employee
double salaries = 0
Company(Employee[] employee) {
this.employee = employee
}
public double salariesPay() {
salaries = 0
if(employee == null || employee.length == 0){
return 0
}
for (int i = 0i <employee.lengthi++) {
salaries += employee[i].earnings()
}
return salaries
}
}
public class HardWork {
public static void main(String args[]) {
Employee[] employee = new Employee[20]
for (int i = 0i <employee.lengthi++) {
if (i % 3 == 0)
employee[i] = new WeekWorker(i)//set value
else if (i % 3 == 1)
employee[i] = new MonthWorker(i)//set value
else if (i % 3 == 2)
employee[i] = new YearWorker(i)//set value
}
Company company = new Company(employee)
System.out.println("company:" + company.salariesPay())
}
}
编译通过
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)