[C] [langue] Des exemples de vomissements C finition que vous voulez - Aller à une autre pièce montée
annuaire
Exemple 45 variable structure « nombre de jours »
Exemple 46 tableau de structures « Entrez le rendement des élèves et des spectacles »
Des exemples de pointeur structure variable 47
Exemple 48 structure de réseau de pointeur
Exemple 49 variables syndicales
Des exemples lecteur de caractères 51
Exemples 52 à lire et écrire la chaîne
Exemple 53 fonction de sortie formaté
Des exemples de fonction de mise en forme d'entrée 54
Exemples 55 pour ouvrir et fermer des fichiers
Exemple 45 variable structure « nombre de jours »
varstruct.c
/* 计算天数 */
# include <stdio.h>
struct
{
int year;
int month;
int day;
} data; /* 定义一个结构并声明对象为data */
void main()
{
int days;
printf("请输入日期(年、月、日):");
scanf("%d, %d, %d", &data.year, &data.month, &data.day);
switch(data.month)
{
case 1: days = data.day;
break;
case 2: days = data.day+31;
break;
case 3: days = data.day+59;
break;
case 4: days = data.day+90;
break;
case 5: days = data.day+120;
break;
case 6: days = data.day+151;
break;
case 7: days = data.day+181;
break;
case 8: days = data.day+212;
break;
case 9: days = data.day+243;
break;
case 10: days = data.day+273;
break;
case 11: days = data.day+304;
break;
case 12: days = data.day+334;
break;
}
if(data.year%4==0&&data.year%100!=0 || data.year%400==0)
if(data.month>=3)
days = days + 1;
printf("%d月%d日是%d年的第%d天.\n", data.month, data.day, data.year, days);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam045$ ./a.out
请输入日期(年、月、日):2020,2,26
2月26日是2020年的第57天.
Exemple 46 tableau de structures « Entrez le rendement des élèves et des spectacles »
arrstruct.c
/* 输入学生成绩并显示 */
# include <stdio.h>
struct student
{
char number[6];
char name[6];
int score[3];
} stu[2];
void output(struct student stu[2]);
void main()
{
int i, j;
for(i=0; i<2; i++)
{
printf("请输入学生%d的成绩:\n", i+1);
printf("学号:");
scanf("%s", stu[i].number);
printf("姓名:");
scanf("%s", stu[i].name);
for(j=0; j<3; j++)
{
printf("成绩 %d. ", j+1);
scanf("%d", &stu[i].score[j]);
}
printf("\n");
}
output(stu);
}
void output(struct student stu[2])
{
int i, j;
printf("学号 姓名 成绩1 成绩2 成绩3\n");
for(i=0; i<2; i++)
{
printf("%-6s%-6s", stu[i].number, stu[i].name);
for(j=0; j<3; j++)
printf("%-8d", stu[i].score[j]);
printf("\n");
}
}
course
guo@ubuntu:~/test/Clanguage/2/Exam046$ ./a.out
请输入学生1的成绩:
学号:77
姓名:zhangsan
成绩 1. 88
成绩 2. 99
成绩 3. 100
请输入学生2的成绩:
学号:78
姓名:lisi
成绩 1. 67
成绩 2. 89
成绩 3. 99
学号 姓名 成绩1 成绩2 成绩3
77 zhangsX88 99 100
78 lisi 67 89 99
Des exemples de pointeur structure variable 47
strpoint.c
# include <stdio.h>
# include <string.h>
void main()
{
struct student
{
long num;
char name[30];
char sex[10];
float score;
};
struct student stu;
struct student *p;
p = &stu;
stu.num = 97032;
strcpy(stu.name, "小明");
strcpy(stu.sex, "男");
stu.score = 98.5;
printf("学号: %ld\n姓名: %s\n性别: %s\n分数: %4.2f\n",
stu.num, stu.name, stu.sex, stu.score);
printf("\n");
printf("学号: %ld\n姓名: %s\n性别: %s\n分数: %4.2f\n",
(*p).num, (*p).name, (*p).sex, (*p).score);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam047$ ./a.out
学号: 97032
姓名: 小明
性别: 男
分数: 98.50
学号: 97032
姓名: 小明
性别: 男
分数: 98.50
Exemple 48 structure de réseau de pointeur
strparray.c
# include <stdio.h>
/* 定义一个全局的结构体 */
struct student
{
long num;
char name[20];
char sex;
int age;
};
/* 声明结构体数组并赋初值 */
struct student stu[4] = {{97032, "xiao ming", 'M', 20},
{97033, "xiao wang", 'M', 20},
{97034, "xiao tong", 'M', 21},
{97035, "xiao shui", 'F', 18}};
void main()
{
/* 定义一个结构体指针变量 */
struct student *p;
printf(" 学号 姓名 性别 年龄\n");
for(p=stu; p<stu+4; p++)
printf("%-8ld%-12s%-10c%-3d\n", p->num, p->name,
p->sex, p->age);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam048$ ./a.out
学号 姓名 性别 年龄
97032 xiao ming M 20
97033 xiao wang M 20
97034 xiao tong M 21
97035 xiao shui F 18
Exemple 49 variables syndicales
union.c
# include <stdio.h>
union data
{
int a;
float b;
double c;
char d;
} exa;
void main()
{
exa.a = 6;
printf("%d\n", exa.a);
exa.c = 67.2;
printf("%5.1f\n", exa.c);
exa.d = 'W';
exa.b = 34.2;
printf("%5.1f, %c\n", exa.b, exa.d);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam049$ ./a.out
6
67.2
34.2, �
Exemple 50 type énuméré
enum.c
# include <stdio.h>
void main()
{
/* 经过下面的定义后,默认有:blue=0 red=1 ... black=4 */
enum color {blue, red, yellow, purple, black};
enum color i, j, k, pri;
int n, loop;
n = 0;
for(i=blue; i<=black; i++) /* i代表第一次所取铅笔的颜色 */
for(j=blue; j<=black; j++) /* j代表第二次所取铅笔的颜色 */
if(i!=j) /* 第一次和第二次所取铅笔颜色不同 */
{
for(k=blue; k<=black; k++) /* k代表第三次所取铅笔的颜色 */
if((k!=i)&&(k!=j)) /* 三次所取铅笔颜色各不相同 */
{
n++; /* 能得到三种不同颜色铅笔的可能取法加1 */
printf("%-6d", n);
/* 将当前i、j、k所对应的颜色依次输出 */
for(loop=1; loop<=3; loop++)
{
switch(loop)
{
case 1: pri = i;
break;
case 2: pri = j;
break;
case 3: pri = k;
break;
default:
break;
}
switch(pri)
{
case blue: printf("%-10s", "blue");
break;
case red: printf("%-10s", "red");
break;
case yellow: printf("%-10s", "yellow");
break;
case purple: printf("%-10s", "purple");
break;
case black: printf("%-10s", "black");
break;
default:
break;
}
}
printf("\n");
}
}
printf("total: %5d\n", n);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam050$ ./a.out
1 blue red yellow
2 blue red purple
3 blue red black
4 blue yellow red
5 blue yellow purple
6 blue yellow black
7 blue purple red
8 blue purple yellow
9 blue purple black
10 blue black red
11 blue black yellow
12 blue black purple
13 red blue yellow
14 red blue purple
15 red blue black
16 red yellow blue
17 red yellow purple
18 red yellow black
19 red purple blue
20 red purple yellow
21 red purple black
22 red black blue
23 red black yellow
24 red black purple
25 yellow blue red
26 yellow blue purple
27 yellow blue black
28 yellow red blue
29 yellow red purple
30 yellow red black
31 yellow purple blue
32 yellow purple red
33 yellow purple black
34 yellow black blue
35 yellow black red
36 yellow black purple
37 purple blue red
38 purple blue yellow
39 purple blue black
40 purple red blue
41 purple red yellow
42 purple red black
43 purple yellow blue
44 purple yellow red
45 purple yellow black
46 purple black blue
47 purple black red
48 purple black yellow
49 black blue red
50 black blue yellow
51 black blue purple
52 black red blue
53 black red yellow
54 black red purple
55 black yellow blue
56 black yellow red
57 black yellow purple
58 black purple blue
59 black purple red
60 black purple yellow
total: 60
Des exemples lecteur de caractères 51
chario.c
# include <stdio.h>
# include <ctype.h>
void main()
{
char ch;
printf("Please enter some text(input a point to quit).\n");
do{
ch = getchar();
if(islower(ch))
ch = toupper(ch);
else
ch = tolower(ch);
putchar(ch);
} while(ch != '.');
}
course
guo@ubuntu:~/test/Clanguage/2/Exam051$ ./a.out
Please enter some text(input a point to quit).
dgdsij
DGDSIJ
a
A
u
U
q
Q
.
Exemples 52 à lire et écrire la chaîne
stringio.c
/* A simple dictionary */
# include <stdio.h>
# include <string.h>
# include <ctype.h>
char *dic[][40] = {
"luster", "A bright shine on the surface.",
"disgrase", "Loss of honor and respect.",
"glamour", "Strong attraction.",
"tomb", "The place where a dead person is buried.",
"garbage", "Unwanted or spoiled food.",
"bliss", "Great happiness or joy.",
"commend", "Speak favorably of.",
" ", " " /* null end the list */
};
void main()
{
char word[80], ch;
char **point; /* 定义一个二维指针 */
do{
puts("Please enter a word: ");
scanf("%s", word);
/* 将二维数组首地址赋给二维指针p */
point = (char **)dic;
/* 察看字典中是否存在输入的单词 */
do{
if(!strcmp(*point, word))
{
puts("The meaning of the word is: ");
puts(*(point+1));
break;
}
if(!strcmp(*point, word))
break;
point = point + 2;
} while(*point);
if(!*point)
puts("The word is not in dictionary.");
printf("Another? (y/n):");
scanf("%c%*c", &ch); /* %*c表示scanf()读入该区域,但不向任何变量赋值 */
} while(toupper(ch)!='N');
}
course
Please enter a word:
hello
The word is not in dictionary.
Another? (y/n):y
Please enter a word:
luster
The meaning of the word is:
A bright shine on the surface.
Another? (y/n):n
Please enter a word:
commend
The meaning of the word is:
Speak favorably of.
Another? (y/n):
Exemple 53 fonction de sortie formaté
formato.c
# include <stdio.h>
void main()
{
unsigned number;
double item = 1.23456;
for(number=8; number<16; number++)
{
printf("%o ", number); /* 以八进制格式输出number */
printf("%x ", number); /* 以十进制格式(小写)输出number */
printf("%X\n", number); /* 以十进制格式(大写)输出number */
}
printf("\n");
printf("%p\n\n", &item); /* 显示变量item的地址 */
printf("%f\n", item);
printf("%8.2f\n", item); /* 总域宽为8,小数部分占2 */
printf("%-8.2f\n", item); /* 域中左对齐输出(默认右对齐) */
}
course
guo@ubuntu:~/test/Clanguage/2/Exam053$ ./a.out
10 8 8
11 9 9
12 a A
13 b B
14 c C
15 d D
16 e E
17 f F
0x7ffdb1185dc0
1.234560
1.23
1.23
Des exemples de fonction de mise en forme d'entrée 54
formati.c
# include <stdio.h>
void main()
{
int i, j, k;
char str[80];
char *p;
/* 输入的数将分别以十进制、八进制和十六进制读入程序 */
scanf("%d %o %x", &i, &j, &k);
printf("%d %d %d\n", i, j, k); /* 察看我们实际输入的数据 */
printf("Enter a string: ");
scanf("%s", str);
printf("Here is your string: %s\n", str);
printf("Enter an address: ");
scanf("%p", &p);
printf("Value at location %p is %c.\n", p, *p);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam054$ ./a.out
this
0 0 0
Enter a string: Here is your string: this
Enter an address: 0xfd100001
Segmentation fault (core dumped)
Exemples 55 pour ouvrir et fermer des fichiers
fileio.c
# include <stdio.h>
# include <stdlib.h>
void main()
{
/* 定义一个文件指针fp */
FILE *fp;
char ch, filename[10];
printf("Please input the name of file: ");
scanf("%s", filename); /* 输入字符串并赋给变量filename */
/* 以读的使用方式打开文件filename */
if((fp=fopen(filename, "r")) == NULL)
{
printf("Cannot open the file.\n");
exit(0); /* 正常跳出程序 */
}
/* 关闭文件 */
fclose(fp);
}
course
guo@ubuntu:~/test/Clanguage/2/Exam055$ ls
2.c a.out fileio.c
guo@ubuntu:~/test/Clanguage/2/Exam055$ ./a.out
Please input the name of file: fileio.c
guo@ubuntu:~/test/Clanguage/2/Exam055$ ./a.out
Please input the name of file: 1.c
Cannot open the file.