[TOC]
##### 二维数组的动态分配
```
/**
* 二位数组的动态分配。
* function : malloc()
**/
#include <stdio.h>
#include <malloc.h>
int main(void) {
int n;
int i,j;
int **a;
scanf("%d",&n);
a = (int **)malloc(sizeof(int *) * n);
for(i=0;i<n;i++) {
a[i] = (int *)malloc(sizeof(int) * n);
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
```
##### switch,case语句
```
#include <stdio.h>
int main(void) {
int b = 1;
switch(b) {
case 1:
printf("this is 1\n");
break;
case 2:
printf("this is 2\n");
break;
case 3:
printf("this is 3\n");
break;
case 4:
printf("this is 4\n");
break;
default:
printf("this is other\n");
}
return 0;
}
```
##### 课程设计代码实例
```
#include <stdio.h>
#include <malloc.h>
/**
* @author:QD
* @time:2016/11/21
* @func:
*
*/
int choose = -1;
struct NodeN{
int sn;
int score;
struct NodeN *next;
};
typedef struct NodeN *Node;
//function define
void myinit();
void scoreEnter(Node project[]);
void insert(int projectNumber,Node project[],Node tmp);
Node locate(Node L,int score);
void myinit() {
printf("--------------------------------------------------------------\n");
printf("----------------------welcome my system!!!--------------------\n");
printf("-------(1)-Enter the score and school number!-----------------\n");
printf("-------(2)-enter the score and school number!-----------------\n");
printf("-------(3)-enter the score and school number!-----------------\n");
printf("-------(4)-enter the score and school number!-----------------\n");
printf("-------(5)-enter the score and school number!-----------------\n");
printf("-------(6)-Quit!----------------------------------------------\n");
printf("--------------------------------------------------------------\n");
printf("Please enter the number you want to choose:\n");
//.....
scanf("%d",&choose);
return ;
}
void scoreEnter(Node project[]) {
//init node
int projectNumber;
Node tmp = (struct NodeN *)malloc(sizeof(struct NodeN));
//scanf("please enter the project number:%d\n",&projectNumber);
//scanf("please enter the number:%d\n",&(tmp->sn));
//scanf("please enter the score:%d\n",&(tmp->score));
printf("Please enter the project number:");
scanf("%d",&projectNumber);
printf("Please enter the school number:");
scanf("%d",&(tmp->sn));
printf("please enter the score:");
scanf("%d",&(tmp->score));
insert(projectNumber,project,tmp);
}
void insert(int projectNumber,Node project[],Node tmp) {
//project[projectNumber]
Node L = project[projectNumber];
if(L->next == NULL) { // empty
tmp->next = NULL;
L->next = tmp;
} else {
Node p = locate(L,L->score);
tmp->next = p->next;
p->next = tmp;
}
}
Node locate(Node L,int score) {
Node tmp = L;
while(tmp->next != NULL) {
if(tmp->next->score > score) {
return tmp;
}
tmp = tmp->next;
}
return tmp;
}
void print(Node L) {
Node tmp = L->next;
while(tmp!=NULL) {
printf("school number is %d\t",tmp->sn);
printf("score is %d",tmp->score);
printf("\n");
tmp = tmp->next;
}
}
int main(void) {
//init
Node project[40];
int i;
for(i=0;i<40;i++) {
Node tmp = (struct NodeN *)malloc(sizeof(struct NodeN));
tmp->sn = -1;
tmp->score = -1;
tmp->next = NULL; //empty linked list
project[i] = tmp;
}
//user begin
for(;;) {
myinit();
switch(choose) {
case 1:
scoreEnter(project);
break;
//...
default:
printf("bye!\n");
return 0;
}
}
//test
//print(project[2]);
return 0;
}
```