所有代码均在VS2017最新版下编译通过,程序运行正常。这是我自己的答案,太懒了,所以第十二题之后没做(明明自己是个菜鸡还不练 )。这不是官方答案(好像官方答案是挑着题给的,不是全给),仅供参考,如有错误还请海涵。
【第一题】
#include <stdio.h>
int main(void)
{
char lowercase[26];
char ch;
int count;
for (count = 0,ch = 'a'; count < 26,ch <= 'z'; count++,ch++) {
lowercase[count] = ch;
}
for (count = 0; count < 26; count++) {
printf("%c ", lowercase[count]);
}
return 0;
}
【第二题】
#include <stdio.h>
int main(void)
{
char dollar_sign;
int row, column;
dollar_sign = '$';
for (row = 1; row <= 5; row++) {
for (column = 1; column <= row; column++) {
printf("%c", dollar_sign);
}
printf("\n");
}
return 0;
}
【第三题】
#include <stdio.h>
int main(void)
{
char letter_shown;
int row,column;
for (row = 1; row <= 6; row++) {
for (column = 1, letter_shown = 'F'; column <= row; column++, letter_shown--) {
printf("%c", letter_shown);
}
printf("\n");
}
return 0;
}
【第四题】
#include <stdio.h>
int main(void)
{
char letter_shown;
int row, column;
letter_shown = 'A';
for (row = 1; row <= 6; row++) {
for (column = 1; column <= row; column++) {
printf("%c", letter_shown);
letter_shown++;
}
printf("\n");
}
return 0;
}
【第五题】
#include <stdio.h>
int main(void)
{
char letter_input;
char letter_shown;
int row, count_a, count_b, count_c;
printf("Please input the extended aim:\n");
scanf_s("%c", &letter_input);//用户输入终止字母
const count = letter_input - (int)'A' + 1;//定义静态变量count
for (row = 1; row <= count; row++) {
for (count_a = 1; count_a <= (count - row); count_a++) {
printf(" "); //打印升序部分空格
}
for (count_b = 1, letter_shown = 'A'; count_b <= row; count_b++, letter_shown++) {
printf("%c", letter_shown);//打印升序字母
}
for (count_c = 1, letter_shown = letter_input - count + row - 1; count_c <= (row - 1); count_c++, letter_shown--) {
printf("%c", letter_shown);//打印除输入字母外的降序字母
//该循环打印的首字母 = (count - row) + 1,观察得知
}
for (count_a = 1; count_a <= (count - row); count_a++) {
printf(" "); //打印降序部分空格
}
printf("\n");
}
return 0;
}
【第六题】
#include <stdio.h>
int main(void)
{
int lower_limit, upper_limit;
int row;
int ans_square, ans_cube,linshi_integer;
printf("Please input the lower limit number in integer:\n");
scanf_s("%d", &lower_limit);
printf("Please input the upper limit number in integer:\n");
scanf_s("%d", &upper_limit);
printf("\n");
printf("%16s%16s%16s\n", "Integer", "Square", "Cube");
for (row = 1,linshi_integer = lower_limit; row <= (upper_limit - lower_limit + 1); row++,linshi_integer++) {
ans_square = linshi_integer * linshi_integer;
ans_cube = linshi_integer * linshi_integer * linshi_integer;
printf("%16d%16d%16d\n", linshi_integer, ans_square, ans_cube);
}
return 0;
}
【第七题】
#include <stdio.h>
#include <string.h>
int main(void)
{
char input[41];
int count;
printf("Please input a single word:\n");
scanf_s("%s", input,41);
printf("Here is the backward word:\n");
for (count = strlen(input) - 1; count >= 0; count--) {
printf("%c", input[count]);
}
return 0;
}
【第八题】
#include <stdio.h>
int main(void)
{
double a, b;
double linshi1, linshi2,ans;
printf("Please input the first floating-point number:\n");
while (scanf_s("%lf", &a) == 1) {
printf("Please input the second floating-point number:\n");
scanf_s("%lf", &b);
linshi1 = a - b;
linshi2 = a * b;
ans = linshi1 / linshi2;
printf("The answer is: %f\n", ans);
printf("Please input the first floating-point number:\n");
}
return 0;
}
【第九题】
#include <stdio.h>
double calculation(double a, double b);
int main(void)
{
double a, b,ans;
printf("Please input the first floating-point number:\n");
while (scanf_s("%lf", &a) == 1) {
printf("Please input the second floating-point number:\n");
scanf_s("%lf", &b);
ans = calculation(a, b);
printf("The answer is: %f\n", ans);
printf("Please input the first floating-point number:\n");
}
return 0;
}
double calculation(double a, double b) {
double linshi1, linshi2, ans;
linshi1 = a - b;
linshi2 = a * b;
ans = linshi1 / linshi2;
return ans;
}
【第十题】
#include <stdio.h>
int main(void)
{
int lower_limit, upper_limit, count,sum = 0,square;
printf("Enter lower and upper integer limits:");
scanf_s("%d %d", &lower_limit, &upper_limit);
while (upper_limit > lower_limit) {
for (count = lower_limit; count <= upper_limit; count++) {
square = count * count;
sum += square;
}
printf("The sums of the squares from %d to %d is %d\n", lower_limit * lower_limit, upper_limit * upper_limit, sum);
sum = 0;
printf("Enter next set of limits:");
scanf_s("%d %d", &lower_limit, &upper_limit);
}
printf("Done\n");
return 0;
}
【第十一题】
#include <stdio.h>
int main(void)
{
int input[8],count;
printf("Please input eight integers and press the enter key:\n");
for (count = 0; count < 8; count++) {
scanf_s("%d",& input[count]);
}
printf("Here are the reverse order results:\n");
for (count = 7; count >= 0; count--) {
printf("%d ", input[count]);
}
return 0;
}
【第十二题】
#include <stdio.h>
int main(void)
{
double sum = 0.0,count;
int limit;
printf("Please enter the limit of terms:");
scanf_s("%d", &limit);
while (limit > 0) {
if (limit % 2 == 0) { //even term number given
for (count = 1.0;count < (double)limit; count += 2.0) {
sum += (1.0 / count);
}
}
else if (limit % 2 != 0) { //odd term number given
for (count = 1.0;count <= (double)limit; count += 2.0) {
sum += (1.0 / count);
}
}
printf("Sum is %lf\n", sum * 2.0);
sum = 0.0;
printf("Please enter the limit of terms:");
scanf_s("%d", &limit);
}
return 0;
}
弄了一个下午+半个晚上,水平还是有待提高。