高階語言程式設計第八次作業

hfdc發表於2024-11-22

這個作業屬於哪個課程:https://edu.cnblogs.com/campus/fzu/2024C/

這個作業要求在哪裡: https://edu.cnblogs.com/campus/fzu/2024C/homework/13307

學號:092300125

姓名:張天榮

11
1.

#include <stdio.h>
#define LEN 10

void getnchar(char str[], int n);

int main(int argc, char *argv[])
{
    char str[LEN];

    printf("Please enter %d characters:\n", LEN - 1);
    getnchar(str, LEN);
    printf("Result:\n");
    puts(str);
    printf("Done.\n");

    return 0;
}

void getnchar(char str[], int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        str[i] = getchar();
    }
    str[n - 1] = '\0';
}


2.

#include <stdio.h>
#include <ctype.h>
#define LEN 10

void getnchar(char str[], int n);

int main(int argc, char* argv[])
{
    char str[LEN];

    printf("Please enter %d characters:\n", LEN - 1);
    getnchar(str, LEN);
    printf("Result:\n");
    puts(str);
    printf("Done.\n");

    return 0;
}

void getnchar(char str[], int n)
{
    int i = 0;

    while (i < n - 1)
    {
        str[i] = getchar();
        if (isspace(str[i]))
        {
            break;
        }
        ++i;
    }
    str[i] = '\0';
}


3.

#include <stdio.h>
#include <ctype.h>
#define LEN 10

char* getword(char* str);

int main(int argc, char* argv[])
{
    char word[LEN];

    printf("Please enter a word (EOF to quit):\n");
    while (getword(word) != NULL)
    {
        printf("Result: %s\n", word);
        printf("You can enter a word again (EOF to quit):\n");
    }
    printf("Done.\n");

    return 0;
}

char* getword(char* str)
{
    int ch, n = 0;
    char* pt = str;

    while ((ch = getchar()) != EOF && isspace(ch))
    {
        continue;
    }
    
    if (ch == EOF)
    {
        return NULL;
       
    }
    else
    {
        n++;
        *str++ = ch;
       
    }
    while ((ch = getchar()) != EOF && !isspace(ch) && n < LEN - 1)
    {
        *str++ = ch;
        
        n++;
    }
    *str = '\0';
    if (ch != EOF)
    {
        while (getchar() != '\n')
        {
            continue;
        }
    }
    return ch == EOF ? NULL : pt;
}


6.

#include <stdio.h>
#include <string.h>
#define LEN 20

char* s_gets(char* st, int n);
int is_within(char ch, const char* str);

int main(int argc, char* argv[])
{
    char ch, str[LEN];

    printf("Please enter a string (EOF to quit):\n");
    while (s_gets(str, LEN) != NULL)
    {
        printf("Please enter a character: ");
        ch = getchar();
        while (getchar() != '\n')
            continue;
        printf("String: %s\n", str);
        if (!is_within(ch, str))
        {
            printf("Not exist %c in the string.\n", ch);
        }
        else
        {
            printf("Exist %c in the string.\n", ch);
        }
        printf("You can enter a string again (EOF to quit):\n");
    }
    printf("Done.\n");

    return 0;
}

char* s_gets(char* st, int n)
{
    char* ret_val;
    char* find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
        {
            *find = '\0';
        }
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

int is_within(char ch, const char* str)
{
    while (*str)
    {
        if (*str == ch)
        {
            return 1;
        }
        ++str;
    }
    return 0;
}


7.

#include <stdio.h>
#include <string.h>
#define LEN 10

void eatline(void);
char* mystrncpy(char* dest, char* src, int n);
char* s_gets(char* st, int n);

int main(int argc, char* argv[])
{
    int len;
    char target[LEN];
    char source[LEN];

    printf("Please enter a string (EOF to quit):\n");
    while (s_gets(source, LEN) != NULL)
    {
        printf("Please enter a number for copy (> 0): ");
        while (scanf("%d", &len) != 1 || len <= 0)
        {
            eatline();
            printf("Please enter again: ");
        }
        eatline();
        printf("Source string: %s\n", source);
        printf("Target string: %s\n", mystrncpy(target, source, len));
        printf("You can enter a string again (EOF to quit):\n");
    }
    printf("Done.\n");

    return 0;
}

char* s_gets(char* st, int n)
{
    char* ret_val;
    char* find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
        {
            *find = '\0';
        }
        else
        {
            eatline();
        }
    }
    return ret_val;
}

void eatline(void)
{
    while (getchar() != '\n')
        continue;
    return;
}

char* mystrncpy(char* dest, char* src, int n)
{
    int count = 0;

    while (*src != '\0' && count < n)
    {
        *(dest + count) = *src++;
        
        count++;
    }
    *(dest + count) = '\0';
    
    return dest;
}

12
1.

#include <stdio.h>

void critic(int *units);

int main(void)
{
    int units;

    printf("How many pounds to a firkin of butter?\n");
    scanf("%d", &units);

    while (units != 56)
    {
        critic(&units);
    }
    printf("You must have looked it up!\n");

    return 0;
}

void critic(int *units)
{
    printf("No luck, my friend. Try again.\n");
    scanf("%d", units);
    return;
}


2.
pe12-2a.h

#ifndef PE12_2A_H_
#define PE12_2A_H_

void set_mode(int n);
void get_info(void);
void show_info(void);

#endif

pe12-2a.c

#include <stdio.h>
#include "pe12-2a.h"

static int mode;
static double range;
static double fuel;

void set_mode(int n)
{
    if (n > 1)
    {
        printf("Invalid mode specified. Mode %s used.\n",
               (0 == mode) ? "0(metric)" : "1(US)");
    }
    else
    {
        mode = n;
    }
    return;
}

void get_info(void)
{
    if (0 == mode)
    {
        printf("Enter distance traveled in kilometers: ");
    }
    else
    {
        printf("Enter distance traveled in miles: ");
    }
    scanf("%lf", &range);

    if (0 == mode)
    {
        printf("Enter fuel consumed in liters: ");
    }
    else
    {
        printf("Enter fuel consumed in gallons: ");
    }
    scanf("%lf", &fuel);

    return;
}

void show_info(void)
{
    if (0 == mode)
    {
        printf("Fuel consumption is %.2lf liters per 100km.\n",
               (fuel / range) * 100);
    }

    else
    {
        printf("Fuel consumption is %.1lf miles per gallon.\n",
               range / fuel);
    }
    return;
}

pe12-3a.h

#ifndef PE12_3A_H_
#define PE12_3A_H_

void set_mode(int *mode, int *n);
void get_info(int mode, double *range, double *fuel);
void show_info(int mode, double range, double fuel);

#endif

pe12-3a.c

#include <stdio.h>
#include "pe12-3a.h"

void set_mode(int *mode, int *n)
{
    if (*mode > 1)
    {
        printf("Invalid mode specified. Mode %s used.\n",
               (0 == *n) ? "0(metric)" : "1(US)");
    }
    else
    {
        *n = *mode;
    }
    return;
}

void get_info(int mode, double *range, double *fuel)
{
    if (0 == mode)
    {
        printf("Enter distance traveled in kilometers: ");
    }
    else
    {
        printf("Enter distance traveled in miles: ");
    }
    scanf("%lf", range);

    if (0 == mode)
    {
        printf("Enter fuel consumed in liters: ");
    }
    else
    {
        printf("Enter fuel consumed in gallons: ");
    }
    scanf("%lf", fuel);

    return;
}

void show_info(int mode, double range, double fuel)
{
    if (0 == mode)
    {
        printf("Fuel consumption is %.2lf liters per 100 km.\n",
               (fuel / range) * 100);
    }
    else
    {
        printf("Fuel consumption is %.1lf miles per gallon.\n",
               range / fuel);
    }
    return;
}

pe12-3b.c

#include <stdio.h>
#include "pe12-3a.h"

int main(void)
{
    int temp, mode;
    double range, fuel;

    printf("Enter 0 for metric mode, 1 for US mode: ");
    scanf("%d", &mode);
    temp = mode;
    while (mode >= 0)
    {
        set_mode(&mode, &temp);
        get_info(temp, &range, &fuel);
        show_info(temp, range, fuel);
        printf("Enter 0 for metric mode, 1 for US mode");
        printf("(-1 to quit): ");
        scanf("%d", &mode);
    }
    printf("Done.\n");

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int *make_array(int elem, int val);
void show_array(const int ar[], int n);

int main(void)
{
    int *pa;
    int size;
    int value;

    printf("Enter the number of elements: ");
    while (scanf("%d", &size) == 1 && size > 0)
    {
        printf("Enter the initialization value: ");
        scanf("%d", &value);
        pa = make_array(size, value);
        if (pa)
        {
            show_array(pa, size);
            free(pa);
        }
        printf("Enter the number of elements (<1 to quit): ");
    }
    printf("Done.\n");

    return 0;
}

int *make_array(int elem, int val)
{
    int i;
    int *pt = (int *)malloc(elem * sizeof(int));

    if (NULL == pt)
    {
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }
    printf("Output %d numbers:\n", val);
    for (i = 0; i < elem; i++)
    {
        pt[i] = val;
    }
    return pt;
}

void show_array(const int ar[], int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%d%c", ar[i], (i + 1) % 8 == 0 ? '\n' : ' ');
    }
    if (n % 8 != 0)
    {
        printf("\n");
    }
}


9.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 256

int main(void)
{
    int i, n;
    char **pt;
    static char temp[LEN];

    printf("How many words do you wish to enter? ");
    scanf("%d", &n);
    if ((pt = (char **)malloc(n * sizeof(char *))) != NULL)
    {
        printf("Enter %d words now:\n", n);
        for (i = 0; i < n; i++)
        {
            scanf("%255s", temp);
            pt[i] = (char *)malloc((strlen(temp) + 1) * sizeof(char));
            if (NULL == pt[i])
            {
                printf("Memory allocation failed!\n");
                exit(EXIT_FAILURE);
            }
            strcpy(pt[i], temp);
        }
        printf("Here are your words:\n");
        for (i = 0; i < n; i++)
        {
            puts(pt[i]);
            free(pt[i]);
            pt[i] = NULL;
        }
        free(pt);
        pt = NULL;
    }
    else
    {
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }

    return 0;
}

相關文章