C/C++基础整理(2)

来源:互联网 发布:局域网视频监控软件 编辑:程序博客网 时间:2024/05/21 15:05
  1. 写出float x 与“零值”比较的if语句。
    if(x>0.000001&&x<-0.000001)
  2. ARP:IP转MAC
    RARP:MAC转IP
    3.IP地址的编码分为哪俩部分?
    IP地址由两部分组成,网络号和主机号。
    4、局部变量能否和全局变量重名?
    答:能,局部会屏蔽全局。要用全局变量,需要使用”::”;或者使用extern.
    局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局变量。对于有些编译器而言,在同一个函数内可以定义多个同名的局部变量,比如在两个循环体内都定义一个同名的局部变量,而那个局部变量的作用域就在那个循环体内
    5、如何引用一个已经定义过的全局变量?
    答:extern
    可以用引用头文件的方式,也可以用extern关键字,如果用引用头文件方式来引用某个在头文件中声明的全局变理,假定你将那个变写错了,那么在编译期间会报错,如果你用extern方式引用时,假定你犯了同样的错误,那么在编译期间不会报错,而在连接期间报错
例://file_1.cppint counter;//file_2.cppextern int counter;//使用file_1中的counter++ counter;

6、static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
static全局变量与普通的全局变量有什么区别:static全局变量只初使化一次,防止在其他文件单元中被引用;
static局部变量和普通局部变量有什么区别:static局部变量只被初始化一次,下一次依据上一次结果值;
static函数与普通函数有什么区别:static函数在内存中只有一份,普通函数在每个被调用中维持一份拷贝
注:static函数与普通函数作用域不同。仅在本文件。只在当前源文件中使用的函数应该说明为内部函数(static),内部函数应该在当前源文件中说明和定义。对于可在当前源文件以外使用的函数,应该在一个头文件中说明,要使用这些函数的源文件要包含这个头文件
7、程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中。
8:函数指针
(1)可用typedef简化函数指针的定义

typedef bool (*cmpFcn)(const string &,const string &);bool lengthCompare(const string &,const string &);

此时,直接引用函数名等效于在函数名上应用取地址操作符

cmpFcn pf1=lengthCompare;cmpFcn pf2=&lengthCompare;
2)cmpFcn pf=lengthCompare;lengthCompare("hi","bye");//直接调用lengthCompare函数pf("hi","bye");//利用函数指针调用lengthCompare函数,未使用*(*pf)("hi","bye");//利用函数指针调用lengthCompare函数,使用*

9、写出下列代码的输出内容

#include<stdio.h>int inc(int a){return(++a);}int multi(int*a,int*b,int*c){return(*c=*a**b);}typedef int(*FUNC1)(int in);typedef int(*FUNC2) (int*,int*,int*);void show(FUNC2 fun,int arg1, int*arg2){FUNC1 p=&inc;int temp =p(arg1);fun(&temp,&arg1, arg2);printf("%d\n",*arg2);}main(){int a;show(multi,10,&a);return 0;}答:110

10、把一个字符串倒序,如“abcd”倒序后变为“dcba”
答:
方法1:

int main(){char* src = "hello,world";int len = strlen(src);char* dest = (char*)malloc(len+1);//要为\0分配一个空间char* d = dest;char* s = &src[len-1];//指向最后一个字符while( len-- != 0 )*d++=*s--;*d = 0;//尾部要加\0printf("%s\n",dest);free(dest);// 使用完,应当释放空间,以免造成内存汇泄露return 0;}

方法2:

```#include <stdio.h>#include <string.h>main(){char str[]="hello,world";int len=strlen(str);char t;for(int i=0; i<len/2; i++){t=str[i]; str[i]=str[len-i-1]; str[len-i-1]=t;}printf("%s",str);return 0;}

11.在c语言库函数中将一个字符转换成整型的函数是atool()吗,这个函数的原型是什么?
函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);

#include <stdlib.h> #include <stdio.h> int main(void) { long l; char *str = "98765432"; l = atol(lstr); printf("string = %s integer = %ld\n", str, l); return(0); }

12:Windows消息调度机制是:消息队列
13: 用宏定义写出swap(x#define swap(x, y)\
x = x + y;\
y = x - y;\
x = x - y;
14:程序输出:

char str1[] = "abc";char str2[] = "abc";const char str3[] = "abc";const char str4[] = "abc";const char *str5 = "abc";const char *str6 = "abc";char *str7 = "abc";char *str8 = "abc";cout << ( str1 == str2 ) << endl;cout << ( str3 == str4 ) << endl;cout << ( str5 == str6 ) << endl;cout << ( str7 == str8 ) << endl;

结果是:0 0 1 1
str1,str2,str3,str4是数组变量,它们有各自的内存空间;
而str5,str6,str7,str8是指针,它们指向相同的常量区域。
15: 以下代码中的两个sizeof用法有问题吗?[C易]

void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母{    for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )        if( 'a'<=str[i] && str[i]<='z' )            str[i] -= ('a'-'A' );}char str[] = "aBcDe";cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;UpperCase( str );cout << str << endl;

答:函数内的sizeof有问题。根据语法,sizeof如用于数组,只能测出静态数组的大小,无法检测动态分配的或外部数组大小。函数外的str是一个静态定义的数组,因此其大小为6,函数内的str实际只是一个指向字符串的指针,没有任何额外的与数组相关的信息,因此sizeof作用于上只将其当指针看,一个指针为4个字节,因此返回4。
16:请问以下代码有什么问题:

char* s="AAA";printf("%s",s);s[0]='B';//只能通过s[0]访问,不能通过s[0]修改printf("%s",s);

有什么错?
“AAA”是字符串常量。s是指针,指向这个字符串常量,所以声明s的时候就有问题。
cosnt char* s=”AAA”;
然后又因为是常量,所以对是s[0]的赋值操作是不合法的。
17:写一个“标准”宏,这个宏输入两个参数并返回较小的一个。
.#define Min(X, Y) ((X)>(Y)?(Y):(X))//结尾没有;
18: 操作系统中进程调度策略有哪几种?
FCFS(先来先服务),优先级,时间片轮转,多级反馈
18:数组和链表的区别
数组:数据顺序存储,固定大小
连表:数据可以随机存储,大小可动态改变
20:已知一个数组table,用一个宏定义,求出数据的元素个数

#define NTBL#define NTBL (sizeof(table)/sizeof(table[0]))

21:读文件file1.txt的内容(例如):
12
34
56
输出到file2.txt:
56
34
12
(逆序)
答:

#include <stdio.h>void test(FILE *fread, FILE *fwrite){        char buf[1024] = {0};        if (!fgets(buf, sizeof(buf), fread))                return;        test( fread, fwrite );        fputs(buf, fwrite);}int main(int argc, char *argv[]){        FILE *fr = NULL;        FILE *fw = NULL;        fr = fopen("data", "rb");        fw = fopen("dataout", "wb");        test(fr, fw);        fclose(fr);        fclose(fw);        return 0;}

22:输出和为一个给定整数的所有组合
例如n=5
5=1+4;5=2+3(相加的数不能重复)
则输出
1,4;2,3。
答:

#include <stdio.h>int main(void){unsigned long int i,j,k;printf("please input the number\n");scanf("%d",&i);    if( i % 2 == 0)        j = i / 2;elsej = i / 2 + 1;printf("The result is \n");    for(k = 0; k < j; k++)     printf("%d = %d + %d\n",i,k,i - k);return 0;}#include <stdio.h>void main(){unsigned long int a,i=1;scanf("%d",&a);if(a%2==0){     for(i=1;i<a/2;i++)     printf("%d",a,a-i);}elsefor(i=1;i<=a/2;i++)        printf(" %d, %d",i,a-i);}

23:写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k)
要求算法复杂度不能是O(n^2)
谢谢!
可以先用快速排序进行排序,其中用另外一个进行地址查找
代码如下,在VC++6.0运行通过。给分吧^-^

//快速排序

#include<iostream>usingnamespacestd;intPartition (int*L,intlow,int high){inttemp = L[low];intpt = L[low];while (low < high){while (low < high && L[high] >= pt)--high;L[low] = L[high];while (low < high && L[low] <= pt)++low;L[low] = temp;}L[low] = temp;returnlow;}voidQSort (int*L,intlow,int high){if (low < high){intpl = Partition (L,low,high);QSort (L,low,pl - 1);QSort (L,pl + 1,high);}}intmain (){intnarry[100],addr[100];intsum = 1,t;cout << "Input number:" << endl;cin >> t;while (t != -1){narry[sum] = t;addr[sum - 1] = t;sum++;cin >> t;}sum -= 1;QSort (narry,1,sum);for (int i = 1; i <= sum;i++)cout << narry[i] << '\t';cout << endl;intk;cout << "Please input place you want:" << endl;cin >> k;intaa = 1;intkk = 0;for (;;){if (aa == k)break;if (narry[kk] != narry[kk + 1]){aa += 1;kk++;}}cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;cout << "And it's place is:" ;for (i = 0;i < sum;i++){if (addr[i] == narry[sum - kk])cout << i << '\t';}return0;}

24::找错

Void test1(){char string[10];char* str1="0123456789";strcpy(string, str1);// 溢出,应该包括一个存放'\0'的字符string[11]}
Void test2(){char string[10], str1[10];for(I=0; I<10;I++){str1[i] ='a';}strcpy(string, str1);// I,i没有声明。}

25.:写出程序运行结果

int sum(int a){auto int c=0;static int b=3;c+=1;b+=2;return(a+b+c);}void main(){int I;int a=2;for(I=0;I<5;I++){printf("%d,", sum(a));}}// static会保存上次结果

输出:8,10,12,14,16,

26:用递归算法判断数组a[N]是否为一个递增数组。
递归的方法,记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束:

bool fun( int a[], int n ){if( n= =1 )return true;if( n= =2 )return a[n-1] >= a[n-2];return fun( a,n-1) && ( a[n-1] >= a[n-2] );}

27:线形表a、b为两个有序升序的线形表,编写一程序,使两个有序线形表合并成一个有序升序线形表h;
答案在 请化大学 严锐敏《数据结构第二版》第二章例题,数据结构当中,这个叫做:两路归并排序

Linklist *unio(Linklist *p,Linklist *q){linklist *R,*pa,*qa,*ra;pa=p;qa=q;R=ra=p;while(pa->next!=NULL&&qa->next!=NULL){if(pa->data>qa->data){ra->next=qa;qa=qa->next;}else{ra->next=pa;pa=pa->next;}}if(pa->next!=NULL)ra->next=pa;if(qa->next!=NULL)ra->next==qa;return R;}

28: 请问一下程序将输出什么结果?

char *RetMenory(void){       char p[] = “hellow world”;       return p;}void Test(void){       char *str = NULL;       str = RetMemory();       printf(str);}

RetMenory执行完毕,p资源被回收,指向未知地址。返回地址,str的内容应是不可预测的, 打印的应该是str的地址
29:单连表的建立,把’a’–‘z’26个字母插入到连表中,并且倒叙,还要打印!

typedef struct val{   int date_1;    struct val *next;}*p;void main(void){   char c;         for(c=122;c>=97;c--)       { p.date=c;         p=p->next;        }}

30:找出错误

 #include   string.hmain(void){   char   *src="hello,world";    char   *dest=NULL;    dest=(char   *)malloc(strlen(src));    int   len=strlen(str);    char   *d=dest;    char   *s=src[len];    while(len--!=0)      d++=s--;    printf("%s",dest);}

找出错误!!

#include   "string.h"#include "stdio.h"#include "malloc.h"main(void){   char   *src="hello,world";    char   *dest=NULL;    dest=(char   *)malloc(sizeof(char)*(strlen(src)+1));    int   len=strlen(src);    char   *d=dest;    char   *s=src+len-1;    while(len--!=0)      *d++=*s--;*d='\0';    printf("%s",dest);}
0 0
原创粉丝点击