C语言编程题目(2)基本数据类型操作

题目:

0x01 将输入的2进制字符串转换为10进制数输出。

0x02 设计一个复数类型,输入实部和虚部生成一个复数,可进行两个复数求和、两个复数求差、两个复数求积运算。

0x03 用一个整型数组表示10进制大整数,数组的每个元素存储大整数的一位数字,将这个大整数转换为2进制数输出。

0x04 根据输入的数字N,计算N以内(包括N)数据链并统计数据链末尾数字是1的数据个数。例如N=44,则数字链为:44->32->13->10->1,其规则为:4*4+4*4=32,3*3+2*2=13,1*1+3*3=10,1*1+0*0=1。

代码1:

 1 #include <stdio.h>
 2 #include<string.h>
 3 int Bin2Digit(const char *sSrc, int nSrcLen){  //二进制字符串转十进制数字
 4     int num = 0;
 5     for(int i=0;i < nSrcLen;i++)  //从左到右,i是从左开始的索引,nSrcLen是二进制字符串的长度
 6     {
 7         num *= 2;  //乘2相当于左移一位
 8         if(sSrc[i]-'1' == 0)
 9             num += 1;
10         else if(sSrc[i]-'0' == 0)
11             num += 0;
12         else{  // 非0非1,不符合要求
13             printf("字符串存在非法字符!");
14             return -1;
15         }
16     }
17     return num;
18 }
19 int main()
20 {
21     char Binarystring[200];
22     int N = 0;
23     printf("输入二进制字符串:");
24     gets(Binarystring);
25     N = Bin2Digit(Binarystring,strlen(Binarystring));
26     printf("N = %d\n",N);
27     return 0;
28 }

运行结果1:  

    

代码2:

 1 #include <stdio.h>
 2 #include<string.h>
 3 struct complex  //定义复数结构体
 4 {
 5     float real;
 6     float maginary;
 7 };
 8 void PrintComplex(struct complex *member){ //打印复数
 9     if(member->maginary < 0)  //这里的操作是为了输出的美观
10         printf(" %f - %fj \n",member->real,abs(member->maginary));
11     else
12         printf(" %f + %fj \n",member->real,member->maginary);
13 }//复数加法,返回复数结构体
14 struct complex AddComplex(struct complex *member1,struct complex *member2){
15     struct complex child;
16     child.real = member1->real + member2->real; //实部1+实部2
17     child.maginary = member1->maginary + member2->maginary;//虚部1+虚部2
18     //printf("c1 + c2 = ");
19     //PrintComplex(&child);
20     return child;
21 }
22 //复数减法,返回复数结构体
23 struct complex SubComplex(struct complex *member1,struct complex *member2){
24     struct complex child;
25     child.real = member1->real - member2->real;
26     child.maginary = member1->maginary - member2->maginary;
27     return child;
28 }
29 //复数乘法,返回复数结构体
30 struct complex MulComplex(struct complex *member1,struct complex *member2){
31     struct complex child;
32     child.real = (member1->real * member2->real) - (member1->maginary * member2->maginary);
33     child.maginary = (member1->maginary * member2->real) + (member1->real * member2->maginary);
34     return child;
35 }
36 int main(int argc, char const *argv[])
37 {
38     struct complex c1,c2,temp;
39     printf("Input c1 : ");      //分别输入两个结构体的实部、虚部值
40     scanf("%f %f",&c1.real,&c1.maginary);    
41     printf("Input c2 : ");
42     scanf("%f %f",&c2.real,&c2.maginary);
43     printf("c1 is:");           //打印原始的两个复数
44     PrintComplex(&c1);
45     printf("c2 is:");
46     PrintComplex(&c2);
47     temp = AddComplex(&c1,&c2); //复数加,函数结果赋值给中间变量结构体temp
48     printf("c1 + c2 = ");
49     PrintComplex(&temp);
50     temp = SubComplex(&c1,&c2); //复数减,函数结果赋值给中间变量结构体temp
51     printf("c1 - c2 = ");
52     PrintComplex(&temp);
53     temp = MulComplex(&c1,&c2); //复数乘,函数结果赋值给中间变量结构体temp
54     printf("c1 * c2 = ");
55     PrintComplex(&temp);
56     return 0;
57 }

运行结果2:    

    

代码3:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 int my_atoi(char s) //数字字符转数字
 5 {
 6     return s-'0';
 7 }
 8 
 9 int main(){
10     unsigned  long BigNumber=0,num=0;
11     int iArray[10]={0};
12     char sArray[11]={0};
13     char binBuffer[33]={0};
14     int index=0;
15     printf("Input a big number:\t");
16     scanf("%ld",&BigNumber);  //现有用户输入一个数字
17     printf("BigNumber = %d\n", BigNumber); //打印数字
18     itoa(BigNumber,sArray,10); // 十进制 整数转对应的十进制字符串
19      //打印 数字字符串数组 和 长度
20     printf("sArray = '%s' ,length = %d\n",sArray,strlen(sArray));
21     while( index < strlen(sArray) ) //把数字字符串挨个转成整数填入数字数组
22     {
23         iArray[index] = my_atoi(sArray[index]);
24         index += 1;
25     }
26     // 打印整数数组
27     for(index=0;index<strlen(sArray);index++)
28         printf("%d ",iArray[index]);
29     index = 0;
30     while( index < strlen(sArray) )//从整数数组累加恢复开始的整数数字
31     {
32         num *= 10;
33         num += iArray[index];
34         index += 1;
35     }
36     //调用itoa直接得到整数的二进制字符串,参数2代表二进制
37 printf("Number from array is %d, binary string is %s.",num ,itoa(num,binBuffer,2));
38 return 0;
39 }

运行结果3:    

代码4:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 int Calc(int index,int len)  //计算按照规则本数字的下一个数字,len为数字长度
 6 {
 7     int i = 0,res = 0,temp=0; //res累加其结果
 8     while(i < len)
 9     {
10         index %= (int)pow(10,len-i);
11         temp = index / (int)pow(10,len-i-1);
12         res += (int)pow(temp,2);
13         i += 1;
14     }
15     return res;
16 }
17 int print_link(int num,int len) //打印单行列表,num为初始数字,len为其长度
18 {
19     char buf[10];
20     itoa(num,buf,10); //这一步是为了后面求出数字长度
21     while(num > 9)   //打印终止于小于10的数字
22     {
23         printf("%d -->",num);
24         num = Calc(num,strlen(buf));
25         memset(buf,0,sizeof(buf)); 
26         itoa(num,buf,10); 
27     }
28     printf("%d\n", num);
29   return num;
30 }
31 int main(){
32     unsigned int number = 0,temp = 0;
33     char NumBuffer[10]={0};
34     printf("Input a number: ");
35     gets(NumBuffer);       //读取字符串形式整数
36     temp = atoi(NumBuffer); //转成整数
37     while(temp>=10)
38     {
39         if(1 ==print_link(temp,strlen(NumBuffer))) //记录结果为1的数目
40             number++;;
41         temp -= 1;
42     }
43   printf("Number of '1' is %d\n",number);
44     return 0;
45 }

运行结果4:  

    

    

 

posted @ 2019-08-28 11:04  爱吃砂糖橘的白龙  阅读(934)  评论(0编辑  收藏  举报