c++ 练习

字符串的反转:

#include<iostream>
#include<cstring>
using namespace std;
void mystrrev(char * string){
	int len=strlen(string);
	char *p=string,*q=string+len-1;
	char tmp;
	for(int i=0;i<len/2;i++){
		tmp=*p;
		*p=*q;
		*q=tmp;
		p++;
		q--;
	}
}
int main(){
	char str[100];
	cout<<"input string:";
	cin>>str;
	mystrrev(str);
	cout<<"output string:"<<str<<endl;
	return 0;
}


文本读取查询规范:

#include<iostream>
#include<fstream>
using namespace std;

int main(){
	char id[12],str[80];
	bool name,cla;
	ifstream in;
	in.open("data.txt");
	if(!in){
		cout<<"cannot open file.";
		return 1;
	}
	while(in){
		int i=0;
		in.getline(str,80);
		while(str[i]!=','){
			id[i]=str[i];
			i++;
		}
		id[i]=0;
		i++;
		name=false,cla=false;
		while(str[i]!=','){
			if(str[i]!=' ' && str[i]!= '\t' ) name=true;
			i++;
		}
		i++;
			while(str[i]!=0){
				if(str[i]!=' ' && str[i]!='/t') cla=true;
				i++;
			}
			if(in && !name && cla) cout<<"学生"<<id<<":"<<"缺少姓名信息"<<endl;
			if(in && name && !cla) cout<<"学生"<<id<<":"<<"缺少班级信息"<<endl;
			if(in && !name && !cla) cout<<"学生"<<id<<":"<<"缺少姓名,班级信息"<<endl;
			
		}
		cout<<endl;
		in.close();
	
	return 0;
}

data.txt中的内容:

11111111,,1班
22222222,xxx,
33333333,,2班
44444444,zzz,3班
55555555,,


转载的动态内存分配:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* upcase(char *inputstring);
                                                                                                               
int main(void){
    char *str1;
    str1 = upcase("hello");
    printf("str1=%s \n",str1);
    free(str1);
    return 0;
}

char* upcase(char *inputstring)
{
    char *newstring;
    int counter;

    if(!(newstring=(char *) malloc(strlen(inputstring)+5)))
    {   
        printf("Error malloc !\n");
        exit(1);
    }   

    strcpy(newstring,inputstring);
    for(counter =0; counter < strlen(newstring); counter++){
        if(newstring[counter] >= 97 && newstring[counter] <=122)
        {   
            newstring[counter] -= 32; 
        }   
    }   

    return newstring;

}








### C++ 编程练习题及相关学习资源 以下是关于C++编程的一些常见练习题目以及推荐的学习资源: #### 常见的C++编程练习题 1. **百钱买百鸡问题** 下面是一个经典的“百钱买百鸡”问题实现代码,通过三重循环遍历可能的情况并找到符合条件的结果[^1]。 ```cpp #include <iostream> using namespace std; int main() { for (int a = 0; a <= 20; a++) for (int b = 0; b <= 33; b++) for (int c = 0; c <= 100 - a - b; c += 3) if ((a + b + c == 100) && (5 * a + 3 * b + c / 3 == 100)) cout << a << " " << b << " " << c << endl; return 0; } ``` 2. **继承与多态的应用** 这里提供了一个基于继承的经典例子——定义`Mammal`类作为基类,并派生出子类`Dog`。该程序展示了构造函数和析构函数的调用顺序[^2]。 ```cpp #include <iostream> using namespace std; class Mammal { public: Mammal() { cout << "Mammal Constructor Called." << endl; } ~Mammal() { cout << "Mammal Destructor Called." << endl; } }; class Dog : public Mammal { public: Dog() { cout << "Dog Constructor Called." << endl; } ~Dog() { cout << "Dog Destructor Called." << endl; } }; int main() { Dog d; return 0; } ``` 3. **其他经典练习** 可以参考一些常见的C++练习题集锦,例如《50道C++编程练习题》[^3]。这些题目涵盖了基础语法、数组操作、指针应用等多个方面。 --- #### 推荐的学习资源 1. **在线教程网站** - LeetCode 和 Codeforces 提供丰富的算法训练题库,适合提高逻辑思维能力。 - GeeksforGeeks 是一个专注于计算机科学和技术领域知识分享的平台,其中有许多针对初学者到高级开发者的C++相关内容。 2. **书籍推荐** - 《C++ Primer》是一本非常全面且深入浅出介绍C++语言特性的书。 - Stroustrup 的原著《The C++ Programming Language》被认为是权威指南之一。 3. **视频课程** - YouTube 上有大量免费的教学视频系列可供选择,比如由 MyCodeSchool 或 TheCherno 制作的内容。 - Coursera 和 edX 平台上也有许多大学开设的相关课程,部分支持付费获取证书。 4. **实践工具** 使用 Visual Studio、CLion 等集成开发环境可以帮助更高效地编写调试代码;而 GCC/G++ 则是最常用的编译器选项之一。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值