




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
大一下c語言實驗源代碼 021112122-白娟娟實驗一 1.輸入三個整數(shù),使用指針按由小到大的順序排序并輸出。編程思路:先定義三個整型變量和三個指向整型數(shù)據(jù)的指針變量pi(i=1,2,3),然后用這三個指針分別指向三個整型變量,輸入三個整型變量的值,采用*pi去比較大小和輸出。 #include stdafx.h int _tmain(int argc, _TCHAR* argv)int a,b,c,*p1,*p2,*p3,max,min; scanf(%d,%d,%d,&a,&b,&c); p1=&a;p2=&b;p3=&c; if(*p1*p2) min=*p2; *p2=*p1; *p1=min; if(*p1*p3)if(*p2*p2)temp=*p2;*p2=*p1;*p1=temp;實驗二1.輸入十個整數(shù),放在數(shù)組list中,然后用指針法從后向前輸出該數(shù)組中的整數(shù)。編程思路:定義int *p,list10;令p指向數(shù)組的最后一個元素:p=list+9; 或者p=&list0+9; 采用循環(huán)10次,每次輸出*p,然后p-,即前移一個元素。#include main() int list10,*p,i; for(i=0;i=0;p-) printf(%3d,*p);2查找整數(shù)。定義一個函數(shù) search(int *p,int n,int x),在指針p所指向的整型數(shù)組中查找元素x,若找到則返回相應(yīng)下標,否則返回-1。在main()函數(shù)中調(diào)用search()。要求實參用指向整型數(shù)組的指針變量,數(shù)組長度是10,數(shù)據(jù)從鍵盤上輸入。#include stdafx.hint _tmain(int argc, _TCHAR* argv) int search(int *p,int n,int x); int a10,i,x,n,m; scanf(%dn,&x); for(i=0;i10;i+) scanf(%d,&ai); n=10; m=search(a,10,x); printf(%10d,m);int search(int *p,int n,int x)int i;for(i=0;is2,則函數(shù)返回1;若s1*p2返回1, *p1*p2) return 1; else return -1; return 0;實驗四1.定義一個結(jié)構(gòu)體類型表示每個學(xué)生的數(shù)據(jù),包括學(xué)號、姓名、3門課的成績,定義并輸入1個學(xué)生的數(shù)據(jù)到結(jié)構(gòu)體變量中,然后輸出該學(xué)生的姓名、成績和平均成績(保留2位小數(shù))。編程指導(dǎo):定義結(jié)構(gòu)體含No、name、score1、score2、score3、average共6個成員,其中No、name為字符數(shù)組,score1、score2、score3為整型變量,average為浮點型變量。#include stdafx.hint _tmain(int argc, _TCHAR* argv) struct student int num; char name20; int score1; int score2; int score3; float average; boy; scanf(%d%s%d%d%d,&boy.num,,&boy.score1,&boy.score2,&boy.score3); boy.average=(boy.score1+boy.score2+boy.score3)/3.0; printf(平均成績?yōu)?3.2f,boy.average); return 0;2.定義一個結(jié)構(gòu)體類型,包括年月日三個成員變量,輸入一個日期,計算是這一年的第幾天。編程指導(dǎo):定義一個整型數(shù)組存儲各個月份的天數(shù),注意閏年問題。#include main() struct date int year; int month; int day; d; int a12=31,29,31,30,31,30,31,31,30,31,30,31,b=0,c=0,i;if (d.year%4=0&d.year%100!=0|d.year%400=0) b=29;else b=28;a1=b;scanf(%d%d%d,&d.year,&d.month,&d.day);if(d.year%4=0&d.year%100!=0|d.year%400=0) for(i=0;i=d.month-2;i+) c=c+ai;else for(i=0;i=d.month-2;i+) c=c+ai; c=c+d.day; printf(第%d天,c); return 0;3. 第2題用結(jié)構(gòu)體指針變量來實現(xiàn)。#include main() struct date int year; int month; int day; *d,b; d=&b;int a12=31,29,31,30,31,30,31,31,30,31,30,31,e=0,c=0,i;if (*d).year%4=0&(*d).year%100!=0|(*d).year%400=0) e=29;else e=28;a1=e;scanf(%d%d%d,&d-year,&d-month,&d-day);if (*d).year%4=0&(*d).year%100!=0|(*d).year%400=0) for(i=0;i=(*d).month-2;i+) c=c+ai;else for(i=0;i=(*d).month-2;i+) c=c+ai; c=c+(*d).day; printf(第%d天,c); return 0;實驗五1有5個學(xué)生,每個學(xué)生的數(shù)據(jù)包括學(xué)號、姓名、3門課的成績,用賦初值的方法輸入5個學(xué)生的數(shù)據(jù)到結(jié)構(gòu)體數(shù)組中,輸出每個學(xué)生3門課的平均成績(保留2位小數(shù))。輸出格式為:No name score1 score2 score3 average 101 Zhou 93 89 87 -102 Yang 85 80 78 -103 Chen 77 70 83 -104 Qian 70 67 60 -105 Li 72 70 69 -編程思路:定義結(jié)構(gòu)體含No.、name、score1、score2、score3、average共6個成員,其中No、name為字符數(shù)組,score1、score2、score3為整型變量,average為浮點型變量。然后再定義5個元素的結(jié)構(gòu)體數(shù)組,并賦初值。用一重循環(huán)計算average并輸出結(jié)果。#include main() struct student char num3; char name20; int score1; int score2; int score3; float average; boy5; int i; for(i=0;i5;i+)scanf(%s%s%3d%3d%3d,boyi.num,,&boyi.score1,&boyi.score2,&boyi.score3); for(i=0;i5;i+) boyi.average=(boyi.score1+boyi.score2+boyi.score3)/3.0; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%3s%4s%7d%8d%6d%6.2fn,boyi.num,,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;2,在上題中,按平均成績由高到低排序后,輸出每個學(xué)生的成績,輸出格式與上題相同。注意:在排序中交換average成員的數(shù)據(jù)時,其他成員的數(shù)據(jù)也要作對應(yīng)的交換。 方法一:#include main() struct student char num4; char name20; int score1; int score2; int score3; float average; boy5; int i; int k,j; struct student temp; for(i=0;i5;i+) scanf(%s%s%d%d%d,boyi.num,,&boyi.score1,&boyi.score2,&boyi.score3);for(i=0;i5;i+)boyi.average=(float)(boyi.score1+boyi.score2+boyi.score3)/3.0;for(i=0;i5;i+) k=i;for(j=i+1;j=5;j+) if(boyk.averageboyj.average) k=j; temp=boyk; boyk=boyi; boyi=temp; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%s%s%d%d%d%6.2fn,boyi.num,,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;方法二:#include stdafx.hint _tmain(int argc, _TCHAR* argv) struct student char num4; char name20; int score1; int score2; int score3; float average; boy5; int i; int j; struct student temp;for(i=0;i5;i+)scanf(%s%s%d%d%d,boyi.num,,&boyi.score1,&boyi.score2,&boyi.score3);for(i=0;i5;i+)boyi.average=(float)(boyi.score1+boyi.score2+boyi.score3)/3.0;for(i=0;i5;i+) for(j=i+1;j=5;j+) if(boyi.averageboyj.average) temp=boyj; boyj=boyi; boyi=temp; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%s%s%d%d%d%6.2fn,boyi.num,,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;實驗六1定義一個結(jié)構(gòu)體類型表示每個學(xué)生的數(shù)據(jù),包括學(xué)號、姓名、3門課的成績,定義并輸入1個學(xué)生的數(shù)據(jù)到結(jié)構(gòu)體指針變量中,然后輸出該學(xué)生的姓名、成績和平均成績(保留2位小數(shù))。編程指導(dǎo):定義結(jié)構(gòu)體含No、name、score1、score2、score3、average共6個成員,其中No、name為字符數(shù)組,score1、score2、score3為整型變量,average為浮點型變量,要求定義結(jié)構(gòu)體指針變量來處理數(shù)據(jù)。#include main() struct student char num10; char name20; int score1; int score2; int score3; float average; ; struct student *p; struct student s1; p=&s1; scanf(%s%s%d%d%d,p-num,p-name,&p-score1,&p-score2,&p-score3); p-average=(p-score1+p-score2+p-score3)/3.0;printf(%3s%3s%3d%3d%3d%6.2f,p-num,p-name,p-score1,p-score2,p-score3,p-average);return 0;2用結(jié)構(gòu)體類型指針求兩個復(fù)數(shù)之積。編程指導(dǎo):定義一個結(jié)構(gòu)體類型struct fushu float x; float y,包括兩個浮點數(shù)成員。如(a1+b1i)*(a2+b2i)=(a1*b1-a2*b2)+(a1*b2+a2*b1)i。#include main()struct fushu float x; float y; ; struct fushu *p1,*p2; struct fushu s1,s2; float a,b; p1=&s1;p2=&s2; printf(分別輸入兩個復(fù)數(shù)的實部和虛部n); scanf(%f%f%f%f,&p1-x,&p1-y,&p2-x,&p2-y); a=(p1-x*p1-y)-(p2-x*p2-y); b=(p1-x*p2-y)+(p1-y+p2-x); printf(答案為%1.2f+%1.2fi,a,b); return 0;3. 重新實現(xiàn)第1題,其中有關(guān)成績的成員變量改為數(shù)組int score3。#include main() struct student char num10; char name20; int score3; float average; ; struct student *p; struct student s1; p=&s1; int i; float sum=0; printf(請輸入該生的學(xué)號和姓名n); scanf(%s,p-num); scanf(%s,p-name); printf(請輸入該生的成績n); for(i=0;iscorei); sum=sum+p-scorei; p-average=sum/3.0; printf(%3s%3s,p-num,p-name); for(i=0;iscorei); printf(%6.2f,p-average);return 0;實驗七1.建立一個3個節(jié)點的靜態(tài)鏈表,每個節(jié)點包括學(xué)號、姓名、3門課的成績,從鍵盤輸入學(xué)生的數(shù)據(jù),按照鏈表順序依次進行輸出。編程指導(dǎo):定義結(jié)構(gòu)體類型以及3個變量、3個指針,分別輸入數(shù)據(jù),建立鏈表,采用循環(huán)輸出數(shù)據(jù)。注意如何判斷鏈表結(jié)束。#include stdafx.h#define NULL 0struct student char num10; char name20; int score1; int score2; int score3; struct student *next; ;int _tmain(int argc, _TCHAR* argv) struct student a, b, c, *head, *p; p=head=&a; a.next=&b; b.next=&c; c.next=NULL; for(;p!=NULL;p=p-next) scanf(%s%s%d%d%d,p-num,p-name,&p-score1,&p-score2,&p-score3); p=head=&a; for(;p!=NULL;p=p-next)printf(%3s%3s%3d%3d%3dn,p-num,p-name,p-score1,p-score2,p-score3);2.先建立一個具有4個節(jié)點的動態(tài)鏈表,每個節(jié)點包括學(xué)號、姓名、2門課的成績,從鍵盤輸入學(xué)生的數(shù)據(jù);然后輸入一個學(xué)號,在鏈表中進行查找,找到則輸出該節(jié)點中的所有信息;否則輸出未找到。編程指導(dǎo):注意動態(tài)內(nèi)存分配函數(shù)的使用。#include stdafx.h#define NULL 0struct student char num10; char name20; int score1; int score2; struct student *next; ;int _tmain(int argc, _TCHAR* argv) struct student a, b, c,d, *head, *p; char r10; int e; p=head=&a; a.next=&b; b.next=&c; c.next=&d; d.next=NULL;for(;p!=NULL;p=p-next) scanf(%s%s%d%d,p-num,p-name,&p-score1,&p-score2);printf(請輸入您要查找的學(xué)生學(xué)號n);scanf(%s,r);p=head;for(;p!=NULL;p=p-next)e=strcmp(p-num,r);if(e=0) printf(%3s%3s%3d%3dn,p-num,p-name,p-score1,p-score2); break; if(p=NULL) printf(未找到);實驗81.有5個學(xué)生,每個學(xué)生有3門課的成績,從鍵盤上輸入上述數(shù)據(jù)(包括學(xué)號,姓名,成績),計算出每個學(xué)生的平均成績,連同學(xué)號、姓名、成績一起存入文本文件。要求學(xué)號為整型,成績?yōu)楦↑c型,其它為字符型)。編程思路:每個學(xué)生的數(shù)據(jù)是一個節(jié)點,用一個結(jié)構(gòu)體變量來表示,或者采用具有3個元素的結(jié)構(gòu)體數(shù)組來表示。每個結(jié)構(gòu)體還要包括平均成績的成員變量。以打開方式建立一個文本文件。 Struct student int num; char name10; float score3; float average; ;#include stdafx.hstruct student int num;char name20;float s1;float s2;float s3; float average;int _tmain(int argc, _TCHAR* argv) struct student b5; int i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 天津市雙菱中學(xué)2024-2025學(xué)年高二上學(xué)期期中考試化學(xué)試題(含答案)
- 廣東省揭陽新華中學(xué)2024-2025學(xué)年高一下學(xué)期第一次月考化學(xué)試卷(含答案)
- 2024-2025學(xué)年河北省張家口市懷安縣八年級(上)期末物理試卷(含答案)
- 2019-2025年軍隊文職人員招聘之軍隊文職法學(xué)題庫綜合試卷A卷附答案
- 餐飲廚房考試試題及答案
- 配對合同范本(2篇)
- 2025年度施工員(市政工程)專業(yè)技能知識考試題庫及答案(一)
- 口腔牙周病知識培訓(xùn)課件
- 化學(xué)基本知識培訓(xùn)課件
- 私人酒窖租賃服務(wù)酒品保管免責(zé)
- DB11-T 641-2018 住宅工程質(zhì)量保修規(guī)程
- CoDeSys編程手冊
- 1981年高考數(shù)學(xué)全國卷(理科)及其參考答案-1981年高考數(shù)學(xué)
- 義務(wù)教育《歷史》課程標準(2022年版)
- 開工申請開工令模板
- 基于消費者心理的中國奢侈品營銷策略分析——以CHANEL為例市場營銷專業(yè)
- 單元三 電子合同法律實務(wù)
- 廣西獲補償資助高校畢業(yè)生在職在崗情況調(diào)查表
- (完整版)機場報批程序指南(流程)
- 英文繪本Mymum我媽媽
- 穿心打撈學(xué)習(xí)ppt課件
評論
0/150
提交評論