5个函数的实现么
#include <stdioh>#include <stdlibh>
#include <stringh>
int createArray( int n )
{
int i, a;
a = (int)malloc( nsizeof(int) );
memset( a, 0, nsizeof(int) );
printf( "输入 %d 个整数:\n", n );
for( i=0; i<n; i++ )
scanf( "%d", &a[i] );
fflush( stdin );
return a;
}
void printArray( int a, int n )
{
int i;
if ( a == NULL ) return;
for( i=0; i<n; i++ )
{
printf( "%5d ", a[i] );
if ( i%5 == 0 && i!=0 )
printf( "\n" );
}
printf( "\n" );
}
void bubbleSort( int a, int n )
{
int i,j;
if ( a == NULL ) return;
for( i=0; i<n; i++ )
for( j=i; j<n; j++ )
if( a[i] > a[j] )
{
int t = 0;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int binarySearch( int a, int n, int tofind )
{
int i,high, low, mid ;
high = n-1;
low = 0;
mid = (low+high) / 2;
if ( a == NULL ) return -1;
while( high > low )
{
mid = (low+high)/2;
if ( a[mid] > tofind )
high = mid;
else if ( a[mid] < tofind )
low = mid;
else
return mid;
}
return -1;
}
void reverseArray( int a, int n )
{
int i;
if ( a == NULL ) return;
for( i=0; i<n/2; i++ )
{
int t;
t = a[i];
a[i] = a[n-i-1];
a[n-i-1] = t;
}
}
int main()
{
int a=NULL,c=0, n;
printf( "menu\n" );
printf( "input your choice[0-5]:" );
scanf( "%d", &c );
fflush( stdin );
while( c!=0 )
{
switch( c )
{
case 1:
printf( "input array length:" );
scanf( "%d", &n );
fflush( stdin );
if ( n<=0 )
printf( "input invalid,%d\n", n );
else
{
if ( a!= NULL ) free( a );
a = createArray( n );
}
break;
case 2:
printArray( a, n );
break;
case 3:
bubbleSort( a, n );
break;
case 4:
{
int tofind = 0, idx=-1;
printf( "which number you want to search:" );
scanf( "%d", &tofind );
fflush( stdin );
idx = binarySearch( a, n, tofind );
if ( idx < 0 )
printf( "%d not find\n", tofind );
else
printf( "%d found at index %d\n", tofind, idx );
break;
}
case 5:
reverseArray( a, n );
printArray( a, n );
break;
}
printf( "input your choice[0-5]:" );
scanf( "%d", &c );
fflush(stdin);
}
}
这是我以前做过的一个,你可以参考一下。利用k
do
{
//ConsoleWrite("输入0结束,任意键继续");
//k = ConsoleReadKey()KeyChar;
ConsoleClear();
ConsoleWriteLine("主菜单");
ConsoleWriteLine("选择你要的服务:");
ConsoleWriteLine("1、输入信息");
ConsoleWriteLine("2、浏览信息");
ConsoleWriteLine("3、排序");
ConsoleWriteLine("4、查询");
ConsoleWriteLine("5、结束");
int num = 0;
ConsoleWrite("请输入您的选择:");
num = intParse(ConsoleReadLine());
switch (num)
{
case 1:
{
ConsoleClear();
for (int i = 1; i < m+1; i++)
{
ConsoleWrite("请输入学生{0}的学号(姓名):",i);
iArray[i, 0] = ConvertToString(ConsoleReadLine());
for (int j = 1; j < n+1; j++)
{
//ConsoleWriteLine();
ConsoleWrite("{0}的成绩:",iArray[0,j]);
iArray[i, j] = ConvertToString(ConsoleReadLine());
}
}
} break;
case 2:
{
ConsoleClear();
for (int j = 0; j < n + 1; j++)
ConsoleWrite("{0}\t",iArray[0,j]);
for (int i = 1; i < m + 1; i++)
{ ConsoleWriteLine();
for (int j = 0; j < n + 1; j++)
ConsoleWrite("{0}\t", iArray[i, j]);
}
ConsoleWriteLine("\n按任意键回主菜单");
ConsoleReadLine();
} break;
case 3:
{
ConsoleClear();
int num3=0;
string temp,temp1;
int k3 = 1;
do
{
ConsoleClear();
ConsoleWriteLine("选择排序的科目:");
for (int j = 1; j < n + 1; j++)
{
ConsoleWriteLine("{0}、按{1}", j, iArray[0, j]);
}
ConsoleWriteLine("{0}、返回主菜单", n + 1);
if (!intTryParse(ConsoleReadLine(), out num3))
return;
for (int m1 = 1; m1 < n + 1; m1++)
if (num3 == m1)
{
for (int i = 1; i < m; i++)
for (int n1 = i + 1; n1 < m + 1; n1++)
if (stringCompare(iArray[i, num3], iArray[n1, num3]) < 0)
{
temp = iArray[i, num3];
temp1=iArray[i,0];
iArray[i, num3] = iArray[n1, num3];
iArray[i,0]=iArray[n1,0];
iArray[n1, num3] = temp;
iArray[n1, 0] = temp1;
}
ConsoleClear();
ConsoleWrite("{0}\t", iArray[0, 0]);
ConsoleWrite("{0}\t", iArray[0, num3]);
for (int i = 1; i < m + 1; i++)
{
ConsoleWriteLine();
ConsoleWrite("{0}\t", iArray[i, 0]);
ConsoleWrite("{0}",iArray[i,num3]);
}
ConsoleWriteLine("\n{0}最高分为{1},最低分为{2}",iArray[0,num3],iArray[1,num3],iArray[m,num3]);
ConsoleReadLine();
}
else if (num3 == n + 1)
k3 = 0;
} while (k3 != 0);
} break;
case 4:
ConsoleClear();
ConsoleWrite("请输入要查找的学号(姓名):");
string x;
x = ConsoleReadLine();
int pos = -1;
for (int i = 1; i < m+1; i++)
if (stringEquals(x,iArray[i, 0]))
{
pos = i;
ConsoleWriteLine("{0}在数组中位置为{1}", x, pos);
for (int j = 0; j < n + 1; j++)
ConsoleWrite("{0}\t", iArray[0, j]);
ConsoleWriteLine();
for (int j = 0; j < n + 1; j++)
ConsoleWrite("{0}\t", iArray[pos, j]);
}
if (pos == -1)
ConsoleWriteLine("{0}不存在于数组中", x);
ConsoleReadLine();
break;
case 5:
k = 0;
break;
}
}
while (k!=0);
#include <cstdlib>
#include <iostream>
#include <stringh> using namespace std;void showMenu()
{
cout<<"主 菜 单"<<endl;
cout<<"1添加记录 2显示记录"<<endl;
cout<<"3读取记录 4保存记录"<<endl;
}
void input()
{
int select;
cin>>select;
switch(select)
{
case 0:
exit(1); //程序正常退出
case 1:
cout<<"你选择了1"<<endl;
break;
case 2:
cout<<"你选择了2"<<endl;;
break;
case 3:
cout<<"你选择了3"<<endl;
break;
case 4:
cout<<"你选择了4"<<endl;
break;
default:
cout<<"选择错误,请重新输入!"<<endl;
input();//重新选择
}
}
int main(int argc, char argv[])
{
//函数声明
void showMenu();
void input();
//调用
showMenu();
input();
system("PAUSE");
return EXIT_SUCCESS;
}
以上就是关于C语言菜单程序设计全部的内容,包括:C语言菜单程序设计、C语言中怎样定义,才能制作一个以菜单方式运行的程序、C++编写一个菜单显示程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)