计算机软件基础实验
实验一:
键盘输入一组无序数据,添加到线性表中;排序线性表并输出排序结果;键盘输入一个数,并插入到排好序的线性表中(要求插入后的表仍为有序表),输出结果;键盘输入一个数,并从线性表中删除相应的数据,输出结果。
程序清单:
// Experiment1.cpp : 定义控制台应用程序的入口点。 //#include \#include \#include
// 程序实现有各种方法,这里给出一个实例。
// 定义一个线性表
const int nMaxSize = 15; // 最大值 int nLen = 0; // 表中元素个数 int nLinearList[nMaxSize];
// 定义操作 void LSort(); void LOut();
void LInsert(int n); void LDelete(int n);
void main() { // 输入数据并放入线性表中 printf(\// std::cout << \ int nIn = 0; for (int i = 0; i <= 9; i++) { scanf(\// std::cin >> nIn; nLinearList[i] = nIn; nLen++; } LSort(); // 排序线性表 LOut(); // 输出结果 printf(\ scanf(\ LInsert(nIn); // 输入一个数字,并插入到线性表中 LOut();
printf(\ scanf(\ LDelete(nIn); // 输入一个数字,并从线性表中删除 LOut(); char chTmp; printf(\ chTmp = getch(); //return 0; }
void LSort() // 冒泡排序,由大到小 { int j,F,k,M; F=(nLen-1); while(F<16) { k=F-1;F=16; for(j=0;j<=k;j++) { if(nLinearList[j] void LOut() { printf( \ for (int i = 0; i < nLen; i++) { printf( \ } printf( \} void LInsert(int n) { int i,j; i=0; while (i { if (nLinearList[i]<=n) { nLen++; for (j=nLen;j>=i;j--) nLinearList[j+1]=nLinearList[j]; nLinearList[i]=n; break; } i++; } } void LDelete(int n) { int i,j; for(i=0;i 运行结果: 实验二:键盘输入算数表达式,并放入队列当中;应用栈的概念设计表达式求值算法;输出表达式求值结果; 程序清单: // Experiment2.cpp : 定义控制台应用程序的入口点。 //#include \#include \#include \#include // 程序实现有各种方法,这里给出一个实例。 const int MAX_LEN = 10; // 字符串的长度 const int MAX_SIZE = 30; // 栈或队的最大元素个数 char pitem[10] = {'\\0'}; int numbers=0; // 定义一个队列的结构 struct QUEUE { int nMaxSize; // 最大值 int nCount; // 个数 int nFront; // 头 int nRear; // 尾 char szQueue[MAX_SIZE][MAX_LEN]; }; //定义一个栈的结构 struct STACK { int nMaxSize; // 最大值 int nTop; // 栈顶 char szStack[MAX_SIZE][MAX_LEN]; }; // 队列的操作 void InitQueue(QUEUE *q,int nMaxSize); int InQueue(QUEUE *q, char *pItem); int OutQueue(QUEUE *q, char *pItem); //栈的操作 void InitStack(STACK *s,int nMaxSize); int PushStack(STACK *s, char *pItem); int PopStack(STACK *s, char *pItem); void GetTopStack(STACK *s, char *pItem); int Priority(char *op); // 获得操作符的优先级 void Compute(char *num1, char *num2, char *op, char *chResult); int isdigit(char isnum); int main() { // 声明一个队列 struct QUEUE que; InitQueue(&que,MAX_SIZE); // 声明OS栈和NS栈 struct STACK OS,NS; InitStack(&OS,MAX_SIZE); InitStack(&NS,MAX_SIZE); // 输入表达式,并放入到队列当中 printf(\ do { for(int j=0;j<10;j++) pitem[j] = '\\0'; gets(pitem); InQueue(&que, pitem); }while(pitem[0]!=';'); // 显示表达式 do { printf(\ numbers++; }while(que.szQueue[numbers][0]!=';'); // 表达式求值 char x[MAX_LEN]; // 扫描的表达式 char op[MAX_LEN]; // 栈顶运算符 char num1[MAX_LEN], num2[MAX_LEN]; // 两个操作数 char chResult[MAX_LEN]; // 运算结果 OutQueue(&que, x); // 扫描表达式 PushStack(&OS,\ // ;压栈 while (1) { if (isdigit(x[0])) // 是数 PushStack(&NS,x); // 数字压栈 // 计算表达式的值 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库计算机软件实验报告 - 图文在线全文阅读。
相关推荐: