第1章--快速上手

1、写一个程序,它打印出一行hello world!并以换行符结尾的标准输出。

#include <stdio.h>


void main(void)
{
	printf("Hello World!\n");
}

2、写一个程序,从标准输入中读取行数,每一行在标准输出中先打印行号再打印出来,尝试让这个程序能够处理的一行不受限制。

#include <stdio.h>


void main(void)
{
	int line = 1;
	char ch, at_beginning = 1;
	
	printf("pleast input line: ");

	while ((ch = getchar()) != EOF) {
		if (at_beginning == 1) {
			at_beginning = 0;
			printf("%d. ", line++);
		}
		
		putchar(ch);
		if (ch == '\n') {
			printf("pleast input line: ");
			at_beginning = 1;
		}
	}
		
}

3、写一个程序从标准输入中读取字符之后在标准输出中写出,同时应该计算checksum并把它写在字符的后面,checksum用一个signed char变量计算并初始化为-1,每从标准输入读取一个字符,checksum的值就增加,checksum变量的值溢出部分忽略,当所有的字符被写入时,checksum作为一个整型数写出,它可能是个负数,确定在checksum的后面加个换行符,计算时使用ASCII码,如果运行你的程序时输入hello world!后面的输出应为:
Hello world!
102

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char ch;
	char checksum = -1;

	while (((ch = getchar()) != EOF) && (ch != '\n'))
	{
		printf("%c", ch);
		checksum += ch;
	}

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

	return 0;
}

4、写一个程序从标准输入中一行一行读取,直到遇到文件结尾符,计算每个输入行的长度,然后找到最长的一行并打印出来,为了使情况简单,你需要假定输入的行最大字符不超过1000。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX   1000

int main(void)
{
	int i;
	int lines;
	char input[MAX];
	char output[MAX];
	int max_len = 0;

	printf("Input your lines:");
	scanf("%d", &lines);
	gets();    // 清除输入缓冲区数据

	for (i = 1; i <= lines; i++)
	{
		printf("Input line %d:", i);
		gets(input);
		int len = strlen(input);

		if (len > MAX)
		{
			printf("Input longer than %d!\n", MAX);
			return -1;
		}
		
		if (max_len < len)
		{
			max_len = len;
			strcpy(output, input);
		}
	}

	if (max_len > 0)
	{
		printf("The longest string: %s\n", output);
		printf("Length: %d\n", max_len);
	}

	return 0;
}

5、在本章的示例中,rearrange函数中的语句在输入行的长度少于列范围时就退出,这条语句只有在列范围是递增的情况下正确,然而事实并不一定如此(比如3,4,1,2,-1 或者 3,5,6,4,-1),修改rearrange函数使得在任何情况下都正确。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COLS     20
#define MAX_INPUT    1000

int read_column_number(int columns[], int max);
void rearrange(char *output, char const *input, int n_columns, int const columns[]);

int main(void)
{
	int n_columns;
	int columns[MAX_COLS];
	char input[MAX_INPUT];
	char output[MAX_INPUT];

	n_columns = read_column_number(columns, MAX_COLS);

	while (fgets(input, MAX_INPUT, stdin)) {
		printf("Original input: %s", input);
		rearrange(output, input, n_columns, columns);
		printf("Rearranged line: %s\n", output);
	}
	
	return EXIT_SUCCESS;
}

int read_column_number(int columns[], int max)
{
	int num = 0;
	int ch;

	while (((num < max) && (scanf("%d", &columns[num]) == 1)) && (columns[num] >= 0))
		num += 1;
	if (num % 2 != 0) {
		puts("Last column number is not paired.");
		exit(EXIT_FAILURE);
	}

	while ((ch = getchar()) != EOF && ch != '\n')
		;

	return num;
}

void rearrange(char *output, char const *input, int n_columns, int const columns[])
{
	int col;
	int output_col;
	int len;

	len = strlen(input);
	output_col = 0;

	for (col = 0; col < n_columns; col += 2) {
		int nchars = columns[col + 1] - columns[col] + 1;

		if (columns[col] >= len)
			continue;

		if (output_col == MAX_INPUT - 1)
			break;

		if (columns[col + 1] >= len)
			nchars = len - columns[col] - 1;

		if (output_col + nchars > MAX_INPUT - 1)
			nchars = MAX_INPUT - output_col -1;

		strncpy(output +output_col, input +columns[col], nchars);
		output_col += nchars;
	}

	output[output_col] = '\0';
}

6、修改rearrange程序,去除最开始读取到column数组的数必须是奇数的限制,如果读取的为奇数个,最后一个值作为最后一组字符串的开始, 到输入字符串的末尾被复制到输出字符串中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS     20
#define MAX_INPUT    1000

int read_column_number(int columns[], int max);
void rearrange(char *output, char const *input, int n_columns, int const columns[]);

int main(void)
{
	int n_columns;
	int columns[MAX_COLS];
	char input[MAX_INPUT];
	char output[MAX_INPUT];

	n_columns = read_column_number(columns, MAX_COLS);

	while (fgets(input, MAX_INPUT, stdin)) {
		printf("Original input: %s", input);
		rearrange(output, input, n_columns, columns);
		printf("Rearranged line: %s\n", output);
	}
	
	return EXIT_SUCCESS;
}

int read_column_number(int columns[], int max)
{
	int num = 0;
	int ch;

	while (((num < max) && (scanf("%d", &columns[num]) == 1)) && (columns[num] >= 0))
		num += 1;
#if 0
	if (num % 2 != 0) {
		puts("Last column number is not paired.");
		exit(EXIT_FAILURE);
	}
#endif
	while ((ch = getchar()) != EOF && ch != '\n')
		;

	return num;
}

void rearrange(char *output, char const *input, int n_columns, int const columns[])
{
	int col;
	int output_col;
	int len;

	len = strlen(input);
	output_col = 0;

	for (col = 0; col < n_columns; col += 2) {
		int nchars;

		if (col + 1 >= n_columns)
			nchars = len - columns[col];
		else
			nchars = columns[col + 1] - columns[col] + 1;

		if (columns[col] >= len)
			continue;

		if (output_col == MAX_INPUT - 1)
			break;

		if (columns[col + 1] >= len)
			nchars = len - columns[col] - 1;

		if (output_col + nchars > MAX_INPUT - 1)
			nchars = MAX_INPUT - output_col -1;

		strncpy(output +output_col, input +columns[col], nchars);
		output_col += nchars;
	}

	output[output_col] = '\0';
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值