练习1
从键盘上输入字符,将小写字母转换成大写字母。输入“ctrl + z” 结束。
#include<stdio.h>
#include<stdlib.h>
int main() {
char c;
while (scanf("%c", &c) != EOF) {
c != '\n' ? printf("%c", c - 32) : printf("\n");
}
system("pause");
}
练习2
从键盘上输入字符
(1)分别统计一下其中字母,数字,其他字符的个数;
(2)将统计的字母,数字,其他字符的个数以柱状图的形式打印(柱子从高到低)。例如
5
*****
***** 3
***** ***** 2
***** ***** *****
***** ***** *****
alp num oth
#include<stdio.h>
#include<stdlib.h>
void show(int a[]) {
//定义三个整型变量
int max = 0, mid = 0, min = 0;
max = a[0] >= a[1] ? (a[0] >= a[2] ? a[0] : a[2]) : (a[1] >= a[2] ? a[1] : a[2]);
mid = a[0] >= a[1] ? (a[0] >= a[2] ? (a[1] >= a[2] ? a[1] : a[2]) : a[0]) : (a[0] >= a[2] ? a[0] : (a[1] >= a[2] ? a[2] : a[1]));
min = a[0] <= a[1] ? (a[0] <= a[2] ? a[0] : a[2]) : (a[1] <= a[2] ? a[1] : a[2]);
for (int i = max; i > (-1); i--) {
if (i == max)
printf("%3d", i);
else if (i < max) {
printf("***** ");
if (i == mid)
printf("%3d", i);
else if (i < mid) {
printf("***** ");
if (i == min)
printf("%3d", i);
else if (i < min)
printf("***** ");
}
}
printf("\n");
}
if (a[0] >= a[1] && a[1] >= a[2])
printf(" alp num oth");
else if(a[0] >= a[2] && a[2] >= a[1])
printf(" alp oth num");
else if (a[1] >= a[0] && a[0] >= a[2])
printf(" num alp oth");
else if (a[1] >= a[2] && a[2] >= a[0])
printf(" num oth alp");
else if (a[2] >= a[0] && a[0] >= a[1])
printf(" oth alp num");
else
printf(" oth num alp");
}
int main() {
char c;
//定义一个含三个元素的一维数组,分别保存各类字符出现的次数
//其中a[0]表示alp、a[1]表示num、a[2]表示oth
int a[3] = { 0,0,0 }, ret;
while (ret = scanf("%c",&c) != EOF) {
if ((65 <= c && c <= 90) || (97 <= c && c <= 122))
++a[0];
else if (48 <= c && c <= 57)
++a[1];
else
++a[2];
}
//printf("%d %d %d", a[0], a[1], a[2]);
show(a);
printf("\n");
system("pause");
}
练习3
进制转换:
(1) 将十进制数转换成二进制数。输入十进制数输出对应的二进制数,输入“ctrl + z”结束。
(2) 将二进制转换成十进制数。输入二进制数输出对应的十进制数,输入“ctrl + z”结束。
(3) 将十进制数转换成十六进制数。输入十进制数输出对应的十六进制数。输入“ctrl + z” 结束。
(4) 将十六进制数转换成十进制数。输入十六进制数输出对应的十进制数。输入“ctrl + z” 结束。
10进制转2进制:
2进制转10进制:
10进制转16进制
16进制转10进制
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//10->2
void DECtoBIN(){
int i = 0,counter = 0,remainder[16];
printf("Please enter a decimal number: ");
while(rewind(stdin),scanf("%d",&i)!=EOF){
counter = 0;
//每次循环结束后都要将这个数组重新初始化,否则若前一次输入的数字比后一次大,则输出结果不满足要求
for (int k = 0; k < 16; ++k)
remainder[k] = 0;
while (i) {
remainder[counter++] = i % 2;
i = i / 2;
}
for (int k = 15; k >= 0; --k) {
if (remainder[k]==1){
for (int j = k; j >= 0; --j)
printf("%d", remainder[j]);
break; //结束循环
}
}
printf("\nPlease enter a decimal number: ");
}
}
//2->10
void BINtoDEC() {
//利用counter来记录幂
int i = 0, remainder = 0, counter = 0, num = 0;
printf("Please enter a binary number: ");
while (rewind(stdin), scanf("%d", &i) != EOF) {
remainder = 0;
counter = 0;
num = 0;
while (i) {
remainder = i % 10;
num += remainder * pow(2, counter++);
i = i / 10;
}
printf("%d", num);
printf("\nPlease enter a binary number: ");
}
}
//10->16
void DECtoHEX() {
int i = 0;
printf("Please enter a decimal number: ");
while (rewind(stdin), scanf("%d", &i) != EOF) {
printf("%x", i);
printf("\nPlease enter a decimal number: ");
}
}
//16->10
void HEXtoDEC() {
int i = 0;
printf("Please enter a hexadecimal number: ");
while (rewind(stdin), scanf("%x", &i) != EOF) {
printf("%d", i);
printf("\nPlease enter a hexadecimal number: ");
}
}
int main() {
int m = 0, n = 0;
//HEX-16进制 DEC-10进制 BIN-2进制
printf("Please enter 16, 10 or 2: ");
scanf("%d", &m);
switch (m) {
case 16:
HEXtoDEC();
break;
case 10:
printf("Please enter 16 or 2: ");
scanf("%d", &n);
if(n==2)
DECtoBIN();
else
DECtoHEX();
break;
case 2:
BINtoDEC();
}
system("pause");
}
练习4
统计一个整数对应的二进制数的1的个数。输入一个整数(可正可负), 输出该整数的二进制包含1的个数, “ctrl+ z” 结束。
#include<stdio.h>
#include<stdlib.h>
int main() {
int i = 0,remainder = 0,counter = 0;
printf("Please enter a decimal number: ");
while (rewind(stdin),scanf("%d", &i) != EOF) {
counter = 0; //每次循环要重新初始化counter变量
while(i){
remainder = i % 2;
if (remainder)
++counter;
i /= 2;
}
printf("%d", counter);
printf("\nPlease enter a decimal number: ");
}
system("pause");
}
练习5
练习5.1
输入年月日,输出该日期是当年的第几天。
#include<stdio.h>
#include<stdlib.h>
int main() {
int year, mon, day, total;
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
printf("Please enter the year month and day: ");
while (scanf("%d%d%d", &year, &mon, &day) != EOF) {
total = 0;
for (int i = 0; i < mon - 1; ++i) {
total += date[i];
}
total += day;
if (mon > 2) {
total += (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
printf("%4d-%02d-%02d is %dth days of that year.\n", year, mon, day, total);
printf("\nPlease enter the year month and day: ");
}
system("pause");
}
练习5.2
输入两个日期(年 月 日 年 月 日), 输出这两个日期之间差多少天。
公式:sum = delt(year1 year2) - dayofyear(date1) + dayofyear(date2)
#include<stdio.h>
#include<stdlib.h>
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int DOY(int y, int m, int d) {
int total = 0;
for (int i = 0; i < m - 1; ++i) {
total += date[i];
}
total += d;
if (m > 2) {
total += (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
return total;
}
int delt(int p, int q) {
int total = 0;
for (p; p < q; ++p) {
if (p % 4 == 0 && p % 100 != 0 || p % 400 == 0) {
total += 366;
}
else {
total += 365;
}
}
return total;
}
int main() {
int y1, m1, d1, y2, m2, d2, difference;
printf("Please enter a date of any two days: \n");
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
while (scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2) != EOF) {
difference = delt(y1, y2) - DOY(y1, m1, d1) + DOY(y2, m2, d2);
printf("There is a difference of %d days between these two days.\n", difference);
printf("\nPlease enter a date of any two days:\n");
}
system("pause");
}
练习5.3
输入一个日期,输出该日期是星期几。
公式: a. delt % 7 b. (7 – delt % 7) % 7
#include<stdio.h>
#include<stdlib.h>
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int DOY(int y, int m, int d) {
int total = 0;
for (int i = 0; i < m - 1; ++i) {
total += date[i];
}
total += d;
if (m > 2) {
total += (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
return total;
}
int delt(int p, int q) {
int total = 0;
for (p; p < q; ++p) {
if (p % 4 == 0 && p % 100 != 0 || p % 400 == 0) {
total += 366;
}
else {
total += 365;
}
}
return total;
}
void week(int year,int mon, int day) {
int n, difference;
difference = delt(2018, year) - DOY(2018, 1, 1) + DOY(year, mon, day);
n = difference % 7 + 1;
switch (n) {
case 1:
printf("The day is Monday."); break;
case 2:
printf("The day is Tuesday."); break;
case 3:
printf("The day is Wednesday."); break;
case 4:
printf("The day is Thursday."); break;
case 5:
printf("The day is Friday."); break;
case 6:
printf("The day is Saturday."); break;
case 0:
printf("The day is Sunday."); break;
}
}
int main() {
int year, mon, day, n;
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
printf("Please enter the date of day: ");
while (scanf("%d%d%d", &year, &mon, &day) != EOF) {
week(year, mon, day);
printf("\nPlease enter the date of day: ");
}
system("pause");
}
练习5.4
输入 一个日期 和一个整数 n,输出从该日期起经过n天以后的日期。
#include<stdio.h>
#include<stdlib.h>
#define Judge if(0==--n) return;
int isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
}
else {
return 0;
}
}
void calDate(int *y, int *m, int *d, int n) {
int leap = 0;
int date[][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31} };
while (n != 0) {
leap = isLeap(*y);
while (*m <= 12) {
while (*d < date[leap][*m]) {
++*d;
Judge;
}
*d = 1;
++*m;
if (*m <= 12)
Judge;
}
*m = 1;
++*y;
Judge;
}
}
int main() {
int year, mon, day,differ;
while (printf("Please enter a date: "),scanf("%d%d%d", &year, &mon, &day) != EOF) {
printf("Please enter a difference: ");
scanf("%d", &differ);
calDate(&year, &mon, &day, differ);
printf("The date of that day is %4d-%02d-%02d.\n", year, mon, day);
year = 0, mon = 0, day = 0, differ = 0;
}
}
练习5.5
输入一个年份、月份,输出该月份的日历。
#include<stdio.h>
#include<stdlib.h>
int isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
}
else {
return 0;
}
}
int DOY(int y, int m, int d) {
int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int total = 0;
for (int i = 0; i < m - 1; ++i) {
total += date[i];
}
total += d;
if (m > 2) {
total += (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
return total;
}
int delt(int p, int q) {
int total = 0;
for (p; p < q; ++p) {
if (p % 4 == 0 && p % 100 != 0 || p % 400 == 0) {
total += 366;
}
else {
total += 365;
}
}
return total;
}
void printCalender(int y, int m) {
int leap = isLeap(y), d = 1;
int date[][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31} };
int week = (delt(1970, y) - DOY(1970, 1, 5) + DOY(y, m, 1)) % 7 + 1;
printf("\nI---The Calender of %d.%2d---I\n", y, m);
printf("I SUN MON TUE WED THU FRI SAT I\nI");
//打印第一行
for (int i = week; i != 0; --i) {
printf(" ");
}
for (int i = 7 - week; i != 0; --i) {
printf(" %2d", d);
++d;
}
printf(" I");
//打印剩余日期
while (d <= date[leap][m]) {
printf("\nI");
for (week = 7; week != 0; --week) {
printf(" %2d", d);
++d;
if (d > date[leap][m])
goto lable;
}
printf(" I");
}
//打印末尾的空格
lable:
for (--week; week != 0; --week) {
printf(" ");
}
puts(" I");
puts("I-----------------------------I");
}
int main() {
unsigned year, mon;
printf("Please input the year and month whose calender you want to know: ");
scanf("%d%d", &year, &mon);
printCalender(year, mon);
system("pause");
}
练习5.6
输入一个年份,输出该年份的日历。如下图:
<main.c>
#include"calender.h"
//相关函数见上题
void printCalender(int y) {
//leap控制平闰年,m控制月份,n控制打印左右
int leap = isLeap(y), m = 1, n;
int date[][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31} };
int week[2];
int d[2];
printf("\nI---------------------- The Calender of %d ----------------------I\n", y);
while (m != 7) {
week[0] = (delt(1970, y) - DOY(1970, 1, 5) + DOY(y, m, 1)) % 7 + 1;
week[1] = (delt(1970, y) - DOY(1970, 1, 5) + DOY(y, m+6, 1)) % 7 + 1;
d[0] = 1;
d[1] = 1;
printf("I<%2d> SUN MON TUE WED THU FRI SAT <%2d> SUN MON TUE WED THU FRI SAT I\nI", m, m + 6);
//打印第一行
n = 0;
while (n != 2) {
for (int i = week[n]; 0 <= i && i < 7 ; --i) {
printf(" ");
}
if (week[n] == 7) {
printf(" ");
week[n] = 0;
}
for (int i = 7 - week[n]; i != 0; --i) {
printf(" %2d", d[n]);
++d[n];
}
printf(" ");
++n;
}
printf("I");
//打印剩余日期
while (d[0] <= date[leap][m]||d[1] <= date[leap][m+6]) {
printf("\nI");
n = 0;
while (n != 2) {
printf(" ");
for (week[n] = 7; week[n] != 0 && (d[0] <= date[leap][m] || d[1] <= date[leap][m + 6]); --week[n]) {
if (d[n] <= date[leap][m + n * 6]) {
printf(" %2d", d[n]);
}
else {
printf(" ");
}
++d[n];
}
printf(" ");
++n;
//若此时右边月份已经打印完毕,该for循环用于打印左边月份末尾的空格
if (d[1] > date[leap][m + 6] && d[0] > date[leap][m]) {
for (; week[0] != 0; --week[0]) {
printf(" ");
}
}
}
//该for循环用于打印右边月份末尾的空格
for (; week[1] != 0; --week[1]) {
printf(" ");
}
printf("I");
}
++m;
printf("\n");
}
printf("I--------------------------- WONDERFUL ----------------------------I\n");
}
int main() {
unsigned year;
printf("Please input the year whose calender you want to know: ");
scanf("%d", &year);
printCalender(year);
system("pause");
}