Damned Carriage Return | 该死的回车 [未完]

C、C++和Java语言
回复
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28
送出感谢: 0
接收感谢: 0

Damned Carriage Return | 该死的回车 [未完]

#1

帖子 flyinflash » 2008-07-02 2:07

近乎完美解决方案
viewtopic.php?t=117903&highlight=&sid=0 ... dd8b5541f0

问题代码:

代码: 全选

//  Damned Carriage Return
//  Last Modified: 2008-07-02
//  edcr1.c

#include <stdio.h>
#include <stdlib.h>


void get(int *a)
{
  printf("\n");
  printf("\n a = %d ", *a);
}

void set(int *a)
{
  printf("\n");
  printf("\n a = ");
  *a = getchar();
  printf("\n seted ");
}

int main(int argc, char** argv)
{
	int a = 15;
	int key;
	
	while (1)
	{
	  system("clear");
	  printf("\n");
	  printf("\n");
	  printf("\n  Menu ");
	  printf("\n");
	  printf("\n 1. get ");
	  
	  printf("\n");
	  printf("\n 2. set ");
	  printf("\n   ");
	  
	  key = getchar();
	  
	  switch (key)
	  {
	    case '1':
	      get(&a);
	      break;
	      
      case '2':
        set(&a);
        break;
        
      default:
        goto QUIT_FLAG;
        break;
    }
  }
  
  QUIT_FLAG:
	return 0;
}
正常代码

代码: 全选

//  Damned Carriage Return
//  Last Modified: 2008-07-02
//  edcr2.c

#include <stdio.h>
#include "stdiol.h"


void get(int *a)
{
  printf("\n");
  printf("\n a = %d ", *a);
}

void set(int *a)
{
  printf("\n");
  printf("\n a = ");
  *a = getch();
  printf("\n seted ");
}

int main(int argc, char** argv)
{
	int a = 15;
	int key;
	
	while (1)
	{
	  system("clear");
	  printf("\n");
	  printf("\n");
	  printf("\n  Menu ");
	  printf("\n");
	  printf("\n 1. get ");
	  
	  printf("\n");
	  printf("\n 2. set ");
	  printf("\n   ");
	  
	  key = getch();
	  
	  switch (key)
	  {
	    case '1':
	      get(&a);
	      break;
	      
      case '2':
        set(&a);
        break;
        
      default:
        goto QUIT_FLAG;
        break;
    }
  }
  
  QUIT_FLAG:
	return 0;
}

代码: 全选

#ifndef __STDIOL_H__
#define __STDIOL_H__

#include <stdio.h>
#include <stdlib.h>


int getch(void)
{
  int key;
  system("stty raw");
  key = getchar();
  system("stty cooked");
  return key;
}


#endif
睡觉,晚上继续
回复

回到 “C/C++/Java”