C Peimer第11章编程习题

原创 2016年08月29日 12:32:34

两天时间,15道题,看上去简单,做一下就知道每道都要命,眼睛都要废了……大笑

//answers of the questions in the Peimer at Chapter 11
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

#define LINES 4
#define N 20

int main(int argc,char *argv[]) {
	//1
	/*
	char chArr[N + 1];
	void strCopyMy(char *chArr,int n);
	strCopyMy(chArr, N);
	puts(chArr);*/

	//2
	/*
	char chArr[N + 1];
	void strCopyMy_2(char *chArr, int n);
	char temp;
	strCopyMy_2(chArr, N);
	puts(chArr);
	*/

	//3
	/*第二题的程序适用
	char chArr[N + 1];
	void strCopyMy_2(char *chArr, int n);
	char temp;
	strCopyMy_2(chArr, N);
	puts(chArr);
	*/

	//4
	/*
	char chArr[2*N];
	char* strChMy(char *chArr, char ch);
	char ch;
	char *temp;
	int i = 0;
	printf("begin:\n");
	while (i < 10) {
		printf("please enter a line:\n");
		gets(chArr);
		printf("please enter a character:\n");
		ch = getchar();
		getchar();//除掉录入ch后的那一个\n
		temp = strChMy(chArr, ch);
		printf("%c\n", *temp);
		puts(temp);
		i++;
	}*/


	//5
	/*
	int is_within(char *chArr, char ch);
	char chArr[] = "I am a wonderful person!";
	char ch = 'k';
	int position;
	position = is_within(chArr, ch);
	printf("%d\n", position);
	*/

	//6
	/*
	char* strncpyMy(char *str1, char* str2, int n);
	char *str1[2 * N];
	char *str2 = "this is a wonderful story!";
	strncpyMy(str1, str2, 2 * N);
	puts(str1);
	*/

	//7
	/*
	char *string_in(char* chArr1,char *chArr2);
	char chArr1[] = "this is a wonderful story";
	char chArr2[] = "wonderful";
	char *same;
	same = string_in(chArr1, chArr2);
	puts(same);//以返回的指针处开始打印
	*/

	//8
	/*
	void reverseChArr(char *chArr);//attention this function can only receive a string less than 4*N letters.
	char chArr1[] = "this is a wonderful story\n";
	char chArr2[4 * N];
	puts(chArr1);
	reverseChArr(chArr1);
	puts(chArr1);
	*/

	//9
	/*
	char chArr1[4 * N];
	void deleteSpace(char *chArr);
	gets(chArr1);
	while (*chArr1 != ' ')
	{
		deleteSpace(chArr1);
		puts(chArr1);
		gets(chArr1);
	}
	*/

	//10
	/*
	char chArr[LINES][N];
	int choice;
	void ASCIIOrder(char*chArr[], int lines);//按ASCII码排列
	void FirstWordLengthOrder(char*chArr[][N], int lines);//按ASCII码排列
	void LengthOrder(char*chArr[], int lengthArr[],int lines);//按串长排列
	void showArray(char *chArr[], int lines);
	char *head[LINES];
	int lengthArr[LINES];
	for (int i = 0; i < LINES; i++)
	{
		printf("please enter the %dth line:\n", i);
		gets(chArr[i]);
		head[i] = chArr[i];
		*(lengthArr + i) = strlen(chArr[i]);
	}
	int flag = 1;
	while (flag)
	{
		printf("Please choose your option:\n");
		printf("1.output string:\n");
		printf("2.print according to the ascII order:\n");
		printf("3.print according to length:\n");
		printf("4.print according to the length of the first word:\n");
		printf("5.Quit:\n");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			showArray(head, LINES);
			break;
		case 2:
			ASCIIOrder(head, LINES);
			showArray(head, LINES);
			break;
		case 3:
			LengthOrder(head, lengthArr, LINES);
			showArray(head, LINES);
			break;
		case 4:
			FirstWordLengthOrder(head, LINES);
			showArray(head, LINES);
			break;
		case 5:
			flag = 0;
			break;
		default:
			break;
		}

	}
	*/
	
	//11
/*
	char chArr[4 * N];
	char temp;
	int Upper = 0;
	int Lower = 0;
	int Punct = 0;
	int Num = 0;
	int total = 0;
	int i = 0;
	while ((temp = getchar()) != EOF)
	{
		if (temp >= 'A'&&temp <= 'Z')
		{
			Upper++;
		}
		else if (temp >= 'a'&&temp <= 'z')
		{
			Lower++;
		}
		else if (ispunct(temp))
		{
			Punct++;
		}
		else if (temp >= '0'&&temp <= '9')//判断数字也得用ASCII码!
		{
			Num++;
		}
		*(chArr + i) = temp;
		i++;
	}
	*(chArr + N - 1) = '\0';
	puts(chArr);
	printf("\nthe number of upper letters is %d;\n", Upper);
	printf("the number of lower letters is %d;\n", Lower);
	printf("the number of Punct letters is %d;\n", Punct);
	printf("the number of Num letters is %d;\n", Num);
	*/
	
	//12
	//此题写的是相当不好!!!
	//运行此程序需在cmd下运行,需几个参数(小于LINES)
	/*
	//const char *mytal[LINES] = { "adding name swiftly","multiply quickly","following instructions ","understand the C"};
	char mytal[LINES][4 * N];
	char *temp[LINES];
	char *temp2[LINES];
	void ReverseWord(char* chArr[],char *temp2[],int lines);
	for (int i = 0; i < argc; i++)
	{
		strcpy(mytal[i], argv[i]);
	}
	for (int i = 0; i < LINES; i++)
	{
		temp[i] = mytal[i];
		puts(temp[i]);
	}
	printf("**************\n");
	ReverseWord(temp,temp2,LINES);
	for (int i = 0; i < LINES; i++)
	{
		puts(temp2[i]);
	}
	*/

	//13
	//调用ctype.h中的函数strtod();
	/*
	double bottom;
	int index;
	double reNum;
	bottom = atof(argv[1]);
	index = atoi(argv[2]);
	reNum = pow(bottom, index);
	printf("the result is %lf", reNum);
	*/

	//14
	/*
	int AtoIMy(char *chArr[],int lines);
	char chArr[2 * N] = "this is 123 huge456 news!";
	int num;
	num = AtoIMy(chArr, 2 * N);
	puts(chArr);
	printf("%d",num);
	*/

	//15
	//本例亦需在cmd下运行,需参数-p或-u或-l
	char *chArr[4 * N];
	int choice = 0;
	void ToUpperMy(char *chArr);
	void ToLowerMy(char *chArr);
	//printf("%s", argv[1]);
	if (!strcmp(argv[1], "-p"))
	{
		choice = 0;
	}
	else if (!strcmp(argv[1], "-u"))
	{
		choice = 1;
	}
	else if (!strcmp(argv[1], "-l"))
	{
		choice = 2;
	}
	printf("your choice is : %d\n",choice);
	printf("Please enter a line:\n");
	gets(chArr);
	switch (choice)
	{
	case 0:
		puts(chArr);
		break;
	case 1:
		ToUpperMy(chArr);
		puts(chArr);
		break;
	case 2:
		ToLowerMy(chArr);
		puts(chArr);
		break;
	default:
		break;
	}
	return 0;
}

void ToLowerMy(char *chArr) {
	int i = 0;
	while (*chArr) {
		*chArr = tolower(*chArr);
		chArr++;
	}
}

void ToUpperMy(char *chArr) {
	int i = 0;
	while (*chArr) {
		*chArr = toupper(*chArr);
		chArr++;
	}
}
int AtoIMy(char chArr[],int lines) {
	int num = 0;
	int counter=0;
	int times10;
	for (int i = 0; i < lines; i++)
	{
		if (chArr[i] >= '0'&&chArr[i] <= '9')
		{
			num = num * 10 + (int)(chArr[i]-'0');
			counter++;
		}
		else if ((chArr[i] >= '9' || chArr[i] <= '0')&&counter != 0)//在进入数字后遇符号跳出
		{
			break;
		}
	}
	return num;
}

void ReverseWord(char* chArr[], char *temp[], int lines) {
	for (int i = 0; i < LINES; i++)
	{
		temp[i] = chArr[LINES - i-1];
	}	
}
void FirstWordLengthOrder(char *chArr[], int lines) {
	void LengthOrder(char *chArr[], int lengthArr[], int lines);
	int temp[LINES];
	for (int i = 0; i < lines; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (*(*(chArr + i) + j) == ' '|| *(*(chArr + i) + j) == '\0')
			{
				*(temp + i) = j;
				break;
			}
		}
	}
	LengthOrder(chArr, temp, lines);
}


void showArray(char *chArr[], int lines) {
	for (int i = 0; i < lines; i++)
	{
		puts(chArr[i]);
	}
}

void LengthOrder(char *chArr[], int lengthArr[], int lines) {
	//按串长排列
	int max;
	char *temp;
	int midNum;
	for (int i = 0; i < lines - 1; i++)
	{
		max = *(lengthArr + i);
		for (int j = i + 1; j < lines; j++)
		{
			if (max<*(lengthArr+j))
			{
				max = *(lengthArr + j);
				//交换串长
				midNum = *(lengthArr + j);
				*(lengthArr + j) = *(lengthArr + i);
				*(lengthArr + i) = midNum;
				//交换地址
				temp = *(chArr + j);
				*(chArr + j) = *(chArr + i);
				*(chArr + i) = temp;
			}
		}
	}

}
void ASCIIOrder(char *head[],int lines) {
	char *temp;
	for (int i = 0; i < lines - 1; i++)
	{
		for (int j = i + 1; j < lines; j++)
		{
			if (strcmp(head[i], head[j]) > 0)
			{
				temp = head[i];
				head[i] = head[j];
				head[j] = temp;
			}

		}
	}

}

void deleteSpace(char *chArr) {
	int length;
	length = strlen(chArr);
	for (int i = 0; i < length; i++)
	{
		if (*(chArr + i) == ' ') {
			for (int j = i; j < length; j++)
			{
				*(chArr + j) = *(chArr + j + 1);//向前提一个,一直到length+1=='\0'
			}
		
		}
	}
}

void reverseChArr(char *chArr) {
	int length;
	int i = 0;
	char copyArr[4 * N];
	length = strlen(chArr);
	strncpy(copyArr, chArr, length + 1);
	while (*(copyArr+i)!='\0')
	{
		*(chArr + i) = *(copyArr + length - 1 - i);//reverse the sentence! 
		i++;
	}

}


char *string_in(char* chArr1, char *chArr2) {
	char *same = "\0";
	int length1, length2;
	length1 = strlen(chArr1);
	length2 = strlen(chArr2);
	if (length1 > length2) {
		for (int i = 0; i < length1 - length2 + 1; i++) {
			if (strncmp(chArr1+i, chArr2,length2) == 0)//仅截取length2长度以比较
			{
				same = chArr1 + i;
				break;
			}
		}
	}
	return same;
}
char* strncpyMy(char *str1, char* str2, int n) {
	for (int i = 0; i < n; i++)
	{
		if (*(str2 + i) != '\0') {//判断str2是否完结
			*(str1 + i) = *(str2 + i);
		}
		else {
			*(str1 + i) = '\0';
			break;
		}
	}
	return str1;

}
int is_within(char *chArr, char ch) {
	int i = 0;
	int flag = 0;
	while (*(chArr + i) != '\0')
	{
		if (*(chArr + i) == ch) {
			flag = i + 1;
			break;
		}
		i = i++;
	}
	return flag;


}
char* strChMy(char *chArr,char ch) {
	//找含第二个参数字符在第一个参数内第一次出现的指针
	int i = 0;
	char *temp = "";
	while (*(chArr + i) != '\0')
	{
		if (*(chArr + i) == ch) {
			temp = (chArr + i);
			break;
		}
		i = i++;
	}
	return temp;
}
void strCopyMy_2(char *chArr, int n) {
	char temp;
	for (int i = 0; i < n; i++)
	{
		if ((temp = getchar())!= ' ' && temp!='\n' && temp!='\t')//判断是否为所避关键字
		{
			*(chArr + i) = temp;
		}
		else {//如果是特殊字符,立刻结尾,破出
			chArr[i] = '\0';
			break;
		}
	}
	chArr[N] = '\0';//将最后一位给结束字符以免乱码!
}
void strCopyMy(char *chArr,int n) {
	//fgets(chArr, n, stdin);//fgets更安全!
	for (int i = 0; i < n; i++)
	{
		*(chArr + i) = getchar();
	}
	chArr[N] = '\0';//将最后一位给结束字符以免乱码!
}


 

相关文章推荐

C Peimer 第17章编程习题(部分)

高级数据结构,先前接触了许多类似的算法,有一些概念,但要说实现的话,其实对个人还是挺难的。本来这是一个机会,能够真正实现一下,以锻炼这点短处,可是每一小段程序都太费时间了,但实现出来收获却又不是特别的...

C Peimer 第13章编程习题

本来以为什么I\o,还不是手到擒来,结果就被人擒了。整整两天时间,都填这上头了。 #include #include #include #include #define BUFSIZE 10...

C Peimer 第12章编程习题

今天工作量不太大,终于到了动态内存分配,各种变量类型还是挺让人头疼的。 #include #include #include "pe12-2a.h" #define LENGTH 10 in...

C Peimer 第14章编程习题

大一学struct时并没有学好,现在果然要了命。这11道题摆弄了整整三天,终于完成了,但还是磕磕绊绊,唉…… #include #include #include #include ...

《Python核心编程》第11章 习题

11-2 函数。结合你对练习5-2的解,以便你创建一个带一对相同数字并同时返回它们之和以及产物的结合函数。 fun=lambda x,y:x+y print fun(1,3) 11-3 函数...
  • birdzb
  • birdzb
  • 2016年01月22日 16:15
  • 2394

【C专家编程】第8章-第11章

第8章 为什么程序员无法区分万圣节和圣诞节 oct 31 = dec 25 根据位模式构筑图形 整型提升:char、shot和位段类型(无论signed或unsigned)以及枚举类型被提升为int,...

《linux c 编程一站式学习》课后部分习题解答(一)

1、假设变量x和n是两个正整数,我们知道x/n这个表达式的结果要取Floor,例如x是17,n是4,则结果是4。如果希望结果取Ceiling应该怎么写表达式呢?例如x是17,n是4,则结果是5;x是1...

《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)

#include #include using namespace std; void stringRef(string& s) { s += " come blow"; } voi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C Peimer第11章编程习题
举报原因:
原因补充:

(最多只允许输入30个字)