优化这段代码:#include<stdio.h> struct stu { int shf; int jxj; int jzsr; int cyf; int txf; int fzf; int jcf; int ylf; int qt; }; void main () { struct stu fy; struct stu *ps=&fy; ps-> yf=3; ps->shf=2000; ps->jxj=1000; ps->jzsr=800; ps->cyf=800; ps->txf=600; ps->fzf=600; ps->jcf=300; ps->ylf=350; ps->qt=100; printf("第%d月的生活费%d\n",ps-->yf,ps->shf); printf("第%d月的奖学金%d\n",ps-yf,ps-->jxj); printf("第%d月的兼职收入%d\n",ps->yf,ps->jzsr); printf("第%d月的餐饮费%d\n",ps->yf,ps->cyf); printf("第%d月的通信费%d\n",ps->yf,ps->txf); printf("第%d月的服装费%d\n",ps->yf,ps->fzf); printf("第%d月的教材费%d\n",ps->yf,ps->jcf); printf("第%d月的娱乐费%d\n",ps->yf,ps->ylf); printf("第%d月的其它费%d\n",ps->yf,ps->qt); }
时间: 2024-02-28 08:53:59 浏览: 23
代码优化如下:
```c
#include <stdio.h>
struct stu {
int shf;
int jxj;
int jzsr;
int cyf;
int txf;
int fzf;
int jcf;
int ylf;
int qt;
};
int main() {
struct stu fy = {
.yf = 3,
.shf = 2000,
.jxj = 1000,
.jzsr = 800,
.cyf = 800,
.txf = 600,
.fzf = 600,
.jcf = 300,
.ylf = 350,
.qt = 100
};
printf("第%d月的生活费%d\n", fy.yf, fy.shf);
printf("第%d月的奖学金%d\n", fy.yf, fy.jxj);
printf("第%d月的兼职收入%d\n", fy.yf, fy.jzsr);
printf("第%d月的餐饮费%d\n", fy.yf, fy.cyf);
printf("第%d月的通信费%d\n", fy.yf, fy.txf);
printf("第%d月的服装费%d\n", fy.yf, fy.fzf);
printf("第%d月的教材费%d\n", fy.yf, fy.jcf);
printf("第%d月的娱乐费%d\n", fy.yf, fy.ylf);
printf("第%d月的其它费%d\n", fy.yf, fy.qt);
return 0;
}
```
优化点如下:
1. 使用了初始化器初始化结构体 `fy` 的所有成员变量。
2. `main` 函数的返回值类型应该是 `int`。
3. 箭头运算符改为点运算符。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)