[讨论]这个C语言作业1的效率不能提高了吗?

C、C++和Java语言
回复
头像
toawinner47
帖子: 200
注册时间: 2006-11-08 20:46
送出感谢: 1 次
接收感谢: 1 次
联系:

[讨论]这个C语言作业1的效率不能提高了吗?

#1

帖子 toawinner47 » 2007-05-15 18:14

一、使用printf()函数显示下列菜单:
Menu
======================
1.Input the students's names and scores
2.Search scores of some students
3.Mondify scores of some students
4.List all students' scores
5.Quit the system
========================
Please input your choise(1-5):


我用了vim Menu.c新建了如下的文件:
#include<stdio.h>
main()
{
printf(" Menu\n");
printf("=======================\n");
printf(" 1.Input the students' names and scores\n");
printf(" 2.Search scores of some students\n");
printf(" 3.Mondify scores of some students\n");
printf(" 4.List all students' scores\n");
printf(" 5.Quit the system\n");
printf("========================\n");
printf(" Please input your choise(1-5):\n");
}
~
:wq保存退出
用命令~$ gcc -o Menu Menu.c编译,没有什么错误出现
用命令~$ ./Menu 运行程序时显示
Menu
=======================
1.Input the students' names and scores
2.Search scores of some students
3.Mondify scores of some students
4.List all students' scores
5.Quit the system
========================
Please input your choise(1-5):
偶就觉得这样编程的效率太低了吧,你们说像Menu这个词就是一个空格一个空格右移到中间位置的,有没有什么命令让他居中的呀?偶是初学者见笑了。效果如图:
附件
1.png
01.png
上次由 toawinner47 在 2007-05-15 18:18,总共编辑 1 次。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
送出感谢: 0
接收感谢: 7 次
联系:

#2

帖子 BigSnake.NET » 2007-05-15 18:16

需要写这么多个printf么..
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
shuhill
帖子: 62
注册时间: 2007-04-20 13:05
送出感谢: 0
接收感谢: 0

#3

帖子 shuhill » 2007-05-16 16:45

用数组,写循环不行么?

小弟不会C++. N年前学过.
头像
arthur
帖子: 76
注册时间: 2006-08-13 16:06
送出感谢: 0
接收感谢: 0

#4

帖子 arthur » 2007-05-24 14:36

代码: 全选

#include<stdio.h>
main()
{
printf(" Menu\n"
    "=======================\n"
    " 1.Input the students' names and scores\n" 
    " 2.Search scores of some students\n" 
    " 3.Mondify scores of some students\n" 
    " 4.List all students' scores\n" 
    " 5.Quit the system\n" 
    "========================\n" 
    " Please input your choise(1-5):\n");
} 
头像
arthur
帖子: 76
注册时间: 2006-08-13 16:06
送出感谢: 0
接收感谢: 0

#5

帖子 arthur » 2007-05-24 15:06

或者用C++:

代码: 全选

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	cout << setw(42) << right << "MENU" << endl;
	cout << setfill('=') << setw(80) << "" << endl;
	cout << setfill(' ');
	cout << setw(8) << "I." << "Menu item I.." << endl;
	cout << setw(12) << "1." << "Submenu item 1.." << endl;
	cout << setw(12) << "2." << "Submenu item 2.." << endl;
	cout << setw(12) << "3." << "Submenu item 3.." << endl;
	cout << setw(12) << "4." << "Submenu item 4.." << endl;
	cout << setw(12) << "5." << "Submenu item 5.." << endl;
	cout << setw(12) << "6." << "Submenu item 6.." << endl;
	cout << setw(12) << "7." << "Submenu item 7.." << endl;
	cout << setw(12) << "8." << "Submenu item 8.." << endl;
	cout << setw(12) << "9." << "Submenu item 9.." << endl;
	cout << setw(12) << "10." << "Submenu item 10.." << endl;
	cout << setw(12) << "11." << "Submenu item 11.." << endl;
	cout << setw(12) << "12." << "Submenu item 12.." << endl;
	cout << setw(12) << "13." << "Submenu item 13.." << endl;
	cout << setw(8) << "II." << "Menu item II......." << endl;
	cout << setw(8) << "III." << "Menu item II.........." << endl;
	cout << setfill('=') << setw(80) << "" << endl;
	cout << "Please input your choise:" << endl;
	return 0;
}
附件
ss.png
luhuadeng
帖子: 4
注册时间: 2007-05-26 22:09
送出感谢: 0
接收感谢: 0

#6

帖子 luhuadeng » 2007-05-26 22:15

我这里帮你想了一个办法:
用一个字符文件把你要输出的大块的信息保存起来,
然后把他们读出来就是了。

代码: 全选

#include "stdio.h"

int main(void){
         FILE *fp;
        char temp;
        if((fp=fopen("./menuscript.txt","r"))==NULL)
        {
                printf("ERROR!");
                return 0;
        }
        while(!feof(fp)){
                fscanf(fp,"%c",&temp);
                printf("%c",temp);
        }
        return 1;

}
这样想怎么显示就怎么显示!
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
送出感谢: 0
接收感谢: 7 次
联系:

#7

帖子 BigSnake.NET » 2007-05-26 22:33

代码: 全选

#include "stdio.h"

#define TEXTFOO \
" Menu\n"\
"=======================\n"\
" 1.Input the students' names and scores\n"\
" 2.Search scores of some students\n"\
" 3.Mondify scores of some students\n"\
" 4.List all students' scores\n"\
" 5.Quit the system\n"\
"========================\n"\
" Please input your choise(1-5):\n"

int main(){
    printf(TEXTFOO);
    return 0;
}

代码: 全选

#include <iostream>
using namespace std;

const char* const TEXTFOO =
" Menu\n"
"=======================\n"
" 1.Input the students' names and scores\n"
" 2.Search scores of some students\n"
" 3.Mondify scores of some students\n"
" 4.List all students' scores\n"
" 5.Quit the system\n"
"========================\n"
" Please input your choise(1-5):";

int main(){
    cout<<TEXTFOO<<endl;
    return 0;
}
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
kelvinhan
帖子: 173
注册时间: 2006-11-30 1:29
送出感谢: 0
接收感谢: 0
联系:

#8

帖子 kelvinhan » 2007-05-27 13:03

:D 凑个热闹

代码: 全选


#include "stdafx.h"
#include "string.h"

#define MAX_TIPS_LEN 256
#define MSG_TITLE 0
#define MSG_SEP -1
#define MSG_ITEM 1

typedef struct _menuitem
{
	char key;
	char msg[MAX_TIPS_LEN];
}menu_item;

int menu_select(menu_item *m, int count)
{
	unsigned int maxsize=0;
	int i=0;
	for(i=0; i<count; i++)
	{
		if(strlen(m[i].msg) > maxsize)
			maxsize=strlen(m[i].msg);
	}

	for(i=0; i<count; i++)
	{
		switch(m[i].key)
		{
			case MSG_SEP:for(int j=0; j<maxsize; j++)printf("%s",m[i].msg);printf("\n");break;

			case MSG_TITLE:for(int j=0; j<(maxsize/2 - strlen(m[i].msg)/2); j++)printf(" ");
			default:printf("%s\n", m[i].msg);
		}
	}
	printf("%s","Please input your choise(1-5):"); 
	int c = getchar();
	printf("\n");

	for(i=0; i<count; i++)
	{
		if( m[i].key == c )
		{
			return c;
		}
	}
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	menu_item menu[] = 
	{
		{MSG_TITLE,"Main Menu"},
		{MSG_SEP,"#"},
		{'1',"1.Input the students's names and scores"},
		{'2',"2.Search scores of some students"},
		{'3',"3.Mondify scores of some students"},
		{'4',"4.List all students' scores"},
		{'5',"5.Quit the system"},
		{MSG_SEP,"*"}
	};
	int menu_size = sizeof(menu)/sizeof(menu_item);

	int se=0;
	while( (se = menu_select(menu, menu_size)) != '5' )
	{
		switch(se)
		{
			case '1':printf("you select 1\n");break;
			case '2':printf("you select 2\n");break;
			case '3':printf("you select 3\n");break;
			case '4':printf("you select 4\n");break;
			case '5':printf("you select 4\n");break;
			default:printf("error,pls input 1-5\n");break;
		}
	}

	return 0;
}
回复

回到 “C/C++/Java”