您的位置:首页 > 其它

07.21C_struct

2016-07-21 08:16 274 查看


【项目1-复数结构体】

编写一个程序,首先定义一个复数数据类型,即结构类型。然后按照复数的运算规则进行计算,并按照复数表示的格式进行输出,请将程序补充完整。
解法:
#include <stdio.h>
#include <stdlib.h>

int main()
{
typedef struct
{
double real;
double imag;

}complex_n;
complex_n com1,com2,com3;
printf("please enter the real part and imagine part of the two complex number:");
scanf("%lf%lf%lf%lf",&com1.real,&com1.imag,&com2.real,&com2.imag);
com3.real=com1.real*com2.real-com1.imag*com2.imag;
com3.imag=com1.real*com2.imag+com1.imag*com2.real;
printf("the result is %.2lf+%.2lfi",com3.real,com3.imag);
return 0;
}


【项目2-学生成绩处理】

(1)定义学生结构体,在结构体数组中,输入了学生学号和两门课的成绩,计算均分后,输出成绩单,请在下面的程序基础上,将程序补充完整。 



解法:
#include <stdio.h>
#include <stdlib.h>
#define N 100

struct student
{
int num;
int score_c;
int score_math;
double aver;
};
int main()
{
int n;
int i;
int pass=0;
struct student stu
;
printf("please enter the amount of the student:");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;++i)
{
printf("please enter the number score of c and math of the %d-th student:\n",i);
scanf("%d%d%d",&((stu+i)->num),&((stu+i)->score_c),&((stu+i)->score_math));
(stu+i)->aver=((stu+i)->score_c+(stu+i)->score_math)/2;
if((stu+i)->score_c<60&&(stu+i)->score_math<60)
++pass;
}
printf("score report\n");
fflush(stdin);
printf("number\tc\tmath\taverage\t\n");
for(i=0;i<n;++i)
{
printf("%d\t%d\t%d\t%.2lf\t",(stu+i)->num,(stu+i)->score_c,(stu+i)->score_math,(stu+i)->aver);
printf("\n");
}
printf("the amount of the students did not pass:%d",pass);
return 0;
}


【项目3-点结构体】

下面定义了一个表示平面上一点的结构体类型:
(1)请编写程序,输入一点的横纵坐标,输出该点到原点的距离 
(2)请编写程序,输入两点p1和p2的坐标,输出两点之间的距离,以及p1关于x轴的对称点,p2关于原点的对称点,运行结果如下图所示:
解法:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct point
{
float x;
float y;
};
int main()
{
float distance;
struct point p1,p2;
printf("please enter two number x,y:");
scanf("%f%f%f%f",&(p1.x),&(p1.y),&(p2.x),&(p2.y));
distance=sqrt(pow((p1.x-p2.x),2)+pow((p1.y-p2.y),2));
printf("the distance between two points is:%.2f\n",distance);
printf("点p1关于x轴的对称点(%.2f %.2f)\n",-(p1.x),(p1.y));
printf("点p2关于原点对称的点(%.2f %.2f)\n",-(p2.x),-(p2.y));
return 0;
}


【项目4-体重监测器】

根据世界卫生组织推荐的体重标准,男性的标准体重=(身高cm-80)×70﹪,女性的标准体重=(身高cm-70)×60﹪。标准体重正负10﹪为
4000
正常体重;标准体重正负10﹪~ 20﹪为体重过重或过轻;标准体重正负20﹪以上为肥胖或体重不足,其中超重计算公式为:超重(%)=[(实际体重-标准体重)/(标准体重)]×100%,体重的单位是kg。 

请编程序,输入一个人的姓名、性别、身高、体重,用一句人性味浓的话语输出其体重情况(正常、过重、过轻、肥胖或体重不足),这个程序可以用于体重计上为人们实时报告体重情况。 
解法:#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct person
{
char name[20];
char sex;
float height;
float weight;
};
int main()
{
float sf_weight;
float sm_weight;
float percent;
struct person p;
printf("please enter your name:");
scanf("%s",p.name);
fflush(stdin);
printf("please enter your sex,height,weight:");
scanf("%c%f%f",&(p.sex),&(p.height),&(p.weight));
if(p.sex=='F')
{
sf_weight=(p.height-70)*0.6;
percent=(p.weight-sf_weight)/sf_weight;
}
if(p.sex=='M')
{
sm_weight=(p.height-80)*0.7;
percent=(p.weight-sm_weight)/sm_weight;
}
if(percent<0.1||percent>(-0.1))
printf("your body figure is perfect ,you look so hot!");
else if((percent>=0.1&&percent<=0.2)||(percent<=(-0.1)&&percent>=(-0.2)))
printf("a little overweight,keep working out!!");
else if(percent>0.2&&percent<(-0.2))
printf("you are a big fat man,lose weight urgently!!!!!!");
return 0;
}


【项目5-个税计算器之码数分离】

在学习分支结构时,我们做过一个个人所得税计算器if语句版,在其中,税率等数据直接写进了程序。这并不是一个好的方案。 

下面定义了一个结构体数组,保存各个收入范围个税计算的基础数据,这样,一定程度上,将代码与数据分离,这是工程中常见的做法。 

请先对照附后的个人所得税计算方法,理解下面的结构体数组:


【项目5-个税计算器之码数分离】

在学习分支结构时,我们做过一个个人所得税计算器if语句版,在其中,税率等数据直接写进了程序。这并不是一个好的方案。 

下面定义了一个结构体数组,保存各个收入范围个税计算的基础数据,这样,一定程度上,将代码与数据分离,这是工程中常见的做法。 

请先对照附后的个人所得税计算方法,理解下面的结构体数组:

解法:#include <stdio.h>
#include <stdlib.h>
struct salary
{
double dSalary;//个人月收入
double dIncome;//税后工资
double dTax;//应该缴纳的税款
};
struct tax_threshold
{
double paytax;//工资超过个税起征点的部分
double tax_rate;//收入对应的税率
double deduction;//固定扣除
};
int main()
{
int i;
struct salary sal;
struct tax_threshold tax[7]=
{
{0,0.03,0},
{1500,0.1,105},
{4500,0.2,105},
{9000,0.25,555},
{35000,0.3,1005},
{55000,0.35,2755},
{80000,0.45,5505},
};
printf("please enter you salary:");
scanf("%lf",&(sal.dSalary));
for(i=0;i<7;++i)
{
if(tax[i].paytax<(sal.dSalary-3500)&&(sal.dSalary-3500)<tax[i+1].paytax)
{
sal.dTax=(sal.dSalary-3500)*tax[i].tax_rate-tax[i].deduction;
sal.dIncome=sal.dSalary-sal.dTax;
}
}
printf("the tax you need to pay:%.2lf\n",sal.dTax);
printf("your final income after tax is:%.2lf\n",sal.dIncome);

return 0;
}


【项目6-日期结构体】

(1)定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天。
(2)输入两个人的生日,求出他们相差多少天。
解法:#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int firstfew();
int differ();

struct dayth
{
int iDayth;//第几天
int iDiffer;//相差几天
};
struct data
{
int iYear;
int iMonth;
int iDay;
struct dayth dayth1;
};

int main()
{
struct data data1[2];
printf("please enter your birthday:(year month day):");
scanf("%d%d%d",&(data1[0].iYear),&(data1[0].iMonth),&(data1[0].iDay));
fflush(stdin);
printf("please enter another person's birthday:(year month day)");
scanf("%d%d%d",&(data1[1].iYear),&(data1[1].iMonth),&(data1[1].iDay));
struct data *ps=data1;
ps->dayth1.iDayth=firstfew(ps);
(ps+1)->dayth1.iDayth=firstfew((ps+1));
ps->dayth1.iDiffer=(ps+1)->dayth1.iDiffer=differ(ps);
printf("the first man's birthday is the %d-th day of the year\n",ps->dayth1.iDayth);
printf("the second man's birthday is the %d-th day of the year\n",(ps+1)->dayth1.iDayth);
printf("the distance between the two birthday is:%d\n",ps->dayth1.iDiffer);
return 0;
}
int firstfew(struct data *pd)
{
int feb=0;//二月天数
if(pd->iYear%4==0&&pd->iYear%400==0)
feb=29;
else
feb=28;
switch(pd->iMonth)
{
case 1: pd->dayth1.iDayth=pd->iDay;break;
case 2: pd->dayth1.iDayth=31+pd->iDay;break;
case 3: pd->dayth1.iDayth=31+feb+pd->iDay;break;
case 4: pd->dayth1.iDayth=31*2+feb+pd->iDay;break;
case 5: pd->dayth1.iDayth=31*2+feb+30+pd->iDay;break;
case 6: pd->dayth1.iDayth=31*3+feb+30+pd->iDay;break;
case 7: pd->dayth1.iDayth=31*3+feb+30*2+pd->iDay;break;
case 8: pd->dayth1.iDayth=31*4+feb+30*2+pd->iDay;break;
case 9: pd->dayth1.iDayth=31*5+feb+30*2+pd->iDay;break;
case 10: pd->dayth1.iDayth=31*5+feb+30*3+pd->iDay;break;
case 11: pd->dayth1.iDayth=31*6+feb+30*3+pd->iDay;break;
case 12: pd->dayth1.iDayth=31*6+feb+30*4+pd->iDay;break;
}
return(pd->dayth1.iDayth);
}
int differ(struct data *ps)
{
int differ[2];
int dif;//今年生日相差集体几天
differ[0]=firstfew(ps);
differ[1]=firstfew((ps+1));
dif=abs(differ[0]-differ[1])+1;
return(dif);
}


【项目7-紧急救援】

洪水突降,人们被困在各个屋顶上。如图所示: 


 

救生船每次将从大本营出发,救了人之后将人送回大本营。已知救生船速度50米/分钟,逐个屋顶救人,每人上船1分钟,下船0.5分钟。以救援大本营为原点,输入每个屋顶的位置坐标和屋顶上的人数,求出所有人都到达大本营并登陆所用的时间。 

要求使用结构体表示屋顶的坐标和人数。
解法:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int time();
struct point
{
int x;//horizontal coordinate
int y;//vertical coordinate
int num;//amount of people in each point
};
int main()
{
int n=0;
printf("please enter the amount of the roof needed to be rescued:");
scanf("%d",&n);
fflush(stdin);

int i=0;
int t;
struct point pm
;
struct point *p=pm;

do
{
printf("please enter the amount of the people and the coordinates of x,y of %d-th point:",(i+1));
scanf("%d%d%d",&((p+i)->num),&((p+i)->x),&((p+i)->y));
++i;
}while(i<n);
t=time(p,n);
printf("the total time spent in rescuing all the people is:%d",t);
return 0;
}
int time(struct point *p,int n)
{
int i;
int t=0;//total time used
int d=0;//total distance
int num=0;//total amount of the people
for(i=0;i<n;++i)
{
d +=sqrt(pow((p+i)->x,2)+pow((p+i)->y,2));
num += (p+i)->num;
}
t=d/50+num*1.5;
return(t);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: