結對人員:信1201-2班高揚、信1201-1班韓雪東
一、設計思路
對於大數溢出問題開始很迷茫,不大懂該干什麼,思路匮乏。因為只要溢出這個數就不會被存儲被改變,這樣導致很難去判斷溢出後該怎樣做,但後來我們改變了一下思路,采用逆向思維,首先找到能存儲的最大數,用它來減去將要運行的數,從而與即將要加的數比較,如果小了咋說明相加後會產生溢出,直接結束程序。
二、源代碼
1 // ceshi.cpp : Defines the entry point for the console application.
2 //作者:韓雪東,高揚
3 //時間:2015/3/28
4
5 //#include "stdafx.h"
6 #include "stdio.h"
7 #include "stdlib.h"
8 #include "time.h"
9
10 int shuchu(int m[],int szcdx,int xhy)//m[]表示要測試的數組,szchx表示數組長度,xhy表示循環條件
11 {
12 int t,p;
13 int max,sum;
14 //緩存數組賦值
15 int c[10];
16 int v[10];
17 for(t=szcdx-xhy-1;t<szcdx;t++)
18 {
19 c[t-szcdx+xhy+1]=m[t];
20 }
21 //循環
22 for(t=xhy;t>=0;t--)
23 {
24 sum=0;
25 for(p=0;p<=t;p++)
26 {
27 if(2147483647-sum<c[p])
28 {
29 printf(" 數值過大 ");
30 system("PAUSE");
31
32 }
33 sum=sum+c[p];
34 }
35 v[t]=sum;
36
37
38
39 }
40 //循環輸出最大值
41 max=v[0];
42 for(t=0;t<xhy+1;t++)
43 {
44 if(max<=v[t])
45 {
46 max=v[t];
47 }
48 printf(" %d ",v[t]);
49 }
50
51 return max;
52 }
53
54 int main(int argc, char* argv[])
55 {
56 srand(time(NULL));
57 int a[10];
58 for(int j=0;j<10;j++)
59 {
60 a[j]=rand()%100000+20000000;
61 }
62
63 int maxx[10];
64
65 for(int i=9;i>=0;i--)
66 {
67 printf("包含數組中第%d個數在內的所有相鄰子數組的和:",10-i);
68 maxx[i]=shuchu(a,10,i);
69 printf("\n%d\n\n",maxx[i]);
70
71 }
72 int maxxx=maxx[0];
73 for(i=0;i<10;i++)
74 {
75 if(maxxx<=maxx[i])
76 {
77 maxxx=maxx[i];
78
79 }
80 }
81 printf("\n\n該數組的所有子數組的和的最大值:%d\n\n",maxxx);
82 return 0;
83 }
三、運行結果截圖




四、心得體會
以前的測試程序並沒有過多的考慮過大數溢出的問題,通過這次練習認識到了大數溢出給程序帶來的嚴重性的問題,在以後的編寫程序的時候一定會注意這一個問題。
五、有圖有真相
