c/c++练习–13
- 习题来源:C语言经典编程282例
121.求直角三角形的斜边
#include <stdio.h>
#include <math.h>
int main(void){
float m,n;
printf("Please input two num\n");
scanf("%f %f",&n,&m);
printf("直角三角形的斜边为:%f\n", sqrt(m*m+n*n));
return(0);
}
122.相对最小整数
#include <stdio.h>
#include <math.h>
int main(void){
float m;
printf("Please input a num\n");
scanf("%f",&m);
printf("相对最小整数为:%f\n", ceil(m));
return(0);
}
123.当前时间转换
将当前时间按转换为格林威治时间,同时将时间和格林威治时间输出
#include <stdio.h>
#include <time.h>
int main(void){
time_t tim;
struct tm *t,*gt;
tim = time(NULL);
t = localtime(&tim);
printf("Local time is %s\n",asctime(t));
gt = gmtime(&tim);
printf("Greenwich time is %s\n",asctime(gt));
return(0);
}
124.显示程序运行时间
#include <stdio.h>
#include <time.h>
int main(void){
time_t s_time,e_time;
s_time = time(NULL);
for(int i=0;i<100000;i++)
for(int j=0;j<1000;j++)
;
e_time = time(NULL);
printf("runtime is %f",difftime(e_time,s_time));
return(0);
}
125.显示当前日期及时间
#include <stdio.h>
#include <time.h>
int main(void){
time_t s_time;
struct tm *t;
s_time = time(NULL);
t = localtime(&s_time);
printf("data: %d--%d--%d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
printf("time:%d:%d:%d\n",t->tm_hour,t->tm_min,t->tm_sec);
return(0);
}
126.任意大写字母转小写
#include <stdio.h>
#include<string.h>
int main(void){
char str[100];
gets(str);
strlwr(str);
puts(str);
return(0);
}
127.字符串复制到指定的空间
从键盘中输入字符串1和字符串2,将字符串内容保存到内存空间中
#include <stdio.h>
#include<string.h>
int main(void){
char str1[100],str2[100],*p1,*p2;
gets(str1);
gets(str2);
p1 = str1;
p2 = str2;
puts(p1);
puts(p2);
return(0);
}
128.查找位置信息
从键盘中输入字符串1和字符串2,查找str1字符串中第一个不属于str2字符串中字符的位置,并将该位置输出,在从键盘中输入str3和str4,查找在str3中是否包含str4。
#include <stdio.h>
#include<string.h>
int main(void){
char str1[100],str2[100];
int pos;
gets(str1);
gets(str2);
pos = strspn(str1, str2);
printf("%d",pos);
gets(str1);
gets(str2);
pos = strspn(str1, str2);
printf("%d",pos);
return(0);
}
129.字母检测
从键盘中任意输入一个字母或数字或其他字符,当输入字母时提示“输入的是字母”,否则提示“输入的不是字母”
#include <stdio.h>
#include<ctype.h>
int main(void){
char ch;
while((ch = getchar()) != EOF){
if(isalpha(ch)) printf("Yes\n");
else printf("No\n");
while((ch = getchar()) != '\n');
;
}
return(0);
}
130.对数组进行升序和降序排列
对包含有10个元素125,-26,53,12,-6,95,46,85,-45,785的数分别进行升序与降序排列
#include <stdio.h>
#include<ctype.h>
void num_up(int *p, int size){
int tmp;
for(int i=0;i<size;i++) {
for(int j=0;j<size;j++) {
if(p[i]<p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp ;
}
}
}
}
void num_down(int *p, int size){
int tmp;
for(int i=0;i<size;i++) {
for(int j=0;j<size;j++) {
if(p[i]>p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp ;
}
}
}
}
int main(void){
int num[]={125,-26,53,12,-6,95,46,85,-45,785};
printf("原始序列:");
for(int i=0;i<10;i++)
printf("%d\t",num[i]);
printf("\n升序序列:");
num_up(num,10);
for(int i=0;i<10;i++)
printf("%d\t",num[i]);
printf("\n降序序列:");
num_down(num,10);
for(int i=0;i<10;i++)
printf("%d\t",num[i]);
return(0);
}