C++入门

C++控制台程序 win32报错

这个文件中使用了WinMian而不是main作为入口函数。vc这时的默认设置是针对控制台程序的。
解决方法:
1.进入project->setting->c/c++, 在category(第一行)中选择preprocessor,在processor definitions中删除_WINDOWS, 添加_CONSOLE

2.进入project->setting->Link, 在Project options中将/subsystem:windows.改为/subsystem:console

3.保存设置,Rebuild All.

 

 

简单类构建

 

 #include <iostream.h>
//结构体 默认是public 不能使用安全修饰符
struct Point{
 int x;
 int y;
 void print()
 {
  cout<<x<<endl<<y<<endl;
 }
};
//类默认是private  写一个public:它下面的属性方法都是public 上面的就是private
class PointClass{
 public:int x;
 public:int y;
 public:PointClass()
 {
    x=0;
    y=0;
 }
 //this是影藏的指针 指针使用->符号
 public:PointClass(int a,int b)
 {
    this->x=a;
    this->y=b;
 }
 //一个类只有一个析构函数 没有参数和返回值
   ~PointClass(){
   }
 public:void print()
 {
  cout<<x<<endl<<y<<endl;
 }
 //有缺省值调用时 可以pointp.add(); 亦可以 pointp.add(1);
 public:void add(int i=3){
  cout<<i<<endl;
 }
 //无缺省值调用时 只可以 pointp.min(1);
 public:void min(int i){
  cout<<i<<endl;
 }
};
void main()
{
   Point point;
   point.x=4;
   point.y=6;
   point.print();
   PointClass pointc;
   pointc.print();
   pointc.x=4;
   pointc.y=6;
   pointc.print();
   PointClass pointp(1,2);
   pointp.print();
   pointp.add();
   pointp.min(1);
}

常用的c语言的函数:

字符串的处理:

#include "string.h"
#include <IOSTREAM>
using namespace std;
void main1(){
char str[]="testhelloworl/d";
//----------字符串的常用处理函数
//获取字符串的长度
int length=strlen(str);
cout<<length<<endl;
//截取字符串 strncpy将一个数组的元素拷贝到另一个字符串数组 strncpy(目标指针,源字符串指针,长度)
char* deststr=new char[2];
strncpy(deststr,str+2,2);
cout<<deststr<<endl;
//字符串转换大小写 指针定义的字符串无法转换 char *a="" 不能转换  使用数组定义的可以转换  char a[]="abc"
//strupr会直接将字符串本身也转换成大写
strupr(deststr);
cout<<deststr<<endl;
cout<<strupr(str)<<endl;
    //字符串的切割 如果想找到下一个字符  就是 strtok(NULL,"/")
char* split=strtok(str,"/");
cout<<split<<strtok(NULL,"/")<<endl;
//字符串的比较 相等结果为0  不相等结果为-1 
cout<<strcmp("123","123")<<endl;
//判断某个字符中是否包含另外一个字符 返回值是第二个值开始的位置的指针
cout<<strstr("abc_bcd","_")<<endl;

}

数字处理

#include "string.h"
#include <IOSTREAM>
using namespace std;
void main1(){
char str[]="testhelloworl/d";
//----------字符串的常用处理函数
//获取字符串的长度
int length=strlen(str);
cout<<length<<endl;
//截取字符串 strncpy将一个数组的元素拷贝到另一个字符串数组 strncpy(目标指针,源字符串指针,长度)
char* deststr=new char[2];
strncpy(deststr,str+2,2);
cout<<deststr<<endl;
//字符串转换大小写 指针定义的字符串无法转换 char *a="" 不能转换  使用数组定义的可以转换  char a[]="abc"
//strupr会直接将字符串本身也转换成大写
strupr(deststr);
cout<<deststr<<endl;
cout<<strupr(str)<<endl;
    //字符串的切割 如果想找到下一个字符  就是 strtok(NULL,"/")
char* split=strtok(str,"/");
cout<<split<<strtok(NULL,"/")<<endl;
//字符串的比较 相等结果为0  不相等结果为-1 
cout<<strcmp("123","123")<<endl;
//判断某个字符中是否包含另外一个字符 返回值是第二个值开始的位置的指针
cout<<strstr("abc_bcd","_")<<endl;

}

日期处理

#include "stdio.h"
#include<stdlib.h>
#include "string.h"
#include "time.h"
#include <IOSTREAM>
using namespace std;
void main3(){
time_t tt;
//获取从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起到现在所经过的秒数
time(&tt);
cout<<tt<<endl;
/************************************************************************/
/* struct tm{
int tm_sec;  //代表目前秒数, 正常范围为0-59, 但允许至61 秒
int tm_min;  //代表目前分数, 范围0-59
int tm_hour;  //从午夜算起的时数, 范围为0-23
int tm_mday;  //目前月份的日数, 范围01-31
int tm_mon;  //代表目前月份, 从一月算起, 范围从0-11
int tm_year;  //从1900 年算起至今的年数
int tm_wday;  //一星期的日数, 从星期一算起, 范围为0-6
int tm_yday;  //从今年1 月1 日算起至今的天数, 范围为0-365
int tm_isdst;  //日光节约时间的旗标
};                                                                     */
/************************讲time_t的long类型值转换成tm日期结构************************************/
tm *tmtmp =localtime(&tt);
tm *tmtmp1=gmtime(&tt);//这种方式也可以
//显示格式 yyyy-MM-dd hh:mm:ss
cout<<tmtmp->tm_year+1900<<"-"<<tmtmp->tm_mon+1<<"-"<<tmtmp->tm_mday
<<" "<<tmtmp->tm_hour<<":"<<tmtmp->tm_min<<":"<<tmtmp->tm_sec<<endl;
cout<<tmtmp1->tm_year+1900<<"-"<<tmtmp1->tm_mon+1<<"-"<<tmtmp1->tm_mday
<<" "<<tmtmp1->tm_hour<<":"<<tmtmp1->tm_min<<":"<<tmtmp1->tm_sec<<endl;
//格式化成 比如 3月 自动 变成03
printf("%04d-%02d-%02d %02d:%02d:%02d\r\n",tmtmp1->tm_year+1900,tmtmp1->tm_mon+1,tmtmp1->tm_mday,tmtmp1->tm_hour,tmtmp1->tm_min,tmtmp1->tm_sec);
//格式化的日期写入到变量上
char* formatDate=new char[20];
sprintf(formatDate,"%04d-%02d-%02d %02d:%02d:%02d",tmtmp1->tm_year+1900,tmtmp1->tm_mon+1,tmtmp1->tm_mday,tmtmp1->tm_hour,tmtmp1->tm_min,tmtmp1->tm_sec);
cout<<formatDate<<endl;

//转换为GMT的日期
char* ctt=ctime(&tt);
cout<<ctt<<endl;
}

类型转换

#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <IOSTREAM>
using namespace std;
void main(){
char* ab=new char[10];
//long数字转换成字符串 参数1 被转换成字符串的值  参数2 转换后值被写入的字符串指针  第三个表示进制
char* lstr=ltoa(123,ab,10);
cout<<ab<<lstr<<endl;
//int数字转换成字符串 参数1 被转换成字符串的值  参数2 转换后值被写入的字符串指针  第三个表示进制
char * kk=itoa(11,ab,10);
cout<<kk<<endl;
//字符串转换成数字类型
int val=atoi("888");
cout<<val<<endl;
char* envvalue=getenv("PATH");
cout<<envvalue<<endl;
    putenv("TEST=abc");
}

//调用本地dos命令
system("ipconfig /all");

原文链接: https://www.cnblogs.com/liaomin416100569/archive/2010/06/09/9331647.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++入门

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/11651

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 上午10:21
下一篇 2023年2月7日 上午10:23

相关推荐