【Cocoa and Object-c : Up and Running 笔记】03 Simple C for Object-c

简介: 一、基本概念       1、变量:变量是存放一块数据的容器。变量有类型,用于描述变量所包含的数据的种类。c中的变量内置基本类型有int、unsigned int、float、char、double和long。

一、基本概念

      1、变量:变量是存放一块数据的容器。变量有类型,用于描述变量所包含的数据的种类。c中的变量内置基本类型有int、unsigned int、float、char、double和long。

      2、常量:一旦赋值后就不能改变的量。c中用const关键字声明。

      3、枚举类型enum、类型定义typedef 。

      4、函数:声明函数、返回值、参数、调用函数。

      5、静态变量static、全局变量、局部变量。

      6、条件语句、循环语句。

      7、数组、多维数组、字符数组。

      8、指针、函数与指针、const指针、动态存储。

      9、结构体struct 、头文件


二、实例

     

#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"
#include "Song.h"

// global variables
int  yearCount = 12;
int* allYears;

// utility functions
void setupYears();
int randomSongYear();


main ( int inputCount, char* inputValues[] ) {
  
  // we don't want to count the program itself as a song name
  int songCount = (inputCount - 1);
  
  // tell the user how many song names they entered
  if ( songCount > 0 ) {
    printf ( "You entered %i song names \n", songCount );
    
  } else {
    printf ( "Didn't enter any song names. \n" );
    exit(1);
  }
  
  // fill in the global 'allYears' array
  setupYears();
  
  // seed the random number generator,
  // and get a random number
  sranddev();
  int randomNumber = rand() % 500;
  
  // create an "easy" dynamic array of all of the songs,
  // and a separate array of just the song lengths
  Song allSongs[ songCount ];
  int songLengths[ songCount ];

  int i;
  for (i = 0; i < songCount; i++) {
    
    // choose a random length in seconds (up to 500)
    // and a random song year
    int length = rand() % 500;
    int year = randomSongYear();
    
    // get the song name
    char* songName = inputValues[i+1];
    
    // create the Song instance using all of the other values
    allSongs[i] = createSong ( songName, length, year );
    
    // finally, copy the length to the 'songLengths' array
    songLengths[i] = length;
  }
  
  // display the total length of all songs
  int combinedLength  = sum (songLengths, songCount);
  printf ("The total length of all songs is %i seconds\n", combinedLength);
  
  // loop through the songs again and make an array of float values
  float songLengthsAsFloats[ songCount ];
  
  for (i = 0; i < songCount; i++) {
    songLengthsAsFloats[i] = songLengths[i];
  }
  
  // calculate the average length
  float averageLength = average (songLengthsAsFloats, songCount);
  printf ("The average length is: %.2f seconds\n", averageLength);
  
  // clean up the memory we malloc'd
  free ( allYears );
}

void setupYears () {
  
  // reserve memory for all of the year values. we can't use
  // the "easy" dynamic array because 'years' is a global variable
  allYears = malloc ( sizeof(int) * yearCount );
  
  // choose the starting year
  int oneYear = 2000;
  
  // loop through and fill in each value
  int i;
  for ( i = 0; i < yearCount; i++ ) {
    
    oneYear++;
    allYears[i] = oneYear;
  }
}

int randomSongYear () {
  
  // get a random value between 0 and (yearCount-1)
  int yearIndex = rand() % (yearCount-1);
  
  // now use the index to get a year out of the 'allYears' array
  int year = allYears[ yearIndex ];
  
  return year;
}

三、小结


相关文章
|
6天前
|
JavaScript 前端开发 Java
编程笔记 html5&css&js 073 JavaScript Object数据类型
编程笔记 html5&css&js 073 JavaScript Object数据类型
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用2
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用2
30 0
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用1
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用1
24 0
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用1
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十一天-Es6-object.defineProperty监听属性的访问和设置1
前端学习笔记202306学习笔记第四十一天-Es6-object.defineProperty监听属性的访问和设置1
27 0
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用4深度拷贝
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用4深度拷贝
25 0
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用3
前端学习笔记202306学习笔记第四十天-Es6-object.assign的使用3
24 0
|
9月前
|
前端开发
前端学习笔记202306学习笔记第四十天-Es6-object.assign的注意细节
前端学习笔记202306学习笔记第四十天-Es6-object.assign的注意细节
30 0
|
7月前
|
Java
【面试题精讲】Object类的常见方法有哪些?
【面试题精讲】Object类的常见方法有哪些?
|
8月前
|
Java API 开发工具
Java之API详解之Object类的详细解析(下)
Java之API详解之Object类的详细解析(下)
40 0
|
6天前
|
C#
c# 所有类的最终基类:Object
c# 所有类的最终基类:Object
7 0