報告彙總之c語言數據結構報告篇(二)
簡單計算器
代碼區
#include <stdio.h>
#include <stdlib.h>
char S[100];
char S2[100];
int i=0;
//用來存儲計算對數據
typedef struct sqlist
{
char data[100];
int top;
}s1;
//初始化數據棧
void initSqlist (s1*L)
{
L=(s1*)malloc(sizeof(s1));
L->top=-1;
}
//數據棧進棧
void pushs1 (s1*L,char c)
{
L->top++;
L->data[L->top]=c;
}
//數據棧進棧
int pop (s1*L)
{
int i;
i=L->top;
L->top--;
return L->data[i];
}
//創建運算符棧
typedef struct
{
char op[100];
int top;
} c1;
//初始化運算符棧
void initc1 (c1*P)
{
P=(c1*)malloc(sizeof(c1));
P->top=-1;
}
//運算符壓棧
void pushc1 (c1*P,char c)
{
P->top++;
P->op[P->top]=c;
}
//運算符出棧
char popc1 (c1*P)
{
int i=P->top;
P->top--;
return P->op[i];
}
//獲得需要處理的式子
void get (char s1[100])
{
int i=0;
scanf("%s",s1);
while (s1[i]!=';')
{
printf("%c",s1[i]);
i++;
}
printf("\n");
}
void changeguohao (char S[100],s1*L,c1*P,int i);
//變換中綴爲後綴
void change (char S[100],s1*L,c1*P,int i)
{
get(S);
printf("後綴表達式爲:");
char a;
while (S[i-1]!=';')
{
int s=-1;
if (S[i]=='(')
{
s=P->top;
}
if (S[i]=='('||S[i]==')')
{
if (S[i]==')')
{
while (P->top!=s)
{
a=popc1(P);
printf("%c",a);
}
}
i++;
}
if (S[i]>=49&&S[i]<=57)
{
pushs1(L,S[i]);
a=pop(L);
printf("%c",a);
}
if (S[i]==';')
while (P->top!=-1) {
a=popc1(P);
printf("%c",a);
}
if (S[i]=='+'||S[i]=='-')
{
if (S[i-2]=='+'||S[i-2]=='-')
{
a=popc1(P);
printf("%c",a);
}
pushc1(P,S[i]);
}
if (S[i]=='*'||S[i]=='/')
{
pushc1(P, S[i]);
if(S[i+1]!='(')
{
i++;
pushs1(L,S[i]);
a=pop(L);
printf("%c",a);
}
while (P->top!=-1&&S[i+1]!='(')
{
a=popc1(P);
printf("%c",a);
}
//i++;
//pushc1(P,S[i]);
}
i++;
}
}
//變換有括號時的中綴表達式
void changeguohao (char S[100],s1*L,c1*P,int i)
{
char a;
if (S[i]==')')
{
while (P->top!=-1)
{
a=popc1(P);
printf("%c",a);
}
i++;
}
change(S, L, P, i);
}
//計算後綴表達式
int clculate (char S[100],s1*L,c1*P,int i)
{
printf("請輸入您需要處理的後綴表達式:");
get(S);
i=0;
char a;
char b;
int c=0;
char d = '0';
L->top=-1;
while (S[i]!=';')
{
if (S[i]>=49&&S[i]<=57)
pushs1(L, S[i]);
//數據壓棧
else
{
//對運算符進行判斷,進行計算
if (S[i]=='+')
{
a=pop(L);
b=pop(L);
c=(a-'0')+(b-'0');
d=c+'0';
pushs1(L, d);
}
if (S[i]=='-')
{
a=pop(L);
b=pop(L);
c=(b-'0')-(a-'0');
d=c+'0';
pushs1(L, d);
}
if (S[i]=='*')
{
a=pop(L);
b=pop(L);
c=(a-'0')*(b-'0');
d=c+'0';
pushs1(L, d);
}
if (S[i]=='/')
{
a=pop(L);
b=pop(L);
c=(b-'0')/(a-'0');
d=c+'0';
pushs1(L, d);
}
}
i++;
}
return c;
}
int main ()
{
s1 *L;
L=(s1*)malloc(sizeof(s1));
//initSqlist(L);
L->top=-1;
c1 *P;
P=(c1*)malloc(sizeof(c1));
//initc1(* &P);
P->top=-1;
while (1)
{
printf("請輸入您需要進行的操作:");
printf("0代表轉換爲後綴表達式;1代表計算後綴表達式結果\n");
int n;
scanf("%d",&n);
getchar();
switch (n)
{
case 0:
{
printf("中綴表達式爲:");
change(S, L, P,i);
printf("\n");
break;
}
case 1:
{
printf ("計算的結果爲:%d",clculate(S, L, P, i));
printf("\n");
break;
}
default:
break;
}
}
return 0;
}
報告區
一、 實驗內容描述(問題域描述)
編寫程序,模擬簡單運算器的工作:輸入一個算式(沒有空格),遇等號“=”說明輸入結束,輸出結果。假設計算器只能計算加減乘除運算,運算數和結果都是整數。要求完成以下功能:
(1) 從鍵盤錄入中綴表達式,將中綴表達式轉換爲後綴表達式輸出;
(2) 輸入後綴表達式,計算後綴表達式的值。
二、實驗基本原理與設計(數據結構設計與算法設計)
(1)利用順序棧進行存儲和運算運算符和數字
(2)在讀入信息時碰到數字就輸出,碰到字符先入棧比較後輸出
(3)在進行計算時直接運算,把運算後的結果入棧;直至把最終結果出棧
第二部分:實驗調試與結果分析(可加頁)
一、 調試過程(包括調試方法描述、實驗數據記錄,實驗現象記錄,實驗過程發現的問題等)
1.調試方法描述
① 輸入c程序,並保存;
② 編譯c程序,找出程序的語法錯誤並改正;
③ 輸入測試數據,運行c程序;
④ 若有錯利用斷點一步步運行查錯
⑤ 重複②-④步,直到得到正確的運行結果。
2.實驗輸入/輸出數據記錄
3.實驗過程發現的問題
在進行減法運算的時候我們需要注意是誰減誰,除法運算也一樣。開始我並沒有注意到這一點,導致後面的結果錯誤很多。
在把中綴轉後綴的過程中我忘記將括號刪去導致一開始輸出時括號一起輸出;帶括號的轉換
的複雜程度明顯高於不帶括號所以我將其單獨列出便於查看。
二、 實驗結果及分析(包括結果描述、實驗現象分析、影響因素討論、綜合分析和結論等)
1. 結果描述
轉換過程和計算過程全部正確;結果輸出無誤。
2. 實驗現象分析
所有程序經調試均符合題目要求。
3.影響因素討論
4.算法分析(包括時間和空間)
空間複雜度爲 Sn=O(100)
時間複雜度爲Tn=O(n)
5.結論
經過多次調試,所有程序最終運行都成功了。
三、 實驗小結、建議及體會
學習C語音需要有很大的耐心與毅力,對我們的身體的腦力都是一項很大的挑戰。敲了快整整一 周的代碼,雖然很累,很煩瑣,偶爾還會 被題目難倒,壓力山大。可當自己將題目一
題一題的解決時,心中更多的是成就感,多了一分慰藉。在接下來的日子裏,要更加努力好好學專業基礎,穩固提升。在課餘時間更加註重勞逸結合,多運動,多鍛鍊,這樣學習更有效率。