//
// main.c
// lesson4
//
// Created by Vision on 14-8-30.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[])
{
//1.定义数组:
// int array[5] = { 2, 7, 4, 3, 6};
// array[1] = 213;
// for (int i = 0; i < 5; i++) {
// printf("%d\n", array[i]);
// }
// 结果:
// 2
// 213
// 4
// 3
// 6
//2.sizeof()
// int array[5] = { 2, 7, 4, 3, 6};
// printf("%lu\n", sizeof(double));
// printf("%lu\n", sizeof(long));
// printf("%lu\n", sizeof(int));
// printf("%lu\n", sizeof(array));
// printf("%lu\n", sizeof(unsigned int));
// 结果:
// 8
// 8
// 4
// 20
// 4
//3.sizeof()的用法:
// int array[5] = { 2, 7, 4, 3, 6};
// int count = sizeof(array) / 4;
// int count2 = sizeof(array) / sizeof(int);
// int count3 = sizeof(array) / sizeof(array[0]);
// printf("%d %d %d \n", count, count2, count3);
// for (int i = 0; i < count3; i++) {
// printf("[%2d] : %d\n", i, array[i]);
// }
// 结果:
// 5 5 5
// [ 0] : 2
// [ 1] : 7
// [ 2] : 4
// [ 3] : 3
// [ 4] : 6
//4.数组各数之和与数组和数组的和:
// int array[20] = {0};
//
// int count = sizeof(array) / sizeof(array[0]);
// for (int i = 0; i < count; i++) {
// array[i] = arc4random() % (70 - 30 + 1) +30;
// printf("[%2d] = %d \n", i, array[i]);
// }
// int sum = 0;
// for (int i = 0; i < count; i++) {
// sum += array[i];
// }
// printf("%d\n",sum);
// int array2[count];
// for (int i = 0; i < count; i++) {
// array2[i] = array[i];
// printf("[%2d] = %d \n", i, array2[i]);
//
// }
// int array3[count];
// for (int i = 0; i < count; i++) {
// array3[i] = array2[i] + array[i];
// printf("[%2d] = %d \n", i, array3[i]);
// }
// 结果:
// [ 0] = 50
// [ 1] = 39
// [ 2] = 41
// [ 3] = 50
// [ 4] = 61
// [ 5] = 58
// [ 6] = 35
// [ 7] = 63
// [ 8] = 59
// [ 9] = 66
// [10] = 57
// [11] = 53
// [12] = 45
// [13] = 55
// [14] = 35
// [15] = 54
// [16] = 44
// [17] = 65
// [18] = 70
// [19] = 66
// 1066
// [ 0] = 50
// [ 1] = 39
// [ 2] = 41
// [ 3] = 50
// [ 4] = 61
// [ 5] = 58
// [ 6] = 35
// [ 7] = 63
// [ 8] = 59
// [ 9] = 66
// [10] = 57
// [11] = 53
// [12] = 45
// [13] = 55
// [14] = 35
// [15] = 54
// [16] = 44
// [17] = 65
// [18] = 70
// [19] = 66
// [ 0] = 100
// [ 1] = 78
// [ 2] = 82
// [ 3] = 100
// [ 4] = 122
// [ 5] = 116
// [ 6] = 70
// [ 7] = 126
// [ 8] = 118
// [ 9] = 132
// [10] = 114
// [11] = 106
// [12] = 90
// [13] = 110
// [14] = 70
// [15] = 108
// [16] = 88
// [17] = 130
// [18] = 140
// [19] = 132
//5.数组和数组相加放到一个新的数组中:
// int array1[10];
// int array2[10];
// int array3[10];
// int count = sizeof(array1) / sizeof(array1[0]);
// for (int i = 0; i < count; i++) {
// array1[i] = arc4random() % (40 - 20 + 1) + 20;
// array2[i] = arc4random() % (40 - 20 + 1) + 20;
// array3[i] = array2[i] + array1[i];
// printf("%d\n",array3[i]);
//
// }
// 结果:
// 61
// 66
// 62
// 56
// 63
// 76
// 52
// 63
// 46
// 70
//6.冒泡排序
// int array[] = { 3, 2, 6, 9, 8, 5, 7, 1, 4};
// int count = sizeof(array) / sizeof(array[0]);
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - i - 1; j++) {
// if (array[j] > array[j + 1]) {
// int temp = 0;
// temp = array[j];
// array[j] = array[j + 1];
// array[j + 1] = temp;
//
// }
// }
//
// }
// for (int i = 0; i < count; i++) {
// printf("%02d : %d\n", i, array[i]);
// }
// 优化冒泡排序
// int array[] = { 3, 2, 6, 9, 8, 5, 7, 1, 4};
// int count = sizeof(array) / sizeof(array[0]);
// int flag = 1;//1表示无序 0表示有序
// for (int i = 0; i < count - 1 && 1 == flag; i++) {
// flag = 0;
// for (int j = 0; j < count - i - 1; j++) {
// if (array[j] > array[j + 1]) {
// int temp = 0;
// temp = array[j];
// array[j] = array[j + 1];
// array[j + 1] = temp;
// flag = 1;//发生数据交换,置无序状态
//
// }
// }
// }
// for (int i = 0; i < count; i++) {
// printf("%d %d\n", i, array[i]);
// }
// 结果:
// 00 : 1
// 01 : 2
// 02 : 3
// 03 : 4
// 04 : 5
// 05 : 6
// 06 : 7
// 07 : 8
// 08 : 9
//7:选择排序 (1.挑最小 2.找位置 3.换位置)
// int array[] = { 3, 2, 6, 9, 8, 5, 7, 1, 4};
// int count = sizeof(array) / sizeof(array[0]);
// for (int i = 0; i < count - 1; i++) {
// int minIndex = i;
// for (int j = minIndex + 1; j < count; j++) {
// if (array[minIndex] > array[j]) {
// minIndex = j;
// }
// }
// int temp = array[ i ];
// array[ i ] = array[minIndex];
// array[minIndex] = temp;
// printf("%d",minIndex);
// }
// for (int i = 0; i < count; i++) {
// printf("%d : %d\n", i, array[i]);
// }
// 结果:
// 0 : 1
// 1 : 2
// 2 : 3
// 3 : 4
// 4 : 5
// 5 : 6
// 6 : 7
// 7 : 8
// 8 : 9
//8.折半查找
//
// int n = 0;
// scanf("%d", &n);
// int count = sizeof(array) / sizeof(array[0]);
// int start = 0;
// int end = count - 1;
// int mid = 0;
// while (start <= end) {
// mid = (start + end) / 2;
// if (array[mid] > n)
// {
// end = mid - 1;
//
// } else if (array[mid] < n){
// start = mid + 1;
//
// } else {
//
// break;
// }
// }
// if (start <= end) {
// printf("[%2d] : %d]\n", mid, array[mid]);
// } else {
// printf("not found");
// }
// 结果:
// 5
// [ 4] : 5]
//9.插入排序
// int array[] = { 3, 2, 6, 9, 8, 5, 7, 1, 4};
// int count = sizeof(array) / sizeof(array[0]);
// for (int i = 1; i < count; i++) {
// int j = i;
// int temp = array[j];
// while ((j > 0) && (temp < array[j - 1])) {
// array[j] = array[j - 1];
// j--;
// }
// array[j] = temp;
// }
// for (int i = 0; i < count; i++) {
// printf("[%2d] %d\n", i, array[i]);
// }
//结果:
// [ 0] 1
// [ 1] 2
// [ 2] 3
// [ 3] 4
// [ 4] 5
// [ 5] 6
// [ 6] 7
// [ 7] 8
// [ 8] 9
//10.字符串数组
//关于 数组 长度 拷贝
//输出字符数组
// char string[10] = {'i', 'p', 'h', 'o' ,'n' ,'e'};
// int count = sizeof(string) / sizeof(string[0]);
// for (int i = 0; i < count; i++) {
// printf("%c",string[i]);
// }
// printf("\n");
//输出字符串
// char string1[10] = "iphone";
// int i = 0;
// while (string1[i] != '\0') {
// printf("%c",string1[i]);
// i++;
// }
// printf("\n");
// printf("%c\n",i);//'\0'之后没有数据
// string1[3] = '\0';//'\0'字符串结束符
// printf("%s\n",string1);
// 拷贝字符串
// char string3[] = "android";
// char string4[10] = {'i', 'p', 'h', 'o' ,'n' ,'e' ,'x','x','x'};
// int i = 0;
// while (string3[i] != '\0') {
// string4[i] =string3[i];
// i++;
// }
// string4[i] = '\0';
// printf("%s\n", string4);
//
// i = 0;
// while ((string4[i] = string3[i]) != '\0') {
// i++;
// }
// printf("%s\n",string4);
// 装逼写法:
// i = 0;
// while ((string4[i] = string3[i])) {
// i++;
// }
// printf("%s\n",string4);
//
//字符串的连接
// char string1[30] = "iphone";
// char string2[] = "android";
// int i = 0;
// int j = 0;
// while (string1[i]) {
// i++;
// }
// while ((string1[i] = string2[j])) {
// i++;
// j++;
// }
// printf("%s\n",string1);
//字符串的比较
// char string1[] = "abcd";
// char string2[] = "abcd";
// int result = 0,i = 0;
// while (0 == (result = string1[i] - string2[i] ) && string1[i]) {
// if ( '\0' == string1[i]) {
// break;
// }
// i++;
//
// }
// //逼格很高的写法
// //1.while (0 == (result = string1[i] - string2[i] ) && string1[i]) { i++;}
// //2.while (0 == (result = string1[i] - string2[i] ) && string1[i++]) { }
// printf("result = %d\n", result);
//11.用系统函数实现上面功能
// char string[10] = "iphone";
// puts(string);
// printf("length = %lu\n",strlen(string));//计算字符串长度
//
// char dest[30] = {'\0'};
// strcpy(dest, string);//字符串拷贝
// puts(string);
//
// char string1[] = "android";
// strcat(dest, string1);//字符串拼接
// puts(string);
//字符串比较
// char string1[] = "abgd";
// char string2[] = "abcd";
// printf("result = %d\n", strcmp(string1, string2));
//12.二维数组
// int array[2][3] = { {7, 6, 3},{ 2, 8, 5}};
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 3; j++) {
// printf("%d ", array[i][j]);
// }
// }
//结果:
// 7 6 3 2 8 5
//13.用一维数组 模拟二维数组遍历
// int array[6] = { 7, 6, 3, 2, 8};
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 3; j++) {
// printf("%d ", array[i * 3 + j]);
// }
// }
//14.将⼀一个⼆二维数组的⾏行和列交换,存储到另外⼀一个数组中去。
// int array[2][3] = { 7, 6, 3, 2, 8,9};
// int array2[3][2] = {0};
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 2; j++) {
// array2[i][j] = array[j ][i];
// printf("%d",array2[i][j]);
// }
// printf("\n");
// }
//15.有⼀一个3⾏行4列的⼆二维数组,要求编程找出最⼤大元素,并输出所在 的⾏行和列。
// int array[3][4] = {0};
// int count1 = sizeof(array) / sizeof(array[0]);
// int count2 = sizeof(array[0]) / sizeof(array[0][0]);
// int max = 0;
// int i = 0, j = 0;
// int maxi = 0, maxj = 0;
// for ( i = 0; i < count1; i++) {
// for ( j = 0; j < count2; j++) {
// array[i][j] = arc4random() % ( 70 - 30 + 1 )+ 30;
// printf("%d ",array[i][j]);
//
//
// }
// printf("\n");
// }
// for ( i = 0; i < count1; i++) {
// for ( j = 0; j < count2; j++) {
// if (max < array[i][j]) {
// max = array[i][j];
// maxi = i;
// maxj = j;
// }
// }
// }
// printf("%d %d %d", max , maxi , maxj);
// printf("\n");
//结果:
// 32 69 40 42
// 43 46 58 57
// 68 61 40 38
// 69 0 1
//16.字符串数组
//输出 字符数组
// char str[3][10] = {"iphone","android","win8"};
// int count = sizeof(str) / sizeof(str[0]);
// for (int i = 0; i < count; i++) {
// printf("%s\n",str[i]);
// }
//结果:
// iphone
// android
// win8
// char str[3][10] = {"iphone","android","win8"};
// int count1 = sizeof(str) / sizeof(str[0]);
// int count2 = sizeof(str[0]) / sizeof(str[0][0]);
// for (int i = 0; i < count1; i++) {
// for (int j = 0; j < count2 ; j++) {
// printf("%c",str[i][j]);
// }
// printf("\n");
// }
// 17 .输出 最长的字符串的长度
//方法1
// char names[][20] = {"wei","jiang","sheshou","lee"};
// int count1 = sizeof(names) / sizeof(names[0]);
// int count2 = sizeof(names[0]) / sizeof(names[0][0]);
// int maxn = 0;
// for (int i = 0; i < count1; i++) {
// int n = 0;
// for (int j = 0; j < count2 ; j++) {
// // printf("%u",names[i][j]);
// n++;
// if (names[i][j] == '\0') {
// // printf("%d\n", n - 1);
// if (n > maxn) {
// maxn = n;
// }
// break;
// }
// }
// }
//
// printf("%d\n",maxn - 1);
// 结果:
// 7
// 方法2
// char names[][20] = {"wei","jiang","sheshou","lee"};
// unsigned long maxLength = 0;
// int count = sizeof(names) / sizeof(names[0]);
// for (int i = 0; i < count; i++) {
// if (maxLength < strlen(names[i])) {
// maxLength = strlen(names[i]);
// }
// }
// printf("maxLength = %lu\n",maxLength);
//
// 结果:
// maxLength = 7
//18.对于字符串排序
// char names[][20] = {"wei","jiang","sheshou","lee"};
// int count = sizeof(names) / sizeof(names[0]);
// int flag = 1;
// for (int i = 0; i < count - 1 && 1 == flag; i++) {
// flag = 0;
// for (int j = 0; j < count - i - 1; j++) {
// if (strcmp(names[j], names[j+1]) > 0) {
// char temp[20] = {'\0'};
// strcpy(temp, names[j] );
// strcpy(names[j], names[j + 1]);
// strcpy(names[j + 1], temp);
// flag = 1;
//
// }
// }
// }
// for (int i = 0; i < count; i++) {
// printf("%d %s\n", i, names[i]);
// }
//19.多维数组
// int array[2][4][3] = {0};
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 4; j++) {
// for (int k = 0; k < 3; k++) {
// array[i][j][k] = arc4random() % (40 - 20 + 1) + 20;
// printf("%2d ", array[i][j][k]);
//
// }
// printf("\n");
// }
// printf("\n");
// }
return 0;
}
C语言总结4
最新推荐文章于 2024-03-05 17:00:00 发布