鱼C论坛

 找回密码
 立即注册
查看: 3997|回复: 6

求老鸟教写代码

[复制链接]
发表于 2012-12-23 15:10:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
.实验要求
1.掌握指针和数组作为函数参数时的使用方法。
2.能熟练使用指针操作各种数据对象。
3.掌握指针法和下标法对于数组元素的操作。

实验内容接受用户输入的整数n,随机生成n个0~100间的整数,使用动态数组存储所有元素,分别统计0~59,60~84,85~100之间的元素个数。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-23 20:24:20 | 显示全部楼层
  1. /*
  2. .实验要求
  3. 1.掌握指针和数组作为函数参数时的使用方法。
  4. 2.能熟练使用指针操作各种数据对象。
  5. 3.掌握指针法和下标法对于数组元素的操作。

  6. 实验内容接受用户输入的整数n,随机生成n个0~100间的整数,
  7. 使用动态数组存储所有元素,分别统计0~59,60~84,85~100之间的元素个数
  8. */

  9. #include <stdio.h>
  10. #include <stdlib.h>

  11. typedef struct MyData{
  12.         int* pInts;
  13.         int n;
  14.         int count0_59;
  15.         int count60_84;
  16.         int count85_100;
  17. }MyData;
  18. void Init( MyData* pMyData );
  19. void Deal( MyData* pMyData );
  20. void Print( MyData* pMyData );
  21. void Destroy( MyData* pMyData );



  22. void Init( MyData* pMyData ) {       
  23.         puts("Input n:");
  24.         scanf( "%d", &(pMyData->n) );
  25.         if( pMyData->n>0 ) {               
  26.                 pMyData -> pInts = (int*) malloc( pMyData->n * sizeof(int) );
  27.                 pMyData -> count0_59 = 0;
  28.                 pMyData -> count60_84 = 0;
  29.                 pMyData -> count85_100 = 0;       
  30.         }
  31. }

  32. void Deal( MyData* pMyData ) {
  33.         int i;
  34.         srand( time(NULL) );
  35.         for( i=0; i<pMyData->n; ++i ) {
  36.                 int temp = rand()%101;
  37.                 pMyData->pInts[i] = temp;
  38.                 if( 0<=temp && temp<=59 ) pMyData->count0_59++;
  39.                 else if( 60<=temp && temp<=84 ) pMyData->count60_84++;
  40.                 else if( 85<=temp && temp<=100 ) pMyData->count85_100++;
  41.         }
  42. }

  43. void Print( MyData* pMyData ) {
  44.         int i = 0;
  45.         printf("0~59:%d\n", pMyData->count0_59 );
  46.         printf("60~84:%d\n", pMyData->count60_84 );
  47.         printf("85~100:%d\n", pMyData->count85_100 );
  48.         puts("all data is:");
  49.         for(; i<pMyData->n;++i){
  50.                 printf("%d\n", pMyData->pInts[i]);
  51.         }
  52. }

  53. void Destroy( MyData* pMyData ) {
  54.         if( pMyData->n > 0 ) {
  55.                 free( pMyData->pInts );
  56.         }
  57. }

  58. int main(){
  59.         MyData my_data;
  60.         Init(&my_data);
  61.         Deal(&my_data);
  62.         Print(&my_data);
  63.         Destroy(&my_data);
  64.         return 0;
  65. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-24 21:53:54 | 显示全部楼层
来看看呵呵
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-24 23:58:54 | 显示全部楼层
楼主加油,鱼C加油!我们都看好你哦!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-30 17:19:49 | 显示全部楼层
楼主加油,鱼C加油!我们都看好你哦!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-1 01:13:42 | 显示全部楼层
哇, 板主发贴了。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-1 06:05:26 | 显示全部楼层
我只是来看编码的规范的~:lol
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-13 18:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表