问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:胖子伤不起.cpp
*作 者:白云飞
*完成日期:2014年12月22日
*版 本 号:v1.0
*
*问题描述:输入一个人的姓名,性别,身高,体重,输出体重情况,要求使用结构体类型
*程序输入:一个人的姓名,性别,身高,体重
*程序输出:体重情况
*/
#include <iostream>
using namespace std;
struct People//声明一个累累型
{
char name[20];
char sex;
double height;
double weight;
};
int main()//主函数
{
int ms1,fs1,ms2,fs2;//声明变量、
People people;//定义一个函数
cout<<"请输入您的姓名、性别(m||f)、身高(cm)、体重(kg):"<<endl;
cin>>people.name>>people.sex>>people.height>>people.weight;
ms1=(people.height-80)*0.7;
fs1=(people.height-70)*0.6;
if(people.sex=='m')
{
ms2=(people.weight-ms1)*100/ms1;
if(ms2<10&&ms2>-10)//判断条件是否成立
cout<<"体重正常,继续保持!"<<endl;
else if(ms2>20)
cout<<"再吃就打手!"<<endl;
else if(ms2<-20)
cout<<"你是电线杆吗?"<<endl;
else if(ms2>=10&&ms2<=20)
cout<<"再吃就圆了!"<<endl;
else
cout<<"再不吃就飞起来了!"<<endl;
}
if(people.sex=='f')
{
fs2=(people.weight-fs1)*100/fs1;
if(fs2<10&&fs2>-10)
cout<<"体重正常,继续保持!"<<endl;
else if(fs2>20)
cout<<"再吃就打手!"<<endl;
else if(fs2<-20)
cout<<"你是电线杆吗?"<<endl;
else if(fs2>=10&&fs2<=20)
cout<<"再吃就圆了!"<<endl;
else
cout<<"再不吃就飞起来了!"<<endl;
}
return 0;
}
运算结果:
学习心得:
使用结构体类型比较生疏,还要多加练习。