C语言经典编程282例15(数组排序等)

目录

127 求直角三角形的斜边

128 相对的最小整数和最大整数

129   获取当前日期与时间

130 当前时间转换

131 显示程序运行时间

132 显示当前日期及时间

133 任意大写字母转小写

134 字符串复制到指定空间

135 查找位置信息

136 字母检测

137 对数组进行升序和降序


127 求直角三角形的斜边

求直角三角形的斜边,运行程序输入三角形的2个直角边,输出对应的斜边长度。、

方式一: 

 #include <stdio.h> 

int cal(int a, int b)
{
   return sqrt(pow(a, 2) + pow(b, 2));
}

void main()   
{ 
    int a, b, c;
    printf("输入2边:");
    scanf("%d %d", &a, &b);
	
	c = cal(a, b);
	
	printf("三角形斜边:%d", c);	
}  

方式二:

在math.c中给定直角三角形的2个直角边求其斜边长度的函数

double hypot(double a, double b) 

 #include <stdio.h> 
 #include <math.h> 

int cal(int a, int b)
{
   return sqrt(pow(a, 2) + pow(b, 2));
}

void main()   
{ 
    int a, b, c;
    printf("输入2边:");
    scanf("%d %d", &a, &b);
	
	c = hypot(a, b);
	
	printf("三角形斜边:%d", c);	
}  

 128 相对的最小整数和最大整数

利用数学函数实现以下功能:从键盘中输入一个数,求不小于该数的最小整数和不大于该数的最大整数

在 math.h中:

ceil(x):不小于x的最小整数类型,其中x为double

floor(x):不大于x的最小整数类型,其中x为double

 #include <stdio.h> 
 #include <math.h> 

void main()   
{ 
    float a, value1, value2;
    printf("输入值:");
    scanf("%f", &a);
	
    value1 = ceil(a);
    value2 = floor(a);
	
	printf("不小于a最小整数: %f\n", value1);	
	printf("不大于a最整数: %f", value2);	
}  

 

129   获取当前日期与时间

显示当地时间和日期:

char *ctime(const time_t *tp): ctime函数将结构*tp中的日历时间转换为当地时间

 #include <stdio.h> 
// #include <stdlib.h>
 #include <time.h> 
     
void main()   
{ 
    time_t local;
    
    local = time(NULL);
    
    printf("时间:%s", ctime(&local));
}  

130 当前时间转换

编程实现将当前时间转换成格林威治时间。

格林尼治平均时间(英语:Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台当地的平太阳时,因为本初子午线被定义为通过那里的经线

 struct tm *localtime(const time_t *tp):将*tp中日历时间转换为当地时间

 struct tm *gmtime(const time_t *tp): 将*tp中的日历时间转换为协调世界时间(UTC)

 #include <stdio.h> 
 #include <math.h> 
 #include <time.h>

void main()   
{ 
   time_t local;
   struct tm *p, *ucttime;
   
   local = time(NULL);
   p = localtime(&local);
   printf("当前时间:%s\n", asctime(p));
   
   ucttime = gmtime(&local);
   printf("UTC时间:%s", asctime(ucttime));
   
}  

 131 显示程序运行时间

编程实现求应该程序运行时间,以秒为单位。

double difftime(time_t time2, time_t time1):返回time2 - time1的值(以秒为单位)

 #include <stdio.h> 
 #include <math.h> 
 #include <time.h>

void main()   
{ 
   time_t start, end;
   double diff;
   
   start = time(NULL);
   
   sleep(10);
   
   end = time(NULL);
   
   diff = difftime(end, start);
   
   printf("程序运行时间:%f", diff );
}  

132 显示当前日期及时间

编程实现在屏幕中显示当前日期及时间

 #include <stdio.h> 
 #include <math.h> 
 #include <time.h>

void main()   
{ 
   time_t local;
   struct tm *p;
   
   local = time(NULL);
   p = localtime(&local);
   
   printf("year = %d\n", p->tm_year + 1900);
   printf("month = %d\n", p->tm_mon + 1);
   printf("day = %d\n", p->tm_mday);
   printf("hour = %d\n", p->tm_hour);
   printf("min = %d\n", p->tm_min);
}  

 

133 任意大写字母转小写

利用strlwr()函数实现将输入的大写字母转换成小写字母。

strlwr( char* string): 函数实现将输入的大写字母转换成小写字母。

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

void main()   
{
	char str[100];

	gets(str);
	puts(str);

	strlwr(str);
	puts(str);
}  

134 字符串复制到指定空间

从键盘中输入字符串1和字符串2,将字符串内容保存到内存空间中。

strdup() :  将参数给出的字符串复制到一个安全的地方。此安全空间通过调用malloc 获得:

char *strdup(char *s)   /* 构建一个 s 的副本 */ 
    { 
        char *p; 
         
        p = (char *) malloc(strlen(s)+1); /* +1 为了存放‘\0’ */ 
        if (p != NULL) 
            strcpy(p, s); 
        return p; 
    } 

若没有空间可以分配,malloc 返回 NULL;strdup 将此值传回,把错误处理留给它的调用者。 

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

void main()   
{
	char str[100], *p;
	
	gets(str);
	p = strdup(str);
	puts(p);

}  

 135 查找位置信息

从键盘中输入str1 和 str2,查找str1字符串中第一个属于str2字符串中字符的位置,并将该位置输出,再从键盘输入str3和str4,查找str3中是否包含str4,无论包含与否给出提示信息。

char *strstr(cs,ct): 返回一个指针,指向字符串ct第一次出现在字符串cs中的位置,如果cs中不包含字符串ct,则返回NULL

size_t strspn(cs,ct): 返回字符串cs中包含的字符的前缀长度

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

void main()   
{
	char str1[100], str2[100], str3[100], str4[100];
	char *pos;
	int  len;
	
	gets(str1);
	gets(str2);
	gets(str3);
	gets(str4);
	
	pos = strstr(str1, str2);
	
	printf("str2 第一次出现在str1中的位置:%s\n:", pos);
	
	len = strspn(str3, str4);
	
	if(len)
	{
		printf("str3 中包含str4的长度:%d:", len);
	}
	else
	{
		printf("str3 中不包含str4的长度");
	}

}  

136 字母检测

从键盘中任意输入一个字母或数字或其他字符,编程实现当输入字母时提示“输入的是字母”,否则提示“输入的不是字母”

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

void main()   
{
	char c;
	
	while(1)
	{
		printf("输入的字符:");
		c = getchar();
	
		if(isalpha(c))
		{
			printf("输入的字符是字母\n");
		}
		else
		{
			printf("输入的字符不是字母\n");
	 	} 
	} 	
}  

现象: 在运行时,发现会出现2次判断输入是否是字母的提示语句。如下图,

原因: 将 输入的回车键也当成一个输入的符号。

 

解决:

多定义一个字符型变量,用来保存 回车键 

char c, c1;

 

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

void main()   
{
	char c, c1;
	
	while(1)
	{
		printf("输入的字符:");
		c = getchar();
		c1 = getchar();
	
		if(isalpha(c))
		{
			printf("输入的字符是字母\n");
		}
		else
		{
			printf("输入的字符不是字母\n");
	 	} 
	} 	
}  

137 对数组进行升序和降序

对包含10个元素的数组进行升序和降序排序。

在实用函数:stdlib.h库函数中:

qsort函数:对数组进行排序

参数

  • base -- 指向要排序的数组的第一个元素的指针。
  • nitems -- 由 base 指向的数组中元素的个数。
  • size -- 数组中每个元素的大小,以字节为单位。
  • compar -- 用来比较两个元素的函数。
 void qsort(void *v[], int left, int right, 
            int (*comp)(void *, void *)) 
    { 
        int i, last; 
        void swap(void *v[], int, int); 
         
        if (left >= right)  /* 如果数组包含元素小于两个 */ 
            return;      /* 则什么也不做 */ 
        swap(v, left, (left + right)/2); 
        last = left; 
        for (i = left+1; i <= right; i++) 
            if ((*comp)(v[i], v[left]) < 0) 
                swap(v, ++last, i); 
        swap(v, left, last); 
        qsort(v, left, last-1, comp); 
        qsort(v, last+1, right, comp);    
   } 

 int (*comp)(void *, void *) 它表示 comp 是一个函数,该函数有两个 void * 参数,并返回一个 int 值。 
在代码行 
    if ((*comp)(v[i], v[left]) < 0) 中 comp 的使用与是符合其声明的:comp 是一个函数的指针,*comp 是该函数,

而 
    (*comp)(v[i], v[left]) 则是对此函数的调用。

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

int Ascending (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int Descending (const void * a, const void * b)
{
   return ( *(int*)b - *(int*)a );
}

void main()   
{
	int array[10];
	int i;
	
	for(i = 0; i < (sizeof(array) / sizeof(int)); i++)
	{
		scanf("%d", &array[i]);
	}
	
	qsort(&array, 10,  sizeof(int), Ascending );
	
	printf("输出升序 \n");
	for(i = 0; i < (sizeof(array) / sizeof(int)); i++)
	{
		printf(" %d ", array[i]);
	}
	
	qsort(&array, 10,  sizeof(int), Descending );
	printf("\n 输出降序 \n");
	
	for(i = 0; i < (sizeof(array) / sizeof(int)); i++)
	{
		printf(" %d ", array[i]);
	}		
}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值