目录
sizeof
运算符 &
void pointerDemo(void)
{
int i = 0;
int p;
p = (int)&i;
printf("0x%x\n", p);
printf("%p\n", &i);
printf("%lu\n", sizeof(int)); //32位系统为4 64位系统为4
printf("%lu\n", sizeof(&i));//32位系统为4 64位系统为8
}
数组与地址:
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[10];
printf("%p\n", &a);
printf("%p\n", a);
printf("%p\n", &a[0]);
printf("%p\n", &a[1]);
return 0;
}
运行结果:
000000000069FE20
000000000069FE20
000000000069FE20
000000000069FE24
--------------------------------
Process exited after 0.1564 seconds with return value 0
scanf
指针
作为参数的指针
访问那个地址上的变量*
传入函数的数组成了什么?
int isPrime(int x, int knownPrimes[], int numberOfKonwPrimes){
//...
}
数组参数
数组变量是特殊的指针
字符类型
字符的输入输出
#include <stdio.h>
int main(int argc, char *argv[])
{
char c;
scanf("%c", &c);
printf("c=%d\n", c);
printf("c=‘%c‘\n", c);
return 0;
}
输出结果:
1
c=49
c=‘1‘
--------------------------------
Process exited after 0.9084 seconds with return value 0
字符计算
大小写转换
#include <stdio.h>
int main(int argc, char *argv[])
{
char c = ‘e‘;
printf("a=%d\n", ‘a‘);
printf("A=%d\n", ‘A‘);
printf("a-A=%d\n", ‘a‘ - ‘A‘);
printf("A-a=%d\n", ‘A‘ - ‘a‘);
c = c + ‘A‘ - ‘a‘;
printf("c+A-a=%c\n", c);
c = c + ‘a‘ - ‘A‘;
printf("c+A-a=%c\n", c);
return 0;
}
a=97
A=65
a-A=32
A-a=-32
c+A-a=E
c+A-a=e
--------------------------------
Process exited after 0.1735 seconds with return value 0
逃逸字符
制表位
Process exited after 0.222 seconds with return value 0
```
回车和换行
字符串变量
字符串常量(字符串字面量)
字符串
字符串输入输出
常见错误
空字符串
string.h
strlen
strcmp
strcpy
strcat
安全问题
安全版本
字符串中找字符
1、题目内容: 题目内容:
你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束。你要输出这行文本中
每个单词的长度。这里的单词与语言无关,可以包括各种符号,比如“it‘s”算一个单词,长
度为 4。注意,行中可能出现连续的空格。
输入格式:
输入在一行中给出一行文本,以‘.’结束,结尾的句号不能计算在最后一个单词的长度内。
输出格式:
在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。
输入样例:
It‘s great to see you here.
输出样例:
4 5 2 3 3 4
题目分析:
程序实现:
#include <stdio.h>
#include "inputText.h"
int main(int argc, char *argv[])
{
inputALineOfText();
return 0;
}
#ifndef inputText_h
#define inputText_h
#include <stdio.h>
#include <string.h>
void inputALineOfText();
#endif
#include "inputText.h"
void inputALineOfText(){
char str[500];
printf("Please input a line of text:");
gets(str);
printf("Your input text‘s word length respectively are:\n");
int wordCount=0;
for(int i=0;i<sizeof(str)/sizeof(str[0]);i++){
//printf("‘%c‘,",str[i]);
if(str[i]==‘ ‘){//单词边界
if(wordCount>0){
printf("%d ",wordCount);
}
wordCount = 0;//重新计数
}
else if(str[i]==‘.‘){
printf("%d",wordCount);
break;
}
else if(str[i]==‘\0‘){
printf("%d",wordCount);
break;
}else{
wordCount++;
}
}
}
测试样例:
Please input a line of text:It‘s great to see you here.
Your input text‘s word length respectively are:
4 5 2 3 3 4
--------------------------------
Process exited after 13.37 seconds with return value 0
2、 题目内容:
NMEA-0183 协议是为了在不同的 GPS(全球定位系统)导航设备中建立统一的 BTCM
(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The National Marine
Electronics Associa-tion)制定的一套通讯协议。GPS 接收机根据 NMEA-0183 协议
的标准规范,将位置、速度等信息通过串口传送到 PC 机、PDA 等设备。
NMEA-0183 协议是 GPS 接收机应当遵守的标准协议,也是目前 GPS 接收机上使用最
广泛的协议,大多数常见的 GPS 接收机、GPS 数据处理软件、导航软件都遵守或者至少兼
容这个协议。
NMEA-0183 协议定义的语句非常多,但是常用的或者说兼容性最广的语句只有
$GPGGA、$GPGSA、$GPGSV、$GPRMC、$GPVTG、$GPGLL 等。
其中$GPRMC 语句的格式如下:
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,1
50706,,,A50
这里整条语句是一个文本行,行中以逗号“,”隔开各个字段,每个字段的大小(长度)不一,
这里的示例只是一种可能,并不能认为字段的大小就如上述例句一样。
字段 0:$GPRMC,语句 ID,表明该语句为 Recommended Minimum Specific
GPS/TRANSIT Data(RMC)推荐最小定位信息
字段 1:UTC 时间,hhmmss.sss 格式
字段 2:状态,A=定位,V=未定位
字段 3:纬度 ddmm.mmmm,度分格式(前导位数不足则补 0)
字段 4:纬度 N(北纬)或 S(南纬)
字段 5:经度 dddmm.mmmm,度分格式(前导位数不足则补 0)
字段 6:经度 E(东经)或 W(西经)
字段 7:速度,节,Knots
字段 8:方位角,度
字段 9:UTC 日期,DDMMYY 格式
字段 10:磁偏角,(000 - 180)度(前导位数不足则补 0)
字段 11:磁偏角方向,E=东 W=西
字段 16:校验值
这里,“”为校验和识别符,其后面的两位数为校验和,代表了“$”和“”之间所有字符
(不包括这两个字符)的异或值的十六进制值。上面这条例句的校验和是十六进制的 50,
也就是十进制的 80。
提示:^运算符的作用是异或。将$和之间所有的字符做^运算(第一个字符和第二个字符异
或,结果再和第三个字符异或,依此类推)之后的值对 65536 取余后的结果,应该和后面
的两个十六进制数字的值相等,否则的话说明这条语句在传输中发生了错误。注意这个十六
进制值中是会出现 A-F 的大写字母的。另外,如果你需要的话,可以用 sscanf(s,"%d", &i)
从字符串 s 中得到其所表达的整数数字给 i。
现在,你的程序要读入一系列 GPS 输出,其中包含$GPRMC,也包含其他语句。在数据的
最后,有一行单独的
END
表示数据的结束。
你的程序要从中找出$GPRMC 语句,计算校验和,找出其中校验正确,并且字段 2 表示已
定位的语句,从中计算出时间,换算成北京时间。一次数据中会包含多条$GPRMC 语句,
以最后一条语句得到的北京时间作为结果输出。
你的程序一定会读到一条有效的$GPRMC 语句。
输入格式:
多条 GPS 语句,每条均以回车换行结束。最后一行是 END 三个大写字母。
输出格式:
6 位数时间,表达为:
hh:mm:ss
其中,hh 是两位数的小时,不足两位时前面补 0;mm 是两位数的分钟,不足两位时前面
补 0;ss 是两位数的秒,不足两位时前面补 0。
输入样例:
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,1507
06,,,A50
END
输出样例:
10:48:13
题目分析:
程序实现:
#include <stdio.h>
#include "GPSValidate.h"
int main(int argc, char *argv[])
{
validateGPS();
return 0;
}
#ifndef GPSValidate_h
#define GPSValidate_h
#include <stdio.h>
#include <string.h>
void validateGPS();
#endif
#include "GPSValidate.h"
#include <stdio.h>
#include <string.h>
/* 十六进制数转换为十进制数 */
long hexToDec(char *source);
/* 返回ch字符在sign数组中的序号 */
int getIndexOfSigns(char ch);
/* 十六进制数转换为十进制数 */
long hexToDec(char *source)
{
long sum = 0;
long t = 1;
int i, len;
len = strlen(source);
for(i=len-1; i>=0; i--)
{
sum += t * getIndexOfSigns(*(source + i));
t *= 16;
}
return sum;
}
/* 返回ch字符在sign数组中的序号 */
int getIndexOfSigns(char ch)
{
if(ch >= ‘0‘ && ch <= ‘9‘)
{
return ch - ‘0‘;
}
if(ch >= ‘A‘ && ch <=‘F‘)
{
return ch - ‘A‘ + 10;
}
if(ch >= ‘a‘ && ch <= ‘f‘)
{
return ch - ‘a‘ + 10;
}
return -1;
}
int calculateGPSCode(char gpsStr[],int arrCount){
if(gpsStr==NULL)
return 0;
int gpsCode=0;
int isBeginCalc = 0;
int isFirstCalc = 1;
//形式参数,sizeof(gpsSrt)的长度始终为8,和实参无关
//for(int i=0;i<sizeof(gpsStr)/sizeof(gpsStr[0]);i++){
for(int i=0;i<arrCount;i++){
if(gpsStr[i]==‘$‘){ //开始计算异或值
isBeginCalc=1;
}else if(gpsStr[i]==‘*‘ || gpsStr[i]==‘\0‘){//结束异或值计算
break;
}else{
if(isBeginCalc==1){
if(isFirstCalc==1){
gpsCode = (int)gpsStr[i];
isFirstCalc=0;
//printf("%c",gpsStr[i]);
}else{
gpsCode = gpsCode ^ (int)gpsStr[i];
//printf("^%c",gpsStr[i]);
}
}
}
}
gpsCode = gpsCode%65536;
return gpsCode;
}
void getGPSCode(char *gpsCode ,const char* gpsStr,int arrCount){
if(gpsStr==NULL)
return;
int j=0;
int isBeginGetCode=0;
for(int i=0;i<arrCount;i++){
if(gpsStr[i] == ‘*‘){//*往后为校验码
isBeginGetCode=1;
}
else if(gpsStr[i] == ‘\0‘){
gpsCode[j]=‘\0‘;
break;
}
else{
if(isBeginGetCode == 1){
gpsCode[j] = gpsStr[i];
j++;
}
}
}
}
void getUTCTime(char *outChar,const char *gpsChar,int length){
if(gpsChar == NULL)
return;
int filedIndex=0;
int j=0;
for(int i=0;i<length;i++){
if(gpsChar[i] == ‘,‘){
filedIndex++;
}
else {
if(filedIndex == 1) {
outChar[j]=gpsChar[i];
j++;
} else if(filedIndex > 1){
outChar[j] = ‘\0‘;
break;
}
}
}
}
int isContainsGPSInfo(char *gpsChar,int length){
if(gpsChar == NULL)
return 0;
int isContain=0;
int filedIndex=0;
for(int i=0;i<length;i++){
if(gpsChar[i] == ‘,‘){
filedIndex++;
}
else {
if(filedIndex == 2) {
if(gpsChar[i] == ‘A‘){
isContain = 1;
break;
}
} else if(filedIndex > 2){
isContain = 0;
break;
}
}
}
return isContain;
}
void getBeiJingTime(char *outChar,char *utcTimeChar){
if(utcTimeChar == NULL)
return;
char utcPrefix[10];
strtok(utcTimeChar,".");
if(utcTimeChar != NULL){
int utcPrefixInt=0;
sscanf(utcTimeChar,"%d",&utcPrefixInt);
int beiJingTimeInt = utcPrefixInt + 80000;
char tmp[10];
sprintf(tmp,"%d",beiJingTimeInt);
int j=0;
int colonIndexFirst = 2;
int colonIndexSecond = 4;
for(int i=0;i<strlen(tmp);i++,j++){
if(i==0){
if(beiJingTimeInt<100000) {//5位
outChar[j]=‘0‘;
outChar[++j]=tmp[i];
colonIndexFirst--;
colonIndexSecond--;
}else{
outChar[j]=tmp[i];
}
} else if(i==colonIndexFirst || i==colonIndexSecond) {
outChar[j]=‘:‘;
outChar[++j]=tmp[i];
}
else if(i==strlen(tmp)-1) {
outChar[j]=tmp[i];
outChar[++j]=‘\0‘;
}
else{
outChar[j]=tmp[i];
}
}
}
}
void validateGPS(){
char str[500];
printf("Please input a GPS text:");
gets(str);
while(strcmp(str,"END")!=0){
printf("%s\n",str);
int length = strlen(str);//sizeof(str)/sizeof(str[0]);
int gpsCode = calculateGPSCode(str,length);
printf("Correct gpsCode is %d\n",gpsCode);
char realGPSCode[20];
getGPSCode(realGPSCode,str,length);
long realCodeLong = hexToDec(realGPSCode);
printf("Real GPSCode is %d\n",realCodeLong);
if(realCodeLong == (long)gpsCode){
int isContainsGPS = isContainsGPSInfo(str,length);
printf("isContainGPS:%d \n",isContainsGPS);
if(isContainsGPS == 1){
char utcTime[20];
getUTCTime(utcTime,str,length);
char beiJingTime[10];
getBeiJingTime(beiJingTime,utcTime);
printf("utcTime:%s\n",utcTime);
printf("Success!\n");
printf("BeiJingTime is %s\n",beiJingTime);
}
}else {
printf("Invalid GPS reenter:\n");
}
gets(str);
}
}
测试样例:
Please input a GPS text:$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
Correct gpsCode is 80
Real GPSCode is 80
isContainGPS:1
utcTime:024813
Success!
BeiJingTime is 10:48:13
END
--------------------------------
Process exited after 13.36 seconds with return value 0
程序设计入门-C语言基础知识-翁恺-第七周:指针与字符串-详细笔记(七)
原文:https://www.cnblogs.com/simple-blog/p/9536605.html