高级语言程序设计课程第八次个人作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C

这个作业要求在哪里:https://edu.cnblogs.com/campus/fzu/2024C/homework/13307

学号:102300108

姓名:陈茜蕾

11.13

第一题

第二题

第三题

问题:一开始没有输出,返回 str 时,它指向的是字符串的末尾
解决:用s来指向str整个字符串

第六题

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#pragma warning (disable:4996)
bool is_within(char*str,char ch) {
	while (*str != '\0') {
		if (ch == *str) {
			return true;
		}
		*str++;
	}
	return false;
}
int main() {
	char s[50];
	int n = 0;
	char ch;
	scanf("%s", s);
	while ((ch=getchar())!=EOF) {
		if (ch == '\n') {
			continue;
		}
		if (is_within(s, ch))
		{
			printf("ch is found.\n");
		}
		else {
			printf("ch is not found.");
		}
		
	}
	return 0;
}

第七题

12.9

第一题

问题:对于函数中的scanf还是加了&,但传进去的已经是地址
解决:去掉&

第二题

头文件

include <stdio.h>

include<string.h>

include<ctype.h>

pragma warning (disable:4996)

void set_mode(int mode);
void get_info();
void show_info();
static int mode;
static double dist;
static double fuel;
源文件

include "pe12-2a.h"

void set_mode(int mode) {
if (!(mode == 0 || mode == 1)&&mode!=-1) {
printf("Invalid mode specified. Mode 1(US) used.\n");
mode = 1;
}
}
void get_info() {
if (mode == 0) {
printf("Enter distance traveled in kilometers:");
}
else {
printf("Enter distance traveled in miles:");
}
scanf("%lf", &dist);
if (mode == 0) {
printf("Enter fuel consumed in liters:");
}
else {
printf("Enter fuel consumed in gallons:");
}
scanf("%lf", &fuel);
}
void show_info() {
printf("Fuel consumption is ");
if (mode == 0) {
printf("Fuel consumption is %.2lf liters per 100 km.\n", fuel * 100.0 / dist);
}
else {
printf("Fuel consumption is %.1lf miles per gallon.\n", dist / fuel);
}
}
主代码

// pe12-2b.c
// 与 pe12-2a.c 一起编译

include "pe12-2a.h"

int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
set_mode(mode);
get_info();
show_info();
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<string.h>

include<ctype.h>

pragma warning (disable:4996)

define METRIC 0

define US 1

void set_mode(int* pm);
void get_info(int mode, double* pd, double* pf);
void show_info(int mode, double distance, double fuel);
源文件
// compile with pe12-3b.c

include "pe12-3a.h"

void set_mode(int* pm)
{
if (*pm != METRIC && pm != US)
{
printf("Invalid mode specified. Mode 1(US) used.\n");
pm = 1;
}
}
void get_info(int mode, double
pd, double
pf)
{
if (mode == METRIC)
printf("Enter distance traveled in kilometers: ");
else
printf("Enter distance traveled in miles: ");
scanf("%lf", pd);
if (mode == METRIC)
printf("Enter fuel consumed in liters: ");
else
printf("Enter fuel consumed in gallons: ");
scanf("%lf", pf);
}
void show_info(int mode, double distance, double fuel)
{
printf("Fuel consumption is ");
if (mode == METRIC)
printf("%.2f liters per 100 km.\n", 100 * fuel / distance);
else
printf("%.1f miles per gallon.\n", distance / fuel);
}
主函数

include "pe12-3a.h"

int main(void)
{
int mode;
double distance, fuel;

printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
	set_mode(&mode);
	get_info(mode, &distance, &fuel);
	show_info(mode, distance, 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<malloc.h>

pragma warning (disable:4996)

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* arr = (int*)malloc(elem * sizeof(int));
for (int i = 0; i < elem; i++) {
arr[i] = val;
}
return arr;
}

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

第九题

include <stdio.h>

include<malloc.h>

include<string.h>

pragma warning (disable:4996)

int main(void)
{
int cnt;
printf("你想读入几个单词? ");
scanf("%d", &cnt);
getchar();
char** p = (char**)malloc(cnt * sizeof(char));
int pi = 0;
char tmp[100];
char
ptmp = tmp;
printf("请输入\n");
for (int i = 0; i < cnt; i++)
{
scanf("%s", tmp, sizeof(tmp));
int length = strlen(tmp);

	p[i] = (char*)malloc((length + 1) * sizeof(char));
	strcpy(p[i], tmp);
}
printf("读入单词如下:\n");
for (int i = 0; i < cnt; i++)
{
	printf("%s\n", p[i]);
}
for (int i = 0; i < cnt; i++) {
	free(p[i]);
}
free(p);

}

posted @ 2024-11-23 21:25  取个昵称真的好难  阅读(1)  评论(0编辑  收藏  举报