#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void jia(int x,int y)
{
printf("%d\n",x+y)
}
void jian(int x,int y)
{
printf("%d\n",x-y)
}
void cheng(int x,int y)
{
printf("%d\n",x*y)
}
int chu(int x,int y)
{
if(y==0)
printf("除数不能为0!!!\n")
printf("%d\n",x/y)
}
void fun(int x,int y,int d)//x为分子,y为分母,d为精确位数
{
int temp
printf("%d.",x/y)//整数部分
while(d>0)
{
x=x%y
x*=10
printf("%d",x/y)
d--
}
}
void showmenu()
{
printf("\t\t欢迎使用傻瓜计算机,请输入整型数据!\n")
printf("\t\t\t代码刚好75行,啊哈哈哈哈哈。。。\n")
printf("1、加\t2、减\t3、乘\t4、除\t5、精确分数\t其他字符默认退出\n")
printf("\t请选择要进行的 *** 作:")
}
int main()
{
int i=0,x=1,y=1,d=1
while(1)
{
system("cls")
showmenu()
scanf("%d",&i)
switch(i)
{
case 1:printf("请输入两个加数:")
scanf("%d%d",&x,&y)
printf("结果:")jia(x,y)break
case 2:printf("请输入两个减数:")
scanf("%d%d",&x,&y)
printf("结果:")jian(x,y)break
case 3:printf("请输入两个乘数:")
scanf("%d%d",&x,&y)
printf("结果:")cheng(x,y)break
case 4:printf("请输入除数和被除数:")
scanf("%d%d",&x,&y)
printf("结果:")chu(x,y)break
case 5:printf("请输入分子,分母,精确位数:")
scanf("%d%d%d",&x,&y,&d)
printf("结果:")fun(x,y,d)break
default :printf("欢迎下次使用,byby-ee!")exit(0)
}
printf("按任意键继续!")getch()
}return 0
}
#include <stdio.h>#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define LIM_LEN 30
void bad_exit(void)
/*-----十六进制字符串转化为十进制-------chapter2_2.3---------------*/
/*-------首先定义一个返回值为int型的幂函数pow1-------*/
int pow1(int m,int n)
{
int i
int x = 1
for(i = 0i < ni++)
{
x *= m
}
return x
}
/*-------首先定义一个返回值将十六进制转化为int型的函数htoi-------*/
int htoi(char s[])
{
int n,m,i
int count = 0
int mzs = 0 //控制幂函数的指数
char c
int ch[LIM_LEN]
enum mark {NO,YES} //定义标记符号
enum mark flag
i = 0
n = strlen(s) //字符串的长度,属于string.h头文件库中
//printf("n=%d\n",n)
if(s[i] == '0') //除去字符串中的0X或者0x
{
i++
if(s[i] == 'x' || s[i] == 'X')
{
i++
}
}
//printf("i=%d\n",i)
//printf("%d\n",i)
flag = YES
//printf("%d\n",YES)
for(i,m = 0i < n && flag == YESi++) //将字符串转化为相应的十六进制数字并放入一个int数组中
{
c = s[i]
if(isdigit(c))
{
ch[m] = s[i] - '0'
}
else if(c >= 'a' && c <= 'f')
{
ch[m] = s[i] - 'a' + 10
}
else if(c >= 'A' && c <= 'F')
{
ch[m] = s[i] - 'A' + 10
}
else
{
flag = NO
atexit(bad_exit) //参数为函数指针,虽然在内部定义但是还需要在文件中函数声明,要不就是错误
exit(EXIT_FAILURE) //参数EXIT_FAILURE和EXIT_SUCCESS
//puts("the input isn't 0-9 or a-f!") //打印但是会计算一个溢出错误值,上述方法可避免
}
m++
}
//printf("i=%d\n",i)
//printf("m=%d\n",m)
m = m-1 //m多计数一次,需要退回一次--------------->每次一定考虑是否多计数!!!
for(mm >= 0m--) //累加来求出int型的值
{
count += ch[m] * pow1(16,mzs)
mzs++
}
return count
}
/*-------错误退出注册函数给atexit()使用-------*/
void bad_exit(void)
{
puts("the input isn't 0-9 or a-f!")
}
/*-------首先定义一个返回值将十六进制转化为int型的函数htoi的驱动函数-------*/
void htoi_test()
{
int count
char n_nums[] = "0X12a" //298
count = htoi(n_nums)
printf("%d\n",count)
}
/*-----十六进制字符串转化为十进制-------chapter2_2.3------over---------*/
typedef struct Node {char a
struct Node *lchild
struct Node *rchild
}BiNode, *BiTree
void PosTraverse(BiTree T) {
if (T == NULL) return
PosTraverse(T->lchild)
PosTraverse(T->rchild)
printf("%c", T->a)
}
void BuildTree(char *pre, char* mid, BiTree root) {
int len = strlen(pre)
if (0 == len) return
char *p = strchr(mid, pre[0])
int pos = p - mid
root->a = pre[0]
root->lchild = root->rchild = NULL
if (pos != 0) {
BiTree left
root->lchild = (BiTree)malloc(sizeof(BiNode))
left = root->lchild
char *left_pre = (char*)malloc(sizeof(char) * (pos + 1))
char *left_mid = (char*)malloc(sizeof(char) * (pos + 1))
strncpy(left_pre, pre + 1, pos)
strncpy(left_mid, mid, pos)
left_pre[pos] = 0
left_mid[pos] = 0
BuildTree(left_pre, left_mid, left)
}
if (pos != len - 1) {
BiTree right
root->rchild = (BiTree)malloc(sizeof(BiNode))
right = root->rchild
char *right_pre = (char*)malloc(sizeof(char) * (len - pos))
char *right_mid = (char*)malloc(sizeof(char) * (len - pos))
strncpy(right_pre, pre + 1 + pos, len - pos - 1)
strncpy(right_mid, mid + 1 + pos, len - pos - 1)
right_pre[len - pos - 1] = 0
right_mid[len - pos - 1] = 0
BuildTree(right_pre, right_mid, right)
}
}
int main() {
BiNode temp
BuildTree("abcdefg", "cbdafge", &temp)
PosTraverse(&temp)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)