您的当前位置:首页正文

C语言设计经典英文练习题

2023-11-07 来源:年旅网
C Practice

Print 9*9 multiplication table • 1*1=1

• 1*2=2 2*2=4

• 1*3=3 2*3=6 3*3=9

• 1*4=4,2*4=8,3*4=12 4*4=16 • ……

• 1*9=9,…. 9*9=81

#include int main(void) {

int x,y,z;

for(x=1;x<=9;x++) {

for(y=1;y<=x;y++) {

z=x*y;

printf(\"%d*%d=%d\ }

printf(\"\\n\"); } }

Calculate 1+2!+3!+……20!

#include int main () { int i;

long long sum = 0, jc=1; for (i = 1; i <= 20; ++i){ jc *= i; sum += jc; }

printf (\"%d\\n\ sum); return 0; }

Input x and print y Y=2X,X<0;Y=4X-2,0<=X<10;6X(X+3),X>=10

#include int main() {

float x,y;

printf(\"Please input X:\"); scanf(\"%f\ if(x<0) y=2*x;

else if(x<10) y=4*x-2; else

y=6*x*(x+3);

printf(\"Y=%f\\n\ }

Input a string from keyboard, write a program to calculate the number of alpha, digit space and other character.

#include int main() {

char str[100]; int i=0;

int num=0,ch=0,blank=0,other=0; gets(str);

while(str[i]!='\\0') {

if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) ch++;

else if(str[i]>='0' && str[i]<='9') num++;

else if(str[i]==' ') blank++; else

other++;

i++; }

printf(\"数字%d个,字母%d个,空格%d个,其他%d个\\n\ return 0; }

Read 5 integer from keyboard to sort in descending order

#include int main() {

int a[5]={0}; int i,j,t;

printf(\"请依次输入5个整数\\n\");

for(i=0;i<5;i++) //输入5个数 scanf(\"%d\

for(i=0;i<5;i++) //从大到小排序 for(j=i+1;j<5;j++) if(a[i]t=a[i]; a[i]=a[j]; a[j]=t; }

for(i=0;i<5;i++) //输出5个数 printf(\"%d \ }

Create Fibonacci numbers, F1=1,F2=1,Fn=Fn-1+Fn-2(n>2), write a function to generate and print first n Fibonacci numbers.

#include #include

int main() {

int a=1,b=1,sum; int n,i;

scanf(\"%d\

printf(\"%d\\n%d\\n\ for (i=3;i<=n;) {

sum=a+b; b=a; a=sum;

if(i%1==0)printf(\"%d\\n\ i++; }

return 0; }

Write a program to produce the alphabet set A to Z and then saved it into a file.

#include #include int main() { FILE *fp; char ch, filename[10]; printf(\"输入文件名:\\n\"); scanf(\"%s\ if ((fp = fopen(filename, \"w\")) == NULL) { printf(\"error\\n\"); exit(0); } for (ch = 'A'; ch >= 'A'&&ch <= 'Z'; ch++) { fputc(ch, fp); putchar(ch); } fclose(fp); putchar(10); return 0; }

Read a series of character from keyboard and write it to file, read file again to display it on screen.

#include #include int main() { FILE *fp; char ch, filename[10]; printf(\"输入文件名:\\n\"); scanf(\"%s\ if ((fp = fopen(filename, \"w\")) == NULL) { printf(\"error\\n\"); exit(0); } ch = getchar();

printf(\"输入一行字符回车结束:\\n\"); while((ch=getchar())!='\\n') { fputc(ch, fp); putchar(ch); } fclose(fp); putchar(10); printf(\"输入文件名:\\n\"); scanf(\"%s\ if ((fp = fopen(filename, \"r\")) == NULL) { printf(\"error\\n\"); exit(0); } char a; if ((fp = fopen(filename, \"r\")) == NULL) { printf(\"error\\n\"); exit(0); } while (!feof(fp)) { a = fgetc(fp); putchar(a); } fclose(fp); return 0; }

Input one string from keyboard and use pointer to calculate to length of string

#include #include int main() { char a[20]; char *p=a; scanf(\"%s\ int i=0; while (*p != '\\0') {

p++; i++; } printf(\"%d\\n\ return 0; }

Give one strings and use pointer to copy this string to another.

#include int main() { char a[20], b[20]; char *from=a, *to=b; scanf(\"%s\ for(;*from != '\\0';) { *to = *from; from++, to++; } *to = '\\0'; printf(\"%s\ return 0; }

Write 1 to 20 to a file, read it and put the sum of these number at the end of file.

#include #include int main() { FILE *f; int b[20], j, sum = 0; for (j = 0; j < 20; j++) { b[j] = j + 1; sum = sum + b[j]; } char a[10]; printf(\"输入文件名:\\n\"); scanf(\"%s\ if ((f = fopen(a, \"w\")) == NULL)

{ printf(\"error\"); exit(0); } for (j = 0; j < 20; j++) { fprintf(f, \"%d \ } fprintf(f, \"%d\ fclose(f); return 0; }

Use malloc() function to allocate 10 int units to 10 numbers and print it from 6th number using pointer.

#include #include #define len sizeof(int) int main() { int *p[10]; int a[10], i; for (i = 0; i < 10; i++) scanf(\"%d\ for (i = 0; i < 10; i++) { p[i] = (int *)malloc(len); *p[i] = a[i]; } for (i = 5; i < 10; i++) printf(\"%d \ return 0; }

Write a function with two integer number arguments and two pointer arguments to calculate two number sum and difference, return the sum and difference with two pointers to calling function.

#include int main()

{ int f(int *p1, int *p2); int g(int *p1, int *p2); int a, b; int *p1, *p2; p1 = &a, p2 = &b; scanf(\"%d %d\ printf(\"%d\\n%d\ return 0; }

int f(int *p1, int *p2) { int sum; sum = *p1 + *p2; return sum; }

int g(int *p1, int *p2) { int dif; dif = *p1 - *p2; return dif; }

Using pointer to read an array of integer and print its elements in reverse order

#include int main() {

int n, c, d, a[100], b[100];

printf(\"Enter the number of elements in array\\n\"); scanf(\"%d\

printf(\"Enter the array elements\\n\"); for (c = 0; c < n ; c++) scanf(\"%d\

for (c = n - 1, d = 0; c >= 0; c--, d++) b[d] = a[c];

for (c = 0; c < n; c++) a[c] = b[c];

printf(\"Reverse array is\\n\"); for (c = 0; c < n; c++) printf(\"%d\\n\ return 0; }

运算符与表达式: 1.constant 常量 2. variable 变量 3. identify 标识符 4. keywords 关键字 5. sign 符号

6. operator 运算符 7. statement语句 8. syntax 语法

9. expression 表达式 10. initialition 初始化

11. number format 数据格式 12 declaration 说明

13. type conversion 类型转换 14.define 、definition 定义 条件语句: 1.select 选择

2. expression 表达式

3. logical expression 逻辑表达式 4. Relational expression 关系表达式 5.priority优先 6. operation运算 7.structure 结构 循环语句: 1.circle 循环 2. condition 条件 3. variant 变量 4. process过程 5.priority优先 6. operation运算 数组:

1. array 数组 2. reference 引用 3. element 元素 4. address 地址 5. sort 排序

6. character 字符 7. string 字符串 8. application 应用 函数: 1.call 调用

2.return value 返回值 3.function 函数 4. declare 声明

5. `parameter 参数 6.static 静态的 7.extern 外部的 指针:

1. pointer 指针 2. argument 参数 3. array 数组

4. declaration 声明 5. represent 表示 6. manipulate 处理

结构体、共用体、链表: 1 structure 结构 2 member成员 3 tag 标记

4 function 函数 5 enumerate 枚举

6 union 联合(共用体) 7 create 创建 8 insert 插入 9 delete 删除 10 modify 修改 文件:

1、file 文件 2、open 打开 3、close 关闭 4、read 读 5、write 写 6、error 错误

序号 主要章节 常用英汉对照词汇 备注 1 运算符与表达式

( operator and expression ) 汉语 英语 常量 constant 变量 variable 标识符 identify 关键字 keywords 符号 sign

运算符 operator 语句 statement 语法 syntax

表达式 Expression 初始化 Initialization

数据格式 number format 说明 Declaration

类型转换 type conversion 定义 Define 、 definition 2 条件语句 ( condition

statement) 选择 select 表达式 expression

逻辑表达式 logical expression 关系表达式 Relational expression 优先 priority 运算 operation 结构 structure 3 循环语句

(circle statement) 循环 circle 条件 condition 变量 variant 过程 process 优先 priority 运算 operation 4 函数

(function) 调用 call 返回值 return value 函数 function 声明 declare 参数 parameter 静态的 static 外部的 extern 5 数组和指针 (array and

pointer) 数组 array 引用 reference 元素 element 地址 address 排序 sort

字符 character 字符串 string 应用 application 指针 pointer 参数 argument 数组 array

声明 declaration 表示 represent 处理 manipulate 6 结构体、 共用体

(structures 、 union ) 结构 structure 成员 member 标记 tag

函数 function 枚举 enumerate

联合 ( 共用体 ) union 创建 create 插入 insert 删除 delete 修改 modify 7 文件

( file) 文件 file 打开 open 关闭 close 读 read 写 write 错误 error

因篇幅问题不能全部显示,请点此查看更多更全内容