homework_18

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
//例如:命令行参数输入:test.exe - a 1 2  >执行1 + 2输出3

int main(int argc,char* argv[]){
	double x = 0;
	double y = 0;
	char chose[3] = {0};
	if (argc != 4){
		printf("parameter error\n");
		return 0;
	}

	x = atoi(argv[2])*1.0;
	y = atoi(argv[3])*1.0;

	switch (*(argv[1] + 1)){
	case 'a':
		printf("%f\n",x+y);
		break;
	case 's':
		printf("%f\n",x-y);
		break;
	case 'm':
		printf("%f\n", x*y);
		break;
	case 'd':
		if (y == 0){
			printf("The divisor cannot be 0 !\n");
		}
		else{
			printf("%f\n", x / y);
		}
		break;
	}
	system("pause");
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

//字符串比较大小的函数
//返回0说明相等,返回负数是str1小于str2
int my_cmp(const char* str1, const char* str2){
	assert(str1 != NULL && str2 != NULL);
	while (*str1++ && *str2++){
		if (*str1 != *str2){
			return  *str1 - *str2;
		}
	}
	return 0;
}
//排序的函数
void my_sort(char *str[],int sz){
	int i = 0;
	int j = 0;
	int flag = 0;
	for (i = 0; i < sz-1; i++){
		for (j = 0; j < sz - i - 1; j++){
			if (my_cmp(str[j], str[j+1]) > 0){
				char* tmp = str[j];
				str[j] = str[j+1];
				str[j+1] = tmp;
				flag = 1;
			}
		}
		if (flag == 0){
			//说明本身就是有序的,无需再次排列
			break;
		}
	}
	
}
int main(void){
	char *str[] = { "DEFG", "ABC", "WXYZ", "LMN", "OPQRST" };//指针数组
	
	int i = 0;
	int sz = sizeof(str) / sizeof(str[0]);
	for (i = 0; i < sz; i++){
		printf("%s ",str[i]);
	}
	my_sort(str, sz);
	printf("\n---------------\n");
	for (i = 0; i < sz; i++){
		printf("%s ", str[i]);
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38032942/article/details/80350975