我想问一下,当我按下“Y”时,我将如何添加重复功能。 [英] I would like to ask how I going to add the repeat function once I press "Y"

查看:76
本文介绍了我想问一下,当我按下“Y”时,我将如何添加重复功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //   TMA2Q3.cpp:定义控制台应用程序的入口点。  
//

#include stdafx.h
#include stdio.h
#include string.h


int main(){
int maxNumber = 0 ;

printf( 按名称和ID编制:12345 \ n );
printf( Class:3SPG1 \\\
);
printf( 此程序接受一组数字并执行排序和索引。\ n< /跨度>);
printf( 请输入要排序的数字(最多20个):);
scanf_s( %d,& maxNumber);

int * aryNumbers = NULL;
int * sortedIndex = NULL;
int * sortedNumbers = NULL;
aryNumbers = realloc(aryNumbers,maxNumber);
sortedIndex = realloc(sortedIndex,maxNumber);
sortedNumbers = realloc(sortedNumbers,maxNumber);

for int i = 1 ; i< = maxNumber; i ++){
char y [ 2 ];
itoa(i,y, 10 );
printf( 数字键#%s:,& y);
scanf_s( %d,& aryNumbers [i - 1 ]);
sortedNumbers [i - 1 ] = aryNumbers [i - 1 ];
}

printf( 数字的原始顺序为:< /跨度>);
for int i = 0 ; i< maxNumber; i ++){
char z [ 2 ];
itoa(aryNumbers [i],z, 10 );
printf( %s,& z);
}

// 执行排序并分配
int index;
for int i = 0 ; i< maxNumber; i ++){
sortedIndex [i] = i + 1 ;
for int j = 0 ; j< maxNumber; j ++){
if (sortedNumbers [j]> sortedNumbers [i]){
int temp = sortedNumbers [j];
sortedNumbers [j] = sortedNumbers [i];
sortedNumbers [i] = temp;
sortedIndex [i] = j + 1 ;
}
}
}

printf( \ n排序的数字是:);
for int i = 0 ; i< maxNumber; i ++){
char z [ 2 ];
itoa(sortedNumbers [i],z, 10 );
printf( %s,& z);
}

// 查找排序索引
for int i = 0 ; i < maxNumber; i ++){
for int j = 0 ; j< maxNumber; j ++){
if (aryNumbers [i] == sortedNumbers [j]){
sortedIndex [i] = j + 1 ;
}
}
}

for int i = 0 ; i< maxNumber; i ++){
char a [ 2 ];
char b [ 2 ];
char c [ 2 ];
itoa(i + 1 ,a, 10 );
itoa(aryNumbers [i],b, 10 );
itoa(sortedIndex [i],c, 10 );
printf( \ n编号#%s(%s)的索引是%s,& a,& b,& c);
}
printf( \ n你想重复这个程序吗?(Y / N ):);

char 输入;
scanf_s( %d,& input);

return 0 ;
}





我的尝试:



我从互联网上进行了研究,它的使用时间......循环,我试过几次,它无法工作......需要帮助和建议。非常感谢。

解决方案

您找到的建议是正确的:在或时使用做...而循环。

将你要重复的整个代码整理一遍,它会起作用:

  char  [ 100 ] inp; 

{
...你的代码在这里...
scanf( %s,inp);
} while (inp [ 0 ] == ' y' || inp [ 0 ] == ' Y');
返回 0 ;







引用:

对不起老板,我对编程知识不足0。

这对我来说真的很难,我不明白循环如何工作。

希望你能帮助我。谢谢





A 循环非常简单:

< pre lang =c ++> while (a)
{
b;
}
c;

1)如果条件 a 的计算结果为true,请输入循环。

1.1)执行一个或多个陈述 b

1.2)回到(1)
$ b $的测试b 2)如果条件 a 为假,则执行 c



还有一个 do ...而版本至少执行一次循环:

  
{
b;
} while (a);
c;

1)执行一个或多个语句 b

2)如果条件 a 计算结果为true,循环回(1)

3)否则,执行 c 。 br />


所有这些都在文档中......


// TMA2Q3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "string.h"


int main() {
	int maxNumber = 0;

	printf("Prepared by Name and ID: 12345 \n");
	printf("Class: 3SPG1 \n");
	printf("This program takes in a set of numbers and performs sorting and indexing. \n");
	printf("Please key in how many numbers you want to sort(max.20): ");
	scanf_s("%d", &maxNumber);

	int *aryNumbers = NULL;
	int *sortedIndex = NULL;
	int *sortedNumbers = NULL;
	aryNumbers = realloc(aryNumbers, maxNumber);
	sortedIndex = realloc(sortedIndex, maxNumber);
	sortedNumbers = realloc(sortedNumbers, maxNumber);

	for (int i = 1; i <= maxNumber; i++) {
		char y[2];
		itoa(i, y, 10);
		printf("Key in number #%s: ", &y);
		scanf_s("%d", &aryNumbers[i - 1]);
		sortedNumbers[i - 1] = aryNumbers[i - 1];
	}

	printf("The original order of the numbers are: ");
	for (int i = 0; i < maxNumber; i++) {
		char z[2];
		itoa(aryNumbers[i], z, 10);
		printf("%s ", &z);
	}

	//perform sorting and assign
	int index;
	for (int i = 0; i < maxNumber; i++) {
		sortedIndex[i] = i + 1;
		for (int j = 0; j < maxNumber; j++) {
			if (sortedNumbers[j] > sortedNumbers[i]) {
				int temp = sortedNumbers[j];
				sortedNumbers[j] = sortedNumbers[i];
				sortedNumbers[i] = temp;
				sortedIndex[i] = j + 1;
			}
		}
	}

	printf("\nThe sorted numbers are: ");
	for (int i = 0; i < maxNumber; i++) {
		char z[2];
		itoa(sortedNumbers[i], z, 10);
		printf("%s ", &z);
	}

	//find sorted index
	for (int i = 0; i < maxNumber; i++) {
		for (int j = 0; j < maxNumber; j++) {
			if (aryNumbers[i] == sortedNumbers[j]) {
				sortedIndex[i] = j + 1;
			}
		}
	}

	for (int i = 0; i < maxNumber; i++) {
		char a[2];
		char b[2];
		char c[2];
		itoa(i + 1, a, 10);
		itoa(aryNumbers[i], b, 10);
		itoa(sortedIndex[i], c, 10);
		printf("\nThe index for number #%s (%s) is %s", &a, &b, &c);
	}
	printf("\nDo you want to repeat the program?(Y/N): ");

	char input;
	scanf_s("%d", &input);

	return 0;
}



What I have tried:

I had research from internet, its ask for use while...loop, I had tried few time, its cannot work...need help and advice. Many thanks.

解决方案

The advice you found is right: use a while or do...while loop.
PUt one round the whole of the code you want to repeat, and it will work:

char[100] inp;
do
   {
   ... your code here ...
   scanf("%s", inp);
   } while (inp[0] == 'y' || inp[0] == 'Y');
return 0;




Quote:

So sorry boss, I'm 0 knowledge on programming.
It's really difficult for me, I was not understand how while loop work.
Hopefully you can help me. Thanks



A while loop is really simple:

while (a)
   {
   b;
   }
c;

1) If the condition a evaluates to true, enter the loop.
1.1) Execute the statement or statements b
1.2) Got back round to the test at (1)
2) If the condition a was false, execute c instead.

There is also a do ... while version which executes the loop at least once:

do
   {
   b;
   } while (a);
c;

1) Execute the statement or statements b
2) If the condition a evaluates to true, loop back to (1)
3) Otherwise, execute c instead.

All of this is in the documentation ...


这篇关于我想问一下,当我按下“Y”时,我将如何添加重复功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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