實驗二-線性表的應用_第1頁
實驗二-線性表的應用_第2頁
實驗二-線性表的應用_第3頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗二-線性表的應用實驗二線性表的應用(2學時)一、實驗目的:掌握線性表的基本結構和操作方法,培養學生靈活使用結構解決實際問題的能力。二、實驗內容:設計一個100位以內的長整數加減運算的程序。三、實驗要求:輸入輸出要求:每四位一組,組間用逗號分隔;加和減分別用不同的程序實現程序應考慮輸入數據的符號四、參考程序/*設計一個100位以內的長整數加減運算的程序。實驗要求:輸入輸出要求:每四位一組,組間用逗號分隔;程序應考慮輸入數據的符號*/#include<stdio.h>#include<string.h>#include<stdlib.h>#include&l

2、t;conio.h>#include<malloc.h>#defineLENsizeof(structstudent)structstudentintscore;structstudent*next;intlenm,lenn,lensum;/m與n的長度voidmain()structstudent*creat(int*d,int*len);voidprint(structstudent*head);structstudent*add(structstudent*m,structstudent*n);structstudent*sub(structstudent*m,stru

3、ctstudent*n);intcompare(structstudent*m,structstudent*n);structstudent*m,*n,*sum;ints1,s2,s3,k;charch;printf("輸入第一個數:n");m=creat(&s1,&lenm);/輸入第一個數放在鏈表m中,si為其符號printf("n選擇運算,輸入+或一:”);scanf("%c",&ch);getchar();printf("n輸入第二個數:n");n=creat(&s2,&len

4、n);/輸入第一個數放在鏈表n中,s2為其符號k=compare(m,n);/k=1貝Um>n,k=-1貝Um<n,k=0貝Um=nif(ch='+')/加法運算(if(s1*s2>0)s3=s1;sum=add(m,n);elseif(s1>0&&k=1)s3=1;sum=sub(m,n);/m>0,n<0,m>|n|,m+n=m-|n|if(s1>0&&k=-1)s3=-1;sum=sub(n,m);/m>0,n<0,m<|n|,m+n=-(|n|-m)if(s1<0&a

5、mp;&k=1)s3=-1;sum=sub(m,n);/m<0,n>O,|m|>|n|,m+n=-(|m|-n)if(s1<0&&k=-1)(s3=1;sum=sub(n,m);/m<0,n>O,|m|<|n|,m+n=n-|m|if(k=O)s3=0;)/endif+if(ch='-')/減法運算if(s1*s2<0)(s3=s1;sum=add(m,n);else(if(s1>0&&k=1)(s3=1;sum=sub(m,n);/m>0,n>0,m>n,m-n=m

6、-nif(s1>0&&k=-1)(s3=-1;sum=sub(n,m);/m>O,n>O,mvn,mn=(nm)if(s1<0&&k=1)(s3=-1;sum=sub(m,n);/m<0,n<O,|m|>|n|,m-n=-(|m|-|n|)if(s1<0&&k=-1)(s3=1;sum=sub(n,m);/m<0,n<O,|m|<|n|,m-n=|n|-|m|if(k=0)s3=0;)/endif-if(s3>0)(printf("結果為:+");prin

7、t(sum);)if(s3<0)(printf("結果為:-");print(sum);)if(s3=0)printf("結果為:0");/endmainstructstudent*creat(int*sign,int*n)/輸入數據并建立鏈表首結點存放最低4位數據(voidprint(structstudent*head);structstudent*p1,*p2;inti=0,j,k=0,len;chars5,c130;*sign=1;gets(c);if(c0='-')i=1;*sign=-1;p1=p2=(structstu

8、dent*)malloc(LEN);/建立尾結點p1->score=0;p1->next=NULL;len=strlen(c);for(;i<len;i+)(j=0;while(ci>='0'&&ci<='9')/取出逗號之間的每一組數存放到字符數組s中(sj+=ci+;k+;sj='0'p1=(structstudent*)malloc(LEN);p1->score=atoi(s);/將s轉換成整數p1->next=p2;/插入到鏈表頭p2=p1;*n=k;return(p1);stru

9、ctstudent*add(structstudent*m,structstudent*n)/加法運算(structstudent*p1,*p2,*head;/p2指向鏈表上最后一個結點,pl指向新建立的結點intsumb=0,n1,n2,n3;n1=lenm;n2=lenn;n3=n1>n2?n1:n2;p2=p1=(structstudent*)malloc(LEN);p1->score=(m->score+n->score)%10000;sumb=(m->score+n->score+sumb)/10000;p2->next=NULL;head=

10、p2;m=m->next;n=n->next;while(m!=NULL&&n!=NULL)(p1=(structstudent*)malloc(LEN);p1->score=(m->score+n->score+sumb)%10000;sumb=(m->score+n->score+sumb)/10000;p1->next=NULL;p2->next=p1;p2=p1;m=m->next;n=n->next;while(m!=NULL)(p1=(structstudent*)malloc(LEN);p1->

11、;score=(m->score+sumb)%10000;sumb=(m->score+sumb)/10000;p1->next=NULL;p2->next=p1;p2=p1;m=m->next;while(n!=NULL)(p1=(structstudent*)malloc(LEN);p1->score=(n->score+sumb)%10000;sumb=(n->score+sumb)/10000;p1->next=NULL;p2->next=p1;p2=p1;n=n->next;)lensum=n3;returnhead;

12、/endaddstructstudent*sub(structstudent*m,structstudent*n)/減法運算(structstudent*p1,*p2,*head;intsumb=0,n1,n2;n1=lenm;n2=lenn;p2=p1=(structstudent*)malloc(LEN);if(m->score>=n->score)p1->score=m->score+sumb-n->score;else(p1->score=m->score-n->score+10000;sumb=-1;p1->next=NUL

13、L;m=m->next;n=n->next;head=p2;while(n)p1=(structstudent*)malloc(LEN);p1->next=NULL;if(m->score+sumb>=n->score)(p1->score=m->score+sumb-n->score;sumb=0;else(p1->score=m->score+sumb-n->score+10000;sumb=-1;p2->next=p1;p2=p1;m=m->next;n=n->next;while(m)(p1=(s

14、tructstudent*)malloc(LEN);p1->next=NULL;p1->score=m->score+sumb;sumb=0;p2->next=p1;p2=p1;m=m->next;)lensum=n1;returnhead;/endsubintcompare(structstudent*m,structstudent*n)/比較兩數大小inti=0;if(lenm>lenn)return1;if(lenm<lenn)return-1;while(m&&n)if(m->score>n->score)i=1;if(m->score&l

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論