|
阅读:2659回复:0
c语言练习题1
/*
1.编写一个程序,定义一个判断字符是大写字母的宏, 一个判断字符是小写字母的宏以及实现大小写字母相互转换的宏, 并将用户输入的一个字符串中的大小写字母互换。 */ #include <stdlib.h> #include <stdio.h> #define ISUPPER(c) ((c)>='A' && (c)<='Z') #define ISLOWER(c) ((c)>='a' && (c)<='z') #define TOLOWER(c) (ISUPPER((c)) ? ((c) + ('a' - 'A') ) : (c)) #define TOUPPER(c) (ISLOWER((c)) ? ((c) - ('a' - 'A') ) : (c)) int main(){ char s[20]; int i; printf("请输入字符串"); scanf("%s",s); for(i=0;s;i++){ if(ISUPPER(s)){ s = TOLOWER(s); }else if(ISLOWER(s)){ s = TOUPPER(s); } } printf("转换的结果为:%s\n",s); return 0; } /* 2.设计一个程序,输入应付款金额(0-100之间),如以百元钞付款,应找回最少的钱币个数50元、 20元、10元、5元、1元各多少张。 */ #include <stdlib.h> #include <stdio.h> int main(){ int m; int ws,es,s,w,y; printf("请输入应付金额(0-100)"); scanf("%d",&m); ws = (100 - m) / 50; es = ((100 - m) - ws *50) / 20; s = ((100 - m) - ws *50 - es*20) / 10; w = ((100 - m)- ws *50 - es*20)%10/5; y = (100 - m)- ws *50- es*20 - s*10 - w*5; printf("找零:%d\n50元:%d张\n20元:%d张\n10元:%d张\n5元:%d张\n1元:%d张",100-m,ws,es,s,w,y); return 0; } //方法二 int main(){ int a ,count; printf("请输入应付金额(0,100)"); scanf("%d",&a); if( a>= 100 || a <= 0){ printf("输入有误,金额需要在0-100之间"); }else{ a = 100 - a; printf("需找回"); count = a / 50; if(count>0){ printf("50元%d张",count); a -= count * 50; } count = a/20; if(count>0){ printf("20元%d张",count); a -= count * 20; } count = a/10; if(count>0){ printf("10元%d张",count); a -= count * 10; } count = a/5; if(count>0){ printf("5元%d张",count); a -= count * 5; } if(a>0){ printf("5元%d张",a); } } printf("完毕。"); return 0; } /* 3.编写一个函数fun(char cl ,char c2, char * s),其中包含3个参数,两个是字符型, 一个是字符串,该函数返回一个整数。函数的功能是查找字符c1是否在字符串中出现,如果出现, 则用字符c2取代c1,并统计字符c1出现的次数,然后将次数作为函数返回值。 编写主函数调用该函数。 */ #include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ int fun(char c1, char c2, char *s); int count; char str[20],search,re; printf("请输入字符串,查找字符串,替换字符串"); scanf("%s %c %c",str,&search,&re); count = fun(search,re,str); printf("出现的次数为:%d\n",count); printf("替换后的字符串为%s",str); return 0; } int fun(char c1, char c2, char *s){ int count=0,i,len; printf("%c,%c,%s",c1,c2,s); len = strlen(s); for(i=0;i<len;i++){ if(s==c1){ s = c2; count++; } } return count; } /* 4.编写程序,输入20个大于0的整数存入数组, 用指针的方法对数组中元素排序并输出。 注意:指针p 重新指向数组; */ #include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ int arr[5],*p,temp,i; printf("请输入5个数\n"); p = arr; for(i=0;i<5;i++){ scanf("%d",p++); } p = arr; printf("用户输入的数据:\n"); for(i=0; i<5; i++){ printf(" %d",*p); p++; } printf("\n "); // 排序 for(i=0;i < 4; i++){ for( p = arr; p < arr+5-i-1 ; p++){ if(*p >(*(p+1))){ //交换 temp = *p; *p = *(p+1); *(p+1) = temp; } } } p = arr; printf("排序后的数据:\n"); for(i=0;i<5;i++){ printf(" %d",*p); p++; } printf("\n "); return 0; } |
|
|