《考研机试》(一)C/C++基础

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

《考研机试》(一)C/C++基础

喵喵与呱呱   2020-02-13 我要评论

1.setfill/setw使用

2.定义结构体

3.关于字符串读取

4.排序问题:复试不要求一般用:冒泡排序

5.数字和字符之间转换

6.进制转化:10进制转8进制

7.质数判断

8.字符串拷贝函数strcpy

9.字符串拼接函数strcat

10.字符串比较函数strcmp

11.计算字符串长度函数strlen

#include <iostream>
#include <iomanip> 
#include <math.h>
#include <string.h>
using namespace std;

int main(void){
//	1.setfill/setw使用 
//	float f1 = 2.99;
//	float f2 = 8.9099;
//	int i = 10;
//	cout << setfill('*');
//	//setw是设置域宽,就是输出数据所占几列
//	//如果在默认的情况下,输出的数据不能达到所规定的域宽就用空格填充
//	//setfill是设置填充物 
//	//使用操纵符时,需要包含头文件iomanip 
//	cout << setw(10) << f1 << endl;
//	cout << setw(10) << f2 << endl;
//	cout << setw(10) << i << endl;

//	2.定义结构体
//	struct Student{
//		int id;
//		char name[20];
//	}; 
//	可以使用typedef添加别名
//	typedef struct Student{
//		int id;
//		char name[20];
//	}Student;
//	
//	使用:Student s;  

//	3.关于字符串读取 
//	string str = "hello gy";
//	int i = 0;
//	while(str[i] != '\0'){
//		cout << str[i] << endl;
//		i++;
//	}

//	4.排序问题
//	复试不要求一般用:冒泡排序
//	int len = 6; 
//	int num[] = {5,2,77,1,99,66};
//	for(int i=0; i<len-1; i++){
//		for(int j=0; j<len-1-i; j++){
//			if( num[j]>num[j+1] ){
//				int temp = num[j];
//				num[j] = num[j+1];
//				num[j+1] = temp;
//			}
//		}
//	}
//	for(int i=0; i<len; i++){
//		cout << num[i] << " ";
//	} 

//	5.数字和字符之间转换
//	    例如:将字符'7'转为数字7 
//		char a = '7';
//		int a_num = a - '0';
//		例如:将数字 5 转换成字符'5' 
//		int b_num = 5;
//		char b = b_num + '0';

//  6.进制转化:10进制转8进制
//  int num = 666;
//	int result[100];
//	int len = 0;
//	while(num/8 != 0){
//		result[len] = num%8;
//		len++;
//		num /= 8;
//	}
//	result[len] = num;
//	for(int i=len; i>=0; i--){
//		cout << result[i] <<endl;
//	}

//	7.质数判断
//	int num = 10;
//	int temp = sqrt(num);
//	bool isZhiShu = true;//默认是质数 
//	for(int i=2; i<=temp; i++){
//		if( num%i==0 ){
//			isZhiShu = false;//不是质数 
//			break; 
//		}	
//	}
//	if(isZhiShu){
// 	    cout << "是质数" << endl;	
//	}else{
//		cout << "不是质数" << endl;
//	}

//	8.字符串拷贝函数strcpy
//	char *strcpy(char *dest, const char *src);
//	将参数 src 字符串拷贝至参数 dest 所指的地址
//	char string[10];
//	char *str1 = "abcdefgh";
//	//将str1的内容拷贝给string数组 
//	strcpy(string, str1); 
//	printf("%s\n", string);

//	9.字符串拼接函数strcat
//	char *strcat(char *dest, const char *src);
//	作用:返回dest字符串起始地址,并且dest = dest+src 
//	char str[20];
//	char* str1 = "gyy";
//	char* str2 = "wgg";
//	strcat(str, str1);
//	strcat(str, str2);
//	printf("%s\n", str);

//	10.字符串比较函数strcmp
//	int strcmp(const char *s1, const char *s2);
//	若s1==s2返回0;s1>s2返回大于0;s1<s2返回小于0
//	char *a = "aBcDeF";
//	char *b = "AbCdEf";
//	char *c = "aacdef";
//	char *d = "aBcDeF"; 
//	printf("%d\n",strcmp(a,b)); 
//	printf("%d",strcmp(a,d));
	
//	11.计算字符串长度函数strlen
//	计算指定的字符串长度,不包括结束字符'\0' a
// 	size_t strlen(const char *s);
// 	返回字符串s的字符数
//	但是sizeof返回的是变量声明后所占的内存数,不是实际长度
// 	char str[5] = "abcd";
// 	cout << strlen(str) << endl;//4
// 	cout << sizeof(str) << endl;//5
		
	return 0;
}
 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们