C语言日常小练习-2.4

1.使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
例如:命令行参数输入:test.exe -a 1 2
执行1+2输出3
#include <stdio.h>
#include <Windows.h>
#include <string.h> 
int main(int argc, char* argv[])
{
	int c = 0;
	int a = atoi(argv[2]);
	int b = atoi(argv[3]);

	if (strcmp(argv[1], "-a") == 0){
		c = a + b;
	}

	else if (strcmp(argv[1], "-s") == 0){
		c = a - b;
	}

	else if (strcmp(argv[1], "-m") == 0){
		c = a * b;
	}

	else if (strcmp(argv[1], "-d") == 0){
		c = a / b;
	}

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

	system("pause");
	return 0;
}


2.写冒泡排序可以排序多个字符串。
#include <stdio.h>
#include <Windows.h>

void bubble_sort_str(char *str[], int sz)
{
	int i = 0;
	for (i = 0; i < sz - 1; i++){
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++){
			if (strcmp(*(str + j), *(str + j + 1))>0){
				char *tmp = *(str + j);
				*(str + j) = *(str + j + 1);
				*(str + j + 1) = tmp;
			}
		}
	}
}
int main()
{
	int i = 0;
	char *str[] = { 
		"I love u !",
		"Can I help you ?",
		"Do you love me ?", 
		"How do you think of ?" 
	};
	int sz = sizeof(str) / sizeof(*str);

	bubble_sort_str(str, sz);

	for (i = 0; i < sz; i++)
	{
		printf("%s\n", *(str + i));
	}
	
	system("pause");
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_39026129/article/details/80330185