我正在从Schildt的书中学习C++,并不太明白他在第三种结构中的意思; 有人可以解释一下 - >
要访问结构数组中的特定结构,必须索引结构名称.例如,要显示第三个结构的on_hand成员,您可以编写cout << invtry [2] .on_hand;
一些代码:
struct type{
char item[40];
double cost;
double retail;
int on_hand;
int lead_time;
}invtry[SIZE];
Run Code Online (Sandbox Code Playgroud) 有人可以弄清楚我为什么要使用并集,并且为cin'ed变量和位字段(Schildts C ++书中的任务)使用相同的地址是什么目的?换句话说,我为什么要使用union:
char ch; 结构字节位;
//显示二进制的ASCII码字符。
#include <iostream>
#include <conio.h>
using namespace std;
// a bit field that will be decoded
struct byte {
unsigned a : 1;
unsigned b : 1;
unsigned c : 1;
unsigned d : 1;
unsigned e : 1;
unsigned f : 1;
unsigned g : 1;
unsigned h : 1;
};
union bits {
char ch;
struct byte bit;
} ascii ;
void disp_bits(bits b);
int main()
{
do {
cin >> ascii.ch; …Run Code Online (Sandbox Code Playgroud) 虽然编写非常简单的程序来删除空格,制表符,换行符,但我遇到了一些我实际上并没有先抓到的东西; 即使if条件为真,只有当tab,space或newline不存在时,仍然会因为某些原因执行上面提到的..这里是代码
#include <cstdio>#include <cstring>
#include <stdio.h>
#include <string.h>
#define LGT 100
void rem(char s[])
{
int i;
for(i=(strlen(s)-1);i>=0;i--)
if(s[i]!=' ' || s[i]!='\t' || s[i]!='\n')
break;
s[i+1]='\0';
}
int main(void)
{
char v[LGT]={"sdsfg\t"};
rem(v);
printf("%s\n",v);
getchar();
}
Run Code Online (Sandbox Code Playgroud) 编写一个程序,将输入读取到#并报告序列ei发生的次数.
我有一个序列有点慌乱,如'ieei'其中编译器会进入第3 'e',但从来没有取'i'与getchar(),为什么,如果有人能够自己之前改善这一点,会是好?
char ch;
int sq=0;
while ((ch = getchar()) != '#')
{
if (ch == 'e')
{
ch = getchar();
if (ch == 'e')
ch = getchar();
if (ch == 'i')
sq++;
}
}
printf("Sequence occurs %d %s\n", sq, sq == 1 ? "time" : "times");
Run Code Online (Sandbox Code Playgroud)