試講課
參考資料:《C程式設計(第五版)》譚浩強
目錄
- 課程時長:2.5小時
- 第一章 程式設計和
語言C
- 第二章 算法—程式的靈魂
- 第一章 程式設計和
- 課程時長:6小時
- 第三章 最簡單的
程式設計—順序程式設計C
- 第四章 選擇結構程式設計
- 第五章 循環結構程式設計
- 第三章 最簡單的
- 課程時長:6小時
- 第六章 利用數組批量處理資料
- 第七章 用函數實作子產品化程式設計
- 第八章 善于利用指針
- 課程時長:6小時
- 第九章 使用者自己建立資料類型
- 第十章 對檔案的輸入輸出
- 課程時長:2小時
- 試卷解析
案例展示
- 求階乘
#include<stdio.h>
int main(){
int num,temp = 1;
printf("請輸入num數值:");
scanf("%d",&num);
if(num>=0){
for(int i = 1;i <= num;i++){
temp *= i;
}
printf("%d的階乘值為:%d\n",num,temp);
}else{
printf("請重新輸入num數值!");
}
return 0;
}
效果展示:
- 判斷閏年
#include <stdio.h>
int main(){
printf("2000年~2500年中的閏年年份包括:\n");
for(int a = 2000;a <= 2500;a++){
if((a%4==0&&a%100!=0)||(a%400==0)){
printf("%d\n",a);
}
}
return 0;
}
效果實戰:
- 連結清單實戰
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std;
struct student{
int num;//學号
char name[20];//姓名
int age;//年齡
struct student *next;//指向下一個節點的指針
};
struct student *createList();//建立學生資訊
void displayList(struct student *head);//按照建立順序輸出學生資訊
struct student *reverseList(struct student *head);//與建立順序相反,逆序順序輸出學生資訊
struct student *deleteNodes(struct student *head);//删除某個學生資訊
struct student *insertNodes(struct student *head);//插入某個學生的資訊
void search_student_info(struct student *head);//查找對應學生資訊
void modify_student_info(struct student *head);//修改學生資訊
struct student *destroyList(struct student *head);//清空整個連結清單
struct student *head = NULL;
int main(){
int select;//根據對應不同select的值,調用不同的函數
do{
printf("請輸入select的值:");
scanf("%d",&select);
switch(select){
case 1:
head = createList();
break;
case 2:
displayList(head);
break;
case 3:
head = deleteNodes(head);
break;
case 4:
head = insertNodes(head);
break;
case 5:
search_student_info(head);
break;
case 6:
modify_student_info(head);
break;
case 7:
head = reverseList(head);
// displayList(head);
break;
case 8:
head = destroyList(head);
break;
}
}while(select!=0);
return 0;
}
//1. 建立學生資訊
struct student *createList(){
struct student *head;//頭節點
struct student *p1;//開辟新節點
struct student *p2;//與p1連接配接
int num1;
char name1[20];
int age1;
head = NULL;
int count = 1;
printf("請輸入第1個學生的學号、姓名、年齡(用空格分隔):");
scanf("%d%s%d",&num1,&name1,&age1);
while(age1>0){
p1 = (struct student*)malloc(sizeof(struct student));
p1->num = num1;
strcpy(p1->name,name1);
p1->age = age1;
p1->next = NULL;
if(head == NULL){
head = p1;
}else{
p2->next = p1;
}
p2 = p1;
printf("請輸入第%d個學生的學号、姓名、年齡(用空格分隔):",++count);
scanf("%d%s%d",&num1,&name1,&age1);
}
return head;
}
//2. 與建立順序相同輸出學生資訊
void displayList(struct student *head){
struct student *p;
int n = 0;
if(head!=NULL){
printf("順序輸對外連結表中學生資訊如下:\n");
for(p=head;p!=NULL;p=p->next){
printf("學号:%-6d 姓名:%-20s 年齡:%-6.1d\n",p->num,p->name,p->age);
n++;
}
printf("學生總數:%d\n",n);
}else{
printf("空連結清單!\n");
}
}
//3. 根據學号删除對應學生資訊
struct student *deleteNodes(struct student *head){
struct student *p1;
struct student *p2;
int num2;//要删除學生的學号
printf("請輸入要删除學生的學号:");
scanf("%d",&num2);
if(head == NULL){
printf("連結清單為空\n");
return head;
}
p2 = head;
while(num2!=p2->num&&p2->next!=NULL){ //查找要删除的節點
p1 = p2;
p2 = p2->next;
}
if(num2 == p2->num){
if(p2 == head){ //要删除的是頭節點
head = p2->next;
}else{ //其他節點
p1->next = p2->next;
}
free(p2);
printf("删除了學号為%d的學生資訊!\n",num2);
}else{
printf("該生不存在!\n");
}
return head;
}
//4. 根據學号的大小插入某個學生的資訊
struct student *insertNodes(struct student *head){
struct student *p;//待插入節點
struct student *p1;//待插入節點的前驅節點
struct student *p2;//待插入節點的後繼節點
p2 = head;
p = (struct student *)malloc(sizeof(struct student));
printf("請輸入要加入學生的學号、姓名、年齡:");
scanf("%d%s%d",&p->num,&p->name,&p->age);
if(head == NULL){ //若為空連結清單,則相當于建立一個新節點
head = p;
p->next = NULL;
}else{
while(p->num > p2->num&&p2->next!=NULL){ //查找待插入的位置
p1 = p2;
p2 = p2->next;
}
if(p->num < p2->num){ //頭節點和中間任意節點的插入
if(p == head){ //頭節點
head = p;
p->next = p1;
}else{ // 中間任意節點
p1->next = p;
p->next = p2;
}
}else{//尾節點的插入
p2->next = p;
p->next = NULL;
}
}
return head;
}
//5. 根據學号查找對應學生的其他資訊
void search_student_info(struct student *head){
struct student *p;
int num;//要查找對應學生的學号資訊
printf("請輸入要查找學生的學号:");
scanf("%d",&num);
p = head;
//非空連結清單的情況下
if(head != NULL){
while(p->num!=num&&p->next!=NULL){
p=p->next;
}
//不滿足while循環的第一個條件
if(p->num==num){
printf("你所查找的學号為%d的學生資訊如下:\n",num);
printf("學号:%-6d 姓名:%-20s 年齡:%-6.1d\n",p->num,p->name,p->age);
}else{
//不滿足while循環的最後一個條件
printf("沒有找到學号為%d的學生資訊,請确認學号是否正确!\n",num);
}
}else{//空連結清單的情況
printf("空連結清單!");
}
}
//6. 根據學号修改學生資訊
void modify_student_info(struct student *head){
struct student *p;
int num;
char name[20];
int age;
printf("請輸入您要修改學生的學号:");
scanf("%d",&num);
p = head;
if(head!=NULL){
while(p->num!=num&&p->next!=NULL){
p = p->next;
}
if(p->num==num){
printf("請輸入您要更改後的資訊:\n");
scanf("%d%s%d",&num,&name,&age);
p->num = num;
strcpy(p->name,name);
p->age = age;
}else{
printf("沒有找到學号為%d的學生資訊,請确認學号資訊是否正确!\n");
}
}else{
printf("空連結清單\n");
}
}
//7. 與建立順序相反輸出學生資訊
struct student *reverseList(struct student *head){
/*
1. 借助遞歸
*/
/*
struct student *p;
p = head;
if(p!=NULL){
reverseList(p->next);
printf("學号:%-6d 姓名:%-20s 年齡:%-6.1d\n",p->num,p->name,p->age);
}
*/
/*
2. 借助棧
*/
struct student *p;
p = head;
stack<int> s;
while(p!=NULL){
s.push(p->num);
p = p->next;
}
while(!s.empty()){
p = head;
while(s.top()!=p->num){
p = p->next;
}
if(s.top()==p->num){
printf("學号:%-6d 姓名:%-20s 年齡:%-6.1d\n",p->num,p->name,p->age);
}
s.pop();
}
return head;
/*
3. 改變單連結清單指針指向
改變指針指向後,原連結清單也會被修改
*/
/*
struct student *pre;
struct student *post;
struct student *p;
pre = NULL;
post = NULL;
while(head!=NULL){
post = head->next;
head->next = pre;
pre = head;
head = post;
}
return pre;
*/
}
//8. 清空整個連結清單
struct student *destroyList(struct student *head){
struct student *p;
p = head;
if(p==NULL){
printf("空連結清單!\n");
}
while(p!=NULL&&p->next!=NULL){
p = p->next;
free(p);
}
printf("資訊删除完畢!\n");
head = NULL;
return head;
}
效果展示:
文檔資料連結:
- Github
- Gitee