浙江大学计算机系硕士研究生复试题目解答(1)

来源:互联网 发布:怎么去演艺公司 知乎 编辑:程序博客网 时间:2024/05/01 10:43
 
题目要求:
读入两个小于100的正整数AB,计算A+B。需要注意的是:AB的每一位数字由对应的英文
单词给出。
具体的输入输出格式规定如下:
输入格式:测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两
字符串有一个空格间隔。当AB同时为0时输入结束,相应的结果不要输出。
输出格式:对每个测试用例输出1行,即A+B的值。

输入样例:

one + two
three four + five six
zero seven + eight nine
zero + zero

输出样例:

The result is 3
The result is 90
The result is 96
附加要求:
(1)不能用string.h及其相关库函数
(2)出错检测机制

 

源代码如下:

#include "stdio.h"
#define maxnum 32
#define error -1
int strcompare(char *);
int numcompare(char *);
int compare(char *m,char *c);
int Done();
int main()
{
int i;
do
{
i=Done();
}
while(i);
return 1;
}
int Done()
{
char str1[maxnum/2]={'/0'},str2[maxnum/2]={'/0'},temp;
int i,front=0,low=0,status=0,x,y;
printf("Please enter your formula,end with command 'zero+zero'/n");
for(i=0;i<maxnum;i++)
{
scanf("%c",&temp);
if(temp=='/n')
break;
if(temp=='+')
{
status=1;
continue;
}
if(status==0)/* number 0 or 1 stands for the front part of the formula */
{
str1[front]=temp;
front++;
}
else
if(status==1)
{
str2[low]=temp;
low++;
}
}
if((x=strcompare(str1))!=error&&(y=strcompare(str2))!=error)
{
if((x+y)!=0)
printf("The result is:%d/n",x+y);
return x+y;
}
else
printf("Invalid input!/n");
}
int strcompare(char *c)
{
char str1[maxnum/4]={'/0'},str2[maxnum/4]={'/0'};
int i,temp,tmp,status=1,front=0,low=0,mid=0;/*the number & status of the input*/
for(i=0;i<maxnum/2;i++)/*abstract the string */
{
if(*c=='/0')
break;
if(*c==' ')
{
mid=1;
c++;
}
if(mid==0)
{
str1[front]=*c;
front++;
c++;
}
else
if(mid==1)
{
status=2;
str2[low]=*c;
low++;
c++;
}
}
if(status==1)
{
if((temp=numcompare(str1))!=error)
return temp;
else
return error;
}
else
if(status==2)
{
if((temp=numcompare(str1))!=error&&(tmp=numcompare(str2))!=error)
return temp*10+tmp;
else
return error;
}
}
int numcompare(char *c)
{
char zero[]="zero";
char one[]="one";
char two[]="two";
char three[]="three";
char four[]="four";
char five[]="five";
char six[]="six";
char seven[]="seven";
char eight[]="eight";
char nine[]="nine";
int i;
if(*c=='z')
{
if(compare(c,zero))
return 0;
else
return error;
}
else
if(*c=='o')
{
if(compare(c,one))
return 1;
else
return error;
}
else
if(*c=='t'&&*(c+1)=='w')
{
if(compare(c,two))
return 2;
else
return error;
}
else
if(*c=='t'&&*(c+1)=='h')
{
if(compare(c,three))
return 3;
else
return error;
}
else
if(*c=='f'&&*(c+1)=='o')
{
if(compare(c,four))
return 4;
else
return error;
}
else
if(*c=='f'&&*(c+1)=='i')
{
if(compare(c,five))
return 5;
else
return error;
}
else
if(*c=='s'&&*(c+1)=='i')
{
if(compare(c,six))
return 6;
else
return error;
}
else
if(*c=='s'&&*(c+1)=='e')
{
if(compare(c,seven))
return 7;
else
return error;
}
else
if(*c=='e')
{
if(compare(c,eight))
return 8;
else
return error;
}
else
if(*c=='n')
{
if(compare(c,nine))
return 9;
else
return error;
}
else
return error;
}
int compare(char *c,char *t)
{
for(;*c!=0;c++,t++)
if(*c!=*t)
break;
if(*c=='/0'&&*t=='/0')
return 1;
else
return 0;

原文地址:http://blog.csdn.net/ctu_85/archive/2006/10/15/1334936.aspx