引言
C语言的一些基本例题 全部例题来自于C语言王道训练营 链接如下
B站链接. 书籍:《跟“龙哥”学C语言编程》
1.结构体的scanf读取和输出
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
};
int main()
{
struct student s = { 1001,"wbx",'M',20,88.5,"ShanDong" };
struct student sarr[3];
int i;
printf("%d %s %c %d %5.2f %s\n", s.num, s.name, s.sex, s.age, s.score, s.addr);
for (i=0;i<3;i++)
{
scanf("%d%s %c%d%f%s", &sarr[i].num, &sarr[i].name, &sarr[i].sex, &sarr[i].age, &sarr[i].score, &sarr[i].addr);
}
for (i = 0; i < 3; i++)
{
printf("%d %s %c %d %5.2f %s\n",sarr[i].num, sarr[i].name, sarr[i].sex, sarr[i].age, sarr[i].score, sarr[i].addr);
}
system("pause");
return 0;
}
运行结果
1001 wbx M 20 88.50 ShanDong
------------------------------------------------------
输入
1002 lxx F 19 99 Shanxi
1004 WKK M 18 60 BeiJing
1009 KKS F 25 68 XiAn
------------------------------------------------------
1002 lxx F 19 99.00 Shanxi
1004 WKK M 18 60.00 BeiJing
1009 KKS F 25 68.00 XiAn
请按任意键继续. . .
2.结构体指针的使用
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
struct student {
int num;
char name[20];
char sex;
};
int main()
{
struct student s = { 1001,"wangle",'M' };
struct student sarr[3] = { 1001,"lilei",'M',1005,"zhangsan",'M',1007,"lili",'F' };
struct student* p;
int num;
p = &s;
printf("%d %s %c\n", p->num, p->name, p->sex);
p = sarr;
printf("%d %s %c\n", p->num, p->name, p->sex);
printf("%d %s %c\n", (*p).num, (*p).name, (*p).sex);
printf("-------------------------------\n");
num = p->num++;
printf("num=%d,p->name=%d\n", num, p->num);
num = p++->num;
printf("num=%d,p->name=%d\n", num, p->num);
system("pause");
return 0;
}
运行结果
1001 wangle M
1001 lilei M
1001 lilei M
-------------------------------
num=1001,p->name=1002
num=1002,p->name=1005
请按任意键继续. . .
3.typedef的使用
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
typedef struct student {
int num;
char name[20];
char sex;
}stu,*pstu;
typedef int INTEGER;
int main()
{
stu s = { 1001,"wangle",'M' };
pstu p;
INTEGER i = 10;
p = &s;
printf("i=%d p->num=%d\n", i, p->num);
system("pause");
}
运行结果
i=10 p->num=1001
请按任意键继续. . .
4.链表的增删查改
下面展示一些 可运行的代码 。 func.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student {
int num;
float score;
struct student* pNext;
}stu,*pstu;
void list_tail_insert(pstu*, stu**, int);
void list_head_insert(pstu*, pstu*, int);
void list_sort_insert(pstu*, pstu*, int);
void list_delete(pstu*, stu**, int);
void list_modify(pstu, int,float);
void list_print(pstu);
void list_print_score(pstu);
func.c
#include"func.h"
void list_tail_insert(pstu* pphead, stu** pptail, int i)
{
pstu pnew;
pnew = (pstu)malloc(sizeof(stu));
memset(pnew, 0, sizeof(stu));
pnew->num = i;
if (NULL == *pptail)
{
*pphead = pnew;
*pptail = pnew;
}
else {
(*pptail)->pNext = pnew;
*pptail = pnew;
}
}
void list_print(pstu phead)
{
while (phead != NULL)
{
printf("%-3d", phead->num);
phead = phead->pNext;
}
printf("\n");
}
void list_print_score(pstu phead)
{
while (phead != NULL)
{
printf("%-3d %5.2f\n", phead->num,phead->score);
phead = phead->pNext;
}
printf("\n");
}
void list_head_insert(pstu* pphead, pstu* pptail, int i)
{
pstu pnew;
pnew = (pstu)malloc(sizeof(stu));
memset(pnew, 0, sizeof(stu));
pnew->num = i;
if (*pptail == NULL)
{
*pphead = pnew;
*pptail = pnew;
}
else {
pnew->pNext = *pphead;
*pphead = pnew;
}
}
void list_sort_insert(pstu* pphead, pstu* pptail, int i)
{
pstu pnew;
pstu pcur;
pstu ppre;
pnew = (pstu)malloc(sizeof(stu));
memset(pnew, 0, sizeof(stu));
pnew->num = i;
pcur = *pphead;
ppre = *pphead;
if (NULL == pcur)
{
*pphead = pnew;
*pptail = pnew;
}
else if (i < pcur->num)
{
pnew->pNext = pcur;
*pphead = pnew;
}
else
{
while (pcur != NULL)
{
if (pcur->num > i)
{
ppre->pNext = pnew;
pnew->pNext = pcur;
break;
}
ppre = pcur;
pcur = pcur->pNext;
}
if (pcur == NULL)
{
(*pptail)->pNext = pnew;
*pptail = pnew;
}
}
}
void list_delete(pstu* pphead, stu** pptail, int i)
{
pstu pcur, ppre;
pcur = *pphead;
ppre = *pphead;
if (pcur!= NULL)
{
if (pcur->num == i)
{
*pptail = NULL;
free(pcur);
}
else
{
while (pcur != NULL)
{
if (pcur->num == i)
{
ppre->pNext = pcur->pNext;
free(pcur);
break;
}
ppre = pcur;
pcur = pcur->pNext;
if (NULL == ppre->pNext)
{
*pptail = ppre;
}
if (pcur == NULL)
{
printf("no this node\n");
}
}
}
}
else
{
printf("list is NULL\n");
}
}
void list_modify(pstu phead, int i, float f)
{
while (phead != NULL)
{
if (phead->num == i)
{
phead->score = f;
break;
}
phead = phead->pNext;
}
if (NULL == phead)
{
printf("no this node\n");
}
}
main.c
#include"func.h"
int main()
{
pstu p;
pstu phead = NULL, ptail = NULL;
int i;
float f;
while (scanf("%d", &i) != EOF)
{
list_sort_insert(&phead, &ptail, i);
}
list_print(phead);
while (printf("please input delete num:"), scanf("%d", &i) != EOF)
{
list_delete(&phead, &ptail, i);
list_print(phead);
}
while (printf("please input modify num:"), scanf("%d%f", &i,&f) != EOF)
{
list_modify(phead, i, f);
list_print_score(phead);
}
system("pause");
return 0;
}
运行结果
5 55 10 6 99
^Z
^Z
^Z
5 6 10 55 99
please input delete num:55
5 6 10 99
please input delete num:^Z
^Z
^Z
please input modify num:99
99.5
5 0.00
6 0.00
10 0.00
99 99.50
please input modify num:^Z
^Z
^Z
请按任意键继续. . .
总结
第一次发表一篇完整的博客,对于一些遗漏,读者不要太过深究~ 比较适合新手打基础大佬请绕路~ 希望这些可以帮助你更好的理解C语言 马上就考研了 我居然还在纠结一些基础 真是闲的闲的闲的闲*10000! 过几天在更新后几章的内容 欢迎大家评论、收藏、点赞 !!!
|