- 从键盘上输入字符,将小写字母转换成大写字母。输入“ctl + z” 结束 。
#include#include //从键盘上输入字符,将小写字母转换成大写字母。输入“ctl + z” 结束 int main(){ char c; while (fflush(stdin),scanf_s("%c", &c) != EOF) { if (c >= 'a'&&c<='z' ) { c = c - 32; printf("%c", c); continue; } if (c >= 'A'&&c < ='Z') { printf("%c", c); continue; } else printf("error,input agian\n"); } printf("\n"); system("pause"); return 0;}
- 从键盘上输入字符,(1)分别统计一下其中字母,数字,其他字符的个数, (2)将统计的字母,数字,其他字符的个数以柱状图的形式打印。例如
5
*****
***** 3
***** ***** 2
***** ***** *****
***** ***** *****
alp num oth
#include#include typedef struct { int n; char *name;}elem;elem alp, num, oth, print[3], tmp;void paixu(elem *print){ if ((print[0].n < print[1].n) == 1) { tmp = print[0]; print[0] = print[1]; print[1] = tmp; } if ((print[0].n < print[2].n) == 1) { tmp = print[0]; print[0] = print[2]; print[2] = tmp; } if ((print[1].n < print[2].n) == 1) { tmp = print[1]; print[1] = print[2]; print[2] = tmp; }}int main(){ char c; int cnt[3] = { 0 }; int i = 0, j = 0; int maxc; alp.n = 0; num.n = 0; oth.n = 0; alp.name = "alp"; num.name = "num"; oth.name = "oth"; int flag = 1; while (fflush(stdin), scanf_s("%c", &c) != EOF) { if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z') { alp.n++; printf("%c", c); continue; } else if (c >= 48 && c <= 57) { num.n++; printf("%c", c); continue; } else if (c != '\0') { oth.n++; printf("%c", c); } if (c == '\n')break; } oth.n--; print[0] = alp; print[1] = num; print[2] = oth; paixu(print); for (i = 0; i <= print[0].n; i++) { printf("\n"); if (print[0].n - i <= print[0].n) { if (print[0].n - i == print[0].n) { if (print[0].n == print[1].n) { if (print[1].n == print[2].n) printf("%s\t%s\t%s", print[0].name, print[1].name, print[2].name); else printf("%s\t%s", print[0].name, print[1].name); } else printf("%s", print[0].name); continue; } printf("*****\t"); // if (print[0].n - i <= print[1].n) { if (print[0].n - i == print[1].n) { if (print[1].n == print[2].n) printf("%s\t%s", print[1].name, print[2].name); else printf("%s", print[1].name); continue; } printf("*****\t"); / if (print[0].n - i <= print[2].n) { if (print[0].n - i == print[2].n) { printf("%s", print[2].name); continue; } printf("*****\t"); } } } } printf("\n"); printf("%d\t", print[0].n); printf("%d\t", print[1].n); printf("%d\t", print[2].n); printf("\n"); printf("\n"); system("pause"); return 0;}
3.进制转换。
(1) 将十进制数转换成二进制数。输入十进制数输出对应的二进制数,输入“ctl + z”结束。
#include#include #define N 8void fun(int num, int k){ int arr[N], i; for (i = 0; i = 0; i--) printf("%d", arr[i]); printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n, 2); } printf("\n"); system("pause"); return 0;}
(2)将二进制转换成十进制数。输入二进制数输出对应的十进制数,输入“ctl + z”结束
#include#include #include int main(){ char *p, s[6]; int n; p = s; gets(p); n = 0; while (*p != '\0') { n = n * 2 + *p - '0'; p++; } printf("%d", n); system("pause"); return 0;}
(3) 将十进制数转换成十六进制数。输入十进制数输出对应的十六进制数。输入“ctl + z” 结束。
#include#include #define N 8void fun(int num){ int arr[N], i; for (i = 0; i = 0; i--) switch (arr[i]) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13: printf("D"); break; case 14: printf("E"); break; case 15: printf("F"); break; default: printf("%d", arr[i]); } printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n); } printf("\n"); system("pause"); return 0;}
(4) 将十六进制数转换成十进制数。输入十六进制数输出对应的十进制数。输入“ctl + z” 结束。
#include#include #include int main(){ char *p, s[6]; int n; p = s; gets(p); n = 0; while (*p != '\0') { switch (*p) { case '0':n = 0; break; case '1':n = 1; break; case '2':n = 2; break; case '3':n = 3; break; case '4':n = 4; break; case '5':n = 5; break; case '6':n = 6; break; case '7':n = 7; break; case '8':n = 8; break; case '9':n = 9; break; case 'A':n = 10; break; case 'B':n = 11; break; case 'C':n = 12; break; case 'D':n = 13; break; case 'E':n = 14; break; case 'F':n = 15; break; default:break; } n = n * 16 + *p - '0'; p++; } printf("%d", n); system("pause"); return 0;}
4.统计一个整数对应的二进制数的1的个数。输入一个整数(可正可负), 输出该整数的二进制包含1的个数, “ctl+ z” 结束。
#include#include #define N 8void fun(int num, int k){ int arr[N], i, cnt = 0; for (i = 0; i = 0; i--) { if (arr[i] == 1)cnt++; printf("%d", arr[i]); } printf("共有%d个1",cnt); printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n, 2); } printf("\n"); system("pause"); return 0;}
5.有101个整数,其中有50个数出现了两次,1个数出现了一次, 找出出现了一次的那个数。
#include#include #include int main(){ int a[101]; int i, ret = 0; for (i = 0; i < 50; i++) a[i] = i + 1; for (i = 51; i <= 100; i++) a[i] = i - 50; scanf_s("%d", &a[50]); for (i = 0; i <= 100; ++i) { ret = ret ^ a[i]; } printf("%d\n", ret); printf("\n"); system("pause"); return 0;}
6.
(1)输入年月日,输出该日期是当年的第几天。
#include#include int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year, mon, day,sum=0; scanf_s("%d%d%d", &year, &mon, &day); for (int i = 0; i < mon - 1; i++)sum += a[i]; sum += day; if (mon > 2)sum =sum+(year % 4 && year % 100 || year % 400); printf("%d", sum); printf("\n"); system("pause"); return 0;};
(2)输入两个日期(年 月 日 年 月 日), 输出这两个日期之间差多少天
#include#include int isleap(int year){ return year % 4==0 && year % 100==0 || year % 400==0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 0, mon1 = 0, day1 = 0, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0; scanf_s("%d %d %d", &year1, &mon1, &day1); scanf_s("%d %d %d", &year2, &mon2, &day2); for (int i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); printf("%d天", sum1); for (int i = 0; i < mon2 - 1; i++)sum2 += a[i]; sum2 += day2; if (mon2 > 2)sum2 = sum2 + isleap(year2); printf("%d天", sum2); gap = abs(year2 - year1); sum = sum2 - sum1 + gap * 365 + gap / 4 ; printf("%d天", sum); printf("\n"); system("pause"); return 0;}
(3)输入一个日期,输出该日期是星期几。
#include#include int isleap(int year){ return year % 4==0 && year % 100==0 || year % 400==0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 2018, mon1 = 3, day1 = 4, year2 =0 , mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0; int week = 0; scanf_s("%d %d %d", &year2, &mon2, &day2); for (int i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); for (int i = 0; i < mon2 - 1; i++)sum2 += a[i]; sum2 += day2; if (mon2 > 2)sum2 = sum2 + isleap(year2); gap = abs(year1 - year2); sum = sum2 + gap * 365 + gap / 4 - sum1; week = sum; while (week /7!=0) { week %= 7; } if(week==0||week==7) printf("星期日", week); else printf("星期%d", week); printf("\n"); system("pause"); return 0;}
4.输入 一个日期 和一个整数 n,输出从该日期起经过n天以后的日期。
-> date -> tomorrow of the date
#include#include int isleap(int year){ return year % 4 == 0 && year % 100 == 0 || year % 400 == 0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 2018, mon1 = 3, day1 = 5, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 1, days = 0; int week = 0; int i; scanf_s("%d", &days); for (i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); sum = sum1 + days; year2 = year1; while (sum > 365 + isleap(year2)) { year2 = year2 + gap; sum -= 365 - isleap(year2); } ; if (sum < 365 + isleap(year2)) { for (i = 0; sum > a[i]; i++) { sum -= a[i]; } i++; mon2 = i; day2 = sum; printf("%d年%d月%d日", year2, mon2, day2); } printf("\n"); system("pause"); return 0;}