6.1
/*编写一个程序,创建一个包含26个元素的数组,
并在其中储存26个小写字母,然后打印数组所有内容*/
#include
#define SIZE 27
int main(void)
{
char letters[SIZE];
int n;
int num = 65; //'A'的数值为65
for (n = 0; n < 26; n++)
{
letters[n] = num + n;
}
for (n = 0; n < 26; n++)
printf("%c ",letters[n]);
return 0;
}
6.2
/*使用嵌套循环,按下列各式打印字符*/
#include
int main(void)
{
int m, n;
for (m = 0; m < 5; m++)
{
for (n = 0; n < m + 1; n++)
printf("$");
printf("\n");
}
return 0;
}
6.3
/*使用嵌套循环,按下面的格式打印字母*/
#include
int main(void)
{
int i,j,m;
char l = 'F';
for (i = 0; i < 6; i++)
{
for (j = 0; j < i + 1; j++)
printf("%c", l - j);
printf("\n");
}
return 0;
}
6.4
/*使用嵌套循环,按下面的格式打印字母*/
#include
int main(void)
{
int i, j;
char l = 'A';
for (i = 0; i < 6; i++)
{
for (j = 0; j < i + 1; j++)
printf("%c", l++);
printf("\n");
}
return 0;
}
6.5
/*编写一个程序,提示用户输入大写字母
使用嵌套循环以下面的金字塔型的格式打印字母*/
#include
int main(void)
{
char end;
char start = 'A';
char ch = start;
printf("Please enter a letter: \n");
scanf("%c", &end);
for (int i = 0; i <= end - start; i++, ch = start)
{
for (int j = 0; j < end - start - i; j++)
printf(" ");
for (int k = 0; k <= i; k++, ch++)
printf("%c", ch);
ch = ch - 2;
for (int c = 0; c < i; c++, ch--)
printf("%c", ch);
printf("\n");
}
return 0;
}
6.6
/*编写一个程序打印一个表格,每一行打印一个整数,该数的平方
该数的立方。要求用户输入表格的上下限。使用欧冠一个for循环*/
#include
int main(void)
{
int start, end;
printf("Please enter the start and end:");
scanf("%d %d", &start, &end);
int length = end - start + 1;
for (int i = 0; i < length; i++)
{
printf("%-8d %-8d %-8d\n", start + i, (start + i)*(start + i), (start + i)*(start + i)*(start + i));
}
return 0;
}
6.7
//编写一个程序把一个单词读入一个字符数组中,然后倒序打印这个单词
//
#include
#include
#define SIZE 40
int main(void)
{
char words[SIZE];
int length;
printf("Please enter a word: ");
scanf("%s", words);
length = strlen(words);
printf("length=%d\n", length);
for (int i = 0; i < length; i++)
{
printf("%c", words[length - i -1]);
}
return 0;
}
6.8
//要求输入两个浮点数,打印两数之差除以两数乘积结果
//用户输入非数字之前,程序应循环处理用户输入的每对值
#include
int main(void)
{
float a, b;
printf("Please enter two numbers: ");
while (scanf("%f %f", &a, &b) == 2)
{
printf("quotient=(%f-%f)/(%f*%f)=%f\n", a, b, a, b, (a - b) / (a*b));
printf("Please enter the next two number: ");
}
return 0;
}
6.9
//要求输入两个浮点数,打印两数之差除以两数乘积结果
//用户输入非数字之前,程序应循环处理用户输入的每对值
#include
float do_quotient(float a, float b); //定义函数原型
int main(void)
{
float a, b;
printf("Please enter two numbers: ");
while (scanf("%f %f", &a, &b) == 2)
{
printf("Results: %f ", do_quotient(a,b));
printf("Please enter the next two number: ");
}
return 0;
}
float do_quotient(float a, float b)
{
return (a - b) / (a*b);
}
6.10
//要求输入一个上线整数和一个下限整数,计算从上限到下限范围的所有整数的平方和
//继续提示输入上限和下限整数,知道输入的上限小于下限
#include
int main(void)
{
int low, up, value;
printf("Enter lower and upper integer limits: ");
scanf("%d %d", &low, &up);
while (low < up)
{
int sum = 0;
int length = up - low + 1;
for (int i = 0; i < length; i++)
{
value = low + i;
sum = sum + value * value;
}
printf("The sums of the squares from %d to %d is %d\n",
low*low, up*up, sum);
printf("Enter next set of limits: ");
scanf("%d %d", &low, &up);
}
printf("Job Down!");
return 0;
}
6.11
//编写一个程序,在数组中读入8个整数
//然后按倒序打印这8个整数
#include
int main(void)
{
int nums[8]; //可将8替换为size
printf("Pease enter 8 numbers: ");
for (int i = 0; i < 8; i++)
{
scanf("%d", &nums[i]);
}
printf("Result:\n");
for (int i = 0; i < 8; i++)
printf("%d ", nums[7 - i]);
return 0;
}
6.12
/*计算两个无限序列的综合*/
#include
int main(void)
{
int size;
float sum1, sum2;
float num;
num = 1.0;
printf("Please enter the sums size: ");
scanf("%d", &size);
while(size > 0)
{
sum1 = sum2 = 0;
/*
sum = 0;
for (int i = 1; i < size +1 ; i++)
{
sum = sum + num / i;
}
*/
for (int i = 1; i < size + 1; i++)
{
if (i % 2 == 1)
sum1 = sum1 + num / i;
if (i % 2 == 0)
sum2 = sum2 - num / i;
}
printf("The first shulie result is : %f\n", sum1 - sum2);
printf("The second shulie result is : %f\n", sum1 + sum2);
printf("Please enter the next sums size: ");
scanf("%d", &size);
}
printf("Job Down!");
return 0;
}
6.13
/*编写一个程序,创建一个包含8个元素的int类型数组,分别把
数组元素设置为2的前8次幂。使用for循环设置数组元素的值。使用
do while循环显示数组元素的值*/
#include
#define SIZE 8
int main(void)
{
int nums[SIZE];
int product = 1;
for (int i = 0; i < SIZE; i++)
{
product = product * 2;
nums[i] = product;
}
int i = 0;
do
{
printf("%d ", nums[i]);
i++;
} while (i < SIZE);
return 0;
}
6.14
#include
#define SIZE 8
int main(void)
{
double nums1[SIZE];
double nums2[SIZE];
//初始化sum1
for (int i = 0; i < SIZE; i++)
scanf("%lf", &nums1[i]);
//初始化sum2
double value = 0;
//nums2[0] = nums1[0]; 则for循环中int i = 1
for (int i = 0; i < SIZE; i++)
{
value = value + nums1[i];
nums2[i] = value;
//nums2[i] = nums2[i-1]+nums1[i];
}
//打印数组
for (int i = 0; i < SIZE; i++)
{
printf("%10.2lf ", nums1[i]);
}
printf("\n");
for (int i = 0; i < SIZE; i++)
printf("%10.2lf ", nums2[i]);
return 0;
}
6.15
//读取一行输入,然后把输入的内容倒序打印出来
//未完全读懂题目- -!,参考其他人代码
#include
int main(void)
{
char input[255];
int end = -1;
int index = 0;
for (int i = 0; i < 255; i++)
{
scanf("%c", &input[i]);
index++;
if (input[i] == '\n')
break;
}
for (int i = 0; i < index + 1; i++)
printf("%c", input[index - i]);
return 0;
}
6.16
//Daphne 10%单利投资100美元
//Deirdre 5%复利投资100美元
//比较多少年后Deirdre超过Daphne
#include
int main(void)
{
float sum_da = 100;
float sum_de = 100;
float da, de;
da = 0.1;
de = 0.05;
int n = 0;
int value = 0;
do
{
n++;
sum_da = sum_da + 10;
sum_de = sum_de * 1.05;
printf("%f,%f,%d\n", sum_da, sum_de,n);
} while (sum_de < sum_da);
printf("After %d years later deirdre is more richer than Daphne.", n);
return 0;
}
6.17
//Lucky赢得了100万美元(Why not me),存入年利率8%账户
//每年最后一天Lucky去除10万,多少年取完
#include
int main(void)
{
float rate = 0.08;
float sum = 100; //单位:万
int n=0;
do
{
n++;
sum = sum * (1 + rate) - 10;
printf("%f, %d\n", sum, n);
} while (sum > 0);
printf("All the money will be out of bank at %d years.", n);
return 0;
}
6.18
//丢一个朋友增加余下的一倍朋友
#include
//#define DUNBARNUM 150
int main(void)
{
int friends = 5;
int weeks = 1;
const int DUNBARNUM = 150;
do
{
weeks++;
friends = (friends - 1) * 2;
printf("%d, %d\n", friends, weeks);
//getchar();
} while (friends < DUNBARNUM);
printf("After %d weeks he's friends are more than 150.", weeks);
return 0;
}