高级程序员设计语言第八次个人作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13307
学号:102400229
姓名:石华波
//11.13.1
#include<stdio.h>
void GetStr(char *str,int n);
int main(){
char str[101];
int n=10;
GetStr(str,n);
printf("%s",str);
return 0;
}
void GetStr(char *str,int n){
while(n--){
*(str++)=getchar();
}
*str='\0';
}

//11.13.2
#include<stdio.h>
void GetStr(char *str,int n);
int main(){
char str[101];
int n=10;
GetStr(str,n);
printf("%s",str);
return 0;
}
void GetStr(char *str,int n){
while(n--){
*str=getchar();
if(*str==' '||*str=='\n'||*str=='\t') {
*(str++)='\0';
return;
}
str++;
}
*str='\0';
}

//11.13.3
#include<stdio.h>
void GetWord(char *str);
int main(){
char str[101];
int n=10;
GetWord(str);
printf("%s",str);
return 0;
}
void GetWord(char *str){
char ch;
ch=getchar();
while(ch<=32) ch=getchar();
while(ch!=' '&&ch!='\n'&&ch!='\t'){
*(str++)=ch;
ch=getchar();
}
*str='\0';
}

//11.13.6
#include<stdio.h>
int is_within(char *str,char ch);
int main(){
char str[101];
char ch;
while(scanf("%s %c",str,&ch)!=EOF){
if(is_within(str,ch)) printf("\'%c\' is within \"%s\"\n",ch,str);
else printf("\'%c\' is without \"%s\"\n",ch,str);
}
return 0;
}
int is_within(char *str,char ch){
while(*str!='\0'){
if(ch==*str) return 1;
str++;
}
return 0;
}

//11.13.7
#include<stdio.h>
char *mystrncpy(char *dest,char *src,int n);
int main(){
char src[101];
char dest[10];
char ch;
while(scanf("%s",src)!=EOF){
mystrncpy(dest,src,10);
puts(dest);
}
return 0;
}
char *mystrncpy(char *dest,char *src,int n){
int cnt=0;
while(*src!='\0'&&cnt<=n){
*(dest+cnt)=*src++;
cnt++;
}
if(cnt<=n) *(dest+cnt)='\0';
return dest;
}

//12.9.1
#include<stdio.h>
void critic(int *units);
int main(){
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);
}

//12.9.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
//12.9.2 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",(mode==0)?"0(metric)" : "1(US)");
else mode = n;
return;
}
void get_info(void){
if (mode==0) printf("Enter distance traveled in kilometers: ");
else printf("Enter distance traveled in miles: ");
scanf("%lf", &range);
if (mode==0) printf("Enter fuel consumed in liters: ");
else printf("Enter fuel consumed in gallons: ");
scanf("%lf", &fuel);
}
void show_info(void){
if (mode==0) printf("Fuel consumption is %.2lf liters per 100km.\n",(fuel/range) * 100);
else printf("Fuel consumption is %.1lf miles per gallon.\n",range / fuel);
}
//12.9.2 pe12-2b.c
#include <stdio.h>
#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;
}

//12.9.3 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
//12.9.3 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;
}
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);
}
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);
}
//12.9.3 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;
}

//12.9.8
#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));
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");
}

//12.9.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));
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;
}

浙公网安备 33010602011771号