C primer plus 6 编程练习答案
阅读原文时间:2023年07月17日阅读:1

环境:vs2017

/**编程练习2**/
/**/
#include
int main(void)
{
printf("张三\n");
printf("张\n三\n");
printf("张");
printf("三\n");
getchar();
return ;
}
/**/
#include
int main(void)
{
printf("姓名:张三\n地址:中国\n");
getchar();
return ;
}
/**/
#include
int main(void)
{
int age = , days = ;
printf("请输入您的年龄:");
scanf_s("%d", &age);
days = age * ;
printf("您的年龄为:%d\n您的年龄转换成天数为:%d\n", age, days);
getchar();
getchar();
return ;
}
/**/
#include
void jolly(void);
void deny(void);
int main(void)
{
jolly();
jolly();
jolly();
deny();
getchar();
return ;
}
void jolly(void)
{
printf("For he's a jolly good fellow!\n");
}
void deny(void)
{
printf("Which nobagy can deny!\n");
}
/**/
#include
void br(void);
void ic(void);
int main(void)
{
br();
ic();
printf("India,China,\n");
printf("Brazil,Russia\n");
getchar();
return ;
}
void br(void)
{
printf("Brazil,Russia,");
}
void ic(void)
{
printf("India,China\n");
}
/**/
#include
int main(void)
{
int toes = ;
printf("toes为:%d\ntoes的2倍为:%d\ntoes的平方为:%d\n", toes, * toes, toes*toes);
getchar();
return ;
}
/**/
#include
void smile(void);
int main(void)
{
smile();
smile();
smile();
printf("\n");
smile();
smile();
printf("\n");
smile();
printf("\n");
getchar();
return ;
}
void smile(void)
{
printf("Smile!");
}
/**/
#include
void one_three(void);
void two(void);
int main(void)
{
printf("starting now:\n");
one_three();
printf("done!\n");
getchar();
return ;
}
void one_three(void)
{
printf("one\n");
two();
printf("three\n");
}
void two(void)
{
printf("two\n");
}

/**编程练习3**/
/**/
#include
int main(void)
{
int a;
float b,c;
a = ;//整数上溢
b = 3.4E38 + 10.0f;//浮点数上溢
c = 0.1234E-10 / ;//浮点数下溢
printf("%d\n%f\n%f\n",a, b, c);
getchar();
return ;
}
/**/
#include
int main(void)
{
int a = ;
printf("请输入一个0-127中的一个整数:");
scanf_s("%d", &a);
printf("该数对应的ASCLL码为:%c\n", a);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
printf("\a");//警报
printf("Startled by the sudden sound,Sally shouted,\n");
printf("\"By the Grate Pumpkin,what was that!\"");
getchar();
return ;
}
/**/
#include
int main(void)
{
float a;
printf("输入一个小数:");
scanf_s("%f", &a);
printf("%f\n%e\n%.2a\n", a, a, a);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
int age = ;
long min = ;
printf("请输入您的年龄:");
scanf_s("%ld", &age);
min = age * 3.156E7;
printf("转换为秒为:%ld秒\n", min);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
double a,c;
int b;
printf("请输入水的夸脱数:");
scanf_s("%d", &b);
a = * 3.0E-23;
c = a * b;
printf("水分子数量为:%e\n", c);
getchar();
getchar();
return ;
}
/**/
#include
int main()
{
float tall_m, tall_cm;
printf("请输入您的身高:");
scanf_s("%f", &tall_m);
tall_cm = 2.54*tall_m;
printf("%.2f英寸转化为厘米为:%f厘米",tall_m, tall_cm);
getchar();
getchar();
return ;
}
/**/
#include
int main()
{
float cup,p,a,t,c;
printf("请输入杯数:");
scanf_s("%f", &cup);
p = cup/;
a = * cup;
t = * a;
c = * t;
printf("%f杯等于%f品脱,%f杯等于%f盎司,%f盎司等于%f大汤勺,%f大汤勺等于%f茶勺\n", cup, p, cup, a, a, t, t, c);
getchar();
getchar();
return ;
}

/**编程练习4**/
/**/
#include
int main(void)
{
char surname[] ;
char name[];
printf("请输入您的名:\n");
scanf_s("%s",name,);////////////////////////////////注意!!!scanf_s()在输入数组时需要指明字段大小/////////////////////////////////////////
printf("请输入您的姓:\n");
scanf_s("%s",surname,);
printf("您的名为:%s\n姓为:%s\n", name, surname);
getchar();
getchar();
//getchar();
return ;
}
/**/
#include
#include
int main(void)
{
char name[];
printf("请输入名字:\n");
scanf_s("%s", name, );
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
printf("\"%*s\"\n", strlen(name) + , name);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
float a;
printf("输入一个小数:21.29\n");
scanf_s("%f", &a);
printf("输入的数是:%.1f或%.1e\n", a,a);
printf("输入的数是:%2.2f或%.3E\n", a, a);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
float heigh;
char name[];
printf("请输入你的姓名:\n");
scanf_s("%s", name, );
printf("请输入你的身高(米):\n");
scanf_s("%f", &heigh);
printf("%s,你有%.3f米高", name,heigh);
getchar();
getchar();
return ;
}
/**/
#include
int main(xoid)
{
float speed, size, time;
printf("请输入以兆位每秒(Mb/s)为单位的下载速度和以兆(MB)为单位的文件大小\n");
scanf_s("%f %f", &speed,&size);
time = * size / speed;
printf("At %.2f megabits per second,a file of %.2f megabytes\n\
downloads in %.2f seconds.\n",speed,size,time);
getchar();
getchar();
return ;
}
/**/
#include
#include
int main(void)
{
char surname[];
char name[];
int length1,length2;
printf("输入姓:\n");
scanf_s("%s", surname, );
printf("输入名:\n");
scanf_s("%s", name, );
length1 = strlen(surname);
length2 = strlen(name);
printf("%s %s\n", surname, name);
printf("%*d %*d\n", length1, length1,length2,length2);
printf("%s %s\n", surname, name);
printf("%-*d %-*d\n", length1, length1, length2, length2);
getchar();
getchar();
return ;
}
/**/
#include
#include
int main(void)
{
double a=1.0 / 3.0;
float b=1.0 / 3.0;
printf("a=%.6f\tb=%.6f\n", a, b);
printf("a=%.12f\tb=%.12f\n", a, b);
printf("a=%.16f\tb=%.16f\n", a, b);
printf("float类型的最小有效数字为:%d\tdouble类型的最小有效数字为:%d\n", FLT_DIG, DBL_DIG);
getchar();
return ;
}
/**/
#include
#define GALLON 3.785 //1加仑等于3.785升
#define MILE 1.609 //1英里等于1.609千米
int main(void)
{
float gallon, mile;
printf("输入旅行里程:\n");
scanf_s("%f", &mile);
printf("输入消耗汽油量:\n");
scanf_s("%f", &gallon);
printf("油耗为:%.1f英里/加仑\n", mile / gallon);
printf("油耗转换为:%.1f升/100公里\n", gallon*GALLON / (mile*MILE) * );
getchar();
getchar();
return ;
}

/****编程练习5****/
/**/
#include
int main(void)
{
const int m_to_h = ;
int m,h,left;
printf("输入一个时间(分钟):\n");
scanf_s("%d", &m);
while (m>)
{
h = m/m_to_h;
left=m%m_to_h;
printf("%d分钟转化为小时为:%d小时%d分钟\n", m, h,left);
scanf_s("%d", &m);
}
getchar();
//getchar();
return ;
}
/**/
#include
int main(void)
{
int a,b;
printf("请输入一个整数:\n");
scanf_s("%d", &a);
b = a + ;
while (a<=b) { printf("打印比该数大10的整数为:%d\n", a); a++; } getchar(); getchar(); return ; } /**/ #include
int main(void)
{
const int day_to_week = ;
int days,week,left;
printf("请输入天数:\n");
scanf_s("%d", &days);
while (days>)
{
week = days/day_to_week;
left = days%day_to_week;
printf("%d天转化为%d周%d天\n", days, week, left);
scanf_s("%d", &days);
}
getchar();
getchar();
return ;
}
/**/
#include
#define FEET_TO_CM 30.48
#define INCHE_TO_CM 2.54
int main(void)
{
float heigh,inches;//inches英寸
int foot;//英尺
printf("输入身高(cm):\n");
scanf_s("%f", &heigh);
while (heigh>)
{
foot = heigh / FEET_TO_CM;
inches = (heigh - foot * FEET_TO_CM) / INCHE_TO_CM;
printf("%.2fcm=%d英尺%.2f英寸", heigh, foot, inches);
scanf_s("%f", &heigh);
}
getchar();
//getchar();
return ;
}
/**/
#include
int main(void)
{
int sum=, count=,day=;
printf("输入天数:\n");
scanf_s("%d", &day);
while (count++
int main(void)
{
int sum = , count = , day = ;
printf("输入天数:\n");
scanf_s("%d", &day);
while (count++ < day) { sum = sum + count*count; } printf("%d天挣了%d元钱\n", day, sum); getchar(); getchar(); return ; } /**/ #include
void value3(double a);
int main(void)
{
double a;
printf("输入一个小数:\n");
scanf_s("%lf", &a);
value3(a);
getchar();
getchar();
return ;
}
void value3(double a)
{
double b;
b = a * a*a;
printf("%lf立方为:%lf\n", a, b);
}
/**/
#include
int main(void)
{
int a, b;
printf("\t*****求模运算*****\t\n");
printf("输入一个数作为第2个运算对象:\n");
scanf_s("%d", &a);
printf("输入第二个数作为第1个运算对象:\n");
scanf_s("%d", &b);
printf("求模运算%d%%%d=%d\n", b, a, b%a);
printf("输入下一个数作为第1个运算对象:\n");
scanf_s("%d", &b);
while (b>)
{
printf("求模运算%d%%%d=%d\n", b, a, b%a);
printf("输入下一个数作为第1个运算对象:\n");
scanf_s("%d", &b);
}
printf("结束\n");
getchar();
getchar();
return ;
}
/**/
#include
void Temperatures(double h_tem);
int main(void)
{
double h_tem;//华氏度
printf("请输入华氏温度(输入非数字以退出):\n");
while (scanf_s("%lf", &h_tem)==)
{
Temperatures(h_tem);
printf("请输入华氏温度(输入非数字以退出):\n");
}
printf("程序退出\n");
getchar();
getchar();
return ;
}
void Temperatures(double h_tem)
{
double s_tem;//摄氏度
double k_tem;//开氏度
const double h_to_s = 32.0;//华氏度转摄氏度要用到的常量值
const double s_to_k = 273.16;//摄氏度转开氏度要用到的常量值
s_tem = 5.0 / 9.0 * (h_tem - h_to_s);
k_tem = s_tem + s_to_k;
printf("%.2lf华氏度等于%.2lf摄氏度和%.2lf开氏度\n", h_tem, s_tem, k_tem);
}

/****编程练习6****/
/**/
#include
#define size 26
int main(void)
{
char ch[size];
int a;
for (a = ; a
int main(void)
{
int row, clo;
char ch = '$';
for (row=; row< ; row++) { for (clo = ; clo < row + ; clo++) printf("%c",ch); printf("\n"); } getchar(); return ; } /**/ #include
int main(void)
{
char ch = 'F';
char start, end;
for (end=ch;end>='A'; end--)
{
for (start=ch;start>=end;start--)
printf("%c", start);
printf("\n");
}
getchar();
return ;
}
/**/
#include
int main(void)
{
const char end = 'U';
char start;
int row, clo;
for (row=,start = 'A'; start<=end;row++ ) { for (clo = ;clo <= row;clo++) { printf("%c", start); start++; } printf("\n"); } getchar(); return ; } /**/ #include
int main(void)
{
char start = 'A';
char end;
char ch = start;
int row, clo;
printf("输入一个大写字母:\n");
scanf_s("%c", &end);
printf(" 字母金字塔\n");
for ( row=; row <= end-start; row++,ch = start) { for (clo = ;clo < end - start - row;clo++) printf(" "); for (int k = ;k <= row;k++) { printf("%c", ch); ch++; } ch -= ; for (int c = ;c < row;c++) { printf("%c", ch); ch--; } printf("\n"); } getchar(); getchar(); return ; } /**/ #include
int main( void )
{
int start, end;
printf("输入开始的数字:\n");
scanf_s("%d", &start);
printf("输入结束数字:\n");
scanf_s("%d", &end);
printf("原数\t平方\t立方\n");
for (start;start <= end;start++) { printf("%d\t%d\t%d\n", start, start*start, start*start*start); } getchar(); getchar(); return ; } /**/ #include
#include
#define SIZE 40
int main(void)
{
char word[SIZE];
int i;
printf("请输入一个单词:\n");
scanf_s("%s", word, );
printf("该单词有%d个字母,单词是%s\n", strlen(word), word);
printf("倒序为:");
for (i = strlen(word - );i >= ;i--)//strlen(word - 1)跳过字符串后面的空格
printf("%c", word[i]);
printf("\n");
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
double a, b;
int status;//输入状态
printf("输入2个浮点数(输入q退出):\n");
while (scanf_s("%lf%lf", &a, &b) == )//验证是否输入了2个值
{
printf("结果是%.3lf", (a - b) / (a*b));
printf("继续输入2个浮点数(输入q退出):\n");
}
getchar();
return ;
}
/**/
#include
float result(float a, float b);
int main(void)
{
double a, b;
int status;//输入状态
printf("输入2个浮点数(输入q退出):\n");
while (scanf_s("%lf%lf", &a, &b) == )//验证是否输入了2个值
{
printf("结果是%.3lf", result(a,b));
printf("继续输入2个浮点数(输入q退出):\n");
}
getchar();
return ;
}
float result(float a, float b)
{
float result;//result名称不唯一
result = (a - b) / (a*b);
return result;
}
/**/
#include
int main(void)
{
int up, down;//上限和下限
printf("输入上限整数和下限整数:\n");
scanf_s("%d %d", &up, &down);
while (up>down)
{
int sum = ;//初始化平方和sum
for (int i = sum;i <= up;i++) sum = sum + i * i; printf("平方和从%d到%d是%d\n", up*up, down*down, sum); printf("继续输入上限整数和下限整数:\n"); scanf_s("%d %d", &up, &down); } printf("结束!\n"); getchar(); getchar(); return ; } /**/ #include
#define SIZE 8
int main(void)
{
int a[SIZE];
int i;
for ( i = ; i < SIZE; i++) scanf_s("%d", &a[i]); for (i = SIZE - ;i >= ;i--)
printf("%d", a[i]);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
int times;
float count1 = 1.0f;//分母
float count2 = 1.0f;//分母
float sum1 = 0.0;
float sum2 = 0.0;
printf("输入计算次数(输入0或负数退出):\n");
scanf_s("%d", &times);
for (int i = ;i <= times;i++, count1++) sum1 = sum1 + 1.0 / count1; for (int j = ;j <= times;j++, count2++) { if (j%==) { sum2 = sum2 - 1.0 / count2; } else { sum2 = sum2 + 1.0 / count2; } } printf("第一个公式的计算结果为:%f\n", sum1); printf("第二个公式的计算结果为:%f\n", sum2); getchar(); getchar(); return ; } /**/ #include
#define SIZE 8
int main(void)
{
int arry[SIZE];
int i, n;
for (i = , n = ;i < SIZE;i++, n *= ) arry[i] = n; i = ; do { printf("%d\n", arry[i]); i++; } while (i
#define SIZE 8
int main(void)
{
double arry1[SIZE];
double arry2[SIZE];
int i;

 printf("输入值:\\n");  
 for (i = ;i < SIZE;i++)  
     scanf\_s("%lf", &arry1\[i\]);//读入第一个数组  
 arry2\[\] = arry1\[\];//初始化第二个数组  
 for (i = ;i < SIZE;i++)  
     arry2\[i\] = arry2\[i - \] + arry1\[i\];//计算第二个元组各数之和  
 for (i = ;i < SIZE;i++)  
     printf("%8.2lf", arry1\[i\]);  
 printf("\\n");  
 for (i = ;i < SIZE;i++)  
     printf("%8.2lf", arry2\[i\]);

 getchar();  
 getchar();  
 return ;  

}
/**/
#include
#define SIZE 255
int main(void)
{
char arry[SIZE];
int i = -;
printf("输入一个单词:\n");
do
{
i++;//由arry[0]开始循环读入
scanf_s("%c", &arry[i]);
} while (arry[i]!='\n');
for (;i >= ;i--)
printf("%c", arry[i]);
getchar();
return ;
}
/**/
#include
#define Daphne_RATE 0.10 //Daphne利率
#define Deiedre_RATE 0.05 //Deiedre利率
#define MONEY 100.0 //投资100元
int main(void)
{
int year=;
double Daphne_money=MONEY; //Daphne的投资额
double Deiedre_money=MONEY; //Deiedre的投资额
while (Deiedre_money <= Daphne_money) //当Deiedre投资额超过Daphne的投资额退出循环 { year++; Daphne_money = Daphne_money + MONEY * Daphne_RATE; Deiedre_money = Deiedre_money + Deiedre_money * Deiedre_RATE; } printf("用了%d年Deiedre的投资额超过Daphne的投资额\n", year); printf("Daphne的投资额为:%f\tDeiedre的投资额为%f\n", Daphne_money, Deiedre_money); getchar(); return ; } /**/ #include
#define RATE 0.08
int main(void)
{
double money = ;
int year = ;
while (money>)
{
year++;
money = money + money * RATE;
money = money - ;
}
printf("%d年后Chuckie会取完账户里的钱\n", year);
getchar();
return ;
}
/**/
#include
#define DB_NUMBER 150 //邓巴数
int main(void)
{
int weeks = ;
int friends = ;
while (friends<DB_NUMBER)
{
friends = (friends - weeks) * ;
printf("%d周后,Rabnud博士有%d个朋友\n", weeks, friends);
weeks++;
}
getchar();
return ;
}

/*****编程练习7*****/
/**/
#include
int main(void)
{
char ch;
int spa_count=, lin_count=, oth_count=; //空格数,换行符数,其他字符数
printf("输入字符进行统计(输入字符‘#’退出程序):\n");
while ((ch=getchar())!='#')
{
if (ch == ' ')
spa_count++;
else if (ch == '\n')
lin_count++;
else
oth_count++;
}
printf("你输入的字符中空格数有%d个,换行符有%d个,其他字符数有%d个\n", spa_count, lin_count, oth_count);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
const int line = ;
int chsum = ;//统计字符数
printf("输入字符进行统计(输入‘#’字符结束):\n");
while ((ch=getchar())!='#')
{
printf("%c:%d\t", ch, ch);
chsum++;
if (chsum%line == )
printf("\n");
}
printf("\n");
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
int a;
int ou_count = ;//偶数个数
int ji_count = ;//奇数个数
int ou_sum = ;//偶数的和
int ji_sum = ;//奇数和
float ou_average;//偶数平均值
float ji_average;//奇数平均值
printf("输入整数进行计算(输入‘0’结束):");
while (scanf_s("%d", &a) == && a != )
{
if (a % == )
{
ou_count++;
ou_sum += a;
}
else
{
ji_count++;
ji_sum += a;
}
}
printf("偶数个数:%d\n", ou_count);
if (ou_count>)
{
ou_average = ou_sum / ou_count;
printf("偶数平均值为:%.2f\n", ou_average);
}
printf("奇数个数:%d\n", ji_count);
if (ji_count>)
{
ji_average = ji_sum / ji_count;
printf("奇数平均值为:%.2f\n", ji_average);
}
printf("结束\n");
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
int count1=;//替换句号的次数
int count2=;//替换感叹号的次数
printf("输入字符进行统计(输入‘#’结束):\n");
while ((ch=getchar())!='#')
{
if (ch == '.')
{
putchar('!');
count1++;
}
else if (ch == '!')
{
putchar('!');
putchar('!');
count2++;
}
else
putchar(ch);
}
printf("\n'!'替代'.'进行了%d次\n", count1);
printf("'!!'替代'!'进行了%d次\n", count2);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
int count1 = ;//替换句号的次数
int count2 = ;//替换感叹号的次数
printf("输入字符进行统计(输入‘#’结束):\n");
while ((ch = getchar()) != '#')
{
switch (ch)
{
case'.':
putchar('!');
count1++;
break;
case'!':
putchar('!');
putchar('!');
count2++;
break;
default:
putchar(ch);
}
}
printf("\n'!'替代'.'进行了%d次\n", count1);
printf("'!!'替代'!'进行了%d次\n", count2);
getchar();
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
char fir, sec;//第一个接收到的字符和第二个字符
int count = ;//ei出现的次数
printf("输入进行统计(输入‘#’结束):\n");
while ((ch=getchar())!='#')
{
if (ch=='e')
{
fir = ch;
continue;
}
if (ch=='i')
{
sec = ch;

         if (fir == 'e'&&sec == 'i')  
             count++;                //判断前一个字符是‘e’,第二个是‘i’,则次数加1  
         //初始化fir和sec  
         fir = 'a';  
         sec = 'a';  
         continue;  
     }  
     continue;  
 }  
 printf("'ei'出现了%d次\\n",count);  
 getchar();  
 getchar();  
 return ;  

}
/**/
#include
#define BASE_MONEY 10.00//基本工资
#define BASE_TIME 40//基本加班时间
#define OVER_TIME 1.5//超过基本时间
#define MONY1 300//税率分界线1
#define MONY2 150//税率分界线2
#define RATE1 0.15//MONY1前的税率
#define RATE2 0.20//MONY2的税率
#define RATE3 0.25//剩下的税率
int main(void)
{
double hours;//一周工作时间
double sum;//工资总数
double rate_mony;//税金
double income;//净收入
printf("输入一周工作小时数:\n");
scanf_s("%lf", &hours);
if (hours <= BASE_TIME) sum = hours * BASE_MONEY; else sum = BASE_TIME * BASE_MONEY + (hours - BASE_TIME)*BASE_MONEY*OVER_TIME; if (sum <= MONY1) rate_mony = sum * RATE1; else if (sum <= MONY1 + MONY2) rate_mony = MONY1 * RATE1 + (sum - MONY1)*RATE2; else rate_mony = MONY1 * RATE1 + MONY2 * RATE2 + (sum - MONY1 - MONY2)*RATE3; income = sum - rate_mony; printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_mony, income); getchar(); getchar(); return ; } /**/ #include
#define BASE_TIME 40 //基本加班时间
#define OVER_TIME 1.5 //超过基本时间
#define MONY1 300 //税率分界线1
#define MONY2 150 //税率分界线2
#define BASE_LEVEL1 8.75
#define BASE_LEVEL2 9.33
#define BASE_LEVEL3 10.00
#define BASE_LEVEL4 11.20
#define RATE1 0.15 //MONY1前的税率
#define RATE2 0.20 //MONY2的税率
#define RATE3 0.25 //剩下的税率
int main(void)
{
int choice;
double basy_money;
double hours; //一周工作时间
double sum; //工资总数
double rate_money; //税金
double income; //净收入
printf("*******************************************************\n");
printf("输入与薪资或操作对应的编号进行操作:\n");
printf("1) $8.75/hours \t2) $9.33/hours\n");
printf("3) $10.00/hours \t4) $11.20/hours\n");
printf("5) 退出\n");
printf("*******************************************************\n");
while (scanf_s("%d",&choice) == && choice != )
{
switch (choice)
{
case :
basy_money = BASE_LEVEL1;
break;
case :
basy_money = BASE_LEVEL2;
break;
case :
basy_money = BASE_LEVEL3;
break;
case :
basy_money = BASE_LEVEL4;
break;
default:
printf("请输入1-4数字进行选择(输入数字5退出)。\n请输入正确的数字\n");
continue;
}
printf("输入一周工作小时数:\n");
scanf_s("%lf", &hours);
if (hours <= BASE_TIME) sum = basy_money * hours; else sum = BASE_TIME * basy_money + (hours - BASE_TIME)*basy_money*OVER_TIME; if (sum <= MONY1) rate_money = sum * RATE1; else if (sum <= (MONY1 + MONY2)) rate_money = MONY1 * RATE1 + (sum - MONY1) * RATE2; else rate_money = MONY1 * RATE1 + MONY2 * RATE2 + (sum - MONY1 - MONY2)*RATE3; income = sum - rate_money; printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income); printf("输入下一个数字继续\n"); } getchar(); return ; } /**/ #include
#include
int main(void)
{
int limit;//输入的数
int num;
int div;
bool numIsPrime; //判断是否为素数
printf("输入正整数:");
while (scanf_s("%d",&limit) == && limit > )
{
if (limit > )
printf("以下为素数:\n", limit);
else
printf("没有素数\n");
for (num= ; num <= limit; num++) { for (div = , numIsPrime = true;(div*div) <= num;div++) if (num%div == ) numIsPrime = false; if (numIsPrime) printf("%d是素数。\n", num); } printf("输入正整数(输入q退出):"); } printf("结束\n"); getchar(); return ; } /**/ #include
#define LEVEL1 17850
#define LEVEL2 23900
#define LEVEL3 29750
#define LEVEL4 14875
#define RATE1 0.15
#define RATE2 0.28
int main(void)
{
int choice; //选择号码
long level; //税金等级
double income; //收入
double taxmoney; //税金
printf("****************************************************************************\n");
printf("输入类别编号:\n");
printf("1) 单身 \t2) 户主 \t3) 已婚,共有 \t4) 已婚,离异\n");
printf("5) 退出\n");
printf("****************************************************************************\n");
while (scanf_s("%d",&choice) == && choice != )
{
switch (choice)
{
case :
level = LEVEL1;
break;
case :
level = LEVEL2;
break;
case :
level = LEVEL3;
break;
case :
level = LEVEL4;
break;
default:
printf("请输入1-4数字进行选择(输入数字5退出)。\n请输入正确的数字\n");
break;
}
printf("请输入你的收入:");
scanf_s("%lf", &income);
if (income <= level) taxmoney = income * RATE1; else taxmoney = level * RATE1 + (income - level)*RATE2; printf("你的税金为%.2lf元\n", taxmoney); printf("继续输入选择(输入5退出):\n"); } getchar(); return ; } /**/ #include
#define Y_PRICE 2.05 //洋蓟 单价
#define T_PRICE 1.15 //甜菜
#define H_PRICE 1.09 //胡萝卜
#define DISCOUNT 0.05 //折扣优惠

#define WEIGHT_LEVEL1 5 //重量层次
#define WEIGHT_LEVEL2 20

#define TRANS_LEVEL1 6.5 //运费和包装费层次
#define TRANS_LEVEL2 14

#define TRANS_LEVEL3 0.5 //额外增费
int main(void)
{
char ch; //选择字符
float input; //输入磅数
float y_weight = ; //洋蓟磅数
float t_weight = ; //甜菜磅数
float h_weight = ; //胡萝卜磅数
float total_weight; //总重
float cost; //花费
float total_cost; //总花费:买菜+运费
float discount; //折扣
float tran_cost; //运费
float total_all;
printf("************************************************\n");
printf("输入编号进行选择:\n");
printf("a:洋蓟\tb:甜菜\tc:胡萝卜\nq:退出并打印账单\n");
printf("************************************************\n");
while ((ch = getchar()) != 'q')
{
if (ch == '\n')
continue;
while (getchar() != '\n')
continue;
switch (ch)
{
case 'a':
printf("输入洋蓟的磅数:");
scanf_s("%f", &input);
y_weight = y_weight + input;
break;
case 'b':
printf("输入甜菜的磅数:");
scanf_s("%f", &input);
t_weight = t_weight + input;
break;
case 'c':
printf("输入胡萝卜的磅数:");
scanf_s("%f", &input);
h_weight = h_weight + input;
break;
default:
printf("输入错误,请输入a,b,c选择蔬菜,q退出\n");
break;
}
printf("继续输入进行购物\n");
}
total_weight = y_weight + t_weight + h_weight;
cost = y_weight * Y_PRICE + t_weight * T_PRICE + h_weight * H_PRICE;
if (cost >= )
discount = cost * DISCOUNT;
else
discount = ;
if (total_weight <= ) tran_cost = ; else if (total_weight <= WEIGHT_LEVEL1) tran_cost = TRANS_LEVEL1; else if (total_weight <= WEIGHT_LEVEL2) tran_cost = TRANS_LEVEL2; else tran_cost = TRANS_LEVEL2 + TRANS_LEVEL3 * total_weight; total_all = cost + tran_cost - discount; printf("你的订单如下:\n"); printf("2.05美元/磅的洋蓟订购了%.2f磅\n", y_weight); printf("1.15美元/磅的甜菜订购了%.2f磅\n", t_weight); printf("1.09美元/磅的胡萝卜订购了%.2f磅\n", h_weight); printf("买蔬菜的费用为:%.2f\n", cost); if(cost>)
printf("折扣为:%.2f\n", discount);
printf("运费和包装费为:%.2f\n", tran_cost);
printf("所有费用为:%.2f\n", total_all);
getchar();
getchar();
return ;
}

/****编程练习8****/
/**/
#include
int main(void)
{
char ch;
int a = ;
while ((ch=getchar())!=EOF)
a++;
printf("读取到%d个字符。\n", a);
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
int i=; //统计每行的值
while ((ch=getchar())!=EOF)
{
if (ch == '\n')
{
putchar('\\');
putchar('n');
printf(" %d\n", ch);
i = ;
}
else if (ch == '\t')
{
putchar('\\');
putchar('t');
printf("%d\n", ch);
}
else if (ch < ' ') { putchar('^'); putchar(ch + ); printf("%d\t", ch); } else printf("%c %d\t", ch, ch); if (++i == ) { printf("\n"); i = ; } } getchar(); return ; } /**/ #include
#include
int main(void)
{
int ch;
//统计个数,用不到负数
unsigned A_count = ; //大写
unsigned a_count = ; //小写
unsigned oth_count = ; //其他字符个数
while ((ch=getchar())!=EOF)
{
//if (isupper(ch)) //用isupper()函数判断是否为大写
if(ch>='A'&&ch<='Z') //可以用该行替换上一行,这种方法不用再调用ctype.h A_count++; else if (islower(ch)) //用islower()函数判断是否为小写 a_count++; else oth_count++; } printf("输入中大写字母有%d个\n", A_count); printf("小写字母有%d个\n", a_count); printf("其他字符有%d个\n", oth_count); getchar(); return ; } /**/ #include
#include
#include
int main(void)
{
char ch;
int words = , count = ; //单词 字母总数
bool inword = false; //判断是否是一个单词
float average;
while ((ch=getchar())!=EOF)
{
if (isalpha(ch))
count++; //如果是个字符,字母+1
if (!isspace(ch) && !inword)
{
inword = true;
words++; //如果不是空格且inword为true,单词+1
}
if (isspace(ch) && inword)
inword = false; //如果是个空格,且inword为true,此时并不是个完整单词,需要初始化inword
if (ispunct(ch))
continue; //如果是字符,中断本次循环
}
average = count / words;
printf("平均每个单词有%.2f个字母", average);
getchar();
return ;
}
/**/
#include
int main(void)
{
int high = ;
int low = ;
int guess=(low+high)/;
char response;
printf("你在1-100中选一个数字,我能猜出那个数字!");
printf("\n如果我回答正确,就输入'y'");
printf("\n如果我猜的数太高,就输入'h'");
printf("\n如果我猜的数太小,就输入'l'\n");
printf("你选择的数字是 %d?\n", guess);
while ((response=getchar())!='y')
{
if (response == '\n')
continue;
if (response != 'h'&&response != 'l')
{
printf("对不起,输入错误!请重新输入!\n");
continue;
}
if (response == 'h')
high = guess - ;
else if (response == 'l')
low = guess + ;
guess = (high + low) / ;
printf("你选择的数字是 %d?\n", guess);
}
printf("哈哈!我猜对了!\n");
getchar();
getchar();
return ;
}
/**/
#include
#include
char get_first(void);
int main(void)
{
char ch;
ch = get_first();
putchar(ch);
getchar();
return ;
}
char get_first(void)
{
char ch;
ch = getchar();
while (isspace(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
/**/
#include
#include
#include
#define BASY_MONEY1 8.75 //基本工资
#define BASY_MONEY2 9.33
#define BASY_MONEY3 10.00
#define BASY_MONEY4 11.20

#define BASY_TIME 40 //基本工作时间
#define EXTR_TIME 1.5 //加班

#define LIN_MONEY1 300 //税率分界线
#define LIN_MONEY2 150

#define RATE1 0.15 //税率
#define RATE2 0.20
#define RATE3 0.25
int get_first(void); //创建函数,如果输入为空,则丢弃
int main(void)
{
char ch;
double basy_money;
double hours;
double sum; //工资总数
double rate_money; //税金
double income; //净收入
printf("*******************************************************\n");
printf("输入与薪资或操作对应的编号进行操作:\n");
printf("a) $8.75/hours \tb) $9.33/hours\n");
printf("c) $10.00/hours \td) $11.20/hours\n");
printf("q) 退出\n");
printf("*******************************************************\n");
while ((ch=get_first())!='q') //调用get_first()函数
{
if (ch =='\n')
continue;
ch = tolower(ch); //将大写字母转为小写
switch (ch)
{
case 'a':
basy_money = BASY_MONEY1; break;
case 'b':
basy_money = BASY_MONEY2; break;
case 'c':
basy_money = BASY_MONEY3; break;
case 'd':
basy_money = BASY_MONEY4; break;
default:
printf("请输入a-d进行选择(输入q退出)。\n请输入正确的数字\n"); break;
}
printf("输入一周工作小时数:\n");
scanf_s("%lf", &hours);
if (hours <= BASY_TIME) sum = basy_money * hours; else sum = basy_money * BASY_TIME + (hours - BASY_TIME)*basy_money*EXTR_TIME; if (sum <= LIN_MONEY1) rate_money = sum * RATE1; else if (sum <= (LIN_MONEY1 + LIN_MONEY2)) rate_money = LIN_MONEY1*RATE1 + (sum - LIN_MONEY1)*RATE2; else rate_money = LIN_MONEY1 * RATE1 + LIN_MONEY2 * RATE2 + BASY_MONEY2 + (sum - LIN_MONEY1 - LIN_MONEY2)*RATE3; income = sum - rate_money; printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income); printf("输入下一个数字继续\n"); } getchar(); return ; } int get_first(void) { int ch; ch = getchar(); while (isspace(ch)) ch = getchar(); while (getchar() != '\n') continue; return ch; } /**/ #include
#include
int get_first(void);
float get_num(void);
int main(void)
{
char ch;
float first, second;
printf("输入你想进行运算的选项:\n");
printf("a.加法\ts.减法\n");
printf("m.乘法\td.除法\n");
printf("q.退出\n");
while ((ch=get_first())!='q')
{
ch = tolower(ch);
if (ch != 'a'&& ch != 's'&& ch != 'm'&& ch != 'd')
{
printf("你只能输入a,s,m,d,q中的一个:");
continue;
}
printf("输入第一个数:");
first = get_num();
printf("输入第二个数:");
second = get_num();
if (ch == 'd')
{
while (second == )
{
printf("输入一个大于0的数:");
second = get_num();
}
}
switch (ch)
{
case 'a':
printf("%.1f+%.1f=%.1f\n", first, second, first + second); break;
case 's':
printf("%.1f-%.1f=%.1f\n", first, second, first - second); break;
case 'm':
printf("%.1f*%.1f=%.1f\n", first, second, first*second); break;
case 'd':
printf("%.1f/%.1f=%.1f\n", first, second, first / second); break;
default:
;
break;
}
printf("输入你想进行运算的选项:\n");
printf("a.加法\ts.减法\n");
printf("m.乘法\td.除法\n");
printf("q.退出\n");
}
getchar();
return ;
}
int get_first(void)
{
int ch;
while ((ch = getchar()) == ' ' || ch == '\t' || ch == '\n')
continue;
while (getchar() != '\n')
continue;
return ch;
}
float get_num(void)
{
float input;
char ch;
while (scanf_s("%f",&input)!=)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf("%c不是数字\n请输入例如2.5,-1.78E8,3之类的数字:",ch);
}
return input;
}

/****编程练习8****/
/**/
#include
int main(void)
{
char ch;
int a = ;
while ((ch=getchar())!=EOF)
a++;
printf("读取到%d个字符。\n", a);
getchar();
return ;
}
/**/
#include
int main(void)
{
char ch;
int i=; //统计每行的值
while ((ch=getchar())!=EOF)
{
if (ch == '\n')
{
putchar('\\');
putchar('n');
printf(" %d\n", ch);
i = ;
}
else if (ch == '\t')
{
putchar('\\');
putchar('t');
printf("%d\n", ch);
}
else if (ch < ' ') { putchar('^'); putchar(ch + ); printf("%d\t", ch); } else printf("%c %d\t", ch, ch); if (++i == ) { printf("\n"); i = ; } } getchar(); return ; } /**/ #include
#include
int main(void)
{
int ch;
//统计个数,用不到负数
unsigned A_count = ; //大写
unsigned a_count = ; //小写
unsigned oth_count = ; //其他字符个数
while ((ch=getchar())!=EOF)
{
//if (isupper(ch)) //用isupper()函数判断是否为大写
if(ch>='A'&&ch<='Z') //可以用该行替换上一行,这种方法不用再调用ctype.h A_count++; else if (islower(ch)) //用islower()函数判断是否为小写 a_count++; else oth_count++; } printf("输入中大写字母有%d个\n", A_count); printf("小写字母有%d个\n", a_count); printf("其他字符有%d个\n", oth_count); getchar(); return ; } /**/ #include
#include
#include
int main(void)
{
char ch;
int words = , count = ; //单词 字母总数
bool inword = false; //判断是否是一个单词
float average;
while ((ch=getchar())!=EOF)
{
if (isalpha(ch))
count++; //如果是个字符,字母+1
if (!isspace(ch) && !inword)
{
inword = true;
words++; //如果不是空格且inword为true,单词+1
}
if (isspace(ch) && inword)
inword = false; //如果是个空格,且inword为true,此时并不是个完整单词,需要初始化inword
if (ispunct(ch))
continue; //如果是字符,中断本次循环
}
average = count / words;
printf("平均每个单词有%.2f个字母", average);
getchar();
return ;
}
/**/
#include
int main(void)
{
int high = ;
int low = ;
int guess=(low+high)/;
char response;
printf("你在1-100中选一个数字,我能猜出那个数字!");
printf("\n如果我回答正确,就输入'y'");
printf("\n如果我猜的数太高,就输入'h'");
printf("\n如果我猜的数太小,就输入'l'\n");
printf("你选择的数字是 %d?\n", guess);
while ((response=getchar())!='y')
{
if (response == '\n')
continue;
if (response != 'h'&&response != 'l')
{
printf("对不起,输入错误!请重新输入!\n");
continue;
}
if (response == 'h')
high = guess - ;
else if (response == 'l')
low = guess + ;
guess = (high + low) / ;
printf("你选择的数字是 %d?\n", guess);
}
printf("哈哈!我猜对了!\n");
getchar();
getchar();
return ;
}
/**/
#include
#include
char get_first(void);
int main(void)
{
char ch;
ch = get_first();
putchar(ch);
getchar();
return ;
}
char get_first(void)
{
char ch;
ch = getchar();
while (isspace(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
/**/
#include
#include
#include
#define BASY_MONEY1 8.75 //基本工资
#define BASY_MONEY2 9.33
#define BASY_MONEY3 10.00
#define BASY_MONEY4 11.20

#define BASY_TIME 40 //基本工作时间
#define EXTR_TIME 1.5 //加班

#define LIN_MONEY1 300 //税率分界线
#define LIN_MONEY2 150

#define RATE1 0.15 //税率
#define RATE2 0.20
#define RATE3 0.25
int get_first(void); //创建函数,如果输入为空,则丢弃
int main(void)
{
char ch;
double basy_money;
double hours;
double sum; //工资总数
double rate_money; //税金
double income; //净收入
printf("*******************************************************\n");
printf("输入与薪资或操作对应的编号进行操作:\n");
printf("a) $8.75/hours \tb) $9.33/hours\n");
printf("c) $10.00/hours \td) $11.20/hours\n");
printf("q) 退出\n");
printf("*******************************************************\n");
while ((ch=get_first())!='q') //调用get_first()函数
{
if (ch =='\n')
continue;
ch = tolower(ch); //将大写字母转为小写
switch (ch)
{
case 'a':
basy_money = BASY_MONEY1; break;
case 'b':
basy_money = BASY_MONEY2; break;
case 'c':
basy_money = BASY_MONEY3; break;
case 'd':
basy_money = BASY_MONEY4; break;
default:
printf("请输入a-d进行选择(输入q退出)。\n请输入正确的数字\n"); break;
}
printf("输入一周工作小时数:\n");
scanf_s("%lf", &hours);
if (hours <= BASY_TIME) sum = basy_money * hours; else sum = basy_money * BASY_TIME + (hours - BASY_TIME)*basy_money*EXTR_TIME; if (sum <= LIN_MONEY1) rate_money = sum * RATE1; else if (sum <= (LIN_MONEY1 + LIN_MONEY2)) rate_money = LIN_MONEY1*RATE1 + (sum - LIN_MONEY1)*RATE2; else rate_money = LIN_MONEY1 * RATE1 + LIN_MONEY2 * RATE2 + BASY_MONEY2 + (sum - LIN_MONEY1 - LIN_MONEY2)*RATE3; income = sum - rate_money; printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income); printf("输入下一个数字继续\n"); } getchar(); return ; } int get_first(void) { int ch; ch = getchar(); while (isspace(ch)) ch = getchar(); while (getchar() != '\n') continue; return ch; } /**/ #include
#include
int get_first(void);
float get_num(void);
int main(void)
{
char ch;
float first, second;
printf("输入你想进行运算的选项:\n");
printf("a.加法\ts.减法\n");
printf("m.乘法\td.除法\n");
printf("q.退出\n");
while ((ch=get_first())!='q')
{
ch = tolower(ch);
if (ch != 'a'&& ch != 's'&& ch != 'm'&& ch != 'd')
{
printf("你只能输入a,s,m,d,q中的一个:");
continue;
}
printf("输入第一个数:");
first = get_num();
printf("输入第二个数:");
second = get_num();
if (ch == 'd')
{
while (second == )
{
printf("输入一个大于0的数:");
second = get_num();
}
}
switch (ch)
{
case 'a':
printf("%.1f+%.1f=%.1f\n", first, second, first + second); break;
case 's':
printf("%.1f-%.1f=%.1f\n", first, second, first - second); break;
case 'm':
printf("%.1f*%.1f=%.1f\n", first, second, first*second); break;
case 'd':
printf("%.1f/%.1f=%.1f\n", first, second, first / second); break;
default:
;
break;
}
printf("输入你想进行运算的选项:\n");
printf("a.加法\ts.减法\n");
printf("m.乘法\td.除法\n");
printf("q.退出\n");
}
getchar();
return ;
}
int get_first(void)
{
int ch;
while ((ch = getchar()) == ' ' || ch == '\t' || ch == '\n')
continue;
while (getchar() != '\n')
continue;
return ch;
}
float get_num(void)
{
float input;
char ch;
while (scanf_s("%f",&input)!=)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf("%c不是数字\n请输入例如2.5,-1.78E8,3之类的数字:",ch);
}
return input;
}

/*********编程练习9***********/
/*复习题9*/
#include
void mennu(void); //菜单函数
int limt(int up, int down); //上下限函数
int main(void)
{
int ch;
mennu();
while ((ch=limt(,))!=)
{
printf("%d一个正确的选择!\n",ch);
mennu();
}
getchar();
getchar();
getchar();
return ;
}
void mennu(void)
{
printf("请选择列表中对应的数字:\n");
printf("1) 复制文件 \t2) 移动文件\n");
printf("3) 删除文件 \t4) 退出\n");
printf("输入你选择的数字:\n");
}
int limt(int up, int down)
{
int input;
int a=scanf_s("%d", &input);
while (a==&&(input > up || input < down)) { printf("输入数值超过限制!\n"); mennu(); scanf_s("%d", &input); } if (a!=) { printf("没有数字输入!\n"); input = ; } return input; } /*编程练习1*/ #include
double min(double x, double y);
int main(void)
{
double a, b;
printf("输入两个小数,将输出较小的值(输入q退出):\n");
while (scanf_s("%lf %lf", &a, &b)==)
{
printf("%lf和%lf中较小的数是:%lf", a, b, min(a, b));
printf("继续输入进行比较(输入q退出):\n");
}
getchar();
return ;
}
double min(double x, double y)
{
return x < y ? x : y; } /**/ #include
void chline(char ch, int i, int j);
int main(void)
{
char c;
int row;
int clum;
printf("该程序要求输入一个字符并按输入的行列数打印\n");
printf("输入你想打印的字符:");
scanf_s("%c", &c);
printf("输入你想打印的行和列(用空格分开):");
scanf_s("%d %d", &row, &clum);
printf("\n");
chline(c, row, clum);
getchar();
getchar();
return ;

}
void chline(char ch, int i, int j)
{
for (int x = ; x < i; x++) { for (int y = ; y < j; y++) putchar(ch); putchar('\n'); } } /**/ #include
void chline(char ch, int i, int j);
int main(void)
{
char ch;
int row, clo;
printf("输入想要打印的字符:");
while ((ch=getchar())!='#')
{
if (ch == '\n')
continue;
printf("输入想要打印的次数和行数:");
if (scanf_s("%d %d", &clo, &row) != )
break;
chline(ch, clo, row);
printf("继续输入下一个想打印的字符(输入#退出):");
}
getchar();
return ;
}
void chline(char ch, int i, int j)
{
for (int x = ; x < i; x++) { for (int y = ; y < j; y++) putchar(ch); putchar('\n'); } } /**/ #include
double Har_average(double num1, double num2); //调和平均数
int main(void)
{
double a = 3.14, b = 3.15, c;
c=Har_average(a, b);
printf("%lf", c);
getchar();
return ;
}
double Har_average(double num1, double num2)
{
double down_num1, down_num2, down_avweage;
down_num1 = / num1;
down_num2 = / num2;
down_avweage = / ((down_num1 + down_num2) / );
return down_avweage;
}
/**/
#include
void large_of(double *p1, double *p2);
int main(void)
{
double a, b;
printf("输入两个值:");
while (scanf_s("%lf %lf",&a,&b)==)
{
large_of(&a, &b);
printf("交换后的值为%lf和%lf\n", a, b);
printf("继续输入(输入q退出):");
}
getchar();
return ;
}
void large_of(double *p1, double *p2)
{
if (*p1 > *p2)
*p2 = *p1;
else
*p1 = *p2;
}
/**/
#include
void change(double *p1, double *p2, double*p3);
int main(void)
{
double a, b, c;
while (scanf_s("%lf %lf %lf", &a, &b,&c) == )
{
change(&a, &b, &c);
printf("交换后的值为%lf和%lf和%f\n", a, b,c);
printf("继续输入(输入q退出):");
}
getchar();
return ;
}
void change(double *p1, double *p2, double *p3)
{
double max, min, mid;
max = *p1;
if (max < *p2) max = *p2; if (max < *p3) max = *p3; min = *p1; if (min > *p2)
min = *p2;
if (min > *p3)
min = *p3;
mid = *p1 + *p2 + *p3 - max - min;
*p1 = min;
*p2 = mid;
*p3 = max;
}
/**/
#include
#include
int report(char ch);
int main(void)
{
char ch;
int pos; //数值位置
printf("请输入字符:");
while ((ch=getchar())!=EOF)
{
if (ch == '\n')
{
continue;
}
pos = report(ch);
if (pos == -)
{
printf("%c不是字母!\n", ch);
}
else
{
printf("%c在字母表中的位置是%d!\n", ch, pos);
}
printf("继续输入:");
}
getchar();
return ;
}
int report(char ch)
{
if (isalpha(ch)) //判断是否为字母
{
return toupper(ch) - 'A' + ; //将小写转换为大写;也可以将大写转换为小写,‘A’要改为‘a’
}
else
{
return -;
}
}
/**/
#include
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("输入数字和指数,计算整数幂结果(q退出):");
while (scanf_s("%lf %d",&x,&exp)==)
{
xpow = power(x, exp);
if (xpow == )
continue;
printf("%.3g的%d次幂为%.5g\n", x, exp, xpow);
printf("继续输入(q退出):");
}
getchar();
return ;
}
double power(double n, int p)
{
double pow = ;
if (p == )
{
if (n==)
{
printf("0的0次幂未定义,请输入其它值:\n");
}
pow = ;
}
else if (p==)
{
pow = ;
}
else if (p>)
{
for (int i = ; i <= p; i++) { pow *= n; } } else { pow = / power(n, -p); //运用了递归 ///////不用递归的写法:for(int i=1; i <= -p; i++) ////////////////// { //////////////////////////// pow*=n; //////////////////////////// } //此种方法返回的是 1/pow } return pow; } /**/ ////上题所示///////// /**/ #include
void to_base_n(int x, int system);
int main(void)
{
int num;
int system_num;
int status;
printf("输入一个整数:");
while (scanf_s("%d",&num)==)
{
printf("输入进制(2-10):");
while ((status=scanf_s("%d",&system_num))==&&(system_num<||system_num>))
{
printf("输入进制超过限定值,请输入2-10之间的数字:");
}
if (status != )
break;
to_base_n(num, system_num);
printf("\n");
printf("继续输入一个整数(q退出):");
}
getchar();
return ;
}
void to_base_n(int x, int system)
{
int r;
r = x % system;
if (x >= system)
{
to_base_n(x / system, system); //递归
}
putchar('' + r); //因为putchar()函数只能输出字符,我们要输出的是字符,所以运用ASCALL码值加上r
}
/**/
#include
void Fibonacci(int n);
int main(void)
{
int n;
printf("输入整数:");
while ((scanf_s("%d",&n)==))
{
Fibonacci(n);
printf("继续输入(q退出):");
}
getchar();
return ;
}
void Fibonacci(int n)
{
int f1, f2, f3;
printf("斐波那契是:");
for (int i = ; i <= n; i++)
{
if (i == )
{
f1 = ;
printf("%d", f1);
}
else if (i==)
{
f2 = ;
printf("%d", f2);
}
else
{
f3 = f2 + f1;
f1 = f2;
f2 = f3;
printf("%d", f3);
}
putchar(' ');
}
putchar('\n');
}

/********编程练习10**********/
/**/
#include
#define MONTHS 12
#define YEARS 5
int main(void)
{
const float rain[YEARS][MONTHS] = //2010-2014年降水量
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
int year, month;
float subtot, total; //初始化要放在循环中
printf("年份\t降水量\n");
for (year = ,total=; year < YEARS; year++) { for (month = ,subtot = ; month < MONTHS; month++) subtot += *(*(rain + year) + month); //每年每月降水量 printf("%d\t%.1f\n", + year, subtot); total += subtot; //5年总降水量 } printf("\n每年的平均降水量为:%.1f\n", total / YEARS); printf("每月5年中平均降水量:\n"); printf(" 1 2 3 4 5 6 7 8 9 10 11 12\n"); for (month = ; month < MONTHS; month++) { for (year = , subtot = ; year < YEARS; year++) { subtot += *(*(rain + year) + month); } printf("%4.1f", subtot / YEARS); } getchar(); return ; } /**/ #include
void copy_arr(double target[], double source[], int n);
void copy_ptr(double *target, double *source, int n);
void copy_ptrs(double *target, double *source_begin, int *source_end );
int main(void)
{
int i;
double source[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double target1[];
double target2[];
double target3[];
printf("source: ");
for ( i = ; i < ; i++) { printf("%.1lf", source[i]); printf(" "); } putchar('\n'); copy_arr(target1, source, ); printf("target1:"); for (i = ; i < ; i++) { printf("%.1lf", target1[i]); putchar(' '); } putchar('\n'); copy_ptr(target2, source, ); printf("target2:"); for ( i = ; i < ; i++) { printf("%.1lf", *(target2 + i)); putchar(' '); } putchar('\n'); copy_ptrs(target3, source, source+); printf("target3:"); for (i = ; i < ; i++) { printf("%.1lf", target3[i]); putchar(' '); } putchar('\n'); getchar(); return ; } void copy_arr(double target[], double source[], int n) { for (int i = ; i < n; i++) { target[i] = source[i]; } } void copy_ptr(double *target, double *source, int n) { for (int i = ; i < n; i++) { *(target + i) = *(source + i); } } void copy_ptrs(double *target, double *source_begin, int *source_end) { while (source_begin < source_end) { *target = *source_begin; target++; source_begin++; } } /**/ #include
#define SIZE 10
void show_arr(const arr[], int n);
int max_arr(const arr[], int n);
int main(void)
{
int arr[SIZE] = { ,,,,,,,,, };
int max;
show_arr(arr, SIZE);
max = max_arr(arr, SIZE);
printf("%d是数组中的最大值。\n", max);
getchar();
return ;
}
int max_arr(const arr[], int n)
{
int max = arr[];
for (int i = ; i < n; i++) { if (max < arr[i]) max = arr[i]; } return max; } void show_arr(const arr[], int n) { for (int i = ; i < n; i++) { printf("%d", arr[i]); putchar(' '); } putchar('\n'); } /**/ #include
#define SIZE 10
double max(const arr[], int n);
void show_arr(const arr[], int n);
int main(void)
{
int arr[SIZE] = { ,,,,,,,,, };
int max_sub;
show_arr(arr, SIZE);
max_sub = max(arr, SIZE);
printf("最大值的下标是%d\n", max_sub);
getchar();
return ;
}
double max(const arr[], int n)
{
int max = arr[];
int sub; //最大值下标
for (int i = ; i < n; i++) { if (max < arr[i]) max = arr[i]; sub = i; } return sub; } void show_arr(const arr[], int n) { for (int i = ; i < n; i++) { printf("%d", arr[i]); putchar(' '); } putchar('\n'); } /**/ #include
#define SIZE 10
double diff_val(const double arr[], int n);
int main(void)
{
double arr[SIZE] = { ,,,,,,,,, };
double value;
value = diff_val(arr, SIZE);
printf("最大值与最小值的差是%lf\n", value);
getchar();
return ;
}
double diff_val(const double arr[], int n)
{
double max = arr[];
double min = arr[];
for (int i = ; i < n; i++) { if (max < arr[i]) { max = arr[i]; } else if(min>arr[i])
{
min = arr[i];
}
}
return max - min;
}
/**/
#include
#define SIZE 10
void reverse(double arr[], int n);
int main(void)
{
double arr[SIZE] = { ,, ,,,,,,, };
printf("排序前的结果是:");
for (int i = ; i < SIZE; i++) { printf("%.2lf", arr[i]); putchar(' '); } putchar('\n'); reverse(arr, SIZE); printf("排序后的结果是:"); for (int i = ; i < SIZE; i++) { printf("%.2lf", arr[i]); putchar(' '); } putchar('\n'); getchar(); return ; } void reverse(double arr[], int n) { for (int i = ; i < n / ; i++) { double tmp = arr[i]; arr[i] = arr[n - - i]; arr[n - - i] = tmp; } } /**/ #include
#define ROWS 3 //行
#define COLS 4 //列
void copy_arr(double target_arr[][COLS], double source_arr[][COLS], int row);
int main(void)
{
double source_arr[ROWS][COLS] =
{
{1.1,2.2,3.3,4.4},
{2.2,2.3,2.4,2.5},
{3.3,4.4,5.5,6.6}
};
double target_arr[ROWS][COLS];
copy_arr(target_arr, source_arr, ROWS);
printf("结果为:\n");
for (int i = ; i < ROWS; i++) { for (int j = ; j < COLS; j++) { printf("%.2lf", target_arr[i][j]); putchar(' '); } putchar('\n'); } getchar(); return ; } void copy_arr(double target_arr[][COLS], double source_arr[][COLS], int row) { for (int i = ; i < row; i++) { for (int j = ; j < COLS; j++) { target_arr[i][j] = source_arr[i][j]; } } } /**/ #include
#define SIZE1 7
#define SIZE2 3
void copy_arr(int target_arr[],const int source_arr[], int n);
int main(void)
{
int source_arr[SIZE1] = { ,,,,,, };
int target_arr[SIZE2];
int values;
copy_arr(target_arr, source_arr + , SIZE2);
for (int i = ; i < SIZE2; i++) { printf("%d", target_arr[i]); putchar(' '); } printf("\n"); getchar(); return ; } void copy_arr(int target_arr[], const int source_arr[], int n) { for (int i = ; i < n; i++) { target_arr[i] = source_arr[i]; } } /**/ #include
void copy_arr(int rows, double target_arr[][],double source_arr[][]);
void show_arr(int rows, double target_arr[][], double source_arr[][]);
int main(void)
{
double source_arr[][] =
{
{1.1,2.2,3.3,4.4,5.5},
{2.2,3.3,4.4,5.5,6.6},
{3.3,4.4,5.5,6.6,7.7}
};
double target_arr[][];
copy_arr(, target_arr, source_arr);
show_arr(, target_arr, source_arr);
getchar();
return ;
}
void copy_arr(int rows, double target_arr[][], double source_arr[][])
{
for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { target_arr[i][j] = source_arr[i][j]; } } } void show_arr(int rows, double target_arr[][], double source_arr[][]) { for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { printf("%.2lf", source_arr[i][j]); putchar(' '); } } putchar('\n'); for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { printf("%.2lf", target_arr[i][j]); putchar(' '); } } putchar('\n'); } /**/ #include
void add_arr(int source_arr1[], int source_arr2[], int target_arr[], int n);
int main(void)
{
int source_arr1[] = { ,,, };
int source_arr2[] = { ,,, };
int target_arr[];
add_arr(source_arr1, source_arr2, target_arr, );
for (int i = ; i < ; i++) { printf("%d", target_arr[i]); putchar(' '); } putchar('\n'); getchar(); return ; } void add_arr(int source_arr1[], int source_arr2[], int target_arr[], int n) { for (int i = ; i < n; i++) { target_arr[i] = source_arr1[i] + source_arr2[i]; } } /**/ #include
void times2_arr(int source_arr[][], int target_arr[][], int rows);
void show_arr(int source_arr[][], int target_arr[][], int rows);
int main(void)
{
int source_arr[][] =
{
{,,,,},
{,,,,},
{,,,,}
};
int target_arr[][];
times2_arr(source_arr, target_arr, );
show_arr(source_arr, target_arr, );
putchar('\n');
getchar();
return ;
}
void times2_arr(int source_arr[][], int target_arr[][], int rows)
{
for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { target_arr[i][j] = *source_arr[i][j]; } } } void show_arr(int source_arr[][], int target_arr[][], int rows) { for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { printf("%4d", source_arr[i][j]); //putchar(' '); //putchar(' '); } } putchar('\n'); for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { printf("%4d", target_arr[i][j]); //putchar(' '); } } putchar('\n'); } /**/ #include
#define YEARS 5
#define MONTHS 12
void total_rain(const float rain[][MONTHS], int years);
void average(const float rain[][MONTHS], int years);
int main(void)
{
const float rain[YEARS][MONTHS] = //2010-2014年降水量
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
total_rain(rain, YEARS);
printf("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NEV DEC\n");
average(rain, YEARS);
putchar('\n');
getchar();
return ;
}
void total_rain(const float rain[][MONTHS], int years)
{
int i, j;
float subtotal,total = ;
for ( i = ; i < years; i++) //每次循环需要将total归零。因为计算的是每年每月降水量 { for ( j = , subtotal = ; j < MONTHS; j++) { subtotal += rain[i][j]; } printf("%d\t%.1f\n", + i, subtotal); total += subtotal; } printf("5年总降水量为%.1f\n", total / years); } void average(const float rain[][MONTHS], int years) { int i, j; float subtotal; for ( i = ; i < MONTHS; i++) { for ( j = , subtotal = ; j < years; j++) { subtotal += rain[j][i]; } printf("%-4.1f", subtotal / years); } } /**/ #include
void input_arr(double arr[][], int rows);
double average_cols(double arr[], int cols);
double average_all(double arr[][], int rows);
double max_arr(double arr[][], int rows);
void show_arr(double arr[][], int rows);
int main(void)
{
double arr[][];
printf("输入15个浮点值:\n");
input_arr(arr, );
putchar('\n');
for (int i = ; i < ; i++) { printf("%d组数据的平均值是%lf\n", i+, average_cols(arr[i], )); } printf("所有数据的平均值是%lf\n", average_all(arr, )); printf("15个数据中最大值为:%lf\n", max_arr(arr, )); show_arr(arr, ); getchar(); return ; } void input_arr(double arr[][], int rows) { for (int i = ; i < rows; i++) { printf("请输入第%d组数据:\n", i + ); for (int j = ; j < ; j++) { scanf_s("%lf", &arr[i][j]); getchar(); } } } double average_cols(double arr[], int cols) { double sum = 0.0; for (int i = ; i < cols; i++) { sum += arr[i]; } return (sum / cols); } double average_all(double arr[][], int rows) { double sum = 0.0; for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { sum += arr[i][j]; } } return (sum / ( * rows)); } double max_arr(double arr[][], int rows) { double max = 0.0; for (int i = ; i < rows; i++) { for (int j = ; j < ; j++) { if (max
#define ROWS 3
#define COLS 5
void input_arr(int rows,int cols, double arr[rows][cols]);
double average_cols(int cols,double arr[cols]);
double average_all(int rows, int cols, double arr[rows][cols]);
double max_arr(int rows, int cols, double arr[rows][cols]);
void show_arr(int rows, int cols, double arr[rows][cols]);
int main(void)
{

 getchar();  
 return 0;  

}*/