好好學習,少熬夜(文不對題)

Shengjie發表於2016-11-28

上週一結束了c語言考試,題目很簡單,題型為選擇題、程式閱讀題、程式設計題。在考之前,花了4個晚上的時間,將老師提示過的課本例題及程式碼敲了一遍,看了兩遍。

結束c語言考試之後,緊接著的是物件導向程式設計課,主講java語言。換了個老師講課,挺有趣,會類比,會延伸,比c語言老師有意思。

今天週一,校歷上為第12周。離散數學的課程在這周也要結束了,下下週或許就考試了。

好好學習,少熬夜。

以下,為c語言考試複習的程式碼,存此留念:

p3
例1.3:

#include <stdio.h>
int sumab(int x, int y);
int main(void)
{
    int a, b, sum;
    printf("請輸入變數a與b的值:");
    scanf("%d %d", &a, &b);
    sum = sumab(a, b);
    printf("a與b的和等於%d\n", sum);
    return (0);
}

int sumab(int x, int y)
{
    int z;
    z = x + y;
    return z;
}

p22
(1):

#include <stdio.h>

int main(void)
{
    int m, n, k;

    puts("Please input two integers:");
    printf("m:"); scanf("%d", &m);
    printf("n:"); scanf("%d", &n);

    k = m;
    m = n;
    n = k;

    printf("m:%d\n", m);
    printf("n:%d\n", n);

    return (0);
}

(3):

#include <stdio.h>

int main(void)
{
    int n;
    int a, b, c;

    printf("Please input a integer:");
    scanf("%d", &n);

    a = n % 10;
    b = (n / 10) % 10;
    c = n / 100;

    printf("%d%d%d\n", a, b, c);

    return (0);
}

(4):

#include <stdio.h>

int main(void)
{
    int n;
    int a, b, c;

    printf("Please input a integer:");
    scanf("%d", &n);

    a = n % 10;
    b = (n / 10) % 10;
    c = n / 100;

    int sumabc;

    sumabc = a + b + c;

    printf("%d\n", sumabc);

    return (0);
}

p35
(1):

#include <stdio.h>

int main(void)
{
    int r, h;
    double pi = 3.14;

    printf("請輸入圓柱的半徑:"); scanf("%d", &r);
    printf("請輸入圓柱的高:"); scanf("%d", &h);

    double c; //圓周長 2*pi*r
    c = 2 * pi * r;

    double s;//圓面積pi*r*r
    s = pi * r * r;

    double s2;//圓柱表面積2*pi*r*r+2*pi*r*h
    s2 = s * 2 + c * h;

    double v;//圓柱體積pi*r*r*h
    v = s * h;

    printf("圓周長:%.2f\n", c);
    printf("圓面積:%.2f\n", s);
    printf("圓柱表面積:%.2f\n", s2);
    printf("圓柱體積:%.2f\n", v);

    return (0);
}

(2):

#include <stdio.h>

int main(void)
{
    int f;

    printf("請輸入華氏溫度值:"); scanf("%d", &f);

    double c;

    c = ((double)5 / 9) * (f - 32); // 這裡的強制型別轉換需要注意

    printf("攝氏溫度值為:%.2f\n", c);

    return (0);
}

p46
例4.6:

#include <stdio.h>

int main(void)
{
    float x, y;

    printf("Input x:");
    scanf("%f", &x);

    if (x < 10)
        if(x < 0) y = 5 * x - 6;
        else y = 4 * x;
    else 
        y = 2 * x + 3;

    printf("x = %f, y = %f\n", x, y);

    return (0);
}

例4.7:

#include <stdio.h>

int main(void)
{
    float x, y;

    printf("Input x:");
    scanf("%f", &x);

    switch (x >= 0)
        {
            case 0 : y = 5 * x - 6; break;
            case 1 : switch (x >= 10) 
                    {
                        case 0 : y = 4 * x; break;
                        case 1 : y = 2 * x + 3; break;
                    }     
        }

    printf("y = %f\n", y);

    return (0);
}

例4.8:

#include <stdio.h>

int main(void)
{
    float a, b, c, t;

    printf("Please input three integers:");
    scanf("%f %f %f", &a, &b, &c);

    if (a > b) {
        t = a; a = b; b = t;
    }

    if (a > c) {
        t = a; a = c; c = t;
    }

    if (b > c) {
        t = b; b = c; c = t;
    }

    printf("%f %f %f\n", a, b, c);

    return (0);
}

例4.10:

#include <math.h>
#include <stdio.h>

int main(void)
{
    float a, b, c, d, x1, x2, p, q;

    printf("輸入方程係數 a, b, c:");
    scanf("%f %f %f", &a, &b, &c);

    d = b * b - 4 * a * c;

    if (fabs(d) <= 1e-6) //d == 0; fabs是求絕對值函式
        printf("有兩個相等的實根:%8.4f\n", -b / (2 * a));
    else if (fabs(d) > 1e-6) //d > 0
        {
            x1 = (-b + sqrt(d)) / (2 * a); //sqrt是求平方根函式
            x2 = (-b - sqrt(d)) / (2 * a);
            printf("有兩個不相等的實根:%8.4f 和 %8.4f\n", x1, x2);
        }
    else 
        {
            p = -b / (2 * a);
            q = sqrt(-d) / (2 * a);
            printf("有兩個共軛復根\n");
            printf("%8.4f + %8.4f\n", p, q);
            printf("%8.4f - %8.4f\n", p, q);
        }

    return (0);
}

p51
(1):

#include <stdio.h>

int main(void)
{
    int a, b, c;

    printf("請輸入三個數:");
    scanf("%d %d %d", &a, &b, &c);

    if (a + b > c && a + c > b && b + c > a)
        printf("能\n");
    else 
        printf("不能\n");

    return (0);
}

(4):

#include <stdio.h>

int main(void)
{
    int a, b, c, d, t;

    printf("請輸入4個整數:");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    if (a > b) {t = a; a = b; b = t;}
    if (a > c) {t = a; a = c; c = t;}
    if (a > d) {t = a; a = d; d = t;}
    if (b > c) {t = b; b = c; c = t;}
    if (b > d) {t = b; b = d; d = t;}
    if (c > d) {t = c; c = d; d = t;}

    printf("從小到大順序輸出:%d %d %d %d\n", a, b, c, d);

    return (0);
}

(5):

#include <stdio.h>

int main(void)
{
    int n3, a, b, c;

    printf("請輸入一個三位整數:");
    scanf("%d", &n3);

    c = n3 % 10;
    b = (n3 / 10) % 10;
    a = n3 / 100;

    printf("%d %d %d\n", a, b, c); //將個位數十位數百位數輸出

    if (c < 7 && b % 3 == 0 && a * a > 20)
        printf("YES\n");
    else
        printf("NO\n");

    return (0);
}

p60
例5.9:

#include <stdio.h>

int main(void)
{
    int i = 0, n;
    long sum = 0;

    while (i < 100)
    {
        scanf("%d", &n);
        i++;
        if (n <= 0) continue; // 如果n<=0,則跳過continue後的語句,直接開始判斷下一次迴圈
        sum += n;
    }

    printf("sum = %ld\n", sum);

    return (0);
}

p62
例5.12:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float i = 1.0;
    int k = 1;
    double t = 1.0, pi = 0;

    do {
        pi = pi + t;
        i += 2;
        k = -k;
        t = k / i;
    } while (fabs(t) >= 1e-6);

    pi = pi * 4;
    printf("pi = %f\n", pi);
    return (0);
}

p65
(1):

#include <stdio.h>

int main(void)
{
    float sum = 1.0;
    int k = 1;
    float i = 1;
    float z = 0;

    do {
        sum = sum + z;
        i += 1;
        k = -k;
        z = k / i;
    } while (i <= 100);

    printf("%f\n", sum);

    return (0);
}
#include <stdio.h>

int main(void)
{
    int c, s;
    float sum = 0;

    for (c = 1; c <= 100; c++)
    {
        if (c % 2 == 0) s = -1;
        else s = 1;
        sum += s * (1 / (float)c);
    }
    printf("%f\n", sum);

    return (0);
}

(3):

#include <stdio.h>

int main(void)
{
    float sum = 1.0;
    int n;

    for (n = 2; n <= 100; n++)
        sum = sum + (float)(n + 1) / n;

    printf("%f\n", sum);

    return (0);
}

p68
例6.1:

#include <stdio.h>

int main(void)
{
    int n, sum = 0, a[10];
    float aver;
    
    for (n = 0; n < 10; n++)
    {
        scanf("%d", &a[n]);
        sum = sum + a[n];
    }
    aver = sum / 10.0;

    for (n = 0; n <= 9; n++)
    {
        printf("%d,", a[n]);
    }

    printf("%d, %f\n", sum, aver);

    return (0);
}

例6.2:

#include <stdio.h>

#define N 10

int main(void)
{
    int n, m, sign = 0;
    int num[N] = {16, 35, 48, 29, 56, 43, 93, 64, 90, 48};

    printf("Please input the number:");
    scanf("%d", &n);

    for (m = 0; m < N; m++)
        if (n == num[m])
        {
            printf("%d, %d\n", m, num[m]);
            sign = 1;
        }
        if (sign != 1)
            printf("Have no this number.\n");

    return (0);
}

p77
例6.9:

#define N 7
#include <stdio.h>

int main(void)
{
    int i, j, a[N][N];
    
    for (i = 1; i < N; i++)
    {
        a[i][i] = 1;
        a[i][1] = 1;
    }

    for (i = 3; i < N; i++)
        for (j = 2; j <= i - 1; j++)
            a[i][j] = a[i - 1][j - 1] + a[i - 1][j];

    for (i = 1; i < N; i++)
    {
        for (j = 1; j <= i; j++)
            printf("%5d", a[i][j]);
            printf("\n");
    }

    return (0);
}

p91
(1):

#include <stdio.h>
#define N 5
int main(void)
{
    int a[N] = {2, 3, 5, 7, 11};
    int b[N] = {1, 4, 6, 8, 9};
    int c[N] = {0};
    int i;

    for (i = 0; i < N; i++)
    {
        c[i] = a[i] + b[i];
    }

    for(i = 0; i < N; i++)
        printf("%d\n", c[i]);

    return (0);
}

(5):

#include <stdio.h>
#define N 100
int main(void)
{
    int i;
    int score;
    int a[N];
    int n = 0;
    float total = 0;
    float ave;
    int count;

    puts("請輸入學生的成績:");
    scanf("%d", &score);

    while (score >= 0)
    {
        a[n] = score;
        n++;
        total += score;
        scanf("%d", &score);
    }

    ave = (double)total / n;

    for (i = 0; i < n; i++)
    {
        if (a[i] >= ave)
            count++;
    } 

    printf("平均成績:%f\n", ave);
    printf("不低於平均分的人數:%d\n", count);

    return (0);
}

p96
例7.5:

#include <stdio.h>
#include <math.h>

int prime(int i)
{
    int j, k, flag = 1;
    k = sqrt(i);

    for (j = 2; j <= k; j++)
        if (i % j == 0) {
            flag = 0; break;
        }
    return flag;
}

int main(void)
{
    int i;
    for (i = 3; i < 100; i++)
        if (prime(i) == 1)
            printf("%4d", i);
    printf("\n");
    return (0);
}

p101
例7.8:

#include <stdio.h>
long fact(int n);

int main(void)
{
    int n;

    printf("please enter n:");
    scanf("%d", &n);

    printf("n! = %ld\n", fact(n));

    return (0);
}

long fact(int n)
{
    if (n == 1 || n == 0)
        return (1);
    else 
        return (n * fact(n - 1));
}

p117
(4):

#include <stdio.h>

int prime(int n)
{
    int i;
    int k = 1;
    for (i = 2; i < n; i++)
        if (n % i == 0) {
            k = 0; break;
        }
    return (k);
}

int main(void)
{
    int n;

    printf("Please input a integer:");
    scanf("%d", &n);

    printf("%d\n", prime(n));

    return (0);
}

(6):

#include <stdio.h>
#define N 100
void delete_char(char str[], char ch)
{
    int j, k;

    for (j = k = 0; str[j] != '\0'; j++) {
        if (str[j] != ch)
            str[k++] = str[j]; //先賦值再自增
    }
        str[k] = '\0';
}

int main(void)
{
    char str1[N] = {0};
    char ch1;

    printf("Please input a string:");
    scanf("%s", str1);

    printf("Please input a string that you want to be deleted:");
    scanf("%s", &ch1);

    delete_char(str1, ch1);

    printf("%s\n", str1);

    return (0);
}

p130
例9.2:

#include <stdio.h>
int main(void)
{
    int num1 = 12, *p1;
    float num2 = 3.14, *p2;
    char ch = 'p', *p3;

    p1 = &num1;
    p2 = &num2;
    p3 = &ch;

    printf("num1 = %d, *p1 = %d\n", num1, *p1);
    printf("num2 = %4.2f, *p2 = %4.2f\n", num2, *p2);
    printf("ch = %c, *p3 = %c\n", ch, *p3);

    return (0);
}

例9.3:

#include <stdio.h>
int main(void)
{
    int num1, num2, *p, *p1, *p2;
    p1 = &num1;
    p2 = &num2;

    printf("Input the first number:");
    scanf("%d", p1);

    printf("Input the second number:");
    scanf("%d", p2);

    printf("num1 = %d, num2 = %d\n", num1, num2);

    if (*p1 > *p2)
    {
        p = p1;
        p1 = p2; 
        p2 = p;
    }

    printf("min = %d, max = %d\n", *p1, *p2);

    return (0);
}

p136
例9.2
(1):

#include <stdio.h>
void copystr(char str[], char str2[])
{
    int i = 0;
    while (str[i] != '\0')
    {
        str2[i] = str[i];
        i++;
    }   
    str2[i] = '\0';
}

int main(void)
{
    char a[20] = "I love china!";
    char b[20] = "good!";
    printf("a = %s b = %s\n", a, b);
    copystr(a, b);
    printf("a = %s b = %s\n", a, b);
    return (0);
}

(2):// 此題有問題

#include <stdio.h>

void copystr(char str1[], char str2[])
{
    int i = 0;
    while (str1[i] != '\0')
    {
        str2[i] = str1[i];
        i++;
    }
    str2[i] = '\0';
}

int main(void)
{
    int i;
    char *a = "I love china!";
    char *b = "good!";
    printf("a = %s, b = %s\n", a, b);
    copystr(a, b);
    for (i = 0; *(a + i) != '\0'; i++)
        putchar(*(a + i));
    printf("\n");
    for (i = 0; *(b + i) != '\0'; i++)
        putchar(*(b + i));
    return (0);
}

(3):// 此題有問題

#include <stdio.h>
void copystr(char *str1, char *str2)
{
    for (; *str1 != '\0'; str1++, str2++)
        *str2 = *str1;
    *str2 = '\0';
}

int main(void)
{
    int i;
    char *a = "I love china!", *b = "good!";
    for (i = 0; *(a + i) != '\0'; i++)
        putchar(*(a + i));
    printf("\n");
    for (i = 0; *(b + i) != '\0'; i++)
        putchar(*(b + i));
    printf("\n");
    copystr(a, b);
    for (; *a != '\0'; a++) printf("%c", *a);
        printf("\n");
    for (; *b != '\0'; b++) printf("%c", *b);
        printf("\n");
    return (0);
}

(4):

#include <stdio.h>
void copystr(char *str1, char *str2)
{
    int i = 0;
    for (; (*(str2 + i) = *(str1 + i)) != '\0'; i++);
}

int main(void)
{
    char a[20] = "I love china!";
    char b[20] = "good!";
    printf("a = %s, b = %s\n", a, b);
    copystr(a, b);
    puts(a);
    puts(b);
    return (0);
}

p181
(1):

#include <stdio.h>
#define N 100
struct sp
{
    char snum[20];
    char sname[20];
    float price;
    int total;
    float sum;
};

int main(void)
{
    struct sp a[N]; 
    int i;

    puts("請輸入「商品編號」、「商品名」、「單價」、「數量」:");
    for (i = 0; i < N; i++) {
        scanf("%s", a[i].snum);
        scanf("%s", a[i].sname);
        scanf("%f", &a[i].price);
        scanf("%d", &a[i].total);
        a[i].sum = a[i].price * a[i].total;
    }

    for (i = 0; i < N; i++)
        printf("商品編號:%s, 商品名:%s, 單價:%f, 數量:%d, 總價:%f\n", a[i].snum, a[i].sname, a[i].price, a[i].total, a[i].sum);

    return (0);
}

(2):

#include <stdio.h>
#define M 2

struct stud
{
    char num[5];
    char name[10];
    int score[3];
    float aver;
};

int main(void)
{
    int i, j, k;
    struct stud s[M];

    puts("請輸入學號、姓名和3門課的成績:");
    for (i = 0; i < M; i++)
    {
        scanf("%s", s[i].num);
        scanf("%s", s[i].name);
        k = 0;
        for (j = 0; j <= 2; j++)
            {
                scanf("%d", &s[i].score[j]);
                k = k + s[i].score[j];
            }
        s[i].aver = k / 3.0;
    }

    puts("學號、姓名、3門課的成績以及平均分:");
    for (i = 0; i < M; i++)
    {
        printf("%s %s ", s[i].num, s[i].name);
        for (j = 0; j <= 2; j++)
            printf("%d ", s[i].score[j]);
        printf("%.2f", s[i].aver);
        printf("\n");
    }

    return (0);
}

p212
(4):

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
    FILE *fp;
    char ch, filename[10];

    gets(filename);
    if ((fp = fopen(filename, "w")) == NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }

    ch = getchar();

    while (ch != '#')
    {
        fputc(ch, fp);
        putchar(ch);
        ch = getchar();
    }

    fclose(fp);

    return (0);
}

(5):

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
    FILE *fp;
    char str1[100]; 
    int i = 0;

    if ((fp = fopen("text", "w")) == NULL)
    {
        printf("cannot open the file\n");
        exit(0);
    }

    printf("Please input a string:\n");
    gets(str1);

    while (str1[i] != '\0')
    {
        if (str1[i] >= 'a' && str1[i] <= 'z')
            str1[i] = str1[i] - 32;
            fputc(str1[i], fp);
        i++;
    }

    fclose(fp);

    return (0);
}

(6):

#include <stdlib.h>
#include <stdio.h>
#define N 5
int main(void)
{
    struct record
    {
        int num;
        char name[4];
        int count;
        float price;
    } s[N] = {{1, "aaa", 10, 12.3}, {2, "bbb", 20, 23.45}, {3, "ccc", 20, 13.45},
            {4, "ddd", 50, 78.6}, {5, "eee", 30, 34.21}};

    FILE *fp;
    int k;

    if ((fp = fopen("data", "wb")) == NULL)
    {
        printf("cannot open the file.\n");
        exit(0);
    }

    for (k = 0; k < N; k++)
        fwrite(&s[k], sizeof(struct record), 1, fp);

    fclose(fp);

    return (0);
}

相關文章