目前共有97篇帖子。 內容轉換:不轉換▼
 
點擊 回復
1714 96
【期末複習】C語言期末複習
一派護法 十九級
65樓 發表于:2016-1-16 16:50
为了降低编译失败的次数,写好代码后我必须仔细检查代码才编译。一定要争取一次编译通过。
一派護法 十九級
66樓 發表于:2016-1-16 18:58
#include <stdio.h>

int decompose(int num, int m[])
{
    int i, j, count = 0;
    for (i = 2; i <= num; i++) // 寻找2~num之间的质数
    {
        // 判断i是不是质数
        for (j = 2; j < i; j++)
        {
             if (i % j == 0)
                 break;
        }
        if (i == j)
        {
            // 如果是质数
            // 判断这个数是否能被这个质数整除
            // 把原本的if改为while就能判断有多少个当前质数相乘
            while (num % i == 0)
            {
                m[count++] = i;
                num /= i;
            }
        }
    }
    return count;
}

void main()
{
    int i, n, count, a[100];
    scanf("%d", &n);
    count = decompose(n, a);
    printf("%d = ", n);
    for (i = 0; i < count; i++)
    {
        printf("%d", a[i]);
        if (i + 1 < count)
            putchar('*');
        else
            putchar('\n');
    }
}
一派護法 十九級
67樓 發表于:2016-1-16 18:58
楼上是第五章的最后一题——【15】
一派護法 十九級
68樓 發表于:2016-1-16 19:00
由于15题比较难,所以再做一遍。
一派護法 十九級
69樓 發表于:2016-1-16 19:32
#include <stdio.h>

int decompose(int n, int q[])
{
    int i, count = 0;
    for (i = 2; i <= n; i++)
    {
        while (n != i && n % i == 0)
        {
            q[count++] = i;
            n /= i;
        }
    }
    q[count++] = n;
    return count;
}

void main()
{
    int q[255], n, m, i;
    printf("Please input a positive integer: ");
    scanf("%d", &n);
    m = decompose(n, q);
    printf("m = %d\n", m);
    printf("%d = ", n);
    for (i = 0; i < m - 1; i++)
        printf("%d*", q[i]);
    printf("%d\n", q[m - 1]);
}
这是经过标准答案修改过的程序。
因为在考虑2的时候已经把2的所有倍数除尽了,到了最后不能再被2整除的时候才开始考虑3 。所以4根本就不可能出现,因此也就无需一个一个地去找质数了。
一派護法 十九級
70樓 發表于:2016-1-16 19:33
至于为什么要在while里面多加一句n!=i,主要是为了确保当输入的n为质数时,返回的数组q至少有一个元素,不至于是空数组。
一派護法 十九級
71樓 發表于:2016-1-16 19:34
因此,这个程序还可以这样改:
int decompose(int n, int q[])
{
    int i, count = 0;
    for (i = 2; i <= n; i++)
    {
        while (n % i == 0)
        {
            q[count++] = i;
            n /= i;
        }
    }
    if (count == 0)
        q[count++] = n;
    return count;
}
一派護法 十九級
72樓 發表于:2016-1-16 19:44
第五章已复习完毕。
接下来先复习简单一点的:第九章——文件。
一派護法 十九級
73樓 發表于:2016-1-16 19:53
【9-1】
#include <stdio.h>
#include <stdlib.h>

void fun(int nums[])
{
    int count = 0;
    int i;
    for (i = 0; count < 10; i++)
    {
        if (i % 2 == 1 && i % 3 == 1 && i % 5 == 1)
            nums[count++] = i;
    }
}

void main()
{
    int nums[10];
    int i;
    FILE *fp;
    if ((fp = fopen("tmp.txt", "w")) == NULL)
    {
        printf("文件打不开。\n");
        exit(0);
    }
   
    fun(nums);
    for (i = 0; i < 10; i++)
    {
        printf("%d ", nums[i]);
        fprintf(fp, "%d ", nums[i]);
    }
    printf("\n");
    fclose(fp);
}
注意fprintf不要忘了第一个参数fp。
一派護法 十九級
74樓 發表于:2016-1-16 19:58
【9-2】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    char str[100];
    int i;
    FILE *fp;
    if ((fp = fopen("tmp2.txt", "w")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    printf("Please enter a string:\n");
    gets(str);
    for (i = 0; str[i] != '\0'; i++)
    {
        printf("%c(%d)", str[i], str[i]);
        fprintf(fp, "%c(%d)", str[i], str[i]);
    }
    printf("\n");
    fclose(fp);
}
一派護法 十九級
75樓 發表于:2016-1-16 20:15
【9-3】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int num, pn = 0, nn = 0;
    FILE *fp;
    if ((fp = fopen("test.txt", "r")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    while (!feof(fp))
    {
        fscanf(fp, "%d", &num);
        if (num > 0)
            pn++;
        else if (num < 0)
            nn++;
    }
    printf("positive=%d\nnegative=%d\n", pn, nn);
}
一派護法 十九級
76樓 發表于:2016-1-16 20:15
刚才Arslanbar突然不能访问了。。。。
一派護法 十九級
77樓 發表于:2016-1-16 20:33
【9-4】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fp1, *fp2;
    char ch;
    if ((fp1 = fopen("file1.txt", "r")) == NULL || (fp2 = fopen("file2.txt", "w")) == NULL)
    {
        printf("无法打开文件\n");
        exit(0);
    }
   
    while (ch = fgetc(fp1), !feof(fp1))
    {
        putchar(ch);
        fputc(ch, fp2);
    }
    fclose(fp1);
    fclose(fp2);
}
一派護法 十九級
78樓 發表于:2016-1-16 20:37
【9-5】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fp;
    char ch;
    int a = 0, b = 0;
    if ((fp = fopen("3.c", "r")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
   
    while (ch = fgetc(fp), !feof(fp))
    {
        if (ch == '{')
            a++;
        else if (ch == '}')
            b++;
    }
    if (a == b)
        printf("OK!\n");
    else
        printf("ERROR!\n");
}
一派護法 十九級
79樓 發表于:2016-1-16 20:46
【9-6】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    char filename[100];
    FILE *fp;
    int count[26] = {0}; // 数组一定要初始化后再使用!
    char ch;
    int i;
    gets(filename);
    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    while (ch = fgetc(fp), !feof(fp))
    {
        if (ch >= 'A' && ch <= 'Z')
            count[ch - 'A']++;
        else if (ch >= 'a' && ch <= 'z')
            count[ch - 'a']++;
    }
    for (i = 0; i < 26; i++)
    {
        printf("%c(%c)=%4d   ", 'A' + i, 'a' + i, count[i]);
        if ((i + 1) % 5 == 0)
            putchar('\n');
    }
    putchar('\n');
    fclose(fp);
}
一派護法 十九級
80樓 發表于:2016-1-16 21:21
【9-7】
输入若干个字符串排序。。。。这个挺复杂的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    FILE *fp;
    int i, j, n, min_j;
    char *temp;
    char *spaceHead;
    char **strlist;
   
    if ((fp = fopen("string.txt", "w")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
   
    printf("Enter number:\n");
    scanf("%d", &n);
    strlist = (char **)calloc(n, sizeof(char *));
    spaceHead = (char *)calloc(n, 100 * sizeof(char));
    printf("Enter strings:\n");
    gets(spaceHead); // remove the \n that follows the just input number
    for (i = 0; i < n; i++)
    {
        strlist[i] = spaceHead + 100 * i;
        gets(strlist[i]);
    }
   
    for (i = 0; i < n - 1; i++)
    {
        min_j = i + 1;
        for (j = i + 2; j < n; j++)
        {
            if (strcmp(strlist[j], strlist[min_j]) < 0)
                min_j = j;
        }
        if (strcmp(strlist[i], strlist[min_j]) > 0)
        {
            temp = strlist[i];
            strlist[i] = strlist[min_j];
            strlist[min_j] = temp;
        }
    }
   
    printf("\nThe new sequence:\n");
    for (i = 0; i < n; i++)
    {
        puts(strlist[i]);
        fputs(strlist[i], fp);
    }
   
    free(spaceHead);
    free(strlist);
    fclose(fp);
}

运行结果:
Enter number:
8
Enter strings:
Blue
Pink
Yellow
Green
Red
Black
White
Purple

The new sequence:
Black
Blue
Green
Pink
Purple
Red
White
Yellow

--------------------------------
Process exited after 41.32 seconds with return value 0
Press any key to continue . . .
一派護法 十九級
81樓 發表于:2016-1-16 21:24
注意,虽然puts可以在屏幕上输出换行符,但是fputs不行。。
所以要单独加一个fputc:
fputs(strlist[i], fp);
fputc('\n', fp);
一派護法 十九級
82樓 發表于:2016-1-16 21:35
【9-8】
#include <stdio.h>
#include <stdlib.h>

#define N 10

struct student
{
    int id;
    char name[50];
    int age;
    char address[50];
} stus[N];

void save()
{
    FILE *fp;
    int i;
    if ((fp = fopen("stu.dat", "w")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    for (i = 0; i < N; i++)
        fwrite(&stus[i], sizeof(stus[i]), 1, fp);
    fclose(fp);
}

void main()
{
    int i;
    printf("Please enter data of 10 students:\n");
    for (i = 0; i < N; i++)
    {
        scanf("%d%s%d%s", &stus[i].id, stus[i].name, &stus[i].age, stus[i].address);
    }
    save();
}
一派護法 十九級
83樓 發表于:2016-1-16 21:46
【9-9】
#include <stdio.h>

struct student
{
    int id;
    char name[50];
    int age;
    char address[50];
} stu;

void main()
{
    int i;
    FILE *fp;
    if ((fp = fopen("stu.dat", "r")) == NULL)
    {
        printf("文件打不开!\n");
        exit(0);
    }
    for (i = 1; i <= 9; i += 2)
    {
        fseek(fp, (i - 1) * sizeof(struct student), SEEK_SET);
        fread(&stu, sizeof(struct student), 1, fp);
        printf("%d %10s %d %s\n", stu.id, stu.name, stu.age, stu.address);
    }
    fclose(fp);
}
运行结果:
201101       Lily 19 room01
201103      Sunny 21 room03
201105       John 19 room05
201107      Sunny 20 room06
201109        Joe 20 room07

--------------------------------
Process exited after 0.006697 seconds with return value 0
Press any key to continue . . .
一派護法 十九級
84樓 發表于:2016-1-16 21:47
201101 Lily       19 room01
201103 Sunny      21 room03
201105 John       19 room05
201107 Sunny      20 room06
201109 Joe        20 room07

--------------------------------
Process exited after 0.004761 seconds with return value 0
Press any key to continue . . .
输出的时候注意要写%-10s才对!
一派護法 十九級
85樓 發表于:2016-1-16 21:53
【9-10】
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int n, sum = 0;
    FILE *fp;
    if ((fp = fopen("int.txt", "r")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    while (!feof(fp))
    {
        fscanf(fp, "%d", &n);
        sum += n;
    }
    fclose(fp);
    if ((fp = fopen("int.txt", "a")) == NULL)
    {
        printf("文件打不开\n");
        exit(0);
    }
    fprintf(fp, " %d", sum);
    fclose(fp);
}
一派護法 十九級
86樓 發表于:2016-1-16 21:53
第九章复习完毕!
一派護法 十九級
87樓 發表于:2016-1-17 14:57
【7-1】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *insert(char *s1, char *s2, int n)
{
    int len1, len2;
    int i;
    char *s3;
   
    len1 = strlen(s1);
    len2 = strlen(s2);
    s3 = (char *)calloc(len1 + len2 + 1, sizeof(char));
    for (i = len1; i >= n; i--) // 这里i--不要写错了
    {
        s3[i + len2] = s1[i];
    }
   
    for (i = 0; i < len2; i++)
    {
        s3[i + n] = s2[i];
    }
   
    for (i = 0; i < n; i++)
        s3[i] = s1[i];
    return s3;
}

void main()
{
    char *s1 = "abcdef";
    char *s2 = "ghi";
    char *s3 = insert(s1, s2, 5); // 这里不要漏写第三个参数5
    puts(s3);
    free(s3);
}
一派護法 十九級
88樓 發表于:2016-1-17 15:21
【7-2】
这个没有用指针,所以不符合题目要求。
#include <stdio.h>
#include <stdlib.h>

#define N 10

void shift(int a[], int m)
{
    int *b, i, j;
    b = (int *)calloc(m, sizeof(int));
    for (i = 0; i < m; i++)
        b[i] = a[N - 4 + i];
   
    for (i = N - m - 1; i >= 0; i--)
        a[m + i] = a[i];
    for (i = 0; i < m; i++)
        a[i] = b[i];
    free(b);
}
void main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int i;
    shift(a, 8);
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);
    printf("\n");
}
一派護法 十九級
89樓 發表于:2016-1-17 15:25
不过要用指针很简单,把所有的a[i]换成*(a+i)就行了
一派護法 十九級
90樓 發表于:2016-1-17 15:32
回復88樓 @巨大八爪鱼 的內容:
【7-2】
这个没有用指针,所以不符合题目要求。
#include <stdio.h>
#include <stdlib.h>
#def...
这里N - 4 + i应该改成N - m + i
一派護法 十九級
91樓 發表于:2016-1-17 15:35
改成指针:
#include <stdio.h>
#include <stdlib.h>

#define N 10

void shift(int a[], int m)
{
    int *b, i, j;
    if (m >= N)
        return;
    b = (int *)calloc(m, sizeof(int));
    for (i = 0; i < m; i++)
        *(b + i) = *(a + N - m + i);
   
    for (i = N - m - 1; i >= 0; i--)
        *(a + m + i) = *(a + i);
    for (i = 0; i < m; i++)
        *(a + i) = *(b + i);
    free(b);
}
void main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int i;
    shift(a, 6);
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);
    printf("\n");
}
一派護法 十九級
92樓 發表于:2016-1-17 15:40
这是参考答案:
#include <stdio.h>

void print(int *a)
{
    int i;
    for (i = 0; i < 10; i++)
        printf("%d ", a[i]);
    printf("\n");
}

void shift(int *p, int m)
{
    int i, j, t;
    for (i = 0; i < m; i++)
    {
        t = p[9];
        for (j = 8; j >= 0; j--)
            p[j + 1] = p[j];
        p[0] = t;
        print(p);
    }
}

void main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int i;
    shift(a, 6);
}

运行结果:
10 1 2 3 4 5 6 7 8 9
9 10 1 2 3 4 5 6 7 8
8 9 10 1 2 3 4 5 6 7
7 8 9 10 1 2 3 4 5 6
6 7 8 9 10 1 2 3 4 5
5 6 7 8 9 10 1 2 3 4

--------------------------------
Process exited after 0.01482 seconds with return value 6
Press any key to continue . . .
一派護法 十九級
93樓 發表于:2016-1-17 15:42
【文件相关函数的参数位置回忆】
FILE *fp;
    rewind(fp);
    fseek(fp, 10, 0);
    fputc('c', fp);
    fgetc(fp);
    fputs("sss", fp);
一派護法 十九級
94樓 發表于:2016-1-17 15:53
#include <stdio.h>

#define N 10

void print(int *a)
{
    int i;
    for (i = 0; i < 10; i++)
        printf("%d ", a[i]);
    printf("\n");
}

void shift(int *a, int m)
{
    int i, j, temp;
    for (i = 0; i < m; i++)
    {
        temp = a[9]; // 注意这里不是a[0]
        for (j = N - 2; j >= 0; j--)
            a[j + 1] = a[j];
        a[0] = temp;
    }
}

void main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int i;
    shift(a, 6);
    print(a);
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:1714 回複數:96
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-1-17 22:13
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。