最近对于C语言进行了一个重新学习,用来弥补上课那会没有人这听落下的知识,对于课后题目进行了练习并整理结果,部分参考自其他博客,欢迎大家批评指正。(练习1-1到练习1-12会在后面补上) 练习1-13 编写一个程序,打印输入中单词长度的直方图。水平防线的直方图比较容易绘制,垂直方向的直方图则要困难些。
#include"stdio.h"
#include<stdlib.h>
#define MAXWORD 10
#define IN 1
#define OUT 0
int main()
{
int c, state;
int nw[MAXWORD], i;
int nc;
int nh;
int len;
state = OUT;
nh = 0;
for (i = 1; i < MAXWORD; ++i)
nw[i] = 0;
while ((c = getchar()) != EOF) {
if (c == '\t' || c == '\n' || c == ' ') {
state = OUT;
if (nc > 0) {
nw[nh] = nc;
nc = 0;
}
}
else if (state == OUT) {
state = IN;
nc = 1;
++nh;
}
else
++nc;
}
for (i = 1; i < MAXWORD; ++i) {
printf("%d - %d : ", i, nw[i]);
len = nw[i];
while (len > 0) {
putchar('*');
--len;
}
putchar('\n');
}
}
练习1-14 编写一个程序,打印输入中的各个字符出现频度的直方图。
#include"stdio.h"
#include<stdlib.h>
int main()
{
int c,i;
int n_num, n_letter, n_key, n_other;
n_num = 0;
n_letter = 0;
n_key = 0;
n_other = 0;
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9')
++n_num;
else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
++n_letter;
else if (c == ' ')
++n_key;
else if (c == '\t')
n_key = n_key + 2;
else
++n_other;
}
printf("amount of figures:%d", n_num);
for (i = 0; i < n_num; ++i)
printf("*");
printf("\n");
printf("amount of letters:%d", n_letter);
for (i = 0; i < n_letter; ++i)
printf("*");
printf("\n");
printf("amount of spaces:%d", n_key);
for (i = 0; i < n_key; ++i)
printf("*");
printf("\n");
printf("number of others:%d", n_other);
for (i = 0; i < n_other; ++i)
printf("*");
printf("\n");
return 0;
}
练习1-15 重新编写1.2节中的温度转换的程序,使用函数实现温度转换计算。
#include"stdio.h"
#include<stdlib.h>
void fahr_to_celsius(float f);
void celsius_to_fahr(float c);
int main()
{
float f;
float c;
scanf_s("%f", &f);
fahr_to_celsius(f);
scanf_s("%f", &c);
celsius_to_fahr(c);
return 0;
}
void fahr_to_celsius(float f)
{
float c;
c = 5 * (f - 32) / 9;
printf("%f", c);
}
void celsius_to_fahr(float c)
{
float f;
f = c* 9 / 5 + 32;
printf("%f", f);
}
练习1-16 修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多的打印文本。
这里是引用https://www.cnblogs.com/DeadGardens/p/4781076.html
#include"stdio.h"
#include<stdlib.h>
#define MAXLINE 10
int getline(char line[], int maxline);
int main()
{
int len;
int max;
char line[MAXLINE];
while ((max = getline(line, MAXLINE)) > 0)
{
printf("输入的行的长度为:%d。文本是%s\n", max, line);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i, j;
j = 0;
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
{
if (i <= lim - 2)
s[i] = c;
++j;
}
if (j > lim)
s[lim - 1] = '\0';
else
s[i + 1] = '\0';
return j;
}
练习1-17 编写一个程序,打印长度大于80个字符的所有输入行。
#include"stdio.h"
#include<stdlib.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
void print_80(int len, char line[]);
int main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
print_80(len, line);
}
if (max > 0)
printf("%s,%d", longest,max);
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[]) {
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
void print_80(int len, char line[])
{
if (len > 80)
printf("%d,%s",len, line);
}
练习1-18 编写一个程序,删除每个输入行末尾的空格及制表符,并删除完全是空格的行。
#include"stdio.h"
#include<stdlib.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void Delete_space(int len, char line[]);
int main()
{
int len;
char line[MAXLINE];
while ((len = getline(line, MAXLINE)) > 0) {
printf("%d,%s",len, line);
Delete_space(len, line);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void Delete_space(int len, char line[])
{
while (len > 0) {
if (line[len - 2] == '\t' || line[len - 2] == ' ') {
line[len - 2] = '\0';
--len;
}
else
break;
}
printf("%d,%s", len, line);
}
练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。
#include"stdio.h"
#include<stdlib.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void reverse(char line[]);
int main()
{
int len;
char line[MAXLINE];
while ((len = getline(line, MAXLINE)) > 0)
reverse(line);
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void reverse(char line[])
{
int len = 0;
int i = 0;
char new_line[MAXLINE];
while (line[len] != '\n')
++len;
for (len = len - 1;len >= 0; --len) {
new_line[i] = line[len];
++i;
}
new_line[i + 1] = '\0';
printf("%s", new_line);
}
|