上周一结束了c语言考试,题目很简单,题型为选择题、程序阅读题、编程题。在考之前,花了4个晚上的时间,将老师提示过的课本例题及代码敲了一遍,看了两遍。
结束c语言考试之后,紧接着的是面向对象程序设计课,主讲java语言。换了个老师讲课,挺有趣,会类比,会延伸,比c语言老师有意思。
今天周一,校历上为第12周。离散数学的课程在这周也要结束了,下下周或许就考试了。
好好学习,少熬夜。
以下,为c语言考试复习的代码,存此留念:
p3
例1.3:
#include <stdio.h>
int sumab(int x, int y);
int main(void)
{
int a, b, sum;
printf("请输入变量a与b的值:");
scanf("%d %d", &a, &b);
sum = sumab(a, b);
printf("a与b的和等于%d\n", sum);
return (0);
}
int sumab(int x, int y)
{
int z;
z = x + y;
return z;
}
p22
(1):
#include <stdio.h>
int main(void)
{
int m, n, k;
puts("Please input two integers:");
printf("m:"); scanf("%d", &m);
printf("n:"); scanf("%d", &n);
k = m;
m = n;
n = k;
printf("m:%d\n", m);
printf("n:%d\n", n);
return (0);
}
(3):
#include <stdio.h>
int main(void)
{
int n;
int a, b, c;
printf("Please input a integer:");
scanf("%d", &n);
a = n % 10;
b = (n / 10) % 10;
c = n / 100;
printf("%d%d%d\n", a, b, c);
return (0);
}
(4):
#include <stdio.h>
int main(void)
{
int n;
int a, b, c;
printf("Please input a integer:");
scanf("%d", &n);
a = n % 10;
b = (n / 10) % 10;
c = n / 100;
int sumabc;
sumabc = a + b + c;
printf("%d\n", sumabc);
return (0);
}
p35
(1):
#include <stdio.h>
int main(void)
{
int r, h;
double pi = 3.14;
printf("请输入圆柱的半径:"); scanf("%d", &r);
printf("请输入圆柱的高:"); scanf("%d", &h);
double c; //圆周长 2*pi*r
c = 2 * pi * r;
double s;//圆面积pi*r*r
s = pi * r * r;
double s2;//圆柱表面积2*pi*r*r+2*pi*r*h
s2 = s * 2 + c * h;
double v;//圆柱体积pi*r*r*h
v = s * h;
printf("圆周长:%.2f\n", c);
printf("圆面积:%.2f\n", s);
printf("圆柱表面积:%.2f\n", s2);
printf("圆柱体积:%.2f\n", v);
return (0);
}
(2):
#include <stdio.h>
int main(void)
{
int f;
printf("请输入华氏温度值:"); scanf("%d", &f);
double c;
c = ((double)5 / 9) * (f - 32); // 这里的强制类型转换需要注意
printf("摄氏温度值为:%.2f\n", c);
return (0);
}
p46
例4.6:
#include <stdio.h>
int main(void)
{
float x, y;
printf("Input x:");
scanf("%f", &x);
if (x < 10)
if(x < 0) y = 5 * x - 6;
else y = 4 * x;
else
y = 2 * x + 3;
printf("x = %f, y = %f\n", x, y);
return (0);
}
例4.7:
#include <stdio.h>
int main(void)
{
float x, y;
printf("Input x:");
scanf("%f", &x);
switch (x >= 0)
{
case 0 : y = 5 * x - 6; break;
case 1 : switch (x >= 10)
{
case 0 : y = 4 * x; break;
case 1 : y = 2 * x + 3; break;
}
}
printf("y = %f\n", y);
return (0);
}
例4.8:
#include <stdio.h>
int main(void)
{
float a, b, c, t;
printf("Please input three integers:");
scanf("%f %f %f", &a, &b, &c);
if (a > b) {
t = a; a = b; b = t;
}
if (a > c) {
t = a; a = c; c = t;
}
if (b > c) {
t = b; b = c; c = t;
}
printf("%f %f %f\n", a, b, c);
return (0);
}
例4.10:
#include <math.h>
#include <stdio.h>
int main(void)
{
float a, b, c, d, x1, x2, p, q;
printf("输入方程系数 a, b, c:");
scanf("%f %f %f", &a, &b, &c);
d = b * b - 4 * a * c;
if (fabs(d) <= 1e-6) //d == 0; fabs是求绝对值函数
printf("有两个相等的实根:%8.4f\n", -b / (2 * a));
else if (fabs(d) > 1e-6) //d > 0
{
x1 = (-b + sqrt(d)) / (2 * a); //sqrt是求平方根函数
x2 = (-b - sqrt(d)) / (2 * a);
printf("有两个不相等的实根:%8.4f 和 %8.4f\n", x1, x2);
}
else
{
p = -b / (2 * a);
q = sqrt(-d) / (2 * a);
printf("有两个共轭复根\n");
printf("%8.4f + %8.4f\n", p, q);
printf("%8.4f - %8.4f\n", p, q);
}
return (0);
}
p51
(1):
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("请输入三个数:");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
printf("能\n");
else
printf("不能\n");
return (0);
}
(4):
#include <stdio.h>
int main(void)
{
int a, b, c, d, t;
printf("请输入4个整数:");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a > b) {t = a; a = b; b = t;}
if (a > c) {t = a; a = c; c = t;}
if (a > d) {t = a; a = d; d = t;}
if (b > c) {t = b; b = c; c = t;}
if (b > d) {t = b; b = d; d = t;}
if (c > d) {t = c; c = d; d = t;}
printf("从小到大顺序输出:%d %d %d %d\n", a, b, c, d);
return (0);
}
(5):
#include <stdio.h>
int main(void)
{
int n3, a, b, c;
printf("请输入一个三位整数:");
scanf("%d", &n3);
c = n3 % 10;
b = (n3 / 10) % 10;
a = n3 / 100;
printf("%d %d %d\n", a, b, c); //将个位数十位数百位数输出
if (c < 7 && b % 3 == 0 && a * a > 20)
printf("YES\n");
else
printf("NO\n");
return (0);
}
p60
例5.9:
#include <stdio.h>
int main(void)
{
int i = 0, n;
long sum = 0;
while (i < 100)
{
scanf("%d", &n);
i++;
if (n <= 0) continue; // 如果n<=0,则跳过continue后的语句,直接开始判断下一次循环
sum += n;
}
printf("sum = %ld\n", sum);
return (0);
}
p62
例5.12:
#include <stdio.h>
#include <math.h>
int main(void)
{
float i = 1.0;
int k = 1;
double t = 1.0, pi = 0;
do {
pi = pi + t;
i += 2;
k = -k;
t = k / i;
} while (fabs(t) >= 1e-6);
pi = pi * 4;
printf("pi = %f\n", pi);
return (0);
}
p65
(1):
#include <stdio.h>
int main(void)
{
float sum = 1.0;
int k = 1;
float i = 1;
float z = 0;
do {
sum = sum + z;
i += 1;
k = -k;
z = k / i;
} while (i <= 100);
printf("%f\n", sum);
return (0);
}
#include <stdio.h>
int main(void)
{
int c, s;
float sum = 0;
for (c = 1; c <= 100; c++)
{
if (c % 2 == 0) s = -1;
else s = 1;
sum += s * (1 / (float)c);
}
printf("%f\n", sum);
return (0);
}
(3):
#include <stdio.h>
int main(void)
{
float sum = 1.0;
int n;
for (n = 2; n <= 100; n++)
sum = sum + (float)(n + 1) / n;
printf("%f\n", sum);
return (0);
}
p68
例6.1:
#include <stdio.h>
int main(void)
{
int n, sum = 0, a[10];
float aver;
for (n = 0; n < 10; n++)
{
scanf("%d", &a[n]);
sum = sum + a[n];
}
aver = sum / 10.0;
for (n = 0; n <= 9; n++)
{
printf("%d,", a[n]);
}
printf("%d, %f\n", sum, aver);
return (0);
}
例6.2:
#include <stdio.h>
#define N 10
int main(void)
{
int n, m, sign = 0;
int num[N] = {16, 35, 48, 29, 56, 43, 93, 64, 90, 48};
printf("Please input the number:");
scanf("%d", &n);
for (m = 0; m < N; m++)
if (n == num[m])
{
printf("%d, %d\n", m, num[m]);
sign = 1;
}
if (sign != 1)
printf("Have no this number.\n");
return (0);
}
p77
例6.9:
#define N 7
#include <stdio.h>
int main(void)
{
int i, j, a[N][N];
for (i = 1; i < N; i++)
{
a[i][i] = 1;
a[i][1] = 1;
}
for (i = 3; i < N; i++)
for (j = 2; j <= i - 1; j++)
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
for (i = 1; i < N; i++)
{
for (j = 1; j <= i; j++)
printf("%5d", a[i][j]);
printf("\n");
}
return (0);
}
p91
(1):
#include <stdio.h>
#define N 5
int main(void)
{
int a[N] = {2, 3, 5, 7, 11};
int b[N] = {1, 4, 6, 8, 9};
int c[N] = {0};
int i;
for (i = 0; i < N; i++)
{
c[i] = a[i] + b[i];
}
for(i = 0; i < N; i++)
printf("%d\n", c[i]);
return (0);
}
(5):
#include <stdio.h>
#define N 100
int main(void)
{
int i;
int score;
int a[N];
int n = 0;
float total = 0;
float ave;
int count;
puts("请输入学生的成绩:");
scanf("%d", &score);
while (score >= 0)
{
a[n] = score;
n++;
total += score;
scanf("%d", &score);
}
ave = (double)total / n;
for (i = 0; i < n; i++)
{
if (a[i] >= ave)
count++;
}
printf("平均成绩:%f\n", ave);
printf("不低于平均分的人数:%d\n", count);
return (0);
}
p96
例7.5:
#include <stdio.h>
#include <math.h>
int prime(int i)
{
int j, k, flag = 1;
k = sqrt(i);
for (j = 2; j <= k; j++)
if (i % j == 0) {
flag = 0; break;
}
return flag;
}
int main(void)
{
int i;
for (i = 3; i < 100; i++)
if (prime(i) == 1)
printf("%4d", i);
printf("\n");
return (0);
}
p101
例7.8:
#include <stdio.h>
long fact(int n);
int main(void)
{
int n;
printf("please enter n:");
scanf("%d", &n);
printf("n! = %ld\n", fact(n));
return (0);
}
long fact(int n)
{
if (n == 1 || n == 0)
return (1);
else
return (n * fact(n - 1));
}
p117
(4):
#include <stdio.h>
int prime(int n)
{
int i;
int k = 1;
for (i = 2; i < n; i++)
if (n % i == 0) {
k = 0; break;
}
return (k);
}
int main(void)
{
int n;
printf("Please input a integer:");
scanf("%d", &n);
printf("%d\n", prime(n));
return (0);
}
(6):
#include <stdio.h>
#define N 100
void delete_char(char str[], char ch)
{
int j, k;
for (j = k = 0; str[j] != '\0'; j++) {
if (str[j] != ch)
str[k++] = str[j]; //先赋值再自增
}
str[k] = '\0';
}
int main(void)
{
char str1[N] = {0};
char ch1;
printf("Please input a string:");
scanf("%s", str1);
printf("Please input a string that you want to be deleted:");
scanf("%s", &ch1);
delete_char(str1, ch1);
printf("%s\n", str1);
return (0);
}
p130
例9.2:
#include <stdio.h>
int main(void)
{
int num1 = 12, *p1;
float num2 = 3.14, *p2;
char ch = 'p', *p3;
p1 = &num1;
p2 = &num2;
p3 = &ch;
printf("num1 = %d, *p1 = %d\n", num1, *p1);
printf("num2 = %4.2f, *p2 = %4.2f\n", num2, *p2);
printf("ch = %c, *p3 = %c\n", ch, *p3);
return (0);
}
例9.3:
#include <stdio.h>
int main(void)
{
int num1, num2, *p, *p1, *p2;
p1 = &num1;
p2 = &num2;
printf("Input the first number:");
scanf("%d", p1);
printf("Input the second number:");
scanf("%d", p2);
printf("num1 = %d, num2 = %d\n", num1, num2);
if (*p1 > *p2)
{
p = p1;
p1 = p2;
p2 = p;
}
printf("min = %d, max = %d\n", *p1, *p2);
return (0);
}
p136
例9.2
(1):
#include <stdio.h>
void copystr(char str[], char str2[])
{
int i = 0;
while (str[i] != '\0')
{
str2[i] = str[i];
i++;
}
str2[i] = '\0';
}
int main(void)
{
char a[20] = "I love china!";
char b[20] = "good!";
printf("a = %s b = %s\n", a, b);
copystr(a, b);
printf("a = %s b = %s\n", a, b);
return (0);
}
(2):// 此题有问题
#include <stdio.h>
void copystr(char str1[], char str2[])
{
int i = 0;
while (str1[i] != '\0')
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
}
int main(void)
{
int i;
char *a = "I love china!";
char *b = "good!";
printf("a = %s, b = %s\n", a, b);
copystr(a, b);
for (i = 0; *(a + i) != '\0'; i++)
putchar(*(a + i));
printf("\n");
for (i = 0; *(b + i) != '\0'; i++)
putchar(*(b + i));
return (0);
}
(3):// 此题有问题
#include <stdio.h>
void copystr(char *str1, char *str2)
{
for (; *str1 != '\0'; str1++, str2++)
*str2 = *str1;
*str2 = '\0';
}
int main(void)
{
int i;
char *a = "I love china!", *b = "good!";
for (i = 0; *(a + i) != '\0'; i++)
putchar(*(a + i));
printf("\n");
for (i = 0; *(b + i) != '\0'; i++)
putchar(*(b + i));
printf("\n");
copystr(a, b);
for (; *a != '\0'; a++) printf("%c", *a);
printf("\n");
for (; *b != '\0'; b++) printf("%c", *b);
printf("\n");
return (0);
}
(4):
#include <stdio.h>
void copystr(char *str1, char *str2)
{
int i = 0;
for (; (*(str2 + i) = *(str1 + i)) != '\0'; i++);
}
int main(void)
{
char a[20] = "I love china!";
char b[20] = "good!";
printf("a = %s, b = %s\n", a, b);
copystr(a, b);
puts(a);
puts(b);
return (0);
}
p181
(1):
#include <stdio.h>
#define N 100
struct sp
{
char snum[20];
char sname[20];
float price;
int total;
float sum;
};
int main(void)
{
struct sp a[N];
int i;
puts("请输入「商品编号」、「商品名」、「单价」、「数量」:");
for (i = 0; i < N; i++) {
scanf("%s", a[i].snum);
scanf("%s", a[i].sname);
scanf("%f", &a[i].price);
scanf("%d", &a[i].total);
a[i].sum = a[i].price * a[i].total;
}
for (i = 0; i < N; i++)
printf("商品编号:%s, 商品名:%s, 单价:%f, 数量:%d, 总价:%f\n", a[i].snum, a[i].sname, a[i].price, a[i].total, a[i].sum);
return (0);
}
(2):
#include <stdio.h>
#define M 2
struct stud
{
char num[5];
char name[10];
int score[3];
float aver;
};
int main(void)
{
int i, j, k;
struct stud s[M];
puts("请输入学号、姓名和3门课的成绩:");
for (i = 0; i < M; i++)
{
scanf("%s", s[i].num);
scanf("%s", s[i].name);
k = 0;
for (j = 0; j <= 2; j++)
{
scanf("%d", &s[i].score[j]);
k = k + s[i].score[j];
}
s[i].aver = k / 3.0;
}
puts("学号、姓名、3门课的成绩以及平均分:");
for (i = 0; i < M; i++)
{
printf("%s %s ", s[i].num, s[i].name);
for (j = 0; j <= 2; j++)
printf("%d ", s[i].score[j]);
printf("%.2f", s[i].aver);
printf("\n");
}
return (0);
}
p212
(4):
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char ch, filename[10];
gets(filename);
if ((fp = fopen(filename, "w")) == NULL)
{
printf("cannot open file\n");
exit(0);
}
ch = getchar();
while (ch != '#')
{
fputc(ch, fp);
putchar(ch);
ch = getchar();
}
fclose(fp);
return (0);
}
(5):
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char str1[100];
int i = 0;
if ((fp = fopen("text", "w")) == NULL)
{
printf("cannot open the file\n");
exit(0);
}
printf("Please input a string:\n");
gets(str1);
while (str1[i] != '\0')
{
if (str1[i] >= 'a' && str1[i] <= 'z')
str1[i] = str1[i] - 32;
fputc(str1[i], fp);
i++;
}
fclose(fp);
return (0);
}
(6):
#include <stdlib.h>
#include <stdio.h>
#define N 5
int main(void)
{
struct record
{
int num;
char name[4];
int count;
float price;
} s[N] = {{1, "aaa", 10, 12.3}, {2, "bbb", 20, 23.45}, {3, "ccc", 20, 13.45},
{4, "ddd", 50, 78.6}, {5, "eee", 30, 34.21}};
FILE *fp;
int k;
if ((fp = fopen("data", "wb")) == NULL)
{
printf("cannot open the file.\n");
exit(0);
}
for (k = 0; k < N; k++)
fwrite(&s[k], sizeof(struct record), 1, fp);
fclose(fp);
return (0);
}