⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atox.cpp

📁 c语言教程源码
💻 CPP
字号:
//这个程序在本书所带软盘中。文件名为ATOX.CPP
//这个程序演示字符串/数值转换函数的各种操作。

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

void main(void)
{
	char string1[10] = "32767";
	char string2[10] = "-999999";
	char string3[20] = "3.1415926";
	char string4[20] = "-0.00002890";
	char *string5 = "100";

	int num1;
	long num2;
	float pi, alpha;
	int wrong_num, num;

	num1 = atoi(string1);
	num2 = atol(string2);
	pi = atof(string3);
	alpha = atof(string4);
	num = atoi(string5)*10;

	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	cout << "pi = " << pi << endl;
	cout << "alpha = " << alpha << endl;
	cout << "num = " << num << endl;

	cout << endl;
	cout << "atoi(\"0009\") = " << atoi("0009") << endl;
	cout << "atof(\"1.990\") = " << atof("1.990") << endl;
	cout << "2*atoi(\"200\") = " << 2*atoi("200") << endl;
	cout << "atof(\"1.25\")*2 = " << atof("1.25")*2 << endl;

	cout << endl;
	wrong_num = atoi("32800");	//字符串代表的数太大
	cout << "wrong_num = atoi(\"32800\") = " << wrong_num << endl;

	cout << "wrongreal = atof(\"3.1415.926\") = " << atof("3.1415.926") << endl;
}

/*程序运行后的结果如下:
num1 = 32767
num2 = -999999
pi = 3.14159
alpha = -2.89e-05
num = 1000

atoi("0009") = 9
atof("1.990") = 1.99
2*atoi("200") = 400
atof("1.25")*2 = 2.5

wrong_num = atoi("32800") = -32736
wrong_real = atof("3.1415.926") = 3.1415
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -