目前共有24篇帖子。
今天上课写的C程序
1樓 180.84.33.* 2015-11-27 10:03
#include <stdio.h>
//#include <stdlib.h>

/*int *fun()
{
    //int *p = (int *)malloc(sizeof(int));
    int a;
    int *p = &a;
    *p = 12;
    return p;
}*/

void invert(int *array, int n)
{
    int *i, *j, temp;
    i = array;
    j = array + n - 1;
    while (i < j)
    {
        temp = *i;
        *i = *j;
        *j = temp;
        i++;
        j--;
    }
}

void main()
{
    /*int a[] = {15, -27, 63, 49, 115, 663, 774};
    int *const p = &a[2];
    printf("%d\n", (*p)++);
    printf("%d\n", *p);*/

/*    int *p2 = fun();
    int *p3;
    *p2 += 2;
    p3 = fun();
    printf("%d\n", *p3);
    printf("%d\n", *p2);*/
    //free(p2);
    //free(p3);

    int a[] = {2, 4, 7, 3, 6, 9, 11, 14, 8};
    int len = sizeof(a) / sizeof(int);
    int i;
    for (i = 0; i < len; i++)
        printf("%-3d", *(a + i));
    printf("\n");

    invert(a, 4);
    for (i = 0; i < len; i++)
        printf("%-3d", *(a + i));
    printf("\n");
}

2樓 180.84.33.* 2015-11-27 10:04
学校机房内的电脑虽然默认情况下不能上网,但是把网卡的IP地址改为自动获取(DNS不需要改为自动获取),大概半分钟后就能上网了。
3樓 180.84.33.* 2015-11-27 10:05
4樓 巨大八爪鱼 2015-11-27 10:24
当函数要返回一个指针的时候,这个指针不能指向该函数内部定义的局部变量,但可以指向静态变量或者malloc函数分配的空间。
如果函数真的返回了指向局部变量的指针的话,那么在主函数中就不得对指向的空间的值进行任何写操作,只能读取其值,否则会出错。
5樓 202.115.90.* 2015-11-27 16:26
#include <stdio.h>
void main()
{
int x,y,z;
for(x=0;x<=100;x++)
for(y=0;y<=100;y++)
{
z=100-x-y;
if(x*5+y*3+z/3==100)
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
6樓 202.115.90.* 2015-11-27 16:46
#include <stdio.h>

void main()
{
int a = 10;
printf("%d\n", a = a + 2); //12
printf("%d\n", a += 2); //14
}
7樓 202.115.90.* 2015-11-27 16:50
#include <stdio.h>

void main()
{
char str[] = "abcdefg";
char *pStr = str;
*pStr++ = 'R';
printf("%s\n", str); //Rbcdefg
printf("%s\n", pStr); //bcdefg
}
8樓 202.115.90.* 2015-11-27 17:24
// Page 211
#include <stdio.h>
#include <string.h>

void insert(char *s1, const char *s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int pos = -1; // when not found
int i;

/* Find the position */
for (i = 0; i < len1; i++)
{
if (*(s1 + i) == *s2)
{
pos = i; // found
break;
}
}

/* Move */
if (pos != -1)
{
for (i = len1; i >= pos - 1; i--)
{
// At first when i == len1, the character is '\0'
// and then '\0' will be moved to the end
*(s1 + i + len2 - 1) = *(s1 + i);
}
}

/* Copy chars from s2 to s1 */
if (pos == -1)
{
pos = len1;
*(s1 + len1) = *s2; // copy the first character of s2
*(s1 + len1 + len2) = '\0';
}
// copy the rest
for (i = 1; i < len2; i++)
*(s1 + pos + i) = *(s2 + i);

// abcdefghijklmn
// f123
// abcdef ghijklmn
// abcdef123ghijklmn

// abcdef
// d45
// abcd ef
// abcd45ef

// abcdef
// 45
// abcdef45
}

void main()
{
char s1[50] = "abcdef";
char s2[50] = "d45";
insert(s1, s2);
printf("%s\n", s1);

strcpy(s1, "abcdef");
strcpy(s2, "45");
insert(s1, s2);
printf("%s\n", s1);

strcpy(s1, "abcdefghijklmn");
strcpy(s2, "f123");
insert(s1, s2);
printf("%s\n", s1);
}
9樓 202.115.90.* 2015-11-27 17:26

回复:8楼

这是我完全自己写的版本,没有看书上的。

10樓 202.115.90.* 2015-11-27 17:29
不得不说书上211页那个版本的程序比我写的简洁得多。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
 
 
©2010-2025 Arslanbar [手機版] [桌面版]
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。