注册 登录
编程论坛 C++教室

小白求助,运算结果二为什么是负数?

wl0421 发布于 2022-08-27 20:53, 3406 次点击
#include<bits/stdc++.h>
using namespace std;
int main ()
{
    char c1,c2,c3;
    c1='a';
    c2='z';
    c1=c1-32;
    c2=c2+32;
    c3='0'+9;
    cout<<int(c1)<<" "<<int(c2)<<" "<<int(c3)<<" "<<endl;
    return 0;
}
 运行结果 65  -102  57
第二个结果不是应该122吗?请问错哪里了?为什么会是负数?
15 回复
#2
apull2022-08-28 00:35
c2=c2+32;是c2的值是字母z的ascii码122,你再加32,超过127就成-102。
#3
rjsp2022-08-28 21:57
估计他把 'Z' 写成了 'z'
#4
mature1192022-09-03 20:00
超过127了啊
#5
op1232022-09-04 17:56
超出了最大的ascll码,127
#6
chenyucheng2022-09-14 11:30
回复 2楼 apull
变补码
#7
mature1192022-09-20 21:43
#include<bits/stdc++.h>这是设么头文件,求指导?????
#8
apull2022-09-21 08:57
回复 7楼 mature119
俗称万能头文件,里面引用了几乎所有的C++头文件,里面几个头文件VC6不支持。
#9
mature1192022-09-21 11:58
回复 8楼 apull
vs2019不能用这个,只有dec能用吗?
#10
rjsp2022-09-21 16:59
回复 9楼 mature119
#include<bits/stdc++.h> 并不是C++标准规定的。
C++20标准规定的是 import std.core; 等等;
C++23标准规定的是 import std.core; 等等,以及 import std;

在VC2022中,可以使用 import std.core;,前提是你选装了模块功能的库。
#11
apull2022-09-21 19:57
以下是引用mature119在2022-9-21 11:58:49的发言:

vs2019不能用这个,只有dec能用吗?


可以用,把下面内容保存到vs2019的 include\bits\stdc++.h


程序代码:


// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.>.

/** @file stdc++.h

 *  This is an implementation file for a precompiled header.

 
*/

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#if __cplusplus >= 201703L
#include <charconv>
#include <filesystem>
#endif

#12
Xovery2022-10-22 02:05
VC2022中cont和printf都可以使用,有什么区别吗?
#13
apull2022-10-22 08:41
回复 12楼 Xovery
printf是c语言的东西,c++兼容c语言的结果。
#14
rjsp2022-10-22 11:04
以下是引用Xovery在2022-10-22 02:05:58的发言:

VC2022中cont和printf都可以使用,有什么区别吗?

printf 是 C语言标准库 带来的,C++只是继承了它。printf的缺点是 容易写错 + 无法扩展;
std::ostream 是 C++语言为了避开printf的缺点而设计出来的。ostream的缺点是 不直观 + 为了兼容printf而效率低下(自身效率并不低) + 有状态;
C++开始推广std::format,能避开以上所有缺点,当然实际效果如何,有待时间来检验。
#15
mochu2022-11-24 16:32
程序代码:
#include<bits/stdc++.h>
using namespace std;
int main ()
{
    unsigned char c1,c2,c3;
    c1='a';
    c2='z';
    c1=c1-32;
    c2=c2+32;
    c3='0'+9;
    cout<<int(c1)<<" "<<int(c2)<<" "<<int(c3)<<" "<<endl;
    return 0;
}


声明变量时加一个无符号数据类型
#16
billliu662023-01-31 16:05
这样就可以了:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
    unsigned char c1, c2, c3;
    c1 = 'a';
    c2 = 'z';
    c1 = c1 - 32;
    c2 = c2 + 32;
    c3 = '0' + 9;
    cout << int(c1) << " " << int(c2) << " " << int(c3) << " " << endl;
    return 0;
}
1