#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#define MAX 100
typedef struct
{
int data[MAX];
int top;
}seqstack;
int main()
{
void PUSH(seqstack *s, int x);
//void OUT(seqstack *s);
int POP(seqstack *s);
void any_D(int d);
void PUSH(seqstack* s, int x);
int POP(seqstack* s);
void gotoxy(int x, int y);
int color(int num);
void menu();
void any_D(int d);
void D_any(int d);
void end();
void SET();
int i;
SET();
do
{
system("cls");
menu();
scanf("%d", &i); //用户进行选择
switch (i)
{
case 1:
D_any(2);
break;
case 2:
D_any(8);
break;
case 3:
D_any(16);
break;
case 4:
any_D(2);
break;
case 5:
any_D(8);
break;
case 6:
any_D(16);
break;
case 7:
end();
break;
default:
printf("Wrong Choie!Please Select again!\n");
}
getchar();
} while (i != 7);
return 0;
}
void SET()
{
SetConsoleOutputCP(437);
}
//进栈(int)
void PUSH(seqstack *s, int x)
{
if (s->top == MAX)
{
printf("Overflow!\n");
}
else
{
s->data[s->top] = x;
(s->top)++;
}
}
//出栈(int)
int POP(seqstack *s)
{
int e;
if (s->top == 0)
{
printf("SeqStack is NULL!\n");
return 0;
}
else
{
(s->top)--;
e = s->data[(s->top)];
}
return e;
}
//二、八、十六转换为十进制
void any_D(int d)
{
int sum = 0;
char e;
int i, t;
int dd=1;
seqstack *s;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
printf("Please input number(%d)!\n",d);
getchar();
scanf("%c",&e);
while(e!='\n')
{
if(e>='0' && e<='9')
PUSH(s,e-'0');
//PUSH(s,e-48);
else if(e>='A'&& e<='F')
PUSH(s,e-'A'+10);
//PUSH(s,e-55);
scanf("%c",&e);
}
t = s->top;
for (i = 0; i < t; i++)
{
e=POP(s);
sum=sum+dd*e;
dd*=d;
}
printf("Turn to number(10) to number(%d) is %d\n",d, sum);
getchar();
getchar();
}
//十进制转二、八、十六进制
void D_any(int d)
{
seqstack *s;
int r;
int x;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
printf("Please input a number(10):");
scanf("%d", &x);
while (x)
{
PUSH(s, x%d);
x /= d;
}
printf("Turn number(10) to number(%d):\n", d);
while ((s->top) > 0)
{
r = POP(s);
if (r >= 0 && r <= 9)
{
printf("%d", r);
}
else
printf("%c", r + 'A' - 10);
}
getchar();
}
/*菜单的内容*/
//定位函数
void gotoxy(int x, int y)
{
COORD Pos;
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
Pos.X = x;
Pos.Y = y;
SetConsoleCursorPosition(hCon, Pos);
}
//颜色函数
int color(int num)
//num为每一种颜色所代表的数字,范围是0~15
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), num);
return 0;
}
void print_1()
{
int i;
printf("%c", 201);
for (i = 0; i < 35; i++)
{
printf("%c", 205);
}
printf("%c\n", 187);
}
void print_()
{
int i;
printf("%c", 199);
for (i = 0; i < 35; i++)
{
printf("%c", 196);
}
printf("%c\n", 182);
}
void print_2()
{
int i;
printf("%c", 200);
for (i = 0; i < 35; i++)
{
printf("%c", 205);
}
printf("%c\n", 188);
}
void menu()
{
print_1();
printf("%c", 186);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15 | 64 | 64 | 128);
printf(" \t Please make a choice\t ");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(1);
printf(" 1.Convert 10 to 2 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(3);
printf(" 2.Convert 10 to 8 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(5);
printf(" 3.Convert 10 to 16 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(8);
printf(" 4.Convert 2 to 10 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(9);
printf(" 5.Convert 8 to 10 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(10);
printf(" 6.Convert 16 to 10 \t ");
color(15);
printf("%c\n", 186);
print_();
printf("%c", 186);
color(14);
printf(" 7.exit the project \t ");
color(15);
printf("%c\n", 186);
print_2();
}
void end()
{
system("cls");
gotoxy(55,15);
system("color 01");
printf("TANKY YOU!");
Sleep(100);
system("color 02");
Sleep(100);
system("color 03");
Sleep(100);
system("color 04");
Sleep(100);
system("color 05");
Sleep(100);
system("color 06");
Sleep(100);
system("color 07");
Sleep(100);
system("color 08");
Sleep(100);
system("color 09");
Sleep(100);
system("color A");
Sleep(100);
system("color B");
Sleep(100);
system("color C");
Sleep(100);
system("color D");
Sleep(100);
system("color E");
Sleep(100);
system("color F");
Sleep(100);
printf("\n");
}