如何使这段代码也能在C中运行? [英] How can I make this code also work in C?

查看:82
本文介绍了如何使这段代码也能在C中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C中创建一个读取n个字符的代码,验证字符是0到9之间的字母或数字,如果是,则程序显示其十进制ASCII值,否则将显示一条消息:未知字符。



例如:输入:4

A b 1!

输出:65 98 49未知角色



我尝试过:



//我的代码in C ++

  #include   <   iostream  >  
< span class =code-keyword>使用 命名空间标准;
int main()
{
int i;
int n;
char c;
cin>> N;

for (i = 0 ; i< n; i ++)
{
cin>> C;
if ((c> = ' a'&& c< = ' z')||(c > = ' A'&& c< = ' Z')||(c> = ' 0'&& c< = ' 9'))
{
cout<< ( int )c<< ;
}
else
{
cout<< 未知字符<< ;
}
}
cout<< \ n;
system( pause);
return 0 ;
}

// 我在C中的代码
#include < stdio.h >
int main ()
{
int i,n;
char c;
scanf_s( %d,& n);
for (i = 0 ; i< n; i ++)
{
scanf_s( %c,& c);
if ((c> = ' a'&& c< = ' z')||(c > = ' A'&& c< = ' Z')||(c> = ' 0'&& c< = ' 9'))
{
printf( %d \ n, C);
}
else
{
printf( 未知字符);
}
}
system( pause);
return 0 ;
}

解决方案

为什么在必须使用C时用C ++编写?或者您是否在网上找到了代码?



无论如何,您的代码工作正常,只需要稍微调整即可产生预期结果。



scanf()函数将在出现不匹配字符时停止,并在输入缓冲区中保留所有剩余字符。当读取一个整数值,这意味着通过按返回键放置在缓冲区中的换行符 \ n 会停止转换,但会保留在缓冲区中。然后在循环中再次调用 scanf()时,它会读取被视为未知字符的换行符。



解决方案非常简单:告诉 scanf()阅读并忽略换行符:

 scanf_s( %d \ n,& n); 

要获得所需的输出格式,您只需更换 \ n 在一个空格的 printf()格式字符串中,在打印未知字符时在末尾添加一个空格,最后在终止应用程序之前打印一个新行。


I wanna make a code in C that reads n characters,verifies if the characters are letters or numbers between 0 and 9 and if they are,then the program displays their decimal ASCII value,else it will be displayed a message:"Unknown Character".

Eg:Input:4
A b 1 !
Output:65 98 49 Unknown Character

What I have tried:

//My code in C++

#include<iostream>
using namespace std;
int main( )
{
	int i;
	int  n;
	char c;
	cin >> n;

	for (i = 0; i < n; i++)
	{
		cin >> c;
		if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
		{
			cout << (int)c << " ";
		}
		else
		{
			cout << "Unknown Character" << " ";
		}			
	}
	cout << "\n";
	system("pause");
	return 0;
}

//My code in C
#include<stdio.h>
int main()
{
	int i, n;
	char c;
	scanf_s("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf_s("%c", &c);
		if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
		{
			printf("%d\n", c);
		}
                else
               {
                     printf("Unknown Character");
               }	
	}
	system("pause");
	return 0;
}

解决方案

Why do you wrote it in C++ when you have to use C? Or did you found the code in the net?

Anyway, your code is working and needs only a little tweaking to produce the expeceted result.

The scanf() function will stop when a not matching character occurs leaving all remaining characters in the input buffer. When reading an integer value that means that the line feed character \n placed in the buffer by pressing the return key stops the conversion but is left in the buffer. When then calling scanf() again within the loop, it reads that line feed which is treated as unknown character.

The solution is quite simple: Tell scanf() to read and ignore the line feed:

scanf_s("%d\n", &n);

To get the required output format, all you have to do is replacing the \n in the printf() format string by a space, add a space at the end when printing "Unknown Character", and finally print a new line before terminating the application.


这篇关于如何使这段代码也能在C中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆