本帖最后由 学习stm32f4 于 2018-9-26 13:02 编辑
第十章 第189页 [mw_shl_code=c,true]6.
#include "stdio.h"
#include "stdbool.h"
#include "stdlib.h"
#define STACK_SIZE 100
void push(int i);
int pop(void);
int top=0,contents[STACK_SIZE];
int main(void)
{
int n=-1,n2,n3;
char ch;
printf("Enter an RPN expression: ");
scanf("%c",&ch);
if(ch>='0'&&ch<='9')
n=ch-'0';
else if(ch=='+')
n=43;
else if(ch=='-')
n=45;
else if(ch=='*')
n=42;
else if(ch=='/')
n=47;
else if(ch=='=')
n=61;
else if(ch=='q')
n=113;
else if(ch=='Q')
n=81;
else if(ch==' ')
n=-1;
while(n!=81&&n!=113)
{
if(n>=0&&n<=9)
push(n);
else
{
switch(n)
{
case 43:
n2=pop();
n3=pop();
push(n3+n2);
break;
case 45:
n2=pop();
n3=pop();
push(n3-n2);
break;
case 42:
n2=pop();
n3=pop();
push(n3*n2);
break;
case 47:
n2=pop();
n3=pop();
push(n3/n2);
break;
case 61:
printf("Value of expression: %d\n",pop());
printf("Enter an RPN expression: ");
getchar();
break;
default:
break;
}
}
scanf("%c",&ch);
if(ch>='0'&&ch<='9')
n=ch-'0';
else if(ch=='+')
n=43;
else if(ch=='-')
n=45;
else if(ch=='*')
n=42;
else if(ch=='/')
n=47;
else if(ch=='=')
n=61;
else if(ch=='q')
n=113;
else if(ch=='Q')
n=81;
else if(ch==' ')
n=-1;
}
return 0;
}
void make_empty(void)
{
top=0;
}
bool is_empty(void)
{
return top==0;
}
bool is_full(void)
{
return top==STACK_SIZE;
}
void stack_underflow(void)
{
printf("stack underflow.\n");
}
void stack_overflow(void)
{
printf("stack overflow.\n");
}
void push(int i)
{
if(is_full())
{
stack_overflow();
exit(EXIT_FAILURE);
}
else
contents[top++]=i;
}
int pop(void)
{
if(is_empty())
{
stack_underflow();
exit(EXIT_FAILURE);
}
else
return contents[--top];
}
7.
#include "stdio.h"
#include "windows.h"
void print_digits(char str[]);
static void SetPos(int x,int y);
char digits[][3][4]={
{
{' ','_',' ',' '},
{'|',' ','|',' '},
{'|','_','|',' '},
},
{
{' ',' ',' ',' '},
{' ',' ','|',' '},
{' ',' ','|',' '},
},
{
{' ','_',' ',' '},
{' ','_','|',' '},
{'|','_',' ',' '},
},
{
{' ','_',' ',' '},
{' ','_','|',' '},
{' ','_','|',' '},
},
{
{' ',' ',' ',' '},
{'|','_','|',' '},
{' ',' ','|',' '},
},
{
{' ','_',' ',' '},
{'|','_',' ',' '},
{' ','_','|',' '},
},
{
{' ','_',' ',' '},
{'|','_',' ',' '},
{'|','_','|',' '},
},
{
{' ','_',' ',' '},
{' ',' ','|',' '},
{' ',' ','|',' '},
},
{
{' ','_',' ',' '},
{'|','_','|',' '},
{'|','_','|',' '},
},
{
{' ','_',' ',' '},
{'|','_','|',' '},
{' ','_','|',' '},
}
};
int main(void)
{
print_digits("123-123");
return 0;
}
void print_digits(char str[])
{
int i,j,n=0;
char *p=str;
while(*p!='\0')
{
while(*p>='0'&&*p<='9')
{
for(i=0;i<3;i++)
{
SetPos(4*n,i);
for(j=0;j<4;j++)
{
printf("%c",digits[*p-'0'][j]);
}
}
p++;
n++;
}
p++;
}
}
static void SetPos(int x,int y)
{
COORD point = {x, y};
HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(HOutput, point);
}
或者:
#include "stdio.h"
#include "string.h"
#define MAX_DIGITS 10
void clear_digits_array(void);
void process_digit(int digit,int position);
void print_digits_array(void);
const int segments[10][7]={
{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1}
};
char digits[3][MAX_DIGITS*4];
int main(void)
{
char ch;
int i=0;
clear_digits_array();
printf("Enter digits: ");
while((ch=getchar())!='\n')
{
if(i<MAX_DIGITS&&ch>='0'&&ch<='9')
process_digit(ch-'0',i++);
}
print_digits_array();
return 0;
}
void clear_digits_array(void)
{
memset(digits,' ',sizeof(digits));
}
void process_digit(int digit,int position)
{
int n=4*position;
if(segments[digit][0])
digits[0][n+1]='_';
if(segments[digit][1])
digits[1][n+2]='|';
if(segments[digit][2])
digits[2][n+2]='|';
if(segments[digit][3])
digits[2][n+1]='_';
if(segments[digit][4])
digits[2][n]='|';
if(segments[digit][5])
digits[1][n]='|';
if(segments[digit][6])
digits[1][n+1]='_';
}
void print_digits_array(void)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<MAX_DIGITS*4;j++)
printf("%c",digits[j]);
printf("\n");
}
}
[/mw_shl_code] 附录:设置控制台光标位置
[mw_shl_code=c,true]#include "windows.h"
static void SetPos(int x,int y)
{
COORD point = {x, y};
HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(HOutput, point);
}[/mw_shl_code]
|