结构体再函数中的作用

来源:百度知道 编辑:UC知道 时间:2024/06/09 10:40:26
结构体在函数中能起到什么样的作用?能简化程序吗?能使代码看起来更清晰吗?
#include<iostream>
using namespace std;
int main()
{
struct date
{int year;
int month;
int day;
};
date da;
cout<<"please enter the year:";
cin>>da.year;
cout<<endl;
cout<<"please enter the month:";
cin>>da.month;
cout<<endl;
cout<<"please enter the day:";
cin>>da.day;
cout<<endl;
if(da.year%40==0)

switch(da.month)
{
case 1:cout<<"the day of the date is:"<<da.day<<endl;
case 2:cout<<"the day of the date is:"<<31+da.day<<endl;
case 3:cout<<"the day of the date is:"<<31+29+da.day<<endl;}

else
switch(da.month)
{
case 1:cout<<"the day of the date is:"<<da.day<<endl;
case 2:cout<<"the d

结构体和其他类型基础数据类型一样,例如int类型,char类型
只不过结构体可以做成你想要的数据类型。以方便日后的使用。
在项目中,结构体是大量存在的。研发人员常使用结构体来封装一些属性来组成新的类型。

结构体在函数中的作用不是简便,其最主要的作用就是封装。封装的好处就是可以再次利用。让使用着不必关系这个是什么,只要根据定义使用就可以了。

结构体 相当于一种数据的载体

在你这个程序中 看不出来 结构体的作用

但是 当你需要一个复杂类型的时候 就可以看出来了

比如 你要向令一个类中的方法传递参数 如果不用结构体 你需要传3个参数

year month day 但是 你用结构体 只需要传递date 这个类型的一个参数

结构体的好处很多 用的多了 就明白了

你这个是c++?
我不知道要怎么来写,语法不会
不过不知道你会不会面向对象的语言
使用结构化以后,data就相当于一个类
date da;实例化了一个data对象,命名为da
后面使用da.day等语句就是使用了data类的一些属性
可以使程序看起来简便一点,并可以很好的进行扩展
简化程序的话,把
struct date
{int year;
int month;
int day;
};
放在另一个程序里面,只要在主程序里面调用就可以了

date da;
cout<<"please enter the year:";
cin>>da.year;
cout<<endl;
cout<<"please enter the month:";
cin>>da.month;
cout<<endl;
cout<<"please enter the day:";