结构经典例题,用上了typedef 再进行指针优化
来源:互联网 发布:广州知行通航 编辑:程序博客网 时间:2024/05/22 08:11
/*Description现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。编写一个函数input,用来输入一个学生的数据记录。编写一个函数print,打印一个学生的数据记录。在主函数调用这两个函数,读取N条记录输入,再按要求输出。 N<100Input学生数量N占一行 每个学生的学号、姓名、三科成绩占一行,空格分开。Output每个学生的学号、姓名、三科成绩占一行,逗号分开。Sample Input2a100 zhblue 70 80 90b200 newsclan 90 85 75Sample Outputa100,zhblue,70,80,90b200,newsclan,90,85,75*/#include<stdio.h>typedef struct data{ //定义了一个类型,可以在下面的使用中直接用DataStru char studentID[20]; //来代替所有的struct data; char str[20]; int cj1; int cj2; int cj3;} DataStru;DataStru women[100];int main(){ DataStru print(DataStru temp); int num,i,temp; DataStru input(DataStru); scanf("%d",&num); for (i = 0;i < num;i ++){ women[i]=input(women[i]); } for (i = 0;i < num;i ++){ print(women[i]); printf("\n"); } return 0;}DataStru input(DataStru temp) //为了方便用自定义的函数来进行输入{ scanf("%s ",&temp.studentID); scanf("%s ",&temp.str); scanf("%d ",&temp.cj1); scanf("%d ",&temp.cj2); scanf("%d",&temp.cj3); return temp;}DataStru print(DataStru temp) //用自定义的函数来进行输出{ printf("%s ",temp.studentID); printf("%s ",temp.str); printf("%d ",temp.cj1); printf("%d ",temp.cj2); printf("%d",temp.cj3);}
接下来要用指针进行优化。
//来吧用指针来走一遭。#include<stdio.h>typedef struct data{ //定义了一个类型,可以在下面的使用中直接用DataStru char studentID[20]; //来代替所有的struct data; char str[20]; int cj1; int cj2; int cj3;} DataStru;DataStru women[100];void print(DataStru *); //声明可以在main外void input(DataStru *); //要标明是结构指针int main(){ int num,i,temp; scanf("%d",&num); for (i = 0;i < num;i ++){ input(&women[i]); } for (i = 0;i < num;i ++){ print(&women[i]); printf("\n"); } return 0;}void input(DataStru *p) //为了方便用自定义的函数来进行输入{ //一系列用指针的华丽输入 scanf("%s ",&p->studentID); scanf("%s ",&p->str); scanf("%d ",&p->cj1); scanf("%d ",&p->cj2); scanf("%d",&p->cj3);}void print(DataStru *p) //用自定义的函数来进行输出{ //一系列用指针的华丽输出 printf("%s ",p->studentID); printf("%s ",p->str); printf("%d ",p->cj1); printf("%d ",p->cj2); printf("%d" ,p->cj3);}
阅读全文
0 0
- 结构经典例题,用上了typedef 再进行指针优化
- 指针经典例题
- 结构体指针例题(一)
- 结构体指针例题(二)
- 结构体指针例题(三)
- 终于弄明白了的结构体与typedef的使用,还有结构体指针的使用(二层结构体指针)
- 终于用上blog了
- 今天用上Zoundry了
- 用上Linux了。
- 用上 win7 了
- 用上了 Google Wave
- 用上了vAssistx
- typedef 与结构体、结构体指针类型
- typedef struct lnode(结构体及结构体指针)解惑
- typedef关键字与结构体、结构体指针的定义
- 函数指针用法 以及typedef 优化函数指针
- 【经典例题】抓交通肇事犯答案(结构体版)
- 再探指针与typedef
- Java WebSocket基于注解配置访问ServletAPI
- 算法设计:统计数字问题
- Java学习笔记(7)- Java系统类
- 操作系统的发展史(科普章节)
- Tomcat本地部署 Android内网加载
- 结构经典例题,用上了typedef 再进行指针优化
- foreach循环中remove引起java.util.ConcurrentModificationException异常
- mysql 事务
- 2卷积神经网络相关API详解-2.1TensorFlow之卷积操作API
- es6<一>
- Spark之CASE...WHEN...THEN...的两种实现方式
- js日常
- 流程进度显示
- Android自定义控件5----继承View通讯录索引功能之3在按下或和移动时显示更新提示字母