c语言实验报告.docVIP

  • 66
  • 0
  • 约9.78千字
  • 约 19页
  • 2017-01-06 发布于贵州
  • 举报
c语言实验报告c语言实验报告

计算机软件技术基础实验报告 实验一:线性表的排序与查找 一.实验内容 键盘输入一组无序数据,添加到线性表中; 排序线性表并输出排序结果; 键盘输入一个数,并插入到排好序的线性表中(要求插入后的表仍为有序表),输出结果; 键盘输入一个数,并从线性表中删除相应的数据,输出结果。 二,源程序 // Experiment1.cpp : 定义控制台应用程序的入口点。 // #include stdafx.h #include iostream #include conio.h // 程序实现有各种方法,这里给出一个实例。 // 定义一个线性表 const int nMaxSize = 15; // 最大值 int nLen = 0; // 表中元素个数 int nLinearList[nMaxSize]; // 定义操作 void LSort(); void LOut(); void LInsert(int n); void LDelete(int n); int main(int argc,TCHAR*argv[]) { // 输入数据并放入线性表中 printf(Please input data\n); // std::cout Please input data\n; int nIn = 0; for (int i = 1; i = 10; i++) { scanf(%d,nIn); // std::cin nIn; nLinearList[i] = nIn; nLen++; } LSort(); // 排序线性表 LOut(); // 输出结果 printf(Please input a data to insert \n); scanf(%d,nIn); LInsert(nIn); // 输入一个数字,并插入到线性表中 LOut(); printf(Please input a data to delete \n); scanf(%d,nIn); LDelete(nIn); // 输入一个数字,并从线性表中删除 LOut(); char chTmp; printf(Please input a char to finish this program.); chTmp = getch(); return 0; } void LSort() // 冒泡排序,由大到小 { int j,F,k,M; F=nLen; while(F0) {k=F-1;F=0; for(j=1;j=k;j++) {if(nLinearList[j]nLinearList[j+1]) { M=nLinearList[j]; nLinearList[j]=nLinearList[j+1]; nLinearList[j+1]=M; F=j; }}} } void LOut() { printf( \n); for (int i = 1; i = nLen; i++) { printf( %d, , nLinearList[i] ); } printf( \n); } void LInsert(int n) { int i,j;i=1; while (inLen) { 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=1;i=nLen;i++) {if(nLinearList[i]==n) {for(j=i;j=(nLen-1);j++) nLinearList[j]=nLinearList[j+1]; nLen--; break;}}} 三 运行结果 实验2:栈与队列的应用 实验内容 键盘输入算数表达式,并放入队列当中; 应用栈的概念设计表达式求值算法; 输出表达式求值结果 二.源程序 // Experiment2.cpp : 定义控制台应用程序的入口点。 #include stdio.h #include String.h #include stdlib.h #include math.h // 程序实现有各种方法,这里给出一个实例。 const int MAX_LEN = 10; // 字符串的长度 const int MAX_SIZE =

文档评论(0)

1亿VIP精品文档

相关文档