您的位置:首页 > 理论基础

华科10年计算机考研复试机试

2010-07-10 16:58 519 查看
先说题:

共三道题,1.5小时,C语言实现。时间很紧啊....

a、输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数!(如‘3’,输出:10110011);

b、设计8个任务函数task0()-task7()只输出一句话:如task0()输出“task0 is called!”;设计一个调度函数schedule()输入一个字符串如"012345"然后返回一个函数指针数组和字符串的长度作为执行函数execute()的参数进行调度任务函数。主函数只有调度函数。

c、实现一个加法器,保证两个加数是任意长的整数。

参考程序:

a、

#include<stdio.h>
#include<stdlib.h>
int f(int n){
int c;
for(c=0;n!=0;c++)
n=n&(n-1);
return c;
}
int main(){
char str[1000];
for(;scanf("%s",str)!=EOF;){
for(int i=0;str[i];i++){
int x=f(str[i]);
printf("%d",x&1?0:1);
for(int j=6;j>=0;j--){
printf("%d",(str[i]>>j)&1);
}
}
}
system("pause");
}


数据测试:





b、仅供参考。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef void (*ptask)();
//8个任务函数;
void task0(){
printf("task0 is called!/n");
}
void task1(){
printf("task1 is called!/n");
}
void task2(){
printf("task2 is called!/n");
}
void task3(){
printf("task3 is called!/n");
}
void task4(){
printf("task4 is called!/n");
}
void task5(){
printf("task5 is called!/n");
}
void task6(){
printf("task6 is called!/n");
}
void task7(){
printf("task7 is called!/n");
}
ptask fun[9]={task0,task1,task2,task3,task4,task5,task6,task7};

void execute(ptask* fun,int len){//执行函数
for(int i=0;i<len;i++){
ptask pfun=fun[i];
pfun();
}
}

void schedule(){//调度函数;
ptask fun[100];//定义函数指针数组;
int len;//字符串长度;
char s[1000];
printf("请输入字符串:/n");
scanf("%s",s);
len=strlen(s);
for(int i=0;i<len;i++){
int temp;
temp=s[i]-'0';
if(temp==0)fun[i]=task0;
else if(temp==1)fun[i]=task1;
else if(temp==2)fun[i]=task2;
else if(temp==3)fun[i]=task3;
else if(temp==4)fun[i]=task4;
else if(temp==5)fun[i]=task5;
else if(temp==6)fun[i]=task6;
else if(temp==7)fun[i]=task7;
}
execute(fun,len);
}

int main(){
schedule();
system("pause");
}


数据测试:



c、

#include <iostream>
#include <string>
using namespace std;
string Addition(string one, string two);
int main()
{
int n, i;
string first, second;
cout << "Please enter two numbers: ";
cin >> first >> second;
cout << "The result is: " << Addition(first, second) << endl;
//return 0;
system("pause");
}
string Addition(string one, string two)
{
int j, max;
//分别表示第一,二个字符串的长度
int flen, slen;
string sum;
flen = one.size();
slen = two.size();
if(flen >= slen)
{
sum = one;
for(j=0;j<slen;j++)
sum[flen-j-1] = sum[flen-j-1] + two[slen-j-1] - '0';
max = flen;
}
else
{
sum = two;
for(j=0;j<flen;j++)
sum[slen-j-1] = sum[slen-j-1] + one[flen-j-1] - '0';
max = slen;
}
//如果第j位的数大于9,则将其减10,并向上一位进一
for(j=max-1;j>=1;j--)
{
if(sum[j] > '9')
{
sum[j] -= 10;
sum[j-1]++;
}
}
if(sum[0] > '9')
{
sum[0] -= 10;
sum = "1" + sum;
}
return sum;
}


数据测试:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: